valibuk.net

A place for opinions and notes of a valibuk.

Access Flickr with Ruby and Flickraw

Tagged with: — ondrej at 11:51 pm on Monday, March 29, 2010

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:

  1. 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
    
  2. 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 :)

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • DZone
  • Digg
  • Reddit
  • Technorati
  • Furl
  • NewsVine
  • Slashdot
  • Ma.gnolia
  • StumbleUpon

6 Comments »

Comment by Chris Blow

July 12, 2010 @ 9:42 am

Great example, thanks for the code! It seems that this is the best available API wrapper in ruby. I love that it is 300 lines.

Comment by Matthew Clark

August 13, 2010 @ 4:26 am

Flickr is really a great way to share photos on the net, the photo resizing feature of Flickr is what i like’*.

Comment by Tyler Clarke

October 1, 2010 @ 4:38 pm

the thing that i like most about flicker is the resize feature”-*

Comment by Mat Gilbert

April 3, 2012 @ 2:52 pm

Really nice simple example - I’ve been looking for a Flickr gem and you’ve moved flickraw to the top of my test list. Thanks!

Comment by Fogcat5

July 27, 2012 @ 11:17 pm

One shortcut for you if you don’t want to look up your own id, you can use the string “me” in the search call like this:
flickr.photos.search(:user_id => “me”)

also, note that the search returns paged results in 100 items at a time by default. You have to loop and pass a page number to get all of the matching items.

Comment by shivamd

March 29, 2013 @ 8:21 am

You saved me so much trouble, thank you!

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Preview


commercial break :)

Make an account on slicehost.com -- a really good hosting where you have your own virtual machine. I installed Gentoo there = I like it very much ;)