I was pleased to find out that the Picasa Web Albums API makes it quite easy to programmatically upload and download photos from Picasaweb. So any desktop app that manipulates photos or graphics can easily be extended so that it can interact with web albums. The possibilities are manifold… below is some code that demonstrates a couple of the basic capabilities.
Like everything else on Ubuntu/Debian, you only need about 15 seconds to obtain all you need to get started with this. Just install the python-gdata package:
$ sudo aptitude install python-gdata
Uploading albums to Picasaweb
The following function creates a new Picasaweb album from a sequence of photos and associated captions.
#!/usr/bin/python import gdata.photos.service import os.path def create_new_album(album_title, email, password, photos): """ Creates a new Picasa Web Album with the specified images. album_title: title of album, e.g. "Spring Break 2009" email: Google account name, e.g. yourname@gmail.com password: Google account password photos: sequence of tuples where each entry is (filename, caption) e.g. [("/tmp/photo0001.jpg", "A huge turtle"), ...] """ # Authenticate to Picasa Web Albums. gd_client = gdata.photos.service.PhotosService() gd_client.email = email gd_client.password = password gd_client.source = 'MyPhotoUploader' # Fill in the name of your program here. gd_client.ProgrammaticLogin() # Create the album. album = gd_client.InsertAlbum(title = album_title, summary = '') album_url = '/data/feed/api/user/%s/albumid/%s' % \ (album.user.text, album.gphoto_id.text) # Insert the photos. for filename, caption in file_info: gd_client.InsertPhotoSimple( album_or_uri=album_url, title=os.path.basename(filename), # This shows up as 'Filename' on Picasaweb summary=caption, filename_or_handle=filename, content_type="image/jpeg")
Downloading albums from Picasaweb
Normally you can't download entire albums directly from the web (unless you have the Picasa client software installed). You can however use the API for this purpose (the parts in blue below are the core, non-interactive part of the program):
#!/usr/bin/python
#
# This interactive script prompts the user for an album to download.
import gdata.photos.service
import urllib
def main():
"Downloads a Picasa Web Album of the user's choice to the current directory."
gd_client = gdata.photos.service.PhotosService()
username = raw_input("Username? ") # Prompt for a Google account username.
print_album_names(gd_client, username) # Enumerate the albums owned by that account.
album_id = raw_input("Album ID? ") # Prompt for an album ID.
download_album(gd_client, username, album_id) # Download the corresponding album!
def print_album_names(gd_client, username):
"Enumerates the albums owned by USERNAME."
albums = gd_client.GetUserFeed(user = username)
for album in albums.entry:
print '%-30s (%3d photos) id = %s' % \
(album.title.text, int(album.numphotos.text), album.gphoto_id.text)
def download_album(gd_client, username, album_id):
"Downloads all the photos in the album ALBUM_ID owned by USERNAME."
photos = gd_client.GetFeed('/data/feed/api/user/%s/albumid/%s?kind=photo'
% (username, album_id))
for photo in photos.entry:
download_file(photo.content.src)
def download_file(url):
"Download the data at URL to the current directory."
basename = url[url.rindex('/') + 1:] # Figure out a good name for the downloaded file.
print "Downloading %s" % (basename,)
urllib.urlretrieve(url, basename)
if __name__ == '__main__':
main()
More info
Picasaweb API developer's guide (Python client). From that page you can also find more about bindings for .NET, Java, and PHP.
I hereby release the code in this post into the public domain.
No comments:
Post a Comment