#!/usr/bin/python2.5 import sys import os import urllib2 # Try importing it from the system and then from the named subdir try: import gdata.photos.service import gdata.media import gdata.geo except: sys.path.append('gdata.py-1.1.0/src') import gdata.photos.service import gdata.media import gdata.geo gd_client = gdata.photos.service.PhotosService() def sync ( username, prefix, limit = None ): prefix = os.path.join(prefix, username) if not os.path.isdir(prefix): print "Creating directory %s" % (prefix) os.makedirs(prefix) album_list = gd_client.GetUserFeed(user = username, limit = limit) for album in reversed(album_list.entry): sync_album(album, prefix) def sync_album ( album, prefix ): album_dir = os.path.join(prefix, album.name.text); if not os.path.isdir(album_dir): print "Creating directory %s" % (album_dir) os.makedirs(album_dir) current_dir = set(os.listdir(album_dir)) new_files = [] photos = gd_client.GetFeed(album.GetPhotosUri()) for photo in photos.entry: if not os.path.exists(os.path.join(album_dir, photo.title.text)): print "Downloading: %s -> %s" % (photo.content.src, os.path.join(album_dir, photo.title.text) ) download(photo.content.src, os.path.join(album_dir, photo.title.text)) new_files.append(photo.title.text) unlink = current_dir - set(new_files) for name in unlink: print "Deleting unused file %s" % (os.path.join(album_dir,name)) os.remove(os.path.join(album_dir,name)) def download ( url, destination ): out = open(destination, 'w') out.write(urllib2.urlopen(url).read())
~ $ python2.5 -i PicSync.py >>> sync('ericewarnke','/media/mmc1/Photos/',1) Creating directory /media/mmc1/Photos/ericewarnke Creating directory /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ Downloading: http://lh4.ggpht.com/ericewarnke/SEylJSZCv5I/AAAAAAAACF0/qNqPfjVNSrQ/20080608-CRW_0811.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0811.jpg Downloading: http://lh3.ggpht.com/ericewarnke/SEylLKRiLpI/AAAAAAAACF4/I-KKY8Cj3co/20080608-CRW_0813.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0813.jpg Downloading: http://lh6.ggpht.com/ericewarnke/SEylNPbZVmI/AAAAAAAACF8/ypo8b0Wj2gw/20080608-CRW_0821.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0821.jpg Downloading: http://lh4.ggpht.com/ericewarnke/SEylPCvHQpI/AAAAAAAACGA/1KJU8aLqh0w/20080608-CRW_0823.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0823.jpg Downloading: http://lh5.ggpht.com/ericewarnke/SEylRVDHCZI/AAAAAAAACGE/Y5-ECNzhYE0/20080608-CRW_0826.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0826.jpg Downloading: http://lh3.ggpht.com/ericewarnke/SEylS9oDQXI/AAAAAAAACGI/p4KVric6Cio/20080608-CRW_0834.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0834.jpg Downloading: http://lh4.ggpht.com/ericewarnke/SEylVFBDeOI/AAAAAAAACGQ/_1pzf9TLR0c/20080608-CRW_0840.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0840.jpg Downloading: http://lh4.ggpht.com/ericewarnke/SEylXB6_lII/AAAAAAAACGU/91F6rY3zG0k/20080608-CRW_0845.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0845.jpg