class Mechanize

Public Instance Methods

download(url,file) click to toggle source
# File lib/mechanize-downloader.rb, line 17
def download(url,file)
  url = URI.parse(url)
  cli = HTTPClient.new
  @cookie_jar.cookies(url).each do |cookie|
    cli.cookie_manager.parse(cookie.to_s,url)
  end
  
  length = 0;total = 0
  while true
    res = cli.head(url)
    break unless res.status == 302 # HTTP::HTTPFound
    url = URI.parse(res.header["Location"].to_s)
  end

  total = cli.head(url).header["Content-Length"].to_s.to_i
  t = Thread.new {
    conn = cli.get_async(url)
    io = conn.pop.content

    f = file
    f = ::File::open(file, "wb") unless file.is_a?(IO) or file.is_a?(Tempfile)
    while str = io.read(40)
      f.write str
      length += str.length
    end
    f.close unless (file.is_a?(IO) or file.is_a?(Tempfile))
  }

  pbar = ProgressBar.new("Loading",total)
  while  total > length
    sleep 1
    pbar.set(length)
  end
  pbar.finish
  t.join
end