class Oddb2xml::Downloader

Attributes

agent[R]
file2save[R]
type[R]
url[R]

Public Class Methods

new(options = {}, url = nil) click to toggle source
# File lib/oddb2xml/downloader.rb, line 42
def initialize(options = {}, url = nil)
  @options = options
  @url = url
  @retry_times = 3
  HTTPI.log = false # disable httpi warning
  Oddb2xml.log "Downloader from #{@url} for #{self.class}"
  init
end

Public Instance Methods

init() click to toggle source
# File lib/oddb2xml/downloader.rb, line 58
def init
  @agent = Mechanize.new
  @agent.user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0"
  @agent.redirect_ok = true
  @agent.redirection_limit = 5
  @agent.follow_meta_refresh = true
  if RUBY_PLATFORM =~ (/mswin|mingw|bccwin|cygwin/i) &&
      ENV["SSL_CERT_FILE"].nil?
    cert_store = OpenSSL::X509::Store.new
    cert_store.add_file(File.expand_path("../../../tools/cacert.pem", __FILE__))
    @agent.cert_store = cert_store
  end
  @agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
report_download(url, file) click to toggle source
# File lib/oddb2xml/downloader.rb, line 51
def report_download(url, file)
  Oddb2xml.log sprintf("%-20s: download_as %-24s from %s",
    self.class.to_s.split("::").last,
    File.basename(file),
    url)
end

Protected Instance Methods

read_xml_from_zip(target, zipfile) click to toggle source
# File lib/oddb2xml/downloader.rb, line 85
def read_xml_from_zip(target, zipfile)
  Oddb2xml.log "read_xml_from_zip target is #{target} zip: #{zipfile} #{File.exist?(zipfile)}"
  if Oddb2xml.skip_download?
    entry = nil
    Dir.glob(File.join(DOWNLOADS, "*")).each do |name|
      if target.match(name)
        entry = name
        break
      end
    end
    if entry
      dest = "#{DOWNLOADS}/#{File.basename(entry)}"
      @file2save = dest
      if File.exist?(dest)
        Oddb2xml.log "read_xml_from_zip return content of #{dest} #{File.size(dest)} bytes "
        return IO.read(dest)
      else
        Oddb2xml.log "read_xml_from_zip could not read #{dest}"
      end
    else
      Oddb2xml.log "read_xml_from_zip could not find #{target}"
    end
  end
  xml = ""
  if RUBY_PLATFORM.match?(/mswin|mingw|bccwin|cygwin/i)
    Zip::File.open(zipfile) do |a_zip_file|
      a_zip_file.each do |entry|
        if entry.name&.match?(target)
          Oddb2xml.log "read_xml_from_zip reading #{__LINE__}: #{entry.name}"
          io = entry.get_input_stream
          until io.eof?
            bytes = io.read(1024)
            xml << bytes
            bytes = nil
          end
          io.close if io.respond_to?(:close)
          dest = "#{DOWNLOADS}/#{File.basename(entry.name)}"
          File.open(dest, "w+") { |f| f.write xml }
          Oddb2xml.log "read_xml_from_zip saved as #{dest}"
        end
      end
    end
  else
    Zip::File.foreach(zipfile) do |entry|
      if entry.name&.match?(target)
        Oddb2xml.log "read_xml_from_zip #{__LINE__}: reading #{entry.name}"
        dest = "#{DOWNLOADS}/#{File.basename(entry.name)}"
        entry.get_input_stream { |io| xml = io.read }
        File.open(dest, "w+") { |f| f.write xml }
        Oddb2xml.log "read_xml_from_zip saved as #{dest}"
      end
    end
  end
  xml
end
retrievable?() click to toggle source
# File lib/oddb2xml/downloader.rb, line 75
def retrievable?
  if @retry_times > 0
    sleep 5
    @retry_times -= 1
    true
  else
    false
  end
end