diff --git a/getdistorted/getdistorted.conf b/getdistorted/getdistorted.conf index 33e91a5..a2bccc0 100644 --- a/getdistorted/getdistorted.conf +++ b/getdistorted/getdistorted.conf @@ -67,6 +67,11 @@ "savedir" => '/Users/ward/Private/mp3/[books]/Luke Burrage', "linkdir" => '/Users/ward/ipod_sync/[books]/Luke Burrage', "rename" => [ /%20/, ' ' ], + }, + "the_skeptics_guide_to_the_universe" => { + "rss" => 'http://www.theskepticsguide.org/rss.xml', + "savedir" => '/Users/ward/Private/mp3/[books]/The Skeptics Guide', + "linkdir" => '/Users/ward/ipod_sync/[books]/The Skeptics Guide' } } diff --git a/getdistorted/getdistorted.rb b/getdistorted/getdistorted.rb index 32b689e..e23f1b8 100755 --- a/getdistorted/getdistorted.rb +++ b/getdistorted/getdistorted.rb @@ -33,6 +33,7 @@ Usage: #{$0.sub(/.*\//, "")} [options] -h, --help show this message -l, --list list podcasts -p, --podcast only fetch +-v, --verbose be more verbose EOT exit end @@ -44,7 +45,8 @@ def cmdline [ "-c", "--configfile", GetoptLong::REQUIRED_ARGUMENT ], [ "-h", "--help", GetoptLong::NO_ARGUMENT ], [ "-l", "--list", GetoptLong::NO_ARGUMENT ], - [ "-p", "--podcast", GetoptLong::REQUIRED_ARGUMENT ] + [ "-p", "--podcast", GetoptLong::REQUIRED_ARGUMENT ], + [ "-v", "--verbose", GetoptLong::NO_ARGUMENT ] ) opts.quiet=true @@ -59,9 +61,18 @@ def cmdline if options["-h"] usage end + if options["-v"] + @verbose = true + end return options end +def verbose(msg) + if @verbose + puts msg + end +end + def readconfig(configfile=nil) configfile = configfile.nil? ? "#{ENV['HOME']}/.getdistortedrc" : configfile load configfile @@ -78,17 +89,38 @@ def fetch(uri_str, limit = 10) # You should choose better exception. raise ArgumentError, 'HTTP redirect too deep' if limit == 0 + verbose("Fetching #{uri_str}\n\n") + host = URI.parse(uri_str).host path = URI.parse(uri_str).path query = URI.parse(uri_str).query - Net::HTTP.start(host) {|http| - req = Net::HTTP::Get.new("#{path}?#{query}") + proxy_host = nil + proxy_port = nil + proxy_user = nil + proxy_pass = nil + + if ENV['HTTP_PROXY'] + uri = URI.parse(ENV['HTTP_PROXY']) + proxy_host = uri.host + proxy_port = uri.port + proxy_user, proxy_pass = uri.userinfo.split(/:/) if uri.userinfo + end + + Net::HTTP::Proxy(proxy_host, proxy_port, + proxy_user, proxy_pass).start(host) {|http| + if query + req = Net::HTTP::Get.new("#{path}?#{query}") + else + req = Net::HTTP::Get.new("#{path}") + end req.basic_auth @user, @pass response = http.request(req) case response when Net::HTTPSuccess then response - when Net::HTTPRedirection then fetch(response['location'], limit - 1) + when Net::HTTPRedirection then + verbose("Redirecting to #{response['location']}") + fetch(response['location'], limit - 1) when Net::HTTPNotFound then puts "404 Not Found #{uri_str}"; response.error! when Net::HTTPUnauthorized then puts "401 Authorization Required #{uri_str}"; response.error! else @@ -172,6 +204,7 @@ def getcasts(podcast=nil) xmldoc.elements.each("rss/channel") {|item| item.each_element{|x| if x.name == "item" + verbose("Item found:\n#{x}\n\n") files.push getenclosure(podcast, x) end } @@ -193,6 +226,7 @@ def getcasts(podcast=nil) end end +@verbose = false options = cmdline readconfig options["-l"].nil? || listpodcasts