-
Notifications
You must be signed in to change notification settings - Fork 0
/
bingdesktop.sh
executable file
·111 lines (101 loc) · 2.24 KB
/
bingdesktop.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
#!/bin/bash
#
fileName=$(echo "wallpaper_"$(date --rfc-3339 seconds)".jpg" | sed -e "s/ /T/g")
saveDir="/home/tyler/Pictures/wellpaper/"
timeOut=60 #seconds
debug="/dev/null"
force=0
show_help() {
cat << EOF
Download daily image from bing and set it as the desktop background.
Usage:
$0
Options:
-h --help --> Show this help
-d --debug --> Show debugging output
-f --force --> Force download and setting of file
-a --archive --> Archive this month's pictures
EOF
}
main() {
if [[ $force == 0 ]]; then check_current; fi
if [ ! -d $saveDir ]; then mkdir -p $saveDir; fi
setup_dbus
check_connection
download_and_set
}
setup_dbus() {
PID=$(pgrep gnome-session)
dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ | cut -d= -f2-)
export DBUS_SESSION_BUS_ADDRESS=$dbus
}
check_connection() {
startTime=$SECONDS
until ping -q -c 1 "www.bing.com" > $debug 2>&1; do
if [[ $(($SECONDS - $startTime)) -ge $timeOut ]]; then
logger -i -t bingdesktop -p syslog.err "Can't access www.bing.com"
exit 1
fi
done
}
download_and_set() {
if wget -t 20 --waitretry=1 --retry-connrefused "www.bing.com"$(wget -qO- www.bing.com | grep -o "url:'.*[.]jpg',id" | cut -d"'" -f2 ) -O $saveDir$fileName > $debug 2>&1; then
gsettings set org.gnome.desktop.background picture-uri "file://$saveDir$fileName"
else
rm $saveDir$fileName
logger -i -t bingdesktop -p syslog.err 'Download failed'
exit 1
fi
}
check_current() {
last=`ls -1 $saveDir | tail -n 1 | sed 's/wallpaper_//' | sed 's/T.*//'`
if [[ $last == `date +%F` ]]; then
echo "Already downloaded today's image. Exiting." > $debug
exit
fi
}
archive() {
files=$(ls -1 "$saveDir"*.jpg)
i=0
arDir="$saveDir$(date +%B)-$(date +%g)"
mkdir $arDir
for x in $files; do
y=$(echo $x | sed 's/wallpaper_//' | sed 's/T.*//' | cut -d '-' -f2)
if [[ $y == `date +%m` ]]; then
arFiles[$i]=$x
i=$((i+1))
fi
done
for x in ${arFiles[*]}; do
mv $x $arDir
done
tar cjf "$arDir".tar.bz2 $arDir 2>/dev/null
}
# Parse options
#
while [[ $# -ge 1 ]]; do
key="$1"
case $key in
-h|--help)
show_help
exit
;;
-d|--debug)
debug=`tty`
main
;;
-f|--force)
force=1
main
;;
-a|--archive)
archive
exit
;;
*)
show_help
exit
;;
esac
shift
done