updatepodcastdir.py
Description
updatepodcastdir.py is a Python script to maintain a podcast RSS feed. It scans the MP3 files in a directory, and based on data in their ID3 tags, generates an RSS feed that can be published on your website. I download a lot of stories from NPR, but I needed to have a simple way to share these with my wife, who uses an iPod on her Mac. Podcasting was the way to go.
Requirements
- Python (I use version 2.4.2, probably works with other versions)
Download
Configuration
When describing how to configure this script, it's useful to understand what it does. It takes a template for an RSS feed (simply an XML file) that you have filled out with your website's information. This template should not have any <item> links; these will be added by the script.
The script looks for MP3 files in a particular directory. For each MP3 found, it looks at the ID3 tags to generate an <item> element for that file. It also deletes any old MP3 files (this is configurable).
Once the feed has been constructed, it is written to a directory on your web server, and is available for users to access. Here's a diagram that illustrates the process.
There are two places where configuration data is stored: your RSS template file, and a file called updatepodcastdir.config.
RSS template
This file will be used as the basis for the RSS feed that the script will publish. Here is an example:
<?xml version="1.0"?> <rss version="2.0"> <channel> <title>My Radio Programs</title> <link>http://mpickering.homeip.net/phpwiki/index.php/updatepodcastdir.py</link> <description>Things Mike is listening to</description> <language>en-us</language> <lastBuildDate></lastBuildDate> <webMaster>mpicker0@yahoo.com</webMaster> <ttl>120</ttl> </channel> </rss>
You should put in whatever values you wish for your site, but leave the structure of the file unchanged. <lastBuildDate> is filled in by updatepodcastdir.py, and is the date and time that it was run.
updatepodcastdir.config
Here is an example:
[paths] mp3path: /var/www/localhost/htdocs/podcast rsstemplate: /home/mpickering/podcast/template.rss rssoutfile: /var/www/localhost/htdocs/podcast/shows.rss [web] urlprefix: http://mpickering.homeip.net/podcast/ [misc] maxitems: 15
Now, a description of the fields:
-
[paths] contains pathnames that will be resolved on the machine where the script is running.
- mp3path is the directory where MP3s will be stored. updatepodcastdir.py will scan this directory for MP3 files. This path should be accessible to your web server, since it's where web users will be expecting the MP3s to live.
- rsstemplate is the path to the RSS template (described above). It doesn't need to be web-accessible.
- rssoutfile is the filename where the RSS feed will be written, once it has been generated. This file should be accessible via your web server, since that's what web users will subscribe to.
-
[web] contains addresses that will be resolved by users via the web
- urlprefix is the prefix to attach to MP3 files. If you had a file called myfile.mp3 and used the urlprfix value from above, the URL presented to users would be http://mpickering.homeip.net/podcast/myfile.mp3
-
[misc] contains other settings
- maxitems specifies the maximum number of links you want in your podcast. If you have more than this number of MP3 files in the MP3 directory, the oldest ones will be deleted. This lets you move new files into the directory, and push the older files out.
Usage
Usage is simple, just run
updatepodcastdir.py
This reads updatepodcastdir.config from the current directory. If you want to specify a different configuration file, you can run it like this:
updatepodcastdir.py /path/to/other/config.file
The RSS file
The program uses ID3 tags to determine what values to give the <item> elements.
<item> fields
- <title> is taken from the ID3 field title
- <link> is taken from the ID3v2 field wwwaudiofile
- <description> is taken from the ID3 comment (COMM) field
- <pubDate> is taken from the "last modified" date on the MP3 file
-
<enclosure> is the link to the file.
- The url attribute is generated by concatenating the urlprfix from the configuration file, and the filename
- The length attribute is simply the number of bytes in the MP3 file
- The type attribute is always audio/mpeg