ディレクトリにあるファイルをPodcastRSSにするだけの簡単なお仕事

#!/usr/bin/env ruby

require 'optparse'
require 'uri'
require 'rss/maker'

config = {}
ARGV.options do |o|
  o.on('-t VAL') {|v| config[:target]   = v }
  o.on('-b VAL') {|v| config[:base_uri] = URI.escape(File::dirname(v + '/a') + '/') }
  o.on('-o VAL') {|v| config[:output]   = v }
  o.on('--title VAL') {|v| config[:title]   = v }
  o.parse!
end

time = Time.now.strftime("%a, %d %b %Y %X +0900")

def get_type(f)
  case File::extname(f)
  when '.mp3'
    return 'audio/mpeg'
  when '.m4a'
    return 'audio/x-m4a'
  when '.mp4'
    return 'video/mp4'
  when '.m4v'
    return 'video/x-m4v'
  when '.mov'
    return 'video/quicktime'
  when '.pdf'
    return 'application/pdf'
  end
end

rss = RSS::Maker.make("2.0") do |m|
  rss_uri = config[:base_uri] + File::basename(config[:output])
  m.channel.about       = rss_uri
  m.channel.title       = config[:title] || ''
  m.channel.description = rss_uri
  m.channel.link        = rss_uri
  m.channel.pubDate     = time
  m.items.do_sort       = true

  Dir::glob(config[:target]).each do |f|
    i = m.items.new_item
    uri = URI.escape(config[:base_uri] + File::basename(f))
    time = File.mtime(f).strftime("%a, %d %b %Y %X +0900")

    i.author           = 'nobody@example.com'
    i.dc_creator       = 'nobody@example.com'
    i.description      = File::basename f
    i.title            = File::basename f
    i.link             = uri
    i.pubDate          = time
    i.date             = time
    i.content_encoded  = uri
    i.enclosure.url    = uri
    i.enclosure.length = File::stat(f).size.to_s
    i.enclosure.type   = get_type(f)
    i.guid.content     = uri
  end
end

File.open(config[:output], 'w') do |f|
  f.write rss.to_s
end

使い方

./mkpodcast.rb -t "/tmp/*.mp4" -b "http://localhost/" -o /tmp/podcast.xml --title "こんにちわこんにちわ"

なんか色々不満あるけど、使い捨てスクリプトにはこれくらいで十分かな。細かい使い方はソース嫁と言うことで。

追記

ファイルの最終更新時間でソートするようにしてみた