Category Archives: yahoo

adventure with yahoo yql: part 2

Because yql, have a rest based interface. And it generate data in XML and JSON form, it is interesting to write program for.

Below is a small code I have written using python 2.6. Which I wrote in python shell.

First:

import json,urllib

If you use python 2.5, you should first install simplejson, then:

import simplejson as json
import urllib

my
Next step, in one line:

query=urllib.urlencode({“format”:”json”,”env”:”http://datatables.org/alltables.env”,”q”:”select * from weather.local where location=’myxx0008′”})

urlencode takes a dictionary to encode into a form, that is url friendly.
From dictionary, “format” describe the output format, change “json” to “xml”

“format”:”json”

“env” describe the table source, change ‘http://datatables.org/alltables.env’, to channge data source. BTW the this source contain alot of definition to use..

“env”:”http://datatables.org/alltables.env”

“q” is the query, change, “select * from weather.local where location=’myxx0008′”.In this query, location is mandatory, but it is data source dependent. Use “desc tablename” first is a good idea.

“q”:”select * from weather.local where location=’myxx0008′”

Moving on, once the above command finish. We can use urlopen to generate data to load into json api. Using query generated above.

result=json.load(urllib.urlopen(“http://query.yahooapis.com/v1/public/yql?%s” % query))

One reason to use json here, because getting result data is pretty easy. It is deserialize it into a python dictionary.

For example to get the weather data,

print result[‘query’][‘results’][‘weather’]

It deserialize into python object. Another example to get temperature

print result[‘query’][‘results’][‘weather’][‘cc’][‘tmp’]

To wrap it up, in a simple script

try:
#try to see if it is working
    import json,urllib
except:
#in case you’re using python 2.5, BTW install simplejson #first
    import simplejson as json
    import urllib
#encoding
query=urllib.urlencode({“format”:”json”,”env”:”http://datatables.org/alltables.env”,”q”:”select * from weather.local where location=’myxx0008′”})
#get the data and deserialize it.
result=json.load(urllib.urlopen(“http://query.yahooapis.com/v1/public/yql?%s” % query))
#print temperature data
print result[‘query’][‘results’][‘weather’][‘cc’][‘tmp’]

This a simple script to test yql. Somebody should really write a library to wrap it up. And the above script I use public data tables. Some yahoo data tables, uses oauth. Which I bypass it.

One thing i think of yql is it is convenient to have one place to query data. And with sql like command to filter data is interesting. But still not quite sure whether, this is necessary.