Sha256: a5b94ec7694c64300f10bf5f6f5df5d581e0f455e270e14b8d9d0b75b0ae058d

Contents?: true

Size: 1.86 KB

Versions: 6

Compression:

Stored size: 1.86 KB

Contents

require 'fileutils'

module FlickrAirlift
  module Downloader

    def self.download(user, set = nil)
      # non-pro users don't have 'Original' sizes available.
      ranked_sizes  = ['Original', 'Large', 'Medium']

      username    = user.username
      path        = set.nil? ? username : File.join(username, set.title)
      user_id     = user.id
      photos      = set.nil? ? flickr.photos.search(:user_id => user_id) : flickr.photosets.getPhotos(:photoset_id => set.id)
      photo_count = photos.total
      page_count  = photos.pages

      # Downloading
      puts "#{username} has #{photo_count} pictures"
      puts "* Creating directory: '#{path}'"
      FileUtils.mkdir_p(path) unless File.directory?(path)

      (1..page_count.to_i).each do |page_number|
        puts "* PAGE #{page_number} of #{page_count}"
        iterate_over = set.nil? ? flickr.photos.search(:user_id => user_id, :page => page_number) : photos.photo

        iterate_over.each_with_index do |photo, i|

          photo_id            = photo.id
          downloadable_files  = flickr.photos.getSizes(:photo_id => photo_id)

          ranked_sizes.each do |size_name|
            if df = downloadable_files.find { |downloadable_file| downloadable_file.label == size_name }
              download_url  = df.source
              file_to_write = File.join(path, "#{photo_id}#{File.extname(download_url)}")

              if File.exists?(file_to_write) && File.size(file_to_write) > 0
                puts "** SKIPPING #{file_to_write} because it has already been downloaded"
              else
                puts "** Downloading #{i+1}: #{photo.title} (#{size_name}) from #{download_url}"
                File.open(file_to_write, 'wb') { |file| file.puts Net::HTTP.get_response(URI.parse(download_url)).body }
              end
              break
            end
          end
        end
      end
    end
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
flickr_airlift-0.5.1 lib/flickr_airlift/downloader.rb
flickr_airlift-0.5.0 lib/flickr_airlift/downloader.rb
flickr_airlift-0.4.3 lib/flickr_airlift/downloader.rb
flickr_airlift-0.4.2 lib/flickr_airlift/downloader.rb
flickr_airlift-0.4.1 lib/flickr_airlift/downloader.rb
flickr_airlift-0.4.0 lib/flickr_airlift/downloader.rb