One of the great things about Python is the vast amount of libraries that are available to pull into your project – the resource at Vaults of Parnassus is a great place to start google code also tends to host lots of good python plugins.
The same applies to wrappers for popular web services such as the Amazon API, Facebook, Google Maps and Twitter – all of them provide out-of-the-box access tothese services without having to write a line of XML yourself.
Unfortunately not all libraries are built the same, and sometimes you just have to roll your own – this post is going to show how to use RESTful webservices the output JSON.
The example I have below is a really simple mechanism for running a search through Twitter – something the python twitter wrapper doesn’t cater for and sites such as Twiturly rely on for their content. So, onto the code:
# on purpose so we can easily access elements in
# a template tag
def __init__(self, user, text, graphic, time):
self.user = user
self.text = text
self.graphic = graphic
self.time = time
# The meat of the operation
search_url = 'http://search.twitter.com/search.json?q=Django'
import simplejson as json
raw = urllib.urlopen(search_url)
js = raw.readlines()
js_object = json.loads(js)
#filter it all
tweets = 
for item in js_object['results']:
user = item['from_user']
graphic = item['profile_image_url']
text = item['text']
time = item['created_at']
thistweet = tweet(user, text, graphic, time)
Let’s just run through the elements:
- We use urllib to make the request to the search API – specify in the URL that we want JSON back
- We use the simplejson module to decode the reply
- urllib.urlopen(search_url) pens the request, we need to read the data to a variable in order to process it, that’s why we call the readlines() method
- The json.loads() method converts the json information from a string rather than a file
- We then run through the results of the JSON object using keys – I’d recommend running this through the interactive command window first to get an idea of the structure of the json
- I’ve used a class to hold the tweets becasue I wanted to be able to refence the elements quickly from template tags
That’s it – the simplejson module can also be used to serialise information back – but I think this comes into it’s element when working with AJAX calls – something nto in remit of this article.