#!/usr/bin/python # Simple Hospital Records podcast interface # Jack Weeden 2010 # http://www.ajack.org import sys import getopt import urllib import xml.dom.minidom as minidom from dateutil import parser dparser = parser.parser(); url = 'http://podcast.hospitalrecords.com/HospitalRecordsPodcast.xml' def strip(xmlstr, tag): return xmlstr.replace('<' + tag + '>', '').replace('', '') def listpodcasts(): rawxml = urllib.urlopen(url).read() xml = minidom.parseString(rawxml) items = xml.getElementsByTagName('item') i = 1 print '' for item in items: print '(' + str(i) + ') ' + strip(item.getElementsByTagName('title')[0].toxml(), 'title') i += 1 print '' def downloadpodcast(num): rawxml = urllib.urlopen(url).read() xml = minidom.parseString(rawxml) items = xml.getElementsByTagName('item') if num > len(items): print 'Invalid podcast number, use --list to show available podcasts' else: mp3url = items[num].getElementsByTagName('enclosure')[0].attributes['url'].value print 'Downloading mp3...' remotefile = urllib.urlopen(mp3url) filename = strip(items[num].getElementsByTagName('title')[0].toxml(), 'title').replace(' ', '_') + ".mp3" outfile = open(filename, 'w') outfile.write(remotefile.read()) outfile.close(); remotefile.close() print 'Done'; def podcastinfo(num): rawxml = urllib.urlopen(url).read() xml = minidom.parseString(rawxml) items = xml.getElementsByTagName('item') if num > len(items): print 'Invalid podcast number, use --list to show available podcasts' else: title = strip(items[num].getElementsByTagName('title')[0].toxml(), 'title') pubdate = strip(items[num].getElementsByTagName('pubDate')[0].toxml(), 'pubDate') ts = dparser.parse(pubdate) timestamp = ts.strftime('%a %d %B %Y') info = strip(items[num].getElementsByTagName('description')[0].toxml(), 'description') print '\n' + title + '\n' + timestamp + '\n\n' + info + '\n' def usage(): print 'Usage: \n' print sys.argv[0] + ' --list\tLists recent podcasts' print sys.argv[0] + ' --info n\tShows information (track listing, date) for podcast n' print sys.argv[0] + ' --get n\tDownload podcast n' try: opts, args = getopt.getopt(sys.argv[1:], 'li:g:h', ['list', 'info=', 'get=', 'help']) except getopt.GetoptError: usage() sys.exit(1) if opts == []: usage() sys.exit(1) for opt, arg in opts: if opt in ('--list', '-l'): listpodcasts() elif opt in ('--info', '-i'): podcastinfo(int(arg)) elif opt in ('--get', '-g'): downloadpodcast(int(arg)) elif opt in ('--help', '-h'): usage()