My xkcd script
Anyone whoâs come to joshh.co.uk recently may well notice that a new image has appeared at the bottom right of the site that displays the most recent xkcd comic (at least it might now I sorted out the cron stuff). If anyone is interested, here is the Python (again, sorry) script that performs the stuff.
#!/usr/bin/env python
"""Script to extract the most recent comic from the webcomic xkcd's RSS feed"""
import urllib
from xml.dom import minidom
usock = urllib.urlopen('http://xkcd.com/rss.xml')
xmldoc = minidom.parse(usock)
usock.close()
# warning, warning: nasty one-liner coming
imgname = xmldoc.firstChild.firstChild.childNodes[4].toxml().split(';')[2][:-5]
# sorry about that, sorted soon
filename, headers = urllib.urlretrieve(imgname,
'/home/josh/bin/xkcd-today.png')
That horrific one liner just does the chopping up of the XML in the RSS feed to cut it down to just the image URL. Obviously the filename in the last line should be changed to put the image file where you want it.
