forked from thejandroman/bing-wallpaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bing-wallpaper.sh
executable file
·140 lines (126 loc) · 3.87 KB
/
bing-wallpaper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env bash
# shellcheck disable=SC1117
readonly SCRIPT=$(basename "$0")
readonly VERSION='0.4.0'
readonly RESOLUTIONS=(1920x1200 1920x1080 800x480 400x240)
usage() {
cat <<EOF
Usage:
$SCRIPT [options]
$SCRIPT -h | --help
$SCRIPT --version
Options:
-f --force Force download of picture. This will overwrite
the picture if the filename already exists.
-s --ssl Communicate with bing.com over SSL.
-b --boost <n> Use boost mode. Try to fetch latest <n> pictures.
-q --quiet Do not display log messages.
-n --filename <file name> The name of the downloaded picture. Defaults to
the upstream name.
-p --picturedir <picture dir> The full path to the picture download dir.
Will be created if it does not exist.
[default: $HOME/Pictures/bing-wallpapers/]
-r --resolution <resolution> The resolution of the image to retrieve.
Supported resolutions: ${RESOLUTIONS[*]}
-w --set-wallpaper Set downloaded picture as wallpaper (Only mac support for now).
-h --help Show this screen.
--version Show version.
EOF
}
print_message() {
if [ -z "$QUIET" ]; then
printf "%s\n" "${1}"
fi
}
transform_urls() {
sed -e 's#\\u0026#\&#g' | \
sed -e "s/\\\//g" | \
sed -e "s/[[:digit:]]\{1,\}x[[:digit:]]\{1,\}/$RESOLUTION/" | \
tr "\n" " "
}
# Defaults
PICTURE_DIR="$HOME/Pictures/bing-wallpapers"
RESOLUTION="1920x1080"
# Option parsing
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-r|--resolution)
RESOLUTION="$2"
shift
;;
-p|--picturedir)
PICTURE_DIR="$2"
shift
;;
-n|--filename)
FILENAME="$2"
shift
;;
-f|--force)
FORCE=true
;;
-s|--ssl)
SSL=true
;;
-b|--boost)
BOOST=$(($2-1))
shift
;;
-q|--quiet)
QUIET=true
;;
-h|--help)
usage
exit 0
;;
-w|--set-wallpaper)
SET_WALLPAPER=true
;;
--version)
printf "%s\n" $VERSION
exit 0
;;
*)
(>&2 printf "Unknown parameter: %s\n" "$1")
usage
exit 1
;;
esac
shift
done
# Set options
[ -n "$QUIET" ] && CURL_QUIET='-s'
[ -n "$SSL" ] && PROTO='https' || PROTO='http'
# Create picture directory if it doesn't already exist
mkdir -p "${PICTURE_DIR}"
# Parse bing.com and acquire picture URL(s)
urls=$(curl -sL $PROTO://www.bing.com | \
grep --color '<a href="/th' | \
sed -e "s#.*<a href=\"\(/th.*jpg\)&.*#$PROTO://bing.com\1#" | \
transform_urls)
if [ -n "$BOOST" ]; then
archiveUrls=$(curl -sL "$PROTO://www.bing.com/HPImageArchive.aspx?format=js&n=$BOOST" | \
grep -Eo "url\":\".*?\"" | \
sed -e "s/url\":\"\([^\"]*\)\"/$PROTO:\/\/bing.com\1/" | \
transform_urls)
urls=( "${urls[@]}" "${archiveUrls[@]}" )
fi
for p in "${urls[@]}"; do
if [ -z "$FILENAME" ]; then
filename=$(echo "$p" | sed -e 's/.*[?&;]id=\([^&]*\).*/\1/' | grep -oe '[^\.]*\.[^\.]*$')
else
filename="$FILENAME"
fi
if [ -n "$FORCE" ] || [ ! -f "$PICTURE_DIR/$filename" ]; then
print_message "Downloading: $filename..."
curl $CURL_QUIET -Lo "$PICTURE_DIR/$filename" "$p"
else
print_message "Skipping: $filename..."
fi
done
if [ -n "$SET_WALLPAPER" ]; then
/usr/bin/osascript<<END
tell application "System Events" to set picture of every desktop to ("$PICTURE_DIR/$filename" as POSIX file as alias)
END
fi