Friday, January 30, 2009

Wikipedia Picture of the Day as Wallpaper

This bash script downloads the Wikipedia Picture of the Day (POTD) and sets it as your KDE wallpaper. Works only with KDE 3.5 but probably could be adapted to KDE4 pretty easily. (Hint: Use dbus instead of dcop.) Written by Albert Thuswaldner. Wikipedia recently changed their POTD web pages, so I've made the necessary changes.

#!/bin/bash
# $Id: wikiwall 24 2007-09-02 20:00:08Z thuswa $
#
# Last modified Sun Sep 2 21:58:28 2007 on stalker
# Update count: 221
#
# Copyright (C) 2007 by Albert Thuswaldner, thuswa gmail com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Purpose:
#
# Wikiwall is a bash script which will set the "picture of the day"
# (POTD) from en.wikipedia.org as your KDE desktop wallpaper.
# If you set it to autostart it will give you a new fresh background
# as you login in, on a daily basis.
# The wallpaper pictures are saved in the /tmp directory, a history of one
# week back in time is kept. The script does some basic checking
# if wikipedia is reachable and if the wallpaper already is the current one.
#
# Usage:
# 1.) put in ~/bin
# 2.) make executable
# 3.) test it!
# 4.) (at your option) run: wikiwall --set-auto
# to make it autostart at login
# 5.) Have a great time - all the time! ;)
#
# Tested:
#
# On OpenSUSE 10.2, should work on most Linux distros, and other UNIX flavours
# too albeit the arguments for the ping command have to be modified.
#
# Note #1: The file size of the POTD's at wikipedia are sometimes
# quite large (~10Mb), so you with a "broadband" connection,
# BE WARE!
#
# Note #2: The picture files that are downloaded will be stored in the
# /tmp/kde- directory. This script does not delete
# any of these files, so it is up to you. However,
# the way the file name is constructed:
# "wikiwall.00N", where N equals the current weekday (N=1..7),
# does so that only a maximum of one week of PTOD's will be
# stored on your computer.
#
# Note #3: It is up to you the user of this script to make sure that
# by doing this you honour the license of the artworks that
# you download. This should not be a problem in most cases
# since using a picture on your own private computer
# desktop is considered fair use.

# Get date in correct format
TD=`date +%F` #YYYY-MM-DD
WDN=`date +%u` #Weekday number

#Set temp file name
TMPTPL=/tmp/kde-$USER/wikiwall.00

# Set wikiwall to autostart
if [ "$1" = "--set-auto" ]
then
ln -s $PWD/wikiwall $HOME/.kde/Autostart/wikiwall
fi

# Revert back to yesterdays picture
if [ "$1" = "--revert" ]
then
WDN=`expr $WDN - 1`
if [ $WDN -eq 0 ]
then
WDN=7
fi
TMPFILE=$TMPTPL$WDN
dcop kdesktop KBackgroundIface setWallpaper $TMPFILE 8 # 8=Scale and crop
exit
fi

# Set wikipedia specifics
SITE[1]=en.wikipedia.org
SITE[2]=commons.wikipedia.org
PAGE[1]=${SITE[1]}/wiki/Template:POTD/$TD
PAGE[2]=${SITE[2]}/wiki/Commons:Picture_of_the_day

# get current wallpaper
CURRPIC=`dcop kdesktop KBackgroundIface currentWallpaper 0`
TMPFILE=$TMPTPL$WDN

echo $CURRPIC
echo $TMPFILE

if [ "$CURRPIC" != "$TMPFILE" ]
then

# check if wikipedia is reachable
if ping -c 1 -q -W 2 -w 2 ${SITE[1]} >/dev/null
then

for N in 1 2; do
# extract picture of the day url
POTD=`wget -qO - ${PAGE[$N]} | \
grep File: | sed -e "s,.*href=\",," -e "s,\",," | cut -d ' ' -f 1`

echo ${PAGE[$N]}

POTMP=${SITE[$N]}
POTDURL=$POTMP$POTD
# echo $POTDURL

# extract picture url
PICURL=`wget -qO - $POTDURL | \
grep Full\ resolution | sed -e "s,.*href=\",," -e "s,\",," | cut -d '>' -f 1`

if [ -n "$PICURL" ]
then
echo $PICURL

# get picture
wget -q -O $TMPFILE $PICURL

# set picture as desktop background
dcop kdesktop KBackgroundIface setWallpaper $TMPFILE 8 # 8=Scale and crop
exit
else
if [ $N -eq 1 ]
then
echo no PTOD found, fallback.
else
echo no PTOD found, no new wallpaper set.
fi
fi
done
else
echo wikipedia is unreachable, no new wallpaper set.
fi
else
echo wallpaper already set to current. Resetting.
dcop kdesktop KBackgroundIface setWallpaper $TMPFILE 8 # 8=Scale and crop
fi