fredag 22. januar 2010

Google blogger integration

So, a first blog with some relative meaning to it..

Alright, theres a million ways to integrate a blog. But you want a fast and clean code integration right? Preferably using python? Well, here it is...

Google offers a python client library for the Google data API's, witch has a blogger module. There is an egg available over at pypi, and some outdated documentation on the googlecode site.

Obtaining the API wrapper - the gdata python client library

The simplest way to get the gdata python client is to install it using easy_install.

$ easy_install gdata

Using the gdata python client library

First, lets go through the interesting bit - how to use the gdata client to get your blogs.

import gdata.blogger.client

# Get a client and log in
client = gdata.blogger.client.BloggerClient()
client.client_login('googleaccount_username',
'googleaccount_password',
source='someuserorcomp-someapp-1.0',
service='blogger')

# Build the query.
query = gdata.blogger.client.Query(order_by='updated')

# Request the feed.
feed = client.get_posts(self.blog_id, query)

# Get the blogs.
blogs = []
for entry in feed.entry:
blog = {}
blog['title'] = entry.title.text.encode('utf-8')
blog['link'] = entry.GetHtmlLink().href
blog['content'] = entry.content.text.encode('utf-8')
blogs.append(blog)

# Print the results.
print blogs


Its pretty straight forward.

Remember to only do the client_login bit once ( !!not for every request if your building a web app!! ) and to put restraints in the query to limit load and the amount of data transferred.

For a strings example, check out this page.

enjoy.

Ingen kommentarer:

Legg inn en kommentar