public
Description: Various tools
Homepage:
Clone URL: git://github.com/archiloque/tools.git
tools / podcast.rb
100644 96 lines (85 sloc) 2.572 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Generate a podcast rss file from all the mp3 contained in a directory
# The mp3 are named "YYYY-MM-DD??????" and we get their title from their id3 tags
# Params
# - directory of mp3 files, default '.'
# - root url for the mp3s, default 'http:/localhost/'
# - the podcast title, default 'podcast'
# - image url
# - a target directory where the mp3 files will be copied with a short name if param is here, usefull if you have strange names that aren't url-firendly
# Julien Kirch 2009, available under MIT license
 
require 'rubygems'
require 'id3'
require 'date'
require 'parsedate'
require 'builder'
require 'rss/2.0'
 
# An item in the feed
class RssItem
  include Comparable
  attr_accessor :title, :file_name, :duration, :pub_date, :size
  
  def initialize
    title = ''
  end
  
  def to_s
    "\"#{file_name}\", \"#{title}\", #{duration}, #{pub_date}"
  end
  
  def <=> o
    pub_date <=> o.pub_date
  end
end
 
mp3_directory = ARGV[0] || '.'
root_url = ARGV[1] || 'http://localhost/'
title = ARGV[2] || 'podcast'
image_url = ARGV[3]
target_directory = ARGV[4]
 
mp3s = Array.new
Dir.chdir(mp3_directory)
Dir.glob('*.mp3') do |file_name|
  item = RssItem.new
  file = ID3::AudioFile.new file_name
  if file.tagID3v1
    item.title = file.tagID3v1['TITLE']
  end
  if item.title.nil? || item.title.empty?
    item.title = File.basename(file_name, '.*')
  end
  item.duration = file.audioLength
  date = ParseDate.parsedate file_name[0,10]
  item.pub_date = Time.gm(date[0], date[1], date[2], 0, 0, 0)
  item.size = File.size(file_name)
  if target_directory
    target_name = sprintf("%03d", mp3s.size + 1) + File.extname(file_name)
    File.copy(file_name, target_directory + target_name)
    item.file_name = target_name
  else
    item.file_name = file_name
  end
  mp3s << item
end
 
mp3s.sort
 
result = ''
xml = Builder::XmlMarkup.new(:target=> result, :indent => 2)
xml.instruct! 'xml', :version => '1.0', :encoding => 'UTF-8'
xml.rss(:version=>"2.0", 'xmlns:itunes' =>'http://www.itunes.com/dtds/podcast-1.0.dtd') {
  xml.channel {
    xml.title title
    xml.description title
    if image_url && (!image_url.empty?)
      xml.image {
        xml.url image_url
        xml.title title
        xml.link root_url
      }
    end
    mp3s.each do |mp3|
      xml.item {
        xml.title mp3.title
        xml.link(root_url + mp3.file_name)
        xml.enclosure(:url => (root_url + mp3.file_name), :length => mp3.size, :type => 'audio/mpeg')
        xml.guid(root_url + mp3.file_name)
        xml.pubDate mp3.pub_date.rfc2822
      }
    end
  }
}
print result