Extending the weather API with YQL

Daniel Saxil-Nielsen has been playing with our weather API to offer different designs of a web site depending on the weather. In order to get the data from the weather API he hard-coded an RSS feed for Reading, UK and used Yahoo Pipes to filter it.

This is lovely and does the job but it is a bit old school as we now have YQL to do this job more easily.

For starters, using the Weather API is pretty easy in YQL:

select * from weather.forecast where location="UKXX0117"

Here's the XML outcome and you can see it in the YQL console.

As Daniel's example wanted only the text of the forecast to use as a hook for different design options, we can do that by filtering down the data in YQL. (Notice that you don't need to worry about the namespace - WIN! ):

select item.condition.text from weather.forecast where location="UKXX0117"

Now the main problem with the Weather API (other than it just being an RSS feed and not allowing you to define a date range, for example) is that the location parameter only allows for US zip codes. Post codes outside the land of the free are not allowed. The old workaround was to do a search on the Yahoo weather site, find our ID and use that one - not sexy.

A better workaround that allows us to use a location name uses both the Yahoo Weather API and the YQL "weather.search" table which uses the weather.com API instead. That way we can do a search for the right ID by providing the name of the city and don't need to worry about the IDs:

select item.condition.text from weather.forecast where location in (
select id from weather.search where query="oslo, norway"

Here's the XML outcome and you can see it in the YQL console.

Now, this is relying on a third-party API though (which is rate-limited on top of ours being rate limited) and the great news is that the newer weather API now uses Where On Earth IDs (WOEIDs). This means we can use the Yahoo Geo platform to do the conversion from city name to location. Sadly enough the table in YQL doesn't allow for that yet, so I just quickly wrote a simple table to fix that. Using this table we can use the Geo API and the weather API together.

use 'http://github.com/yql/yql-tables/raw/master/weather/weather.woeid.xml' as weather;
select * from weather where w in (
select woeid from geo.places where text="oslo,norway"
) and u='c';

We can also go full-out and use the execute part of YQL tables to simply have a "get weather by location API":

use 'http://github.com/yql/yql-tables/raw/master/weather/weather.bylocation.xml' as we;
select * from we where location="berlin,germany" and unit='c'

See this in the console and check out the results.

Over the next few days these tables will go in the main YQL branch, but for now you can see just how easy it is to use different APIs in YQL and to extend your reach when you need to.

Chris Heilmann
Yahoo Developer Network