I wrote the following little shell script and published it on GitHub. In the repo there's also a README file that explains how to use it in more detail.
This is the first time I open-sourced/published something I wrote, so I'm interested whether you think that this code is ready for the public.
Some pointers, but feel free to add your own thoughts:
- Does it look professional? If not, what would need to be done?
- Are the comments too much? Was it ok to publish it, although I know that under some circumstances it doesn't work (I wrote about that in the README)?
# random xkcd wallpaper: gets a random xkcd comic from xkcd.com and sets it as the desktop background.
#!/bin/bash
# /random/comic redirects to random comic. Wget gets the index.html of the comic.
wget http://dynamic.xkcd.com/random/comic/
echo $(pwd)
#Searches the line in the index.html file that points to the url where the actual comic is placed.
#The image urls are of the form: http://imgs.xkcd.com/comics/.'name of comic'.(png | jpg)
url=$(cat index.html | grep -o -m 1 http://imgs.xkcd.com/comics/.*\.png)
#Assuming picture format is .png. Gets the name of the image file by only matching what comes after the last forward slash.
name_pic=$(echo $url | grep -o [^/]*\.png)
is_png=1
#Sets url and name_pic in the case of the picture being in .jpg format.
if [ -z "$url" ]
then
url=$(cat index.html | grep -o -m 1 http://imgs.xkcd.com/comics/.*\.jpg)
name_pic=$(echo $url | grep -o [^/]*\.jpg)
is_png=0
fi
#Downloads the image and saves it under its appropriate name.
wget --output-document="$name_pic" "$url"
#Sets the desktop background
gconftool-2 --set --type=string /desktop/gnome/background/picture_filename $(pwd)/"$name_pic"
#Cleans up
rm index.html
#For some reason, if the image is moved to fast (e.g without a wait) the background does not get set.
sleep 1
#The current wallpaper can always be found under "current_xkcd_wallpaper.png" or "current_xkcd_wallpaper.jpg". Also prevents cluttering the directory with images.
#If you want to keep all the pictures you downloaded, uncomment the following lines.
#Cleans up. Makes sure that there is only one current_xkcd_wallpaper image file.
#rm current_xkcd_wallpaper.*
rm current_xkcd_wallpaper.*
if [ $is_png = 1 ] ; then
mv $(pwd)/"$name_pic" $(pwd)/current_xkcd_wallpaper.png
else
mv $(pwd)/"$name_pic" $(pwd)/current_xkcd_wallpaper.jpg
fi