Christian Heilmann

Posts Tagged ‘javascript’

A Speakerrate comments badge or another reason to love Twitter

Tuesday, November 10th, 2009

Another reason to love Twitter: You ask for APIs and you get them. As requested in my Tweet, speaker rate acted very quickly and build an API
for latest comments – and here is my thank you. Using a bit of YQL magic and some JavaScript I put together a badge to show off the latest speakerrate comments on your page:

Speakerrate Badge Test by  you.

Check out the demo page of the badge and get the source and read the docs on GitHub.

Usage:

Simply add a DIV with a link to your speaker page on speakerrate:

Then add the script and call the init() method:


You can also provide a few options to the init() method to change the look and features of the badge:


If you need different styles, just use styled:false or override the ones applied.

Getting a list of Flickr photos by location and/or search term with a YQL open table

Monday, November 2nd, 2009

Displaying photos from Flickr can be daunting. The API needs authentication and the RSS, JSON or LOLcode output is very limited. The way around is using YQL and its Flickr tables. That way it is pretty easy to search Flickr:

select * from flickr.photos.search where text=”panda”

Try out the Panda search in the YQL console.

The output format has a lot of information in there, but sadly enough, not all. For example the real name of the owner or the description is missing. Therefore you need to go through yet another Flickr API to get the whole data set:

select * from flickr.photos.info where photo_id in(
select id from flickr.photos.search where text=”panda”
)

Guess what? You can also try this more detailed query in the console.

I’ve shown before how easy it is to display Flickr Photos retrieved that way:


The issue with that though is that it uses JavaScript and JavaScript may be turned off (think Blackberries). Of course you can do the same thing in PHP but I’d wager to say more people to JavaScript than PHP these days.

The main issue is that Flickr returns the photos in a pretty weird format and that you need a script like the one above to turn it into a simple HTML list.

The good news is that YQL with the execute command allows you to embed JavaScript in your open tables. That way you can write a table that does all the necessary transformations and returns the data as a simple list for immediate use:




select * from {table} where location=”london,UK”
Christian Heilmann
http://www.wait-till-i.com/2009/11/01/getting-a-list-of-flickr-photos-by-location-andor-search-term-with-a-yql-open-table
Searches Flickr by location and/or search term and returns an HTML list that can be immediately used in a mashup.





You’ll notice that while the E4X support is very powerful, it can be a bit confusing to look at on first sight. Once you got your head around though it becomes much cleaner that way.

You can use this table like any other open table via the use command in YQL:

use “http://github.com/codepo8/yql-tables/raw/master/flickr/flickr.photolist.xml” as flickr;
select * from flickr where text=”me” and location=”uk” and amount=20

try it in the console.

I’ve wrapped one more API in there – the Yahoo Geo API to determine a place from a name should you want to search by location. All in all you have three parameters in this open table – all of which are optional:

  • text – the search text
  • location – the geographical location
  • amount – the amount of photos to be returned

If you look at the table source, you can also see that I hard-wired the license of the photos to 4 which is CC-BY. So if you link the photos back to Flickr you both satisfied Flickr’s terms and the original photographer’s.

Now, the easiest way to use this output is by using YQL’s JSON-P-X output format. This is XML with a callback which returns a JSON object with the HTML as a string instead of a convoluted JSON object. See the JSON-P-X output here.

That way you can easily use it in JavaScript:


And also in PHP:


$url = ‘http://query.yahooapis.com/v1/public/yql?q=use%20%22http://github.com/codepo8/yql-tables/raw/master/flickr/flickr.photolist.xml%22%20as%20flickr;%20select%20*%20from%20flickr%20where%20text=%22me%22%20and%20location=%22uk%22%20and%20amount=20&format=xml&diagnostics=false’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$output = preg_replace(‘/.*
    /’,’
      ‘,$output);
      $output = preg_replace(‘/
    .*/’,’
‘,$output);
$output = preg_replace(‘//’,’‘,$output);
$output = preg_replace(‘//’,’‘,$output);
echo $output;
?>

You can see both in action on the demo page.

So by using YQL open tables you can not only access complex APIs with YQL, but you could also write complete mashups in JavaScript and have them executed in a safe environment on Yahoo’s servers. Your end of the mashup is simply the display which could be a form that works with Ajax when JavaScript is available and renders a static page in PHP (or whatever other server-side language) when JavaScript is turned off. You only need to do one HTTP request – the rest is executed and cached on the YQL server farm – everybody wins.

YQLAutoTagger – automatic tag generation with a single line of JavaScript

Thursday, October 29th, 2009

Yesterday I gave a talk at the Stackoverflow Devdays conference in London and as one example of the powers of YQL, I wrote a GreaseMonkey script that enhances the Stackoverflow question form to automatically add tags to the question. You can see a screencast of the script or install it yourself from GitHub. The script uses the Yahoo Term Extractor API and some lines of JS to make that work.

I’ve now cleaned up the script a bit and made it work outside of Greasemonkey as a simple JavaScript to embed into any document. See it in action on YouTube:

The demo page shows you how to do that in your own documents, all you need to do is embed the script and give it the IDs of the form fields:









There are some ways to customise the script and its behaviour, all of which are described on GitHub. Download the script from GitHub and read the docs there.

TTMMHTM: MOD defines Irony, Twitter translation on Yahoo, PhotoSketch, compressing Keynotes and Linux on Badgers

Tuesday, October 6th, 2009

Things that made me happy this morning:

All the tools you need to get ready for “talk like a pirate” day

Thursday, September 17th, 2009

Avast! Saturday is the annual talk like a pirate day and my colleague Tom Croucher has done a tremendous job to create a YQL solution for translation of English to Pirate

In essence he used YQL to store a translation data set and allows us to alter it using the update and storage parts of YQL. He has come up with a few great things to use for talk like a pirate day:

A script to include into any page that will automatically convert it to pirate speak:

 

A bookmarklet to translate any web site: piratize (drag it to your links toolbar).

An open YQL table to add to the pirate dictionary.

Using this, I built the following interfaces:

Have a great talk like a pirate day! Sadly enough I’ll be Aarrr-ing down from a plane as I am flying back to the UK on that date.