split off configuration, add some commandline options

This commit is contained in:
Ward Wouts 2007-02-24 09:01:43 +00:00
parent 0938c8c2c8
commit 46c0a4caa6
2 changed files with 170 additions and 76 deletions

View file

@ -1,4 +1,4 @@
#!/opt/local/bin/ruby
#!/usr/bin/env ruby
# $Id$
# $URL$
@ -19,57 +19,60 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
########################################
# the configuration bit
@podcasts = {
# the podcast name
"distortedview" => {
# where's the rss file
"rss" => 'http://www.distortedview.com/show/index.xml',
# where should enclosures be saved
"savedir" => '/Users/ward/Private/mp3/[books]/Distorted View',
# rename magic, this is used on the file name with sub!
"rename" => [ /_(\d\d\d\d)(\d\d)/, '\2\1' ],
# directory to put symlinks to the new files
"linkdir" => '/Users/ward/ipod_sync/[books]/Distorted View',
# option to write an m3u file after fetching the mp3s
"m3u" => '/Users/ward/dv.m3u',
},
# "tagesschau" => {
# "rss" => 'http://www.tagesschau.de/export/podcast',
# "savedir" => '/Users/ward/ipod_sync/[books]/Tagesschau',
# # delete files that aren't in the rss anylonger
# "delete" => true,
# },
# "pennradio" => {
# "rss" => 'http://penn.freefm.com/pages/podcast/431.rss',
# # %%DATE%% is a magic word here, gets replaced by the pubdate in
# # YYYYMMDD format. Won't work if there is no pubDate in the rss feed
# "rename" => [ /^/, '%%DATE%%-' ],
# "savedir" => '/Users/ward/Private/mp3/[books]/PennRadio',
# "linkdir" => '/Users/ward/ipod_sync/[books]/PennRadio',
# },
"bsdtalk" => {
"rss" => 'http://feeds.feedburner.com/Bsdtalk',
"savedir" => '/Users/ward/Private/mp3/[books]/BSDTalk',
"linkdir" => '/Users/ward/ipod_sync/[books]/BSDTalk',
},
"radio rtfm" => {
"rss" => 'http://www.theregister.co.uk/odds/rtfm/headlines.rss',
"rename" => [ /^/, '%%DATE%%-' ],
"savedir" => '/Users/ward/Private/mp3/[books]/Radio RTFM',
"linkdir" => '/Users/ward/ipod_sync/[books]/Radio RTFM',
}
}
########################################
# code from here on
require 'net/http'
require 'uri'
require 'rexml/document'
require 'date'
require 'getoptlong'
def usage
puts <<EOT
Usage: #{$0.sub(/.*\//, "")} [options]
-c, --configfile <file> use configuration from <file> (default $HOME/.getdistortedrc)
-h, --help show this message
-l, --list list podcasts
-p, --podcast <podcast> only fetch <podcast>
EOT
exit
end
def cmdline
options = Hash.new
begin
opts = GetoptLong.new(
[ "-c", "--configfile", GetoptLong::REQUIRED_ARGUMENT ],
[ "-h", "--help", GetoptLong::NO_ARGUMENT ],
[ "-l", "--list", GetoptLong::NO_ARGUMENT ],
[ "-p", "--podcast", GetoptLong::REQUIRED_ARGUMENT ]
)
opts.quiet=true
opts.each do |opt, arg|
options[opt] = arg
end
rescue
print "#{$!}\n"
usage
end
if options["-h"]
usage
end
return options
end
def readconfig(configfile=nil)
configfile = configfile.nil? ? "#{ENV['HOME']}/.getdistortedrc" : configfile
load configfile
end
def listpodcasts
for podcast in @podcasts.keys.sort
puts podcast
end
exit
end
def fetch(uri_str, limit = 10)
# You should choose better exception.
@ -79,6 +82,7 @@ def fetch(uri_str, limit = 10)
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then fetch(response['location'], limit - 1)
when Net::HTTPNotFound then puts "404 Not Found #{uri_str}"; response.error!
else
response.error!
end
@ -113,9 +117,9 @@ def getenclosure(podcast, item)
if @podcasts[podcast]["rename"]
replacement = @podcasts[podcast]["rename"][1].dup
if replacement.match(/%%DATE%%/)
replacement.sub!(/%%DATE%%/, date)
replacement.gsub!(/%%DATE%%/, date)
end
filename.sub!(@podcasts[podcast]["rename"][0], replacement)
filename.gsub!(@podcasts[podcast]["rename"][0], replacement)
end
@filelist[filename] = true
if ! File.exists?("#{@podcasts[podcast]["savedir"]}/#{filename}")
@ -138,34 +142,51 @@ def getenclosure(podcast, item)
end
end
for podcast in @podcasts.keys.sort
files = Array.new
puts podcast
begin
res = fetch(@podcasts[podcast]["rss"])
@filelist = {}
# body.gsub!(/#{13.chr}#{10.chr}/, "#{10.chr}")
xmldoc = REXML::Document.new(res.body)
xmldoc.elements.each("rss/channel") {|item|
item.each_element{|x|
if x.name == "item"
files.push getenclosure(podcast, x)
end
}
}
if @podcasts[podcast]["delete"]
deleteold(podcast)
end
if @podcasts[podcast]["m3u"]
File.open(@podcasts[podcast]["m3u"], "w"){|f|
files.each{|file|
f.puts file
def getcasts(podcast=nil)
worklist = Array.new
if podcast.nil?
worklist = @podcasts.keys.sort
else
worklist.push podcast
end
for podcast in worklist
files = Array.new
puts podcast
begin
res = fetch(@podcasts[podcast]["rss"])
@filelist = {}
# body.gsub!(/#{13.chr}#{10.chr}/, "#{10.chr}")
xmldoc = REXML::Document.new(res.body)
xmldoc.elements.each("rss/channel") {|item|
item.each_element{|x|
if x.name == "item"
files.push getenclosure(podcast, x)
end
}
}
if @podcasts[podcast]["delete"]
deleteold(podcast)
end
if @podcasts[podcast]["m3u"]
File.open(@podcasts[podcast]["m3u"], "w"){|f|
files.each{|file|
f.puts file
}
}
end
rescue
rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Net::HTTPFatalError
puts " error #{$!} fetching, skipping"
end
rescue
rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Net::HTTPFatalError
puts " error #{$!} fetching, skipping"
end
end
options = cmdline
readconfig
options["-l"].nil? || listpodcasts
if options["-p"].nil?
getcasts
else
getcasts(options["-p"])
end