# Download internet radio playlists from somafm.com, # ready to open and play in a mediaplay (like VLC or XMMS). # # Written by Michiel Overtoom, motoom@xs4all.nl, november 2009 - january 2010. import urllib2 import BeautifulSoup import re def download_playlist(name): ''' Retrieve a playlist from somafm.com and save to a local file. The channel name should be a bare name, such as "bootliquor" or "lush". ''' doc = urllib2.urlopen("http://www.somafm.com/startstream=%s.pls" % name) contents = doc.read() f = open("somafm.%s.pls" % name,"wt") f.write(contents) f.close() if __name__ == "__main__": # Retrieve the somafm.com homepage and pry the channel names from it. re_channelname = re.compile("/([0-9a-z]*)/") # isolate 'doomed56' from a string as ''. doc = urllib2.urlopen("http://www.somafm.com") soup = BeautifulSoup.BeautifulSoup(doc) # print soup channels = set() for a in soup.findAll("a"): href = a['href'] print href m = re_channelname.search(href) if m: channels.add(m.group(1)) # Download a playlist for every channel found. for channel in channels: print "Downloading '%s' playlist" % channel try: download_playlist(channel) except urllib2.HTTPError: print " This channel is nonexistent; ignoring" ''' Luyt: use more whitespace ;) Rhamphoryncus: Ah yes, PEP8 compliance. Thanks for the tip. girasquid: so you would construct a python object to be converted to XML? Luyt: rename "r" to something more descriptive. Add a main function Luyt: I'd also put a blank line after the imports, before building rechannelname Rhamphoryncus: And 'rechannelname' was global unnecessarily too, I moved it in a more appropriate scope. Luyt, Looks fine. Don't use + for string templating. Put underscores to separate works. Use a comment to explain squashed errors. Luyt, Also, BS is deprecated. Luyt, Right, I usually put a comment if I make an exception pass silently. Luyt, (If I imported your module and used downloadplaylist, I might be surprised that bad URLs failed silently.) '''