- 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

@ -33,6 +33,7 @@ Usage: #{$0.sub(/.*\//, "")} [options]
-h, --help show this message
-l, --list list podcasts
-p, --podcast <podcast> only fetch <podcast>
-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