Christian Heilmann

You are currently browsing the Christian Heilmann blog archives for June, 2008.

Archive for June, 2008

Currency conversion API on a shoestring

Saturday, June 21st, 2008

Someone just came to our table at Mashed08 and asked if Yahoo! offers a currency conversion API. We don’t, but a few lines of PHP allows you to get the information from the Yahoo finance site:


function convert($from,$to){
$url= ‘http://finance.yahoo.com/currency/convert?amt=1&from=’.$from.’&to=’.$to.’&submit=Convert’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);
curl_close($ch);
preg_match_all(“/tabledata1”>([^<]+)/”,$feed,$cells);
return $cells[1][1];
}

echo convert(‘USD’,’GBP’);

There’s a whole list of currency codes available on oanda.

A few more lines turns this into a JSON API:


header(‘Content-type:text/javascript’);
$from = $_GET[‘from’];
$to = $_GET[‘to’];
$callback = $_GET[‘callback’];
if(preg_match(“/[A-Z|a-z]{3}/”,$to) && preg_match(“/[A-Z|a-z]{3}/”,$from)){
$to = strToUpper($to);
$from = strToUpper($from);
$url= ‘http://finance.yahoo.com/currency/convert?’ .
‘amt=1&from=’.$from.’&to=’.$to.’&submit=Convert’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);
curl_close($ch);
preg_match_all(“/tabledata1”>([^<]+)/”,$feed,$cells);
if(is_numeric($cells[1][1])){
$out = ‘{“from”:”’.$from.’”,”to”:”’.$to.’”,”factor”:”’.$cells[1][1].’”}’;
} else {
$out = ‘{error not convert currencies, are you sure about the names?”}’;
}

} else {
$out = ‘{error Currency format, must be three letters”}’;
}

if(isset($callback)){
if(preg_match(“/[a-z|A-Z|_|-|$|0-9|.]/”,$callback)){
$out = $callback.’(‘.$out.’)’;
} else {
$out = ‘{error callback method name”}’;
}

}
echo $out;

You have several parameters:

  • from (mandatory): three letter currency code (upper or lower case)
  • to (mandatory): three letter currency code (upper or lower case)
  • callback (optional): the name of the callback method that should be wrapped around the resulting object

If something goes wrong, the API will return an object with an error property, otherwise you’ll get an object with three properties:

  • from: the original currency
  • to: the target currency
  • factor: the conversion factor

Say you store this as convert.php somewhere, then you could do the following:




This is a terrible dirty hack and if Yahoo finance ever changes their HTML (and they will), this will cease to work.

Big companies are like marching bands

Friday, June 20th, 2008

I just read another amazing novel by Douglas Coupland on my 5 hour flight to Ankara called “Eleanor Rigby”. This is a quote I really enjoyed:


Jeremy asked if I liked my job.
“I think big companies are like marching bands. You know the big secret about marching bands, don’t you?”
“No, what is it?”
“Even if half the band is playing random notes, it still sounds kind of like music. The concealment of failure is built right into them. It’s like the piano – as long as you play only the black keys, not the white ones, it’ll sound okay, but on the other hand it’ll never sound like real music either.

Now this sounds negative, but I also like the idea that big companies can have failure without totally vanishing. That’s where research departments and incubators come in.

Building Easy Flickr – Step by Step

Monday, June 16th, 2008

As several people asked how I did the easier Flickr interface, I wrote up some step-by-step instructions, analyzing the issues and then taking the API to work around them.

An easier interface to Flickr

Check out How to create an alternative Flickr interface – step by step.

This is one example where providing a good API can empower developers to remove barriers you might not be aware of for you. I hope to be able to show you more of those in the future.

The code examples are available and are licensed with BSD, so feel free to re-use them.

Easy Flickr – just the photos please

Friday, June 13th, 2008

Following the accessibility hack of YouTube I thought the same could be done for Flickr, and here it is:

Alternative interface to browser Flickr photos easier

Easy Flickr is a very basic interface to look for photos and click through them 20 at a time. It works with and without JavaScript.

Update: It seems there is some confusion as to how this interface works. The main photo is linked to flickr.com (as per API guidelines) and you can navigate up to 20 results by clicking the lower thumbnail to go forward and the upper one to go backward in the results list. This also works with a keyboard and in JAWS but I need to make it more obvious with a hover state and some labels for screen readersand I added some roll-overs and texts for screen readers to make it more obvious..

If you want to host the interface yourself, you need a server with PHP and cURL, but that’s it. Simply unpack the zip file and change the look and feel by changing the style sheet.

Making YouTube easier and more accessible (updated 12/06)

Thursday, June 12th, 2008

Warning: The YouTube API is flaky at the moment, so there might be some outages!

At this year’s Accessibility2.0 conference in London Antonia Hyde from United Response asked the audience for technological solutions to make the social web easier accessible for people with learning disabilities.

Her presentation Rich Media and web apps for people with learning disabilities is available on slideshare.

Whilst not being able to tackle all the issues mentioned (probably the biggest one being captioning) I took some time to play with the YouTube API to create a much easier interface to watch videos. The following screenshot shows the Easy YouTube Player in action:

Easy YouTube player showing a video

Using the player

You can use the player in several ways, the easiest is to just copy and paste a youtube url in the url field. However, there is also a sort of REST interface that allows you to do more:

http://icant.co.uk/easy-youtube/

Shows the player without any movie loaded, empty search fields and playlists.

http://icant.co.uk/easy-youtube/?http://www.youtube.com/watch?v=9i0-btCTdN8

Pre-loads the video of this YouTube address and shows the preview image in the player.

http://icant.co.uk/easy-youtube/?search=panda

Performs a search on YouTube for the term panda and shows links to the videos in the playlist on the right. You can use more than one search word by adding them with a “+”. For example:

http://icant.co.uk/easy-youtube/?search=red+panda

One last option you have is to bookmark certain videos on del.icio.us and tag them for a user. In order to show these videos as a playlist you need to provide your user name and the tag separated by a dash. For example my user name on del.icio.us is “codepo8” and I bookmarked some videos with the tag “easyyoutubeplayer”. The following link will show them all in the playlist:

http://icant.co.uk/easy-youtube/?tag=codepo8-foreasyyoutubeplayer

You can mix and match the different options. If you for example want to show a video and perform a search for other videos you can use:

http://icant.co.uk/easy-youtube/?http://www.youtube.com/watch?v=3UgpfSp2t6k&search=accents

Documentation

The full player documentation with instructions on how to host the player yourself is available in the docs folder Easy YouTube Player documentation.

Download

You can download the player with all the demo files here:

Changes

12/06/08

  • complete re-write of code
  • new buttons – glass were too complex
  • added video size control
  • added search and playlist support
  • added address field to send to friends
  • player now template driven – no more changes in main code needed
  • added documentation
  • added RESTful interface

28/05/08

  • moved buttons to the bottom of the player
  • text is now below the buttons and has an invisible extra “use this button to ” text for screen readers
  • pause button now toggles pause/play
  • mute button now toggles mute/sound
  • the URL of the buttons is not an anchor but a url now (that goes nowhere, but this is just to read out the right command)
  • removed the “current highlight” state
  • added a volume level indicator (as a visual bar and a hidden form field)