Viser innlegg med etiketten google. Vis alle innlegg
Viser innlegg med etiketten google. Vis alle innlegg

torsdag 11. mars 2010

Google picasa integration

There seems to be developing a series of integration blogs as I build my new website - http://asbjornenge.com - and the next step in the series will be a picasa integration.

We will be using the gdata python client library to get albums and photos from picasa. There is an egg available on pypi, so the first thing we need to do is to grab that.

Obtaining the required packages - the gdata client library

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

$ easy_install gdata

That being said, using the easy_install binary directly like this is not the greatest idea. It will install the library globally, and cause clutter with different versions of the library, different versions in collaboration with other library's, different versions for different python versions. A mess...

One easy way to control your environment is to use a buildout based system, like strings, or make sure you isolate the packages required some other way.


Using the gdata client library to get albums and photos

Lets have a look at how we can use the gdata client library to get albums and photos from picasa.

import gdata.photos.service

# Set up the connection
client = gdata.photos.service.PhotosService(email='username@picasa.com',
password='password',
source='username-someapplication.com-1.0')

# Get the albums
albums = client.GetUserFeed(user='username')
photos = {}
for album in albums.entry:
photos[album.title.text] = []
# Get the photos for each album
aphotos = client.GetFeed(
'/data/feed/api/user/%s/albumid/%s?kind=photo' % ('username', album.gphoto_id.text))

# Get information about the photos
for photo in aphotos.entry:
p = {}
p['title'] = photo.title.text
p['url'] = photo.content.src
p['thumb'] = photo.media.thumbnail[2].url
photos[album.title.text].append(p)

# Print the result
print photos


Its pretty self explanatory, so I'll let the code speak for itself. Remember to instantiate the PhotosService only once ( !!not for every request if your building a web app!! ).

For a more detailed example using strings, check out this page.

enjoy.

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.