• Posts Tagged ‘api’

    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\">([^<]+)<\/td>/",$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\">([^<]+)<\/td>/",$feed,$cells);
      if(is_numeric($cells[1][1])){
        $out = ‘{"from":"’.$from.’","to":"’.$to.’","factor":"’.$cells[1][1].’"}’;
      } else {
        $out = ‘{"error":"Could not convert currencies, are you sure about the names?"}’;
      }
    } else {
      $out = ‘{"error":"Invalid 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":"Invalid 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:

    
    <script type="text/javascript"> 
      function converted(obj){
        if(obj.error){
          alert(obj.error);
        } else {
          alert('one ' + obj.from + ' is ' + obj.factor + ' ' + obj.to);
        }
      }
    </script>
    <script type="text/javascript" src="convert.php?from=gbp&to=usd&callback=converted"></script>
    

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

    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:

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

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

    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:

    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:

    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:

    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)

    Problems with the YouTube Chromeless player being unavailable? - Change the URL

    Thursday, June 12th, 2008

    I am almost ready to release v2 of the easy youtube player but out of a sudden everything stopped working. I thought at first my own developer key got banned for incessant re-loading locally, but even Google’s own example is broken.

    The solution to it seems to be to change the URL of the embed code:

    
    // broken
          swfobject.embedSWF('http://gdata.youtube.com/apiplayer?key= ... ')
    // working
          swfobject.embedSWF('http://gdata.youtube.com/apiplayer/cl.swf?key= ... ')
    

    Thanks do d.kunchev who pointed this out on the mailing list

    YouTube now with annotations - can we get those as an API please (captioning)?

    Wednesday, June 4th, 2008

    YouTube just released a new feature for video content generators: annotations. As you can see in this example video of someone jumping out of an aircraft the annotations show up whereever you want to put them on the screen and are time-based. You can even add links and hotspots to other videos and search results which means you can do interactive games using several videos.

    Now this is all cute and nice, but what I’d want is API access to these annotations. This would allow us to provide not only captioning of the video for the hard of hearing but also information for blind visitors. I’ve written about this before, you can easily create an interface to have timed captioning on youtube but playing the captions back is trickier as you have no means of syncing the video (if the video buffers in between the captions and the video get out of sync).

    Now, if YouTube came up with an API access to these captions that fires an event every time a new caption starts with the type of caption and its text value, it would be dead easy to update a hidden form field with that text (or an ARIA live region) to provide a poor man’s captioning and information for the hard of hearing.

    YouTube could become both a larger consumer faced product by enabling more disabled visitors to gain access and a means of captioning video that is intuitive and easy to use.

    I’d be happy to help out!

    Is it time to take mashups and use them to solve real issues?

    Monday, June 2nd, 2008

    This is my presentation given at the BarCamp4 at Gcap in London, talking about my recent move to start doing more mashups again and what lead to it.

    My mashup and accessibility fatigue

    In a nutshell I have to say that I was getting tired of ethical hacking and mashups. Far too many people just create mashups for the sake of putting some information together or prove a technical concept but I just couldn’t see the use of what was produced. We create a lot of ideas, prototypes, proofs of concept, celebrate them as being cool and then never re-visit or turn them into projects.

    I was also bored with the accessibility movement on the web. Instead of concentrating on solutions for people we ran in circles demanding technical solutions or implementation of standards that don’t make much sense in the real world. It was much more important to be compliant with something than to really deliver for the people who needed us to remove barriers for them. It is all about demanding things to be done rather than doing them. And I felt that I wasted my time trying to get something done in this surrounding.

    Boost #1: The social innovation camp

    That changed drastically when I was a judge at the Social Innovation Camp. The concept of the camp was brilliant: allow people who have real world problems to draft up an idea how modern technology like web sites and social networks could help solving or at least making these problems smaller. The entries were massive and ranged from simple things like sharing sites (rent a drill instead of buying one and let it collect dust) to personal growth/learning monitoring systems.

    Boost #2: Enabled by design

    The project that stood out the most for me was Enabled by design which is a showcase site for people with disabilities showing the world what problems they have fulfilling certain day to day tasks (say cutting food) and what tools are available to overcome these problems.

    The second idea of enabled by design is that it should become a place where product designers and production companies could get information about what products are needed and then can start designing and producing those in more appealing ways. Most assistive technology and products are ugly, and they don’t have to be - actually that makes the person who just had to start to use them to fulfill tasks previously easy for them feel even worse. People get as excited about product design as we get about APIs and mashing things up - both of these great amounts of energies could be targeted to solve real-life needs of real people.

    Boost #3 - Ability 2.0 conference and accessihacking YouTube

    With my mindset of giving the accessibility world a swift kick up the backside I gave my talk Fencing-in the habitat at the Accessibility2.0 conference pointing out the useless energy we waste on technical solutions built to satisfy ourselves rather than making a difference for the end user.

    One of the other talks that day was Antonia Hyde talking about the issues users with learning disabilities are facing on the web, especially in regards to online video. Well, I thought to myself, as YouTube has an API, and I’ve been playing around with it already, why not have a go at an accessible YouTube player. I’ve created a prototype and sent that out to Antonia and some other accessibility contacts and the feedback was awesome.

    What confused me most was that I got feedback from schools and blind people thanking me for the player and finally being able to use YouTube. I liked that a lot - realizing that I helped far more people than I thought by tackling something I hadn’t tried before - thinking in detail about the needs of people with learning disabilities!

    The player is going strong and I am now writing documentation for the 2.0 version which will feature a search, playlists created by bookmarking in del.icio.us and more features like zoom.

    Question: What about the future?

    Am I weird (don’t answer that out of context) or is there something in there? Are there more developers out there who are stuck in a rut mashing up data without ever really making a difference with it, or do I care to go there just because I have so much exposure to this world?

    I am imagining (and already started) planning an event for exactly that - social and accessible hacking of currently used internet services. We could have a hackday weekend with spokespeople from different agencies explaining the issues that people with disabilities have to use for example flickr, youtube, last FM and so on and a bunch of hackers to have a go at building alternative interfaces based on the APIs of these companies. I would also like to get people from these companies there to learn about the hacks and maybe take on some of the learnings and put them in the live systems.

    The question is: would that be something you want?

    Wait till I come! is the blog of , a developer evangelist living and working in London, England. Download vcard.

    Feed me, Seymour: Entries (RSS) and Comments (RSS).