DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Terry has posted 2 posts at DZone. View Full User Profile

Download Recent Flickr Pictures With Ruby And The Flickr Api

08.25.2006
| 19191 views |
  • submit to reddit
        // To make this work, you need to get your own flickr api key.  
// Get one here: http://www.flickr.com/services/api/misc.api_keys.html
// Other than that, just plug and chug and have fun!
// The "b" in "wb" in the second open method may not be necessary in
// non-windows environments.

require 'open-uri'
require 'rexml/document'

open('http://www.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=YOUR_KEY_HERE') { |f|
    doc = REXML::Document.new f.read
    i = 0
    doc.elements.each("rsp/photos/photo") { |element|
        if i < 3
            open("images/file" << i.to_s << ".jpg", "wb").
                write(open("http://static.flickr.com/" << \
                element.attributes["server"] << "/" << \
                element.attributes["id"] << "_" << \
                element.attributes["secret"] << "_o.jpg").read)
        else
            break
        end
        i = i + 1
    }
}

puts "Done!"
    

Comments

Snippets Manager replied on Tue, 2011/07/19 - 4:28am

To download all the images from the Internet page you can use ruby gem image_downloader require 'rubygems' require 'image_downloader' downloader = ImageDownloader::Process.new('www.test.com','img_dir/') downloader.parse(:any_looks_like_image => true) downloader.download()

Terry Donaghe replied on Fri, 2006/08/25 - 3:42pm

TDonaghe posts on Aug 25, 2006 at 15:53 For more information about this snippet, please see my post at Rubynoob.com http://www.rubynoob.com/articles/2006/08/23/how-to-use-the-flickr-api-with-a-simple-ruby-script