forked from boncey/ruby-podcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
podcast.rb
134 lines (112 loc) · 3.01 KB
/
podcast.rb
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require 'rubygems'
require 'podcast/version'
require 'find'
require 'rss/0.9'
require 'mp3info'
require 'rss/maker'
require 'uri'
module Podcast
class Feed
attr_reader :title, :link, :description, :image, :base, :language, :version, :about
attr_writer :title, :link, :description, :image, :base, :language, :version, :about
include Enumerable
def initialize
@mp3s = []
@language = "en-us"
@about = "Made with #{NAME}"
@base = ''
end
# add an mp3 file to the podcast
def add_mp3(file)
begin
mp3 = Mp3File.new(file)
@mp3s.push(mp3)
rescue Mp3InfoError => e
puts "Skipping #{file} as it has a problem: #{e}"
end
end
def valid?
title != nil && description != nil && link != nil
end
# add a directory location to the podcast
# the directory will be recursively search
# for mp3 files.
def add_dir(dir)
# we chdir into the directory so that file paths will be relative
pwd = Dir.pwd
Dir.chdir(dir)
Find.find('.') do |f|
if (f =~ /\.mp3$/ && File.size?(f))
f.sub!(%r{^./}, '') # remove leading './'
add_mp3(f)
end
end
# go back to original directory
Dir.chdir(pwd)
end
# the total amount of files in the podcast
def size
@mp3s.size
end
# write the podcast file
def get_rss
#version = "1.0" # ["0.9", "1.0", "2.0"]
version = @version
content = RSS::Maker.make(@version) do |m|
m.channel.title = @title
m.channel.description = @description
m.channel.link = @link
m.channel.language = @language
m.channel.about = @about
m.items.do_sort = true # sort items by date
m.channel.updated = Time.now.to_s
m.channel.author = NAME
if @image != nil
m.image.url = @image
m.image.title = @title
end
for mp3 in @mp3s
item = m.items.new_item
item.title = mp3
## add a base url
if base != ''
link = base + '/' + URI::escape(mp3.path)
else
link = URI::escape(mp3.path)
end
item.link = link
item.date = mp3.mtime
item.enclosure.url = link
item.enclosure.length = mp3.length
item.enclosure.type = mp3.type
end
end
return content
end
def each
@songs.each do |s|
yield s
end
end
end
class Mp3File
attr_reader :artist, :album, :title, :file, :path, :length, :type, :mtime
attr_writer :artist, :album, :title, :file, :path, :length, :type, :mtime
def initialize(f)
file = File.new(f)
info = Mp3Info.open(f)
tag = info.tag()
@file = f
@artist = tag['artist']
@album = tag['album']
@title = tag['title']
@path = file.path()
@length = file.stat.size()
@mtime = file.stat.mtime()
@type = 'audio/mpeg'
end
def to_s
@title
end
end
end