Now that we’ve decided to use Flickr as our main online photo management tool, I had to upload tons of photos to Flickr. The Flickr archives revealed to me that the date of our digital camera is not properly set. Every single taken date is one year in advance :(
So I explored the Flickr organizer looking for a way to fix our first 1,350 photos. Unfortunately there is no way to do it online, except one by one…. (more details in a coming post) So I went back to the code hopping some friendly scripters would have created a nice library on top of Flickr webservices. And actually yes! rflickr does a very good job, very nicely. And it’s in ruby! What could you ask more?!
So zillions of thanks to the rflickr team and here is my commented script as a tiny contribution:
require 'rubygems'
require 'flickr'
# your credentials
MY_APPLICATION_KEY='...'
MY_SHARED_SECRET='...'
# get a valid token
token_cache_file='flickr-token.txt'
flickr = Flickr.new(token_cache_file,MY_APPLICATION_KEY,MY_SHARED_SECRET)
unless flickr.auth.token
flickr.auth.getFrob
url = flickr.auth.login_link
puts "You must visit #{url} to authorize this application. Press enter"+
" when you have done so. This is the only time you will have to do this."
gets
flickr.auth.getToken
end
# some query criteria, note that rflickr expects dates as Time instances
min_upload_date = Time.now - 2*60*60*24
max_taken_date = Time.local(2007,1,1)
photos_per_page=500
# Retrieve photos matching our criteria
# The method signature is really ugly, a map would be neater
photo_list = flickr.photos.search('me', nil, nil, nil, min_upload_date ,
nil, nil, max_taken_date, nil, nil, 500)
puts "Numbers of photos matching the request: #{photo_list.size}"
# then for each photo, increment the year of the taken date
photo_list.each {|photo|
# get more details about this photos
info = flickr.photos.getInfo(photo)
# increment the year
taken_array = info.dates[:taken].to_a
taken_array[5] = taken_array[5]+1
# re-set the taken date
flickr.photos.setDates(photo, nil, Time.gm(*taken_array), 0)
}
# we're done