class UnsplashDownloader::Photos

Attributes

number_of_new_photos[RW]
number_of_photos[RW]

Public Class Methods

new(path, verbose) click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 46
def initialize(path, verbose)
  @mechanize = Mechanize.new
  @photos_urls = []
  @new_photos = []
  @path = path
  @verbose = verbose
end

Public Instance Methods

count_all() click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 70
def count_all
  @number_of_photos = 0
  @photos_urls.each do |line|
    @number_of_photos += 1
  end
  return @number_of_photos
end
count_all_from_file() click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 78
def count_all_from_file
  @number_of_photos = 0
  file = File.open(File.join(@path, "unsplash", "urls.txt"))
  file.each do |line|
    @number_of_photos += 1
  end
  return @number_of_photos
end
count_new() click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 87
def count_new
  @new_photos = []
  for i in 1..@number_of_photos
    File.exist?(File.join(@path, "unsplash", "#{i}.jpg")) ? next : @new_photos.push(i)
  end
  @number_of_new_photos = @new_photos.length
  return @number_of_new_photos
end
download_all() click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 96
def download_all
  file = File.open(File.join(@path, "unsplash", "urls.txt"))
  downloaded_photos = 0
  puts "Downloaded #{downloaded_photos} of #{@number_of_photos}." if @verbose
  file.each_with_index do |line, index|
      @mechanize.get(line).save! File.join(@path, "unsplash", "#{index + 1}.jpg")
      downloaded_photos += 1
      print "Downloaded #{downloaded_photos} of #{@number_of_photos}. #{line}" if @verbose
  end
end
download_new() click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 107
def download_new
  file = File.open(File.join(@path, "unsplash", "urls.txt"))
  downloaded_photos = 0
  puts "Downloaded #{downloaded_photos} of #{@number_of_new_photos}." if @verbose
  file.each_with_index do |line, index|
    if @new_photos.include?(index + 1)
      @mechanize.get(line).save File.join(@path, "unsplash", "#{index + 1}.jpg")
      downloaded_photos += 1
      print "Downloaded #{downloaded_photos} of #{@number_of_new_photos}. #{line}" if @verbose
    else
      next
    end
  end
end
get_urls(elements) click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 54
def get_urls(elements)
  elements.each do |element|
    @photos_urls.push(/https[^\?]*/.match(element.to_s))
  end
  return @photos_urls
end
save_urls() click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 61
def save_urls
  FileUtils.mkdir_p(File.join(@path, "unsplash")) unless File.directory?(File.join(@path, "unsplash"))
  file = File.open(File.join(@path, "unsplash", "urls.txt"), "wb") do |line|
    @photos_urls.each do |url|
      line.puts url
    end
  end
end