Class: Crawline::Downloader
- Inherits:
-
Object
- Object
- Crawline::Downloader
- Defined in:
- lib/crawline.rb
Instance Method Summary collapse
- #download_with_get(url) ⇒ Object
-
#initialize(user_agent) ⇒ Downloader
constructor
A new instance of Downloader.
Constructor Details
#initialize(user_agent) ⇒ Downloader
Returns a new instance of Downloader
11 12 13 14 15 16 |
# File 'lib/crawline.rb', line 11 def initialize(user_agent) @logger = CrawlineLogger.get_logger @logger.debug("Downloader#initialize: start") @user_agent = user_agent end |
Instance Method Details
#download_with_get(url) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/crawline.rb', line 18 def download_with_get(url) @logger.debug("Downloader#download_with_get: start: url=#{url}") uri = URI(url) req = Net::HTTP::Get.new(uri) req["User-Agent"] = @user_agent req["Accept"] = "*/*" http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = true if url.start_with?("https://") @logger.debug("Downloader#download_with_get: request start") res = http.start do http.request(req) rescue Net::OpenTimeout raise DownloadError.new("timeout") end @logger.debug("Downloader#download_with_get: request end: response=#{res}") case res when Net::HTTPSuccess @logger.debug("Downloader#download_with_get: status is success") result = { "url" => url, "request_method" => "GET", "request_headers" => {}, "response_headers" => {}, "response_body" => res.body, "downloaded_timestamp" => Time.now().utc() } req.each_key { |k| result["request_headers"][k] = req[k] } res.each_key { |k| result["response_headers"][k] = res[k] } result when Net::HTTPRedirection @logger.debug("Downloader#download_with_get: status is redirection") redirect_url = URI.join(url, res["location"]).to_s result = download_with_get(redirect_url) result["url"] = url result else @logger.debug("Downloader#download_with_get: status is else: code=#{res.code}, #{res.}") raise DownloadError.new(res.code) end end |