Over a million developers have joined DZone.

Download Recent Flickr Pictures With Ruby And The Flickr Api

·
// 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!"
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}