Class: Tensorflow::Datasets::DownloadManager
- Inherits:
-
Object
- Object
- Tensorflow::Datasets::DownloadManager
- Defined in:
- lib/datasets/download_manager.rb
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #download(urls) ⇒ Object
- #download_resource(resource) ⇒ Object
-
#initialize(dir = Dir.tmpdir) ⇒ DownloadManager
constructor
A new instance of DownloadManager.
Constructor Details
#initialize(dir = Dir.tmpdir) ⇒ DownloadManager
Returns a new instance of DownloadManager.
10 11 12 |
# File 'lib/datasets/download_manager.rb', line 10 def initialize(dir = Dir.tmpdir) @dir = dir end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
8 9 10 |
# File 'lib/datasets/download_manager.rb', line 8 def dir @dir end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
8 9 10 |
# File 'lib/datasets/download_manager.rb', line 8 def uri @uri end |
Instance Method Details
#download(urls) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/datasets/download_manager.rb', line 14 def download(urls) resources = Array(urls).flatten.map {|url| Resource.new(url)} resources.each do |resource| self.download_resource(resource) end resources end |
#download_resource(resource) ⇒ Object
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 |
# File 'lib/datasets/download_manager.rb', line 22 def download_resource(resource) resource.path = File.join(self.dir, resource.filename) return if File.exist?(resource.path) STDOUT << "Downloading #{resource.uri}" << "\n" http = Net::HTTP.new(resource.uri.host, resource.uri.port) http.use_ssl = resource.uri.is_a?(URI::HTTPS) request = Net::HTTP::Get.new(resource.uri.request_uri) http.start do |http| http.request(request) do |response| file_size = response['content-length'].to_f bytes = 0 File.open(resource.path, 'wb') do |file| response.read_body do |chunk| file.write(chunk) bytes += chunk.size #STDOUT << "#{(bytes / file_size * 100).to_i}%\r" end end end end end |