While programming the rotary phone input device
, I needed a way to play the latest version of one of my favorite audcasts(yea, I said audcast because it is an aud
and I don't have an ipod). OK enough of that, let's get back to the problem at hand. The audcast uses RSS
to publish new episodes of their show, and since an RSS feed is a standarized XML file, I should be able to get all the info I need fairly easily.
The basic functionality that I needed was:
- get the XML text from the URL of the RSS feed
- process the XML text and get the URL of the first audio file in the XML
- play the audio file over the internet; fortunately, I know of a great command-line audio player that can do that. Mplayer will do that as well.
There are some people with powerful command-line foo who could probably write a five line bash script with wget and awk to do this, but I'm not one of those people so I decided to use the Python programming language and the code is as follows:
#we will need to parse xml
from xml.dom import minidom
#we need to get files from the interpipesnetweb
#we want to run a subprocess
#define the parser class
self.rssUrl = url
print "checking "+self.rssUrl+"..."
#read the text from the URL
rsstext = urllib.urlopen(self.rssUrl)
#parse the xml from the rss text
xmlDocElement = minidom.parse(rsstext).documentElement
#make an array of the enclosure elements
enclosures = xmlDocElement.getElementsByTagName("enclosure")
#get the first enclosure
enclosure = enclosures
#get the url from the first enclosure
audio_url = enclosure.attributes['url'].value
#set a default rss url
rss_url = "http://feeds.feedburner.com/linuxoutlaws-ogg"
#what shall we use to play the audio?
audio_player = "sap"
# has the user requested a different url?
if len(sys.argv) > 1:
rss_url = sys.argv
#make a parser
parser = RSSParser(rss_url)
#get the file we want to play
file_to_play = parser.getLatest()
#did a file get returned?
if len(file_to_play) > 0:
#launch the player with the file to play
Well there you have it. Read it, change it, use it.