Turning a web folder with data into an API using YQL execute

Yesterday “Johan Bouveng”:http://www.foolishpleasure.net/ challenged me on Facebook with another brain teaser. Johan had found a nice resource for weather forecasts for airports. The data came back in “TAF or METAR“:http://www.alaska.faa.gov/fai/afss/metar%20taf/metintro.htm formats and he already had a parser for this (thank f*** – what a mess these formats). Now, what he wanted to have is an API to get the weather forecast data from the following resources:

  • “http://weather.noaa.gov/pub/data/forecasts/taf/stations/”:http://weather.noaa.gov/pub/data/forecasts/taf/stations/
  • “http://weather.noaa.gov/pub/data/observations/metar/stations/”:http://weather.noaa.gov/pub/data/observations/metar/stations/

Using “YQL”:http://developer.yahoo.com/yql and “YQL execute”:http://developer.yahoo.com/yql/guide/yql-execute-chapter.html this was pretty easy. All I had to do was to write an “open table that reads the correct file”:http://isithackday.com/hacks/airportweather/airportweather.xml.

// check if taf or metar was requested or return an error
if(datatype == 'taf' || datatype == 'metar'){
  var returnobj,url;
  
  // get the correct url using the airport ID and format
  if(datatype=='metar'){
    url = 'http://weather.noaa.gov/pub/data/observations/' + 
          'metar/stations/' + airportid + '.TXT'
  } else {
    url = 'http://weather.noaa.gov/pub/data/forecasts/'+
          'taf/stations/' + airportid +'.TXT';
  }

  // do a REST call and get the response back
  var out = y.rest(url).get().response;

  // if there is no data returned, return an error.
  if(out == ''){
    returnobj = <error>Airport {airportid} not found.</error>;

  // otherwise return the data in the TXT file
  } else {
    returnobj = <airportweather>{out}</airportweather>;
  }
} else{
  // error condition for wrong datatype
  returnobj = <error>Datatype must be either taf or metar.</error>;
}

// give back the data to YQL
response.object = returnobj;

Having done this you can now use it as a table in YQL:

use "http://isithackday.com/hacks/airportweather/airportweather.xml" as aw;
select * from aw where airportid="AAXX" and datatype="taf";

As you can see, you don’t have to be a genius to build your own API :)

Tags: , , , , ,

8 Responses to “Turning a web folder with data into an API using YQL execute”

  1. Johan Bouveng Says:

    This is so beautiful, and useful. This is example will help many people to API:ify the internets. I will sell my soul to Yahoo. Thanks man!

    /J

  2. paulgeraghty (Paul Geraghty) Says:






    This guy blows me away RT @codepo8 Turning a web folder full of data files into an API using YQL execute: [link to post]

    Posted using Chat Catcher

  3. acarlos1000 (Antonio C. Silveira) Says:






    RT @codepo8: Turning a web folder full of data files into an API using YQL execute: [link to post]

    Posted using Chat Catcher

  4. gyldenlove (gyldenlove) Says:






    RT @jaggeree @codepo8 Turning a web folder full of data files into an API using YQL execute: [link to post] – oh hello awesome potential

    Posted using Chat Catcher

  5. jaggeree (Chris Thorpe) Says:






    RT @codepo8: Turning a web folder full of data files into an API using YQL execute: [link to post] – oh hello awesome potential

    Posted using Chat Catcher

  6. sriniworld (Srinivasu) Says:






    RT @codepo8 Turning a web folder full of data files into an API using YQL execute: [link to post]

    Posted using Chat Catcher

  7. Test_Please (Raj Narayanasamy) Says:






    Wait till I come! » Turning a web folder with data into an API …: Yesterday Johan Bouveng challenged me on Faceb… [link to post]

    Posted using Chat Catcher

  8. gscohn (greg cohn) Says:






    RT @codepo8: Turning a web folder full of data files into an API using YQL execute: [link to post] (very cool!)

    Posted using Chat Catcher

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

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