Hi Stan,
I took a look at the various Flickr APIs for ruby and they seem to
vary in utility. The flickr gem is really easy to use but is
optimized for retrieval only. There’s little you can do to set your
own tags etc, since these are implemented as empty methods for now
(at least in flickr-1.0.0.gem …) so I guess there’s not a lot of
mileage in that. Starts easy, gets hard later when you really need
some obscure little feature … The rflickr API seems to be the most
complete but is sparsely documented to put it kindly.
Here’s how I got started … You probably want to make sure you don’t
have the flickr gem installed but you do have the rflickr gem installed.
API INSTALLATION AND ACTIVATION
% sudo gem uninstall flickr
% sudo gem install rflickr
Load irb
% irb
irb(main):001:0> require ‘flickr’
irb(main):002:0> API_KEY = “your-api-key-goes-here”
irb(main):003:0> SHARED_SECRET = “your-shared-secret-goes-here”
irb(main):004:0> flickr = Flickr.new(“/tmp/flickr.cache”, API_KEY,
SHARED_SECRET)
irb(main):005:0> flickr.auth.token
=> nil
irb(main):006:0> flickr.auth.login_link
=> “http://blahblahblah”
You would click on the ‘blahblahblah’ link to authorize the API
usage. I think in your case, you already have an API key which is
authorized so you would instead skip straight to the next step … In
any case, the callback URL for Flickr API will return a frob value on
the callback URL to you … let’s assume the frob value is
‘froblicious’ (I used a normal index page as my callback, e.g http://
web.site.com/existing_content and it got called back as http://
web.site.com/existing_contact?frob=xxxyyyzzzz). Ok, let’s go on …
irb(main):007:0> auth.token.getFrob(‘froblicious’)
irb(main):008:0> auth.token.cache_token
The cache_token method will save it to your cache file (/tmp/
flickr.cache) in our example here and you can from then on do the
Flickr.new(cache_file,api_key,shared_secret) thing without bothering
with auth again …
GETTING STARTED WITH THE RFLICKR API
So if you quit irb, and then load it again,
% irb
irb(main):001:0> require ‘flickr’
irb(main):002:0> API_KEY = “your-api-key-goes-here”
irb(main):003:0> SHARED_SECRET = “your-shared-secret-goes-here”
irb(main):004:0> flickr = Flickr.new(“/tmp/flickr.cache”, API_KEY,
SHARED_SECRET)
irb(main):005:0> flickr.auth.token
=> “this time it’s not nil!”
You will see that you are no more getting a nil auth token and you
should be able to use it from now on. For convenience, you can edit
rflickr/lib/flickr/base.rb and hard code the API_KEY and
SHARED_SECRET there. If you do that, you would only need to call the
constructor with the cache_file argument. You could always hard code
that as well
EXTRACTING FLICKR TAGS
Since you are probably going to be looking for photos with a
particular tag, it’s going to be a little tricky. I don’t see any
Flickr API method that would take a set of tags and give all photos
with that tag. The best that I see is that you can search for
particular tags by user id, but not for all tags!
e.g my tags are as below …
irb(main):018:0> flickr.tags.getListUser ‘82552702@N00’
=> [“accra”, “ada”, “benin”, “busyinternet”, “cotonou”, “dad”,
“estelle”, “estuary”, “family”, “flowers”, “friends”, “fuelstation”,
“gasim”, “ghana”, “guido”, “hpepc”, “inspiron5100”, “kalahari”,
“linksyswrt54g”, “manetparadise”, “mom”, “motorbiketaxis”, “namibia”,
“paul”, “pharaon”, “puppies”, “rain”, “sandra”]
So I’m not too sure how you are going to manage this … let’s try
the flickr.photos.search API instead …
irb(main):020:0> photolist = flickr.photos.search nil, ‘semapedia’
And BINGO. We got the photos
I guess we need to loop through them now in order to get at the tags,
to see what other tags are attached, for example …
irb(main):089:0> def show_tags_for_photolist(photolist)
irb(main):090:1> for photo in photolist
irb(main):091:2> print “#{photo.title} =>\n”
irb(main):092:2> for tag in photo.tags
irb(main):093:3> print “\t#{tag.raw}\n”
irb(main):094:3> end
irb(main):095:2> print “\n”
irb(main):096:2> end
irb(main):097:1> end
And now you can go ahead and do … (if you want all the ‘semapedia
AND nyc’ tagged photos visible to you …)
irb(main):097:1> show_tags_for_photolist(flickr.photos.search(nil,
‘semapedia,nyc’, ‘all’)) ; nil
If you use irb to get a feel for the API or how to get your groove
on, you can add a ; nil to the end of methods that return a long
result, so that stuff doesn’t scroll off the screen …
I really hope this helps getting you started and welcome to rails. It
takes a little time and you would be well advised to go buy the Agile
Web D. with Rails book and perhaps the Ruby for Rails book
as well as Rails Recipes to help you avoid doing all the R&D that
people have already done
– G.