Class: Tensorflow::Datasets::DownloadManager

Inherits:
Object
  • Object
show all
Defined in:
lib/datasets/download_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dirObject (readonly)

Returns the value of attribute dir.



8
9
10
# File 'lib/datasets/download_manager.rb', line 8

def dir
  @dir
end

#uriObject (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