Access Flickr with Ruby and Flickraw
Flickr is a well known site for sharing photos and it is one of the most popular sites that published their services and allow to use them freely.
It is not necessary to work on a photo uploader, photo printing site or a mashup solution to become an user of the services. Sometimes it is interesting enough to get a list of your photos URLs and show them in a favourite flash photo viewer on your own web site.
There are several libraries for Ruby, one of the most up-to-date is Flickraw. Unfortunately it is not mentioned on the official Flickr services page, although it works smoothly, has a good documentation and is actively maintained (btw. the RubyForge project page is outdated, but the github page is up-to-date).
Firstly, install the flickraw gem:
sudo gem install flickraw
Open your favourite text editor or just use irb and…
Load the libraries:
require 'rubygems' require 'flickraw'
To work with user specific photos (and our own are) we need the user id. There are two ways:
- Preferably, use the flickraw code, where it is necessary to specify your user screen name (not in the flickr URL to your profile – you may find it in the flickr’s greeting or in your settings):
id = flickr.people.findByUsername(:username => "YOUR USER SCREEN NAME").id
- Go to the idGettr page, paste your Flickr user profile URL and the page will show your Flickr user id (e.g. 92977300@N00). In this case, simply define the id value:
id = '92977300@N00'
A list of all your photos can be retrieved in this way:
flickr.photos.search(:user_id => id)
or you may specify tags to get photos with specific tags:
flickr.photos.search(:user_id => id, :tags => 'TAG_VALUE')
and for each item of the returned list you may retrieve various details such as URLs (for various sizes), tags, taken date or number of views.
All details about a photo can be retrieved by calling the getInfo method, where the parameter :photo_id is set to a photo id (obtained from the previous search method call):
info = flickr.photos.getInfo(:photo_id => PHOTO_ID)
The whole example source code (with my Flickr user id and a tag for the best photos from 2009):
require 'rubygems' require 'flickraw' #id = flickr.people.findByUsername(:username => "YOUR USER SCREEN NAME").id id = '92977300@N00' puts "Your user ID is #{id}" flickr.photos.search(:user_id => id, :tags => 'best2009').each do |p| info = flickr.photos.getInfo(:photo_id => p.id) # retrieve additional details # p info # uncomment to see other details title = info.title square_url = FlickRaw.url_s(info) original_url = FlickRaw.url_o(info) taken = info.dates.taken views = info.views tags = info.tags.map {|t| t.raw} puts "Photo #{title} with #{views} views and tags #{tags.join(",")}" puts "was taken on #{taken}, see it on #{square_url}" end
A result could be something like this:
Photo IMG_3007 with 0 views and tags Croatia,Split,best2009 was taken on 2009-11-16 11:53:43, see it on http://farm5.static.flickr.com/4040/4356525650_8c1f9ecbf2_s.jpg Photo IMG_2832 with 0 views and tags Croatia,Dubrovnik,best2009 was taken on 2009-11-15 22:05:53, see it on http://farm3.static.flickr.com/2702/4355777967_36892cf527_s.jpg Photo IMG_3409 with 1 views and tags Slovenia,Bled,best2009 was taken on 2009-11-17 14:19:44, see it on http://farm5.static.flickr.com/4033/4356528358_c09e55569f_s.jpg
Obviously, it is possible to store retrieved data in a different form that is understandable by an application such as a flash photo viewer or to perform additional operations like download a photo.
The Flickr services (and the library too) also allow to make changes – you may find several examples that may use another libraries or languages but it should not be so complicated to adopt them.
Enjoy :)