I’ve lately spent some time playing around with the flickr api (yes, I’m a nerd). It’s a neat way of interacting with the photos and the data you might have stored there.
I’ve come up with some Ruby code I thought I’d share, it’s just the logging in and authentication bit, with a sample call; but the plan is to use it to download some of my photos and their associated comments and store them in files or even better in iPhoto. (I’m doing this so I can easily tidy up my flickr account when I want to… at least that’s the current theory)
I’m using Ruby because I’m learning it at the moment, and I thought it would be fun… the code (as is) uses two libraries which you may have to install. One is rb-appscript, which allows you to make applescript (sorry, windows/linux users) calls from ruby (I’m using it to open a safari window, so you can allow the script to make private calls), the other is simplexml
If you’d like to find out more about rb-appscript, here’s a link to a good article on macdevcenter;
You’ll have to get a flickr key and your own secret to use this, and insert them into the following lines of code;
secret = 'YOURSECRET'
api_key = 'YOURAPIKEY'
Here’s the complete listing below;
# Copyright (c) 2007 Chris Horan, mildendomedia.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'rexml/document'
require 'open-uri'
require 'xmlrpc/client'
require 'xmlrpc/parser'
require 'digest/md5'
require 'pp'
require 'xmlsimple'
require 'appscript'
include Appscript
include REXML
# Change the API-KEY and secret to the one provided to you by flickr
secret = 'YOURSECRET'
api_key = 'YOURAPIKEY'
# Create an instance of the flickr service this will return a frob string
server = XMLRPC::Client.new2("http://api.flickr.com/services/xmlrpc/")
digest = Digest::MD5.hexdigest(secret + 'api_key' + api_key)
result = server.call("flickr.auth.getFrob", {
'api_key' => api_key,
'api_sig' => digest}
)
frob = XmlSimple.xml_in(result)
puts "FROB = " + frob
# Create a login link
api_sig = Digest::MD5.hexdigest(secret + 'api_key' + api_key + 'frob' + frob + 'permsread')
login_link = "http://flickr.com/services/auth/?api_key=" + api_key + "&perms=read&frob="+ frob + "&api_sig=" + api_sig
# Call safari with the link and get user confirmation
safari = app('safari').documents.end.make(
:new => :document,
:with_properties => {:URL => login_link}
)
# puts login_link
# Get a token, once you have a token (and a frob) you can reuse this without having to go through the authentication step
digest = Digest::MD5.hexdigest(secret + 'api_key' + api_key + 'frob' + frob)
result = server.call("flickr.auth.getToken", {
'api_key' => api_key,
'frob' => frob,
'api_sig' => digest}
)
auth = XmlSimple.xml_in(result)
token = auth['token'].to_s
puts "TOKEN = " + token
# From here on in you have the ability to make any authorised call on the flickr api
# As an example we can just call 'flickr.photosets.getList' which will return a list of photosets
digest = Digest::MD5.hexdigest(secret + 'api_key' + api_key + 'auth_token' + token)
result = server.call("flickr.photosets.getList", {
'api_key' => api_key,
'auth_token' => token,
'api_sig' => digest}
)
photosets = XmlSimple.xml_in(result)
# Print out the photoset title and it's associated id
puts "----------------------------"
photosets['photoset'].each do |photoset|
puts photoset['title'], photoset['id']
end
puts "----------------------------"
# Let's choose a photoset id to list all the photos available in that set
puts "Enter your choosen photoset id: "
photoset_id = gets
digest = Digest::MD5.hexdigest(secret + 'api_key' + api_key +'auth_token' + token + 'photoset_id' + photoset_id )
result = server.call("flickr.photosets.getPhotos", {
'api_key' => api_key,
'photoset_id' => photoset_id,
'auth_token' => token,
'api_sig' => digest}
)
# print out the returned xml
puts result