- add HTTP_PROXY environment handling

- add verbose option
- fix problems with weird webservers
This commit is contained in:
Ward Wouts 2007-07-17 20:08:21 +00:00
parent 55b43542e3
commit 3df8708096
2 changed files with 43 additions and 4 deletions

View file

@ -67,6 +67,11 @@
"savedir" => '/Users/ward/Private/mp3/[books]/Luke Burrage', "savedir" => '/Users/ward/Private/mp3/[books]/Luke Burrage',
"linkdir" => '/Users/ward/ipod_sync/[books]/Luke Burrage', "linkdir" => '/Users/ward/ipod_sync/[books]/Luke Burrage',
"rename" => [ /%20/, ' ' ], "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'
} }
} }

View file

@ -33,6 +33,7 @@ Usage: #{$0.sub(/.*\//, "")} [options]
-h, --help show this message -h, --help show this message
-l, --list list podcasts -l, --list list podcasts
-p, --podcast <podcast> only fetch <podcast> -p, --podcast <podcast> only fetch <podcast>
-v, --verbose be more verbose
EOT EOT
exit exit
end end
@ -44,7 +45,8 @@ def cmdline
[ "-c", "--configfile", GetoptLong::REQUIRED_ARGUMENT ], [ "-c", "--configfile", GetoptLong::REQUIRED_ARGUMENT ],
[ "-h", "--help", GetoptLong::NO_ARGUMENT ], [ "-h", "--help", GetoptLong::NO_ARGUMENT ],
[ "-l", "--list", 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 opts.quiet=true
@ -59,9 +61,18 @@ def cmdline
if options["-h"] if options["-h"]
usage usage
end end
if options["-v"]
@verbose = true
end
return options return options
end end
def verbose(msg)
if @verbose
puts msg
end
end
def readconfig(configfile=nil) def readconfig(configfile=nil)
configfile = configfile.nil? ? "#{ENV['HOME']}/.getdistortedrc" : configfile configfile = configfile.nil? ? "#{ENV['HOME']}/.getdistortedrc" : configfile
load configfile load configfile
@ -78,17 +89,38 @@ def fetch(uri_str, limit = 10)
# You should choose better exception. # You should choose better exception.
raise ArgumentError, 'HTTP redirect too deep' if limit == 0 raise ArgumentError, 'HTTP redirect too deep' if limit == 0
verbose("Fetching #{uri_str}\n\n")
host = URI.parse(uri_str).host host = URI.parse(uri_str).host
path = URI.parse(uri_str).path path = URI.parse(uri_str).path
query = URI.parse(uri_str).query query = URI.parse(uri_str).query
Net::HTTP.start(host) {|http| 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}") req = Net::HTTP::Get.new("#{path}?#{query}")
else
req = Net::HTTP::Get.new("#{path}")
end
req.basic_auth @user, @pass req.basic_auth @user, @pass
response = http.request(req) response = http.request(req)
case response case response
when Net::HTTPSuccess then 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::HTTPNotFound then puts "404 Not Found #{uri_str}"; response.error!
when Net::HTTPUnauthorized then puts "401 Authorization Required #{uri_str}"; response.error! when Net::HTTPUnauthorized then puts "401 Authorization Required #{uri_str}"; response.error!
else else
@ -172,6 +204,7 @@ def getcasts(podcast=nil)
xmldoc.elements.each("rss/channel") {|item| xmldoc.elements.each("rss/channel") {|item|
item.each_element{|x| item.each_element{|x|
if x.name == "item" if x.name == "item"
verbose("Item found:\n#{x}\n\n")
files.push getenclosure(podcast, x) files.push getenclosure(podcast, x)
end end
} }
@ -193,6 +226,7 @@ def getcasts(podcast=nil)
end end
end end
@verbose = false
options = cmdline options = cmdline
readconfig readconfig
options["-l"].nil? || listpodcasts options["-l"].nil? || listpodcasts