forked from ktmud/bing-wallpaper
-
Notifications
You must be signed in to change notification settings - Fork 83
/
bing-wallpaper.sh
executable file
·133 lines (121 loc) · 3.65 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
#!/usr/bin/env bash
# shellcheck disable=SC1117
SCRIPT=$(basename "$0")
readonly SCRIPT
VERSION='0.5.0'
readonly VERSION
RESOLUTIONS=(UHD 1920x1200 1920x1080 800x480 400x240)
readonly RESOLUTIONS
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
}
# Defaults
PICTURE_DIR="$HOME/Pictures/bing-wallpapers/"
RESOLUTION="1920x1080"
# Option parsing
BOOST=1
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))
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}"
read -ra urls < <(curl -sL "$PROTO://www.bing.com/HPImageArchive.aspx?format=js&n=$BOOST" | \
# Extract the image urls from the JSON response
grep -Po '(?<=url":").*?(?=")' | \
# Set the image resolution
sed -e "s/[[:digit:]]\{1,\}x[[:digit:]]\{1,\}/$RESOLUTION/" | \
# FQDN the image urls
sed -e "s/\(.*\)/${PROTO}\:\/\/www.bing.com\1/" | \
tr "\n" " ")
for pic in "${urls[@]}"; do
if [ -z "$FILENAME" ]; then
filename=$(echo "$pic" | 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" "$pic"
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