Skip to content

Instantly share code, notes, and snippets.

@shunwen shunwen/flickrdl.rb
Last active Mar 14, 2017

Embed
What would you like to do?
require 'flickraw'
require 'mini_magick'
require 'parallel'
def get_photo_urls_from_photoset(flickr, photoset_id)
total_pages = nil
current_page = 1
urls = []
opts = {
photoset_id: photoset_id,
extras: 'url_o',
privacy_filter: 5
}
while total_pages.nil? || total_pages >= current_page
puts "Fetching set #{photoset_id} page #{current_page}"
opts[:page] = current_page
response = flickr.photosets.getPhotos(opts)
response['photo'].each { |photo| urls << photo['url_o'] }
total_pages ||= response['pages'].to_i
current_page = response['page'].to_i + 1
end
urls
end
def download_photo(url, folder)
puts "Downloading #{url} ..."
photo = MiniMagick::Image.open url
filename = url.split('/').last
photo.write File.join(folder, filename)
{file: filename, url: url, status: 'OK'}
rescue => e
{file: filename, url: url, status: 'Error', error: e.inspect}
end
def download_photoset(set, base_folder)
folder = File.join base_folder, set['title']
FileUtils.mkdir_p folder
set['results'] = Parallel.map(set['urls'], in_threads: 3) do |url|
download_photo(url, folder)
end
end
# see https://hanklords.github.io/flickraw/ to fill in these tokens
FlickRaw.api_key = '...'
FlickRaw.shared_secret = '...'
flickr = FlickRaw::Flickr.new
flickr.access_token = '...'
flickr.access_secret = '...'
photosets = flickr.photosets.getList
parsed_sets = Parallel.map(photosets, in_threads: 3) do |set|
set = set.to_hash
set['urls'] = get_photo_urls_from_photoset(flickr, set['id']).compact
puts "Set #{set['title']} has #{set['urls'].count} photos"
set
end
base_folder = File.join(Dir.home, 'flickr')
parsed_sets.each do |set|
download_photoset(set, base_folder)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.