Class: DNN::Downloader
- Inherits:
-
Object
- Object
- DNN::Downloader
- Defined in:
- lib/dnn/datasets/downloader.rb
Class Method Summary collapse
Instance Method Summary collapse
- #download(dir_path) ⇒ Object
-
#initialize(url) ⇒ Downloader
constructor
A new instance of Downloader.
Constructor Details
#initialize(url) ⇒ Downloader
Returns a new instance of Downloader.
19 20 21 22 |
# File 'lib/dnn/datasets/downloader.rb', line 19 def initialize(url) @url = url *, @protocol, @fqdn, @path = *url.match(%r`(https?)://(.+?)(/.+)`) end |
Class Method Details
.download(url, dir_path = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/dnn/datasets/downloader.rb', line 9 def self.download(url, dir_path = nil) unless dir_path Dir.mkdir("#{DOWNLOADS_PATH}/downloads") unless Dir.exist?("#{DOWNLOADS_PATH}/downloads") dir_path = "#{DOWNLOADS_PATH}/downloads" end Downloader.new(url).download(dir_path) rescue => e raise DNN_DownloadError.new(e.) end |
Instance Method Details
#download(dir_path) ⇒ Object
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 |
# File 'lib/dnn/datasets/downloader.rb', line 24 def download(dir_path) puts %`download "#{@url}"` buf = "" if @protocol == "http" port = 80 elsif @protocol == "https" port = 443 else raise "Protocol(#{@protocol}) is not supported." end http = Net::HTTP.new(@fqdn, port) http.use_ssl = true if @protocol == "https" http.start do |http| content_length = http.head(@path).content_length http.get(@path) do |body_segment| buf << body_segment log = "\r" 40.times do |i| if i < buf.size * 40 / content_length log << "=" elsif i == buf.size * 40 / content_length log << ">" else log << "_" end end log << " #{buf.size}/#{content_length}" print log end puts "" end file_name = @path.match(%r`.*/(.+)`)[1] File.binwrite("#{dir_path}/#{file_name}", buf) end |