2009-09-05
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 audio broadcast 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:
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:
Well there you have it. Read it, change it, use it.
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:
#!/usr/bin/env python import sys #we will need to parse xml from xml.dom import minidom #we need to get files from the interpipesnetweb import urllib #we want to run a subprocess import subprocess #define the parser class class RSSParser(): def __init__(self,url): self.rssUrl = url def getLatest(self): try: print "checking "+self.rssUrl+"..." #read the text from the URL rsstext = urllib.urlopen(self.rssUrl) print "parsing..." #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[0] #get the url from the first enclosure audio_url = enclosure.attributes['url'].value return audio_url except: return "" if __name__=="__main__": #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[1] #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 subprocess.call([audio_player,file_to_play])
Well there you have it. Read it, change it, use it.
Comments