-
-
Save evaldasg/b7d797c5086061cf689f0b525806cff3 to your computer and use it in GitHub Desktop.
An example http download with Progress Bar output in the command line with Ruby and the native `net/http` library...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#from http://stackoverflow.com/questions/2301009/get-file-size-before-downloading-counting-how-much-already-downloaded-httpru | |
require 'net/http' | |
require 'uri' | |
require 'progressbar' | |
url = "http://ipv4.download.thinkbroadband.com/5MB.zip" | |
url_base = url.split('/')[2] | |
url_path = '/'+url.split('/')[3..-1].join('/') | |
@counter = 0 | |
Net::HTTP.start(url_base) do |http| | |
response = http.request_head(URI.escape(url_path)) | |
ProgressBar | |
pbar = ProgressBar.new("file name:", response['content-length'].to_i) | |
File.open("test.file", 'w') {|f| | |
http.get(URI.escape(url_path)) do |str| | |
f.write str | |
@counter += str.length | |
pbar.set(@counter) | |
end | |
} | |
pbar.finish | |
end | |
puts "Done." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem "progressbar" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bundle exec ruby download_progress.rb | |
file name:: 100% |oooooooooooooooooooooooooooooooooooooooooooo| Time: 0:00:00 | |
Done. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment