Skip to content

Instantly share code, notes, and snippets.

@seven1m
Created September 3, 2020 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seven1m/91fcc19c0b272c4a6e9791fc376d35b8 to your computer and use it in GitHub Desktop.
Save seven1m/91fcc19c0b272c4a6e9791fc376d35b8 to your computer and use it in GitHub Desktop.
Ruby script to download albums from Flickr
# downloads photos from your Flickr albums; each album is its own folder
#
# usage:
#
# gem install flickr
# FLICKR_API_KEY=abc123def456 FLICKR_SHARED_SECRET=abc123 FLICKR_USER_ID=11111111@N00 ruby download.rb
require 'flickr'
require 'fileutils'
require 'open-uri'
user_id = ENV['FLICKR_USER_ID']
flickr = Flickr.new
albums = flickr.photosets.getList(user_id: user_id)
albums.each_with_index do |album, index|
next if ENV['START'] && index < ENV['START'].to_i - 1
net_tries = 0
begin
path = album['title'].gsub(/[^a-z0-9\- ]/i, '_')
FileUtils.mkdir_p(path)
existing_ids = Dir[path + '/*'].to_a.map { |file| File.split(file).last.split(/\s|\./).first }
page = 1
begin
puts "Downloading #{index + 1} of #{albums.size} - #{album['title']} (page #{page})..."
data = flickr.photosets.getPhotos(photoset_id: album['id'], user_id: user_id, page: page)
data['photo'].each_with_index do |photo, photo_index|
if existing_ids.include?(photo['id'].to_s)
print 'S'
next
end
sizes = flickr.photos.getSizes(photo_id: photo['id'])
orig = sizes.detect { |s| s['label'] == 'Original' }
extension = orig['source'].split('.').last
if photo['title'].strip != ''
filename = "#{photo['id']} - #{photo['title'].strip.gsub(/[^a-z0-9\- ]/i, '_')}.#{extension}"
else
filename = "#{photo['id']}.#{extension}"
end
out_path = File.join(path, filename)
read_tries = 0
begin
raw = open(orig['source']).read
rescue
read_tries += 1
retry if read_tries < 4
print 'X'
next
end
File.open(out_path, 'wb') { |f| f.write(raw) }
print '.'
end
page += 1
puts
end while data['pages'] > page
rescue JSON::ParserError, Net::ReadTimeout
net_tries += 1
retry if net_tries < 4
raise
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment