Christian Heilmann

flickr.whois – using YQL to find Flickr user data from user ID or user name

Thursday, May 14th, 2009 at 2:24 am

One of the very frustrating things about Flickr is that there is a difference between user ID and user name when it comes to accessing the data via the API or even RSS. For example I can get to my home page via http://www.flickr.com/photos/codepo8 but the RSS feed url is http://api.flickr.com/services/feeds/photos_public.gne?id=11414938@N00&lang=en-us&format=rss.

The user name doesn’t work, you need the user ID (which is normally in the format of 11414938@N00). Getting this ID is not straight forward which is why there is the flickr.urls.lookupUser API method and several tools that do this for you.

However, I wanted to get more than just the ID from the name or the name from the ID. Using YQL, this was pretty easy.

I knew I could do a search for photos by providing the user id (try in console):


select * from flickr.photos.search(1) where user_id=”11414938@N00”

This returns a single photo and I can use its ID to get to the rest of my user data by reading out the owner (try this in the console):


select owner from flickr.photos.info where photo_id in
(select id from flickr.photos.search(1) where user_id=”11414938@N00”)

This returns all my user information which is cool, but I needed to find a way to match user name and ID. So I looked at the source code of my flickr page and found a hidden form field with the name “w” and the value of my nsid:

finding your nsid on the flickr homepage by  you.

Using this knowledge, I could get the NSID from a user name with a simple HTML scraper (try scraping in console)


select value from html where url=”http://www.flickr.com/photos/codepo8”
and xpath=”//input[@name=’w’]”

Putting this together, it was easy to build a JavaScript that returns the information for any user given an NSID or the user name:


var yql;
yql = ‘select owner from flickr.photos.info where photo_id in ‘+
‘(select id from flickr.photos.search(1) where ‘;
if(owner.indexOf(“@”)!==-1){
yql += ‘user_id=”’ + owner + ‘”’;
} else {
yql += ‘user_id in (select value from html where ‘+
’ url=”http://www.flickr.com/photos/’ + owner + ‘” and ‘+
‘xpath=”//input[@name=’w’]”)’;
}

yql += ‘) limit 1’;

Encased in a YQL execute table this can now be used to get the user data for either an NSID or a user name (try the open table in console).


use “http://isithackday.com/api/flickr.whois.xml” as flickr.whois;
select * from flickr.whois where owner=”sdeschamps”

Try it out:

If you want to shorten the results, add the “diagnostics=false” to the URL and use JSON as the output.

Share on Twitter