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 flickr.photos.sizes where photo_id in (select id from flickr.photos.search(20,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):
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from flickr.photos.sizes%20where%20photo_id%20in%20(select%20id%20from%20flickr.photos.search(50)%20where%20text%3D%40text%20and%20license%3D%40license)%20and%20label%3D%40label&format=xml&text=cats&license=4&label=Square
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];
out+=’‘;
}
var d = document.createElement(‘div’);
d.innerHTML = out;
document.body.appendChild(d);
}
/*
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){
if(o.query){
var amount = o.amount || 20;
var license = o.license || 4;
var url = ‘http://query.yahooapis.com/v1/public/yql?’ +
‘q=select%20*%20from flickr.photos.sizes’ +
‘%20where%20photo_id%20in%20(select’ +
‘%20id%20from%20flickr.photos.search(’ +
amount + ‘)%20where%20text%3D%40text%20and’ +
‘%20license%3D%40license)’;
if(o.label){
url += ‘%20and%20label%3D%40label’;
}
url += ‘&format=json&callback=’ + o.callback +
‘&text=’ + o.query + ‘&license=’ + license;
if(o.label){
url += ‘&label=’ + o.label;
}
var s = document.createElement(‘script’);
s.src = url;
document.getElementsByTagName(‘head’)[0].appendChild(s);
}
}
leechFlickr(
{
query:’cats’,
label:’Square’,
callback:’display’
}
);
leechFlickr(
{
query:’parrots’,
amount:10,
license:3,
label:’Square’,
callback:’display’
}
);
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.