From 368f45114f302848d7b524d78f3795e51ad4df76 Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Sat, 24 Feb 2007 14:57:47 +0000 Subject: [PATCH] add basic authentication --- getdistorted/getdistorted.rb | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/getdistorted/getdistorted.rb b/getdistorted/getdistorted.rb index d10985f..32b689e 100755 --- a/getdistorted/getdistorted.rb +++ b/getdistorted/getdistorted.rb @@ -78,14 +78,23 @@ def fetch(uri_str, limit = 10) # You should choose better exception. raise ArgumentError, 'HTTP redirect too deep' if limit == 0 - response = Net::HTTP.get_response(URI.parse(uri_str)) - 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 + 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}") + 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::HTTPNotFound then puts "404 Not Found #{uri_str}"; response.error! + when Net::HTTPUnauthorized then puts "401 Authorization Required #{uri_str}"; response.error! + else + response.error! + end + } end def deleteold(podcast) @@ -153,9 +162,11 @@ def getcasts(podcast=nil) files = Array.new puts podcast begin + @user = @podcasts[podcast]["username"].nil? ? nil : @podcasts[podcast]["username"] + @pass = @podcasts[podcast]["password"].nil? ? nil : @podcasts[podcast]["password"] 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|