Subscribing to podcasts from a local directory

At this moment I’m subscribed to the IT Conversations, Rails and Argos podcasts. Occasionally I download a single podcast that seems interesting. But I never seem to listen to these single podcasts. Finding them in my library seems to much of a hassle.

So I created a little Ruby script that creates a podcast rss feed from a given directory. Too bad iTunes doesn’t recognize file://... as a podcast url. Specifying the server makes the script less clean.

#!/usr/bin/ruby -w
#
# Creates a rss feed of podcasts in specified location on server
#

require 'find'
require 'rss/maker'
require 'uri'
require 'htmlentities'

fail "\nUsage: " + File.basename($0) + " feed_dir podcast_url .......\n" unless ARGV.size > 1

feed_dir, podcast_url = $*[0], $*[1]
feed_file = File.basename(podcast_url)
server_dir = File.dirname(podcast_url) + "/" 

def escape_xml text
    text.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;').gsub("'", '&apos;').gsub('"', '&quot;')
end

def find_files(dir)
    files = Array.new
    Find.find(dir) { |file| files << file unless (File.basename(file)[0, 1] == ".") }
    files
end

rss = RSS::Maker.make("2.0") do |maker|
    maker.channel.title = "Podcasts from #{feed_dir}".encode_entities
    maker.channel.link = podcast_url  
    maker.channel.description = maker.channel.title
    find_files(feed_dir).sort { | y, x | 
        File.mtime(x) <=> File.mtime(y)
    }.collect { |file|
        item = maker.items.new_item
        item.title = File.basename(file).encode_entities
        item.date = File.mtime(file)        
        item.link =
            item.guid.content =
            item.enclosure.url =
            URI.escape((file).gsub(feed_dir, server_dir))
        item.enclosure.length = File.stat(file).size
        item.enclosure.type = "audio/mpeg" 
    }
end

podCastFile = File.new(feed_dir + feed_file , "w")
podCastFile.puts rss
podCastFile.close

You can call this script from your crontab

rss_podcast_dirs.rb /directory/with/podcasts http://localhost/your/podcast/podcasts.rss
.

admin