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.

Ingen kommentarer:

Legg inn en kommentar