Christian Heilmann

Posts Tagged ‘licensing’

Searching Flickr photos by license and text and returning defined sizes made easy with YQL

Saturday, February 14th, 2009

We (Nagesh Susarla and moi) are just sitting here at Open Hack Day in Bangalore, India and geek out on YQL trying to find how far we can push it to make the life of a hacker easier.

One of the things was using Flickr photos and making sure we can only get photos of a certain license for a certain text. Here’s the magic we YQL statement we came up with:

select * from where photo_id in (select id from,20) where text=@text and license=@license)  and label=@label

The (20,20) means “get me 20 photos starting at the 20th” and to make it easier say we do the first 50 results instead. The @parameter are placeholders which will be replaced by URL parameters.

As a URL you can use this and send the right parameters, for example find cats with an attribution license and only square photos (75×75pixels):*%20from

Possible licenses are:

1 Attribution-NonCommercial-ShareAlike License
2 Attribution-NonCommercial License
3 Attribution-NonCommercial-NoDerivs License
4 Attribution License
5 Attribution-ShareAlike License
6 Attribution-NoDerivs License
7 No known copyright restrictions

Possible labels are Square,Thumbnail,Small,Medium and Original

To make it easier, you can also wrap the whole thing in a method:

function display(o){
var out = ‘’;
for(var i=0;i var cur = o.query.results.size[i];

var d = document.createElement(‘div’);
d.innerHTML = out;

leechFlickr() by Christian Heilmann
Gets an object as the parameter. Object properties:
query (mandatory) – term to search flickr for
amount – amount of photos (defaults to 20)
license – 1 to 7
label – Square,Thumbnail,Small,Medium and Original
callback – callback function name as string

function leechFlickr(o){
var amount = o.amount || 20;
var license = o.license || 4;
var url = ‘’ +
‘q=select%20*%20from’ +
‘%20where%20photo_id%20in%20(select’ +
‘’ +
amount + ‘)%20where%20text%3D%40text%20and’ +
url += ‘%20and%20label%3D%40label’;

url += ‘&format=json&callback=’ + o.callback +
‘&text=’ + o.query + ‘&license=’ + license;
url += ‘&label=’ + o.label;

var s = document.createElement(‘script’);
s.src = url;







See the code in action and download the script.

Does YQL rock or what? No messy user IDs, no mixing and matching all kind of API methods, just plain yummy data.