07.30.08

Use Flickr_Fu to Bring Your Flickr Photos to Your Ruby on Rails Website

It all started when I decided it was time to make nuttersmark.com xhtml and css valid. I ha d played around with various flickr wrappers and gems in the past to serve up my photos but this time around I decided to use the canned badge that flickr provides users to embed their latest photos on their blog or website.  This seemed to be a good temporary solution while I got the more important components of design iteration #5 ironed out until I decided to see just how far away from valid this site really was.  Turns out it was 'very'.

 

For most of these non-compliances I could be blamed, but the flickr badge itself accounted for quite a few of the errors the W3C checkers were finding.  This was enough to get me to pull my flickr images directly from the API again, but this time around I decided against using the "flickr" or "rflickr" gems and instead give the "flickr_fu" gem a shot thanks to a great write-up over at urbanpuddle.com

 

The setup for flickr_fu is a no brainer and in my opinion a heck of a lot cleaner and more straightforward than with the alternative gems.  All your flickr information is neatly tucked away into a yaml file, namely config/flickr.yml as follows.

 

key: YOUR_API_KEY secret: YOUR_API_SECRET token_cache: token.yml

 

From there, assuming you have an API key to begin with, you can create a new flickr object and be on your way. Referencing the aforementioned yaml file: 

 

flickr = Flickr.new("#{RAILS_ROOT}/config/flickr.yml")

 

This is where the fun begins.  If you're anything like me, you're most interested in grabbing the photos associated with your account.  You'll want to find out what your flickr user id is which can be done very easily here.  Once you have that you can grab your photos using something like the following:

 

photos = flickr.photos.search(:user_id => 'your_user_id_here', 'per_page' => 200, 'page' => 1)

 

So you have 200 of your latest photos all on one big page.  It's helpful to check out what types of attributes these photos have, so let's do a pretty print of one to get the scoop:

 

#<Flickr::Photos::Photo:0x23d94cc @farm="4", @flickr= #<Flickr::Base:0x248c428 @api_key="785fcf655d1a5e62f6441065a18dad8e", @api_secret="6104b4c9b8f413e3", @auth= #<Flickr::Auth:0x2466ca0 @flickr=#<Flickr::Base:0x248c428 ...>, @token=nil>, @photos=#<Flickr::Photos:0x2466e58 @flickr=#<Flickr::Base:0x248c428 ...>>, @token_cache="token.yml">, @geo=nil, @icon_server=nil, @id="2717489746", @is_family="0", @is_friend="0", @is_public="1", @license="0", @machine_tags="", @media="photo", @o_dims=nil, @original_format="jpg", @owner="25233600@N00", @owner_name="nuttersmark", @secret="db26a1c34d", @server="3196", @tags="fromiphone", @taken_at=Wed Jul 30 12:20:32 -0500 2008, @title="Working at Panera, stealing their A/C", @updated_at=Wed Jul 30 12:20:34 -0500 2008, @uploaded_at=Wed Jul 30 12:20:32 -0500 2008, @views=4> )

 

Looks like flickr_fu grabs a whole host of information about a photo, including what tags are associated with the photo, when it was taken, uploaded, updated, etc, and the url of the photo itself.  You can even drill into the different sizes of the photo by calling the "sizes" method on the photo object, which will give you the urls and dimensions for the each size from thumbnail up to the original size. One note about sizes, you will want to go into your account and change the permissions to "anybody" for the original size of the photo if you want to be able to access it using the API.

 

If you are interested in modifying anything on your flickr account from your Rails application, you will need to authorize it by invoking the following method on the flickr object created above:

 

puts flickr.auth.url(:write)

 

This will output a url that if visited will authorize your application. Once authorized, cache the token:

 

flickr.auth.cache_token

 

You can now modify photo objects, such as add a comment to them:

 

photo.add_comment("cool!")

 

The flickr_fu documentation is really what sold it for me, since it seems to be lacking in the other two solutions.  In there you will find a wide variety of cool functions you can utilize to get the most out of your flickr account.  So a long story short, I was able to recreate the functionality of the flickr badge within mere minutes, and the journal section of this site now receives all my latest flickr photos and adds them to the timeline.  Because connectivity to certain API's can be slow at best sometimes i suggest you do what I do and throw all your flickr photo grabbing code into a rake file that's activated by a cron job.  

 

Happy Hacking!



Subscribe to Just a Nutter RSS Feed

Comment_button_spacer

Current Projects

Twitterless

Receive updates when people stop following you on Twitter and keep track of and learn more about your followers.

View Now

oqodo

Oqodo started as a mini competition between a friend and myself to build an app for our friends to keep in touch. It will soon grow into something much bigger.

View Now

My Web Presence

Flickr
Facebook
Digg
Lastfm
Linkedin
Twitter
Basecamp