Class: PinterestDL::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/pinterest_dl/downloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(client: PinterestDL::Client.new, config: PinterestDL.config) ⇒ Downloader

Returns a new instance of Downloader.



7
8
9
10
# File 'lib/pinterest_dl/downloader.rb', line 7

def initialize(client: PinterestDL::Client.new, config: PinterestDL.config)
  @client = client
  @config = config
end

Instance Method Details

#download(url, path:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pinterest_dl/downloader.rb', line 12

def download(url, path:)
  raise ArgumentError, 'url must not be nil/empty' if url.to_s.empty?

  FileUtils.mkdir_p(File.dirname(path))
  response = @client.raw_get(url)
  File.binwrite(path, response.body)
  path
rescue PinterestDL::Error
  raise
rescue StandardError => e
  raise PinterestDL::DownloadError, "Could not download #{url} to #{path}: #{e.message}"
end

#download_batch(urls, dir:, filename: nil, &progress) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pinterest_dl/downloader.rb', line 25

def download_batch(urls, dir:, filename: nil, &progress)
  FileUtils.mkdir_p(dir)
  succeeded = []
  failed = []
  total = urls.size

  urls.each_with_index do |url, index|
    name = filename ? filename.call(url, index) : default_filename(url, index)
    path = File.join(dir, name)
    begin
      download(url, path: path)
      succeeded << path
    rescue PinterestDL::Error => e
      @config.logger.warn("failed to download #{url}: #{e.message}")
      failed << { url: url, error: e }
    end
    progress&.call(index + 1, total, url)
  end

  { succeeded: succeeded, failed: failed }
end