Sunday, 23 November 2008

Trouble in Ambridge

My Dad likes the Archers, so does my mum. Up until about 5 years ago, My dad would record the omnibus onto cassette for playing in the car during commutes to work and back, likewise my mum would listen to it in the house on the hi-fi. Then my dad bought a car without a tape player, so I taught him how to record the omnibus using his computer (connected to the hifi with a line-in cable), this worked but required editing the audio file in Audacity which my dad never got on well with.

Finally earlier this year my Dad entered the 21st Century and began downloading the archers podcast from the BBC website. This was by far the simplest and quickest way. This was until the company my Dad works for banned multimedia content downloading on the company network. The only other connection in the house is dial-up so i promised to develop a work around.

I have written a python script which parses the RSS feed for the archers podcast, extracts the .mp3 links, downloads them to a temporary directory, compresses them into a single zip file which, with the help of a bash scrip, gets uploaded to my University web space for my dad to download every Saturday. The script is shown below, but the reason I blog about this is because for the first time in my linux lifetime, I have written a script to do a task in less time that it would have taken to just do it manually! (hence time to blog)

The only remaining task is to get the script to trigger automatically every Saturday without me having to remember. But Google calendar reminders will suffice for the time being!


import feedparser
import urllib
import os
import zipfile

ARCHERS_XML_URL = "http://downloads.bbc.co.uk/podcasts/radio4/archers/rss.xml"
TEMP_ADDRESS = "/tmp/archers/"

feed = feedparser.parse(ARCHERS_XML_URL)

shows = feed["entries"]
episodes = []

for show in shows:
mp3_url = str(show["links"][0]["href"])
print "Retrieving: "+mp3_url
fname = os.path.basename(mp3_url)
try:
f = urllib.urlretrieve(mp3_url, TEMP_ADDRESS+fname)
episodes.append(TEMP_ADDRESS+fname)
except IOError:
print "Couldn't retrieve file: "+mp3_url

urllib.urlcleanup()

all = zipfile.ZipFile(TEMP_ADDRESS+"archers.zip", 'w')
for file in episodes:
try:
all.write(file)
print "added "+file+" to zip archive archers.zip"
except:
print "Faile to write "+file+" to zip archive archers.zip"
all.close()
all = zipfile.ZipFile(TEMP_ADDRESS+"archers.zip", 'r')
if all.testzip() == None:
print "succesfully wrote episodes to zip file"
else:
print "failure writing episodes to zip file"
all.close()
print "end"

1 comment:

Gavin said...

My step dad just uses listen again, or the radio in his phone, but congrats on getting a linux script to work - I expect one that will brew coffee every morning next :P