Commits

Anonymous committed 5e9b77e

Basic changes to podcastbaker:
* Formatting and spelling
* Customized parameters for breakfast.vu0.org
* Quote URL
* Guard against missing ID3 fields
* Use ctime instead of filename for pubDate

Comments (0)

Files changed (2)

 
 podcastbaker.py
 
-* Generate pubDate based on ctime of the file.
 * Generate GUID per item.
 * Generate recommended iTunes elements.
-#!/opt/local/bin/python
+#!/usr/bin/env python
+
 #
-# PodcastBaker is an easy way to create an iTunes compatable Podcast
+# PodcastBaker is an easy way to create an iTunes compatible podcast
 # from the media files in a directory.
+#
 # Copyright (C) 2011 Noel Schutt
+#
+# Modifications by Boris Buegling
+#
 
-"""PodcastBaker is an easy way to create an iTunes compatable Podcast
+"""PodcastBaker is an easy way to create an iTunes compatible podcast.
 
 This script will read all the files in a folder that match a filter,
-creating a Podcast XML file based on the file metadata. For MP3 files,
+creating a podcast XML file based on the file metadata. For MP3 files,
 the data is pulled from the ID3v2 metadata.
 """
 
-
-version = (0, 1)
+version = (0, 2)
 version_string = ".".join(map(str, version))
 
 from mutagen.mp3 import MP3 # only import for MP3 files for now
 import glob
 import os.path
-from time import strptime, strftime
+from time import localtime, strftime
+from urllib import quote 
 from xml.etree.ElementTree import Element, SubElement, tostring
 
-#============ Settings ============
-# Edit the podcast_info to customize the podcast.
-# If the podcast episodes aren't by date, you'll have more
-# editing to do.
-
-date_format = '%Y-%m-%d' # date format used in XML file
-infile_date_format = '%Y-%m-%d' # used if input file is a different date format
-file_filter = '/Users/noel/Sites/tec/sermons/*.mp3' # absolute path to files
-
 #==== Podcast General Info ===
 # base_url is the location of the files
 # home_url is a link to a webpage for the podcasts
 # title is the title of the podcast as it will appear in iTunes
 # description is a general description of the podcast
-podcast_info = { 'base_url':'http://example.com/podcasts/',
-                 'home_url':'http://example.com/',
-                 'title':'My Podcast',
-                 'description':'My personal Podcast feed.'
+podcast_info = { 'base_url':'http://breakfast.vu0.org/wp-content/uploads/',
+                 'home_url':'http://breakfast.vu0.org',
+                 'title':'Breakfast @manuspielt\'s',
+                 'description':'Der taegliche Videospiel-Podcast.'
                }
 
 #============ END Settings ============
 
+def safe_get_value(dictionary, key):
+	if not dictionary.has_key(key):
+		return ''
+	return dictionary[key]
+
+def timeAsrfc822 (theTime):
+	import rfc822
+	theTime = localtime(theTime)
+	return rfc822.formatdate(rfc822.mktime_tz(rfc822.parsedate_tz(strftime("%a, %d %b %Y %H:%M:%S", theTime))))
 
 top = Element('rss')
 top.set('version', '2.0')
 top.set('xmlns:itunes', 'http://www.itunes.com/dtds/podcast-1.0.dtd')
 
-
 chan = SubElement(top, 'channel')
 
 chan_title = SubElement(chan, 'title')
 chan_description = SubElement(chan, 'description')
 chan_description.text = podcast_info['description']
 
-
-
-for afile in glob.iglob(file_filter) :
-
+for afile in glob.iglob('./*.mp3') :
     audio = MP3(afile)
-    artist = audio["TPE1"]
-    album = audio["TALB"]
+    artist = safe_get_value(audio, "TPE1")
+    album = safe_get_value(audio, "TALB")
 
     item = SubElement(chan, 'item')
     
     podcast_title = SubElement(item, 'title')
-    podcast_title.text = str(audio["TIT2"])
+    podcast_title.text = str(safe_get_value(audio, "TIT2"))
 
     podcast_author = SubElement(item, 'itunes:author')
-    podcast_author.text = str(audio["TPE1"])
+    podcast_author.text = str(safe_get_value(audio, "TPE1"))
 
     podcast_pubDate = SubElement(item, 'pubDate')
-    try :
-        podcast_pubDate.text = strftime(date_format, strptime(os.path.splitext(os.path.split(afile)[1])[0], infile_date_format))
-    except :
-        pass
+    podcast_pubDate.text = timeAsrfc822(os.path.getctime(afile))
 
-    podcast_enclosure = SubElement(item, 'enclosure', {'url':podcast_info['base_url']+os.path.split(afile)[1], 'length':str(int(audio.info.length)), 'type':'audio/mpeg'})
+    fileUrl = quote(os.path.split(afile)[1])
+    podcast_enclosure = SubElement(item, 'enclosure', {'url':podcast_info['base_url']+fileUrl,
+    									'length':str(int(audio.info.length)), 'type':'audio/mpeg'})
 
     podcast_duration = SubElement(item, 'itunes:duration')
     podcast_duration.text = str(int(audio.info.length))
     
 print '<?xml version="1.0"?>'
-print tostring(top)
-
+print tostring(top)