Last active
August 29, 2015 14:06
-
-
Save polgfred/e9339d5c1da23abc106f to your computer and use it in GitHub Desktop.
Sync folder of images with Flickr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'flickraw' | |
require 'retriable/core_ext/kernel' | |
# set your api key and secret | |
FlickRaw.api_key = 'xxx' | |
FlickRaw.shared_secret = 'xxx' | |
# set your oauth access token and secret (you need to authenticate) | |
flickr.access_token = 'xxx' | |
flickr.access_secret = 'xxx' | |
# get all photoset metadata | |
all_sets = retriable { flickr.photosets.getList } | |
# try to find an existing photoset with this title, and download photo list | |
photoset_title = Dir.getwd.split('/').last(2).join(' | ') | |
puts "Photoset: #{photoset_title}" | |
if photoset = all_sets.find { |set| set.title == photoset_title } | |
photos = retriable { flickr.photosets.getPhotos(photoset_id: photoset.id).photo } | |
else | |
photos = [] | |
end | |
# find files underneath this directory and upload any we haven't seen | |
files = Dir['**/*.{jpg,JPG,png,PNG,bmp,BMP,mov,MOV,mpg,MPG,avi,AVI}'] | |
files.each do |file| | |
photo_title = file.split('/').last | |
# skip this photo if it's already in the set | |
if photos.find { |ph| ph.title == photo_title } | |
puts "Photo: #{photo_title} (found, skipping)" | |
next | |
end | |
puts "Photo: #{photo_title}" | |
# try to upload the photo 3 times, enough to deal with weird errors | |
photo_id = retriable { flickr.upload_photo(file, title: photo_title, is_public: 0, is_friend: 0, is_family: 0) } | |
# create a new photoset if needed, otherwise add this photo to the existing one | |
if photoset.nil? | |
photoset = retriable { flickr.photosets.create(title: photoset_title, primary_photo_id: photo_id) } | |
else | |
retriable { flickr.photosets.addPhoto(photoset_id: photoset.id, photo_id: photo_id) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've been using this to sync an entire iPhoto library to Flickr and it works great. The only bit of manual tweaking you may have to do is change the logic on line 15 that creates the photoset name from the current working directory.