Skip to content

Instantly share code, notes, and snippets.

@JerryFleming
Last active December 17, 2015 01:48
Show Gist options
  • Save JerryFleming/5530581 to your computer and use it in GitHub Desktop.
Save JerryFleming/5530581 to your computer and use it in GitHub Desktop.
Update your wallpapers regularly (please don't be frequently) by polling wallpaperswide.com.
#!/bin/bash
# Update your wallpapager regularly.
# Preferrably installing it in your crontab:
#
# (crontab -l | sed '1,3d'; echo "# Update wallpager every half an hour."; echo "0,30 * * * * /path/to/wallpaper.sh") | crontab -
#
# by Jerry Fleming <[email protected]>
# on 2013-03-25.
# No rights reserved. Use at your own risk.
# Your probably don't need this proxy setting.
[ -e /etc/proxy ] && source /etc/proxy
# Your preferred place to store the pictures. Please mkdir and chown as appropriate.
FILE=/var/tmp/wallpaper
# Time to sleep before every web request. Shorter values may get your IP to be blocked.
DELAY=1.0
# Start over again every day, in case the ranking is changed.
now=$(date +%s)
mtime=$(date +%s -r $FILE.seq 2>/dev/null)
age=$(($now - ${mtime:-0}))
[ $age -gt 8640000 ] && {
rm $FILE.name
touch $FILE.seq
}
# See if a user is logged in over a desktop session.
GUI_SESSIONS=$( who | grep "$USER.*(:" | wc -l )
[ $GUI_SESSIONS -eq 0 ] && exit 1
{ read oname page cnt < $FILE.name ; } 2>/dev/null
page=${page:=1}
cnt=${cnt:=1}
same_page=1
while true
do
item=0
curl -s http://wallpaperswide.com/nature-desktop-wallpapers/page/$page \
| sed -n 's#.*<a href="/\([^-]\+\)-wallpapers.html.*#\1#p' \
| uniq \
| while read name
do
item=$(($item+1))
# If this is already another page, item count doesn't matter.
[ $same_page -eq 1 ] && [ $item -lt $cnt ] && continue
# Skip the old item itself.
[ x"$oname" == x"$name" ] && continue
[ -e ~/wallpapers/$name.jpg ] && continue
sleep $DELAY
curl -s http://wallpaperswide.com/download/$name-wallpaper-1920x1080.jpg \
-o $FILE.jpg.new -D $FILE.header \
-e http://wallpaperswide.com/$name-wallpapers.html
# Either it doesn't exist or there is an error.
grep Location $FILE.header > /dev/null && continue
# Ensure that the picture comes at once, to avoid flickering/partial loading.
mv $FILE.jpg.new $FILE.jpg
cp $FILE.jpg ~/wallpapers/$name.jpg
echo $name $page $item > $FILE.name
break
done
[ -e $FILE.header ] && break
page=$(($page+1))
same_page=0
sleep $DELAY
done
rm -f $FILE.header
# This only need to be done once, but doesn't matter if done repeatedly.
gconftool-2 --type string --set /desktop/gnome/background/picture_filename $FILE.jpg
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment