# Flickr tag question

**URL:** <https://www.ruby-forum.com/t/flickr-tag-question/61222>\
**Category:** Rails\
**Created:** [May 22, 2006, 2:59am UTC](https://www.ruby-forum.com/t/flickr-tag-question/61222 "2006-05-22T02:59:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![whoisstan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@whoisstan](https://www.ruby-forum.com/u/whoisstan)\
**Post date:** [May 22, 2006, 2:59am UTC](https://www.ruby-forum.com/t/flickr-tag-question/61222/1 "2006-05-22T02:59:19Z")

</div>

hey,

i am using the flickr wrapper for ruby and need to access the tags of a  
specific photo. i got all the rest working. i must admit i am kind of  
new to  
ruby, but i tried and tried and couldn’t figure it out. among other  
things i  
want to read geotags like in my example here:

![](https://c1.staticflickr.com/1/47/146634506_b4f34aa8fe_b.jpg "Hasidism")

i’m sure it is deadsimple and i just overlooked it.

appreciate it,  
stan

---

<div class="post-metadata">

**Author:** ![whoisstan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@whoisstan](https://www.ruby-forum.com/u/whoisstan)\
**Post date:** [May 22, 2006, 5:46am UTC](https://www.ruby-forum.com/t/flickr-tag-question/61222/2 "2006-05-22T05:46:57Z")

</div>

I made a typo. Grrr.

irb(main):007:0\> auth.token.getFrob(‘froblicious’)  
irb(main):008:0\> auth.token.cache\_token

should have rather been

irb(main):007:0\> flickr.auth.token.getToken(‘froblicious’)  
irb(main):008:0\> flickr.auth.token.cache\_token

Hope this helps!

– G.

---

<div class="post-metadata">

**Author:** ![whoisstan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@whoisstan](https://www.ruby-forum.com/u/whoisstan)\
**Post date:** [May 22, 2006, 5:43am UTC](https://www.ruby-forum.com/t/flickr-tag-question/61222/3 "2006-05-22T05:43:50Z")

</div>

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](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](http://web.site.com/existing_content) and it got called back as http://  
[web.site.com/existing\_contact?frob=xxxyyyzzzz](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.

---

<div class="post-metadata">

**Author:** ![whoisstan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@whoisstan](https://www.ruby-forum.com/u/whoisstan)\
**Post date:** [May 22, 2006, 9:10am UTC](https://www.ruby-forum.com/t/flickr-tag-question/61222/4 "2006-05-22T09:10:26Z")

</div>

On 22/05/06, Guido S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yet another typo. Today must be my inconsistent day, or my consistent  
> day depending on how you want to look at it 🙂

Great explanation though Guido. thanks.  
(And for taking the time to correct it!)

regards

–  
Dave Pawson  
XSLT XSL-FO FAQ.  
[http://www.dpawson.co.uk](http://www.dpawson.co.uk)

---

<div class="post-metadata">

**Author:** ![whoisstan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@whoisstan](https://www.ruby-forum.com/u/whoisstan)\
**Post date:** [May 22, 2006, 5:53am UTC](https://www.ruby-forum.com/t/flickr-tag-question/61222/5 "2006-05-22T05:53:10Z")

</div>

Yet another typo. Today must be my inconsistent day, or my consistent  
day depending on how you want to look at it 🙂

The text below is a little misleading. If you follow it exactly, it  
won’t work …

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](http://web.site.com/existing_content) and it got called back as  
[http://web.site.com/existing\_contact?frob=xxxyyyzzzz](http://web.site.com/existing_contact?frob=xxxyyyzzzz)). Ok, let’s go on  
…

Instead, you get to call

flickr.auth.getToken(‘xxxyyyzzz’) to get your API activated after you  
click on the Flickr button that leads you to your callback URL …  
getting the proper xxxyyyzzz value from that URL, of course!

– G.

---

<div class="post-metadata">

**Author:** ![whoisstan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@whoisstan](https://www.ruby-forum.com/u/whoisstan)\
**Post date:** [May 23, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/flickr-tag-question/61222/6 "2006-05-23T22:02:09Z")

</div>

Hey Guido,

thanks so much for your insights and samples. I haven’t had time to  
really  
test the waters since the weekend, but will do and share my experience  
with  
you.

One question: You told me uninstall flickr and install flickr

% sudo gem uninstall flickr  
% sudo gem install rflickr

But then later in the code it is still the class Flickr im dealing with,  
same as before. How about packaging in ruby, isn’t that something that  
should be enforced? If I would have both installed which is not complety  
unrealistic how would i distinguish between them?

Thanks again for your great help,  
Stan

---

<div class="post-metadata">

**Author:** ![whoisstan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@whoisstan](https://www.ruby-forum.com/u/whoisstan)\
**Post date:** [May 24, 2006, 5:20am UTC](https://www.ruby-forum.com/t/flickr-tag-question/61222/7 "2006-05-24T05:20:04Z")

</div>

Thanks Guido, I think I understand. If would be to write a module that  
is  
used in other applications I would use a path to prevent eventual naming  
conflicts and applications would not just say

require ‘myModule’

but

require ‘org/sampleName/domain/myModule’

does that make sense?

Appreciate it;)  
Stan

---

<div class="post-metadata">

**Author:** ![whoisstan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@whoisstan](https://www.ruby-forum.com/u/whoisstan)\
**Post date:** [May 24, 2006, 12:36am UTC](https://www.ruby-forum.com/t/flickr-tag-question/61222/8 "2006-05-24T00:36:07Z")

</div>

Almost, but not quite. rflickr and flickr are different gems but both  
contain a flickr.rb file that gets loaded on require ‘flickr’ …

Ruby has a ‘class path’ that is uses for loading files and it probably  
loads the first matching file in that path … Actually, I think  
there are several such paths, you have vendor, site and the ruby  
distribution itself. If the ruby runtime files live in  
/usr/local/lib/ruby, then you would have …

1.8 → the runtime files/classes for ruby-1.8  
site\_ruby/1.8 → files for locally installed stuff  
vendor\_ruby/1.8 → files installed with ruby (but not in ruby’s core)

On top of that, gems have their own loading mechanism, so perhaps my  
example should have been something like

require ‘rubygems’  
require\_gem ‘rflickr’

Rubygems manipulates the ‘class path’ to include all activated gems.  
In such a case, require will fall back to using the gems in the path,  
and we are back to the situation where the first match will be loaded  
… so in such a case, it may be better to just use the require\_gem  
method instead …

See [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/185638](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/185638)  
(excerpts below)

The require\_gem does two basic functions:

(1) Activates a gem with an appropriate version number (see below for  
more on “activate”)

(2) Auto requires the autorequire file if the gem has specified one.

After much experience with gems, the RubyGems team has come to the  
conclusion that the autorequire step causes more trouble than its worth.  
The require\_gem command will be probably be deprecated in the future and  
be replaced with a command that only does the activation step.  
(‘activate\_gem’ will probably be then name of this new command, although  
no promises there).

Require\_gem is only needed if you wish to assert which versions of a gem  
you are willing to accept. If you don’t care, it is better to not use  
it.

…

Today, Gems monitors all require requests given to Ruby. If a require  
fails to load a file, then Gems will check its repository to see if  
there is a file that satisfies the require statement. If so, RubyGems  
will activate the latest version of the gem that provides that file and  
will retry the require.

– G.

---

<div class="post-metadata">

**Author:** ![whoisstan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@whoisstan](https://www.ruby-forum.com/u/whoisstan)\
**Post date:** [May 24, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/flickr-tag-question/61222/9 "2006-05-24T14:44:08Z")

</div>

Not really. You would just require ‘myModule’ …

require is a function that rubygems overrides, so when it fails, the  
gems are smart enough to look through their own path  
‘org/sampleName/domain/myModule’ and would find your module for you.

When that doesn’t work, you can use the require\_gem function instead,  
which chooses what to load based on the name of the gem being  
required. You can also tell it what version of the gem to load …

So what you would do if you wrote a module that other applications  
would use, would be to put it in a gem … And try to pick a name that  
no one is using. If that doesn’t work, say, there is a conflict, then  
the version can be used to select which one to really use. I don’t  
think the obvious pathological case of two gems having the same name  
and version happens because then there’s only one file on the gem  
server (and the other would have been overwritten 🙂

– G.
