From 46c0a4caa6a354a2333917d37486935ae1b13ee7 Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Sat, 24 Feb 2007 09:01:43 +0000 Subject: [PATCH] split off configuration, add some commandline options --- getdistorted/getdistorted.conf | 73 ++++++++++++++ getdistorted/getdistorted.rb | 173 ++++++++++++++++++--------------- 2 files changed, 170 insertions(+), 76 deletions(-) create mode 100644 getdistorted/getdistorted.conf diff --git a/getdistorted/getdistorted.conf b/getdistorted/getdistorted.conf new file mode 100644 index 0000000..33e91a5 --- /dev/null +++ b/getdistorted/getdistorted.conf @@ -0,0 +1,73 @@ +# $Id: getdistorted.rb 517 2006-11-15 22:11:39Z ward $ +# $URL: svn+ssh://svn/bigdisk/subversion/wpsvnroot/trunk/scripts/getdistorted/getdistorted.rb $ + +# +# Copyright (c) 2006 Ward Wouts +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# 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', + "rss" => 'http://www.superfreaksideshow.com/members2/show/?feed=rss2', + "username" => "ward@wouts.nl", + "password" => "#{File.open("#{ENV['HOME']}/.dvpass").readline}", + # 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', + }, + "luke_burrage" => { + "rss" => 'http://www.lukeburrage.com/audio/jugglingpodcast.rss.xml', + "savedir" => '/Users/ward/Private/mp3/[books]/Luke Burrage', + "linkdir" => '/Users/ward/ipod_sync/[books]/Luke Burrage', + "rename" => [ /%20/, ' ' ], + } +} + +# vi: filetype=ruby diff --git a/getdistorted/getdistorted.rb b/getdistorted/getdistorted.rb index 59c7363..d10985f 100755 --- a/getdistorted/getdistorted.rb +++ b/getdistorted/getdistorted.rb @@ -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 < use configuration from (default $HOME/.getdistortedrc) +-h, --help show this message +-l, --list list podcasts +-p, --podcast only fetch +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