class Imgfetcha::BatchFetcher

Constants

VALID_TYPES

NOTE: 'bin' stands for `application/octet-stream`

Attributes

output_dir[R]
result[R]
urls[R]

Public Class Methods

new(urls, options) click to toggle source
# File lib/imgfetcha/batch_fetcher.rb, line 11
def initialize(urls, options)
  @urls       = urls
  @output_dir = options[:output_dir] || Dir.pwd
  @verbose    = options[:verbose]
end

Public Instance Methods

run() click to toggle source
# File lib/imgfetcha/batch_fetcher.rb, line 17
def run
  @result = batch_download
  puts "\nDownloaded #{@result.count} images"
  @result
end

Private Instance Methods

batch_download() click to toggle source
# File lib/imgfetcha/batch_fetcher.rb, line 25
def batch_download
  urls.map.with_index do |url, i|
    print_progress(url, i)
    temp_file = URI.open(url)
    type      = detect_type(temp_file)

    next unless validate_type(type)

    write_file(temp_file: temp_file, name: file_name(url, temp_file))
    url
  end.compact
end
detect_type(file) click to toggle source
# File lib/imgfetcha/batch_fetcher.rb, line 53
def detect_type(file)
  content_type = file.meta['content-type']
  MIME::Types[content_type].first.preferred_extension
end
file_name(url, file) click to toggle source

Get basename from a URL

# File lib/imgfetcha/batch_fetcher.rb, line 39
def file_name(url, file)
  name = File.basename(URI.parse(url).path)
  # Append type if valid types aren't contained in the name
  name += ".#{detect_type(file)}" unless VALID_TYPES.any? { |type| name.include?(type) }

  name
end
print_progress(url, index) click to toggle source
validate_type(type) click to toggle source
# File lib/imgfetcha/batch_fetcher.rb, line 58
def validate_type(type)
  return true if VALID_TYPES.include?(type)

  print("ERROR: Type #{type} is invalid")
  false
end
write_file(temp_file:, name:) click to toggle source
# File lib/imgfetcha/batch_fetcher.rb, line 47
def write_file(temp_file:, name:)
  File.open("#{@output_dir}/#{name}", 'wb') do |f|
    f.write(temp_file.read) ? print('OK') : print('ERROR')
  end
end