Flickr is one of my favorite web applications. Often times, instead of building a photo uploading utility for small web sites I am working on, I will simply integrate the clients' Flickr account into their website, using RSS and the Flickr API. Here's how I do it.
Get the RSS Feed
require 'rss'
require 'net/http'
require 'rexml/document'
@flickr_rss = RSS::Parser.parse(open('http://api.flickr.com/services/feeds/photos_public.gne?id=24034605@N04&lang=en-us&format=rss_200').read, false)
Next, I initialize an array that will hold hashes with the values I need for rendering the images in the view. This will make sense in a minute.
@flickr = Array.new
Use Flickr API to get the URL to the thumbnails
@flickr_rss.items.each do |i|
photo_id = flickr_photo_id(i.link)
url = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=#{your_api_key}&photo_id=#{photo_id}"
flickr_response = REXML::Document.new(Net::HTTP.get_response(URI.parse(url)).body)
flickr_response.elements.each('rsp/sizes/size') do |e|
@flickr << {"title" => i.title, "src" => e.attribute("source"), "link" => i.link} if e.attribute("label").value == "Thumbnail"
end
end
def flickr_photo_id(url)
url.split('/').last
end
So now I have an array of hashes with links to the Thumbnails of each Flickr photo in the RSS feed. You can modify the above code very easily to get the Small, Medium, Large etc. sizes.
Render the View
<% @flickr.each_with_index do |f, i| -%> <div align="center"><a href="<%= f['link'] -%>"><img class="img_border" src="<%= f['src'] -%>" border="0" title="<%= f['title'] -%>"/></a></div> <% end -%>
Check it out in action.
Related posts:
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.