One of the more complex things about GeoFill was to get postcode information from an IP. However with a collection of APIs and a collated YQL statement even this was possible.
The first thing I needed to get was the IP of the user. This is done with the GeoIP API based on the GeoLite API from MaxMind. This is available as an open table in YQL and can be used thus:
This gives us a lot of information. What’s really important here is latitude and longitude, as this can be used in the flickr.places API to get a where on earth ID which is a much more defined identifier:
select * from flickr.places where (lat,lon) in (
select Latitude,Longitude from ip.location where ip=””
)
Here the interesting part is the woeid which we can use to dig deeper into geo.places:
select * from geo.places where woeid in (
select place.woeid from flickr.places where (lat,lon) in (
select Latitude,Longitude from ip.location where ip=””
)
I love London – it is my choice of home. That’s why I get very upset when the first impression people get of it is something that drags people down. If you’ve been at Open Hack 2009 in Covent Garden last weekend you might have stumbled upon this man:
Whilst waiting for his food at some burger joint in Soho, Jinho had his camera stolen the day after hack day. Losing 2000 dollars of equipment is bad enough but what really made Jinho sad is that he had taken around 300 photos of the British Museum which he lost with the camera. This is doubly annoying as you cannot take photos in museums in Seoul and he was very elated about the rules in this country.
So, as a sign, could you please tag great photos of the British Museum and especially its exhibits with “sorryjinho“ or add it to this group to show him that London is a great place to visit and we’re happy to fill the gap of lost memories? Let’s show that London photographer colleagues have s(e)oul (ok, I am ashamed already of this one).
As part of my talk on YQL for Open Hack London I’ve thought of an easy way to get Flickr photos that you are allowed to display in your products and hacks.
The wrapper API getFlickrBy works around the somewhat convoluted data returned from Flickr’s API methods and uses YQL to cut the information you want down to the bare necessities. Furthermore the API only returns photos that are licenced with Creative Commons’ “By” license to avoid you using photos you have no right to use (which is a big thing with the Flickr crowd).
The API endpoint is:
http://isithackday.com/api/getFlickrBy.php
You have several parameters to play with:
location
The location you want photos of as defined in the Yahoo Geo API
search
A word you want to search for
format
The format of the returned data, XML, HTML or JSON - preset is XML
callback
A name of a JavaScript function call to wrap the JSON data in, in case you want to use the API in a script node
amount
The amount of photos returned, max is 100 – preset is 20
size
the photo size, “s” for 75×75px, “t” for thumbnail, “m” for medium, omit to get big size
The API returns only what you need: the image title, the owner, the url of the image and the link to Flickr. If you use html as the output format it returns an HTML list of linked images. For output demos and explanations simply call the API without any parameters
I love flickr to bits and I especially like the copy and paste boxes for embedding photos on my own photo stream when I check photos in different sizes.
What annoyed me is that this is not available on other people’s photos. That’s why I wrote a small GreaseMonkey script that embeds a text box with the a link and the medium sized image of the photo you’re currently checking out:
As you probably know, I am spending a lot of time speaking and mentoring at hack days for Yahoo. I go to open hack days, university hack days and even organized my own hackday revolving around accessibility last year.
One of the main questions I get is about technologies to use. People are happy to find content on the web, but getting it and mixing it with other sources is still a bit of an enigma.
Following I will go through a hack I prepared at the Georgia Tech University hack day. I am using PHP to retrieve information of the web, YQL to filter it to what I need and YUI to do the CSS layout and add extra functionality.
The main ingredient of a good hack – the idea
I give a lot of presentations and every time I do people ask me where I get the pictures I use from. The answer is Flickr and some other resources on the internet. The next question is how much time I spend finding them and that made me think about building a small tool to make this easier for me.
The next thing I could have done is deep-dive into the Flick API to get photos that I am allowed to use. Instead I am happy to say that using YQL gives you a wonderful shortcut to do this without brooding over documentation for hours on end.
Using YQL I can get photos from flickr with the right license and easily display them. The YQL statement to search photos with the correct license is the following:
select id from flickr.photos.search(10) where text=’donkey’ and license=4
You can try the flickr YQL query here and you’ll see that the result (once you’ve chosen JSON as the output format) is a JSON object with photo results:
The problem with this is that the user name is not provided anywhere, just their Flickr ID. As I wanted to get the user name, too, I needed to nest a YQL query for that:
select farm,id,secret,server,owner.username,owner.nsid from flickr.photos.info where photo_id in (select id from flickr.photos.search(10) where text='donkey' and license=4)
The next step was getting the data from the other resources I am normally tapping into: Fail blog and I can has cheezburger. As neither of them have an API I need to scrape the HTML data of the page. Luckily this is also possible with YQL, all you need to do is select the data from html and give it an XPATH. I found the XPATH by analysing the page source in Firebug:
This gave me the following YQL statement to get images from both blogs. You can list several sources as an array inside the in() statement:
select src from html where url in (‘http://icanhascheezburger.com/?s=donkey’,’http://failblog.org/?s=donkey’) and xpath=”//div[@class=’entry’]/div/div/p/img”
The result of this query is again a JSON object with the src values of photos matching this search:
The next thing I wanted to do was writing a small script to get the data and give it back to me as HTML. I could have used the JSON output in JavaScript directly but wanted to be independent of scripting. The script (or API if you will) takes a search term, filters it and executes both of the YQL statements above before returning a list of HTML items with photos in them. You can try it out for yourself: search for the term donkey or search for the term donkey and give it back as a JavaScript call
I use cURL to get the data as my server has external pulling of data via PHP disabled. This should work for most servers, actually.
I call cURL to retrieve the data from the flickr yql query, do a json_decode and loop over the results. Notice the rather annoying way of having to assemble the flickr url and image source. I found this by clicking around flickr and checking the src attribute of images rendered on the page. The images with the “ico” class should tell me where the photo was from.
Retrieving the blog data works the same way, all I had to do extra was check for which blog the resulting image came from.
$out.= ‘‘;
if($_GET[‘js’]=’yes’){
$out.= ‘’})’;
}
echo $out;
I close the list and – if JavaScript was desired – the JavaScript object and function call.
} else {
echo ($_GET[‘js’]!==’yes’) ?
‘
Invalid search term.
’ :
‘seed({html:”Invalid search Term!”})’;
}
}
?>
If there was an invalid term entered I return an error message.
Setting up the display
Next I went to the YUI grids builder and created a shell for my hack. Using the generated code, I added a form, my yql api, an extra stylesheet for some colouring and two IDs for easy access for my JavaScript:
HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
“http://www.w3.org/TR/html4/strict.dtd”>
Slide Fodder – find CC licensed photos and funpics for your slides
Slide Fodder
Slide Fodder by Christian Heilmann, hacked live at Georgia Tech University Hack day using YUI and YQL.
The last thing I wanted to add was a “basket” functionality which would allow me to do several searches and then copy and paste all the photos in one go once I am happy with the result. For this I’d either have to do a persistent storage somewhere (DB or cookies) or use JavaScript. I opted for the latter.
The JavaScript uses YUI and is no rocket science whatsoever:
function seed(o){ YAHOO.util.Dom.get(‘content’).innerHTML = o.html;
}
YAHOO.util.Event.on(‘f’,’submit’,function(e){
var s = document.createElement(‘script’);
s.src = ‘yql.php?js=yes&s=’+ YAHOO.util.Dom.get(’s’).value;
document.getElementsByTagName(‘head’)[0].appendChild(s); YAHOO.util.Dom.get(‘content’).innerHTML = ‘‘;
YAHOO.util.Event.preventDefault(e);
});
YAHOO.util.Event.on(‘content’,’click’,function(e){
var t = YAHOO.util.Event.getTarget(e);
if(t.nodeName.toLowerCase()===’img’){
var str = ‘
YAHOO.util.Event.preventDefault(e);
}); YAHOO.util.Event.on(‘basket’,’click’,function(e){
var t = YAHOO.util.Event.getTarget(e);
if(t.nodeName.toLowerCase()===’a’){
t.parentNode.parentNode.removeChild(t.parentNode);
}
YAHOO.util.Event.preventDefault(e);
});
Again, let’s check it bit by bit:
function seed(o){ YAHOO.util.Dom.get(‘content’).innerHTML = o.html;
}
This is the method called by the “API” when JavaScript was desired as the output format. All it does is change the HTML content of the DIV with the id “content” to the one returned by the “API”.
YAHOO.util.Event.on(‘f’,’submit’,function(e){
var s = document.createElement(‘script’);
s.src = ‘yql.php?js=yes&s=’+ YAHOO.util.Dom.get(’s’).value;
document.getElementsByTagName(‘head’)[0].appendChild(s); YAHOO.util.Dom.get(‘content’).innerHTML = ‘
‘src=”http://tweeteffect.com/ajax-loader.gif”’+
‘style=”display:block;margin:2em auto”>‘; YAHOO.util.Event.preventDefault(e);
});
When the form (the element with th ID “f”) is submitted, I create a new script element,give it the right src attribute pointing to the API and getting the search term and append it to the head of the document. I add a loading image to the content section and stop the browser from submitting the form.
YAHOO.util.Event.on(‘content’,’click’,function(e){
var t = YAHOO.util.Event.getTarget(e);
if(t.nodeName.toLowerCase()===’img’){
var str = ‘
I am using Event Delegation to check when a user has clicked on an image in the content section and create a new DIV with the image to add to the basket. When the image was from flickr (I am checking the src attribute) I also add the url of the image source and the user name to use in my slides later on. I add an “x” link to remove the image from the basket and again stop the browser from doing its default behaviour.
YAHOO.util.Event.on(‘basket’,’click’,function(e){
var t = YAHOO.util.Event.getTarget(e);
if(t.nodeName.toLowerCase()===’a’){
t.parentNode.parentNode.removeChild(t.parentNode);
}
YAHOO.util.Event.preventDefault(e);
});
In the basket I remove the DIV when the user clicks on the “x” link.
That’s it
This concludes the hack. It works, it helps me get photo material faster and it took me about half an hour to build all in all. Yes, it could be improved in terms of accessibility, but this is enough for me and my idea was to show how to quickly use YQL and YUI with a few lines of PHP to deliver something that does a job :)