-
Notifications
You must be signed in to change notification settings - Fork 1
/
bing-wallpaper.sh
executable file
·61 lines (55 loc) · 1.7 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
#!/bin/sh
# Queries the Bing API endpoint and returns a url to today's image.
get_url() {
INDEX=0
NUMBER=1
LOCATION="en-US"
ROOT="https://www.bing.com"
API="${ROOT}/HPImageArchive.aspx?format=js&idx=${INDEX}&n=${NUMBER}&mkt=${LOCATION}&uhd=1&uhdwidth=4096&uhdheight=4096"
JSON=$(curl "$API" | tr -d '[:space:]')
PATH=$(echo "$JSON" | sed -e 's/^.\+"url":"//' -e 's/".\+$//')
URL="${ROOT}${PATH}"
echo "$URL"
}
# Extracts the filename from the url of an image.
get_filename() {
URL="$1"
FILENAME=$(echo "$URL" | sed -e 's/^.\+\Wid=//' -e 's/&.\+$//')
echo "$FILENAME"
}
# Downloads an image and returns the download path (while pruning downloaded images if there are too many).
download() {
if [ -z "$XDG_CACHE_HOME" ]; then
CACHE="$XDG_CACHE_HOME/bing-wallpaper"
else
CACHE="$HOME/.cache/bing-wallpaper"
fi
mkdir -p "$CACHE"
if [ "$(ls -A "$CACHE" | wc -l)" -gt "10" ]; then
rm "$CACHE"/*
fi
FILE="$CACHE/$2"
if [ -f "$FILE" ] && file "$FILE" | grep image; then
echo "$FILE: already downloaded" 1>&2
else
curl "$1" --output "$FILE"
fi
echo "$FILE"
}
# Sets the wallpaper to a local image.
set_wallpaper() {
if [ -f "$1" ]; then
if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
for prop in $(xfconf-query --channel xfce4-desktop --list | grep last-image); do
xfconf-query --channel xfce4-desktop --property "$prop" --set "$1"
done
else
echo "Unsupported desktop: ${XDG_CURRENT_DESKTOP}" 1>&2
exit 1
fi
fi
}
URL=$(get_url)
FILENAME=$(get_filename "$URL")
FILE=$(download "$URL" "$FILENAME")
set_wallpaper "$FILE"