import appscript import getpass import sys import os.path iPhoto = appscript.app('iPhoto.app') iphoto_album = iPhoto.photo_library_album.get() import gdata.photos.service import gdata.media import gdata.geo mypass = getpass.getpass() def strip_last_digit(s): for i in range(20): if s.endswith('_%d'%(i)): return s.rsplit('_', 1)[0] return s iphoto_albums = {} all_photos = set() dup_photos = [] names = iphoto_album.photos.image_path.get() comments = iphoto_album.photos.comment.get() for name, comment in zip(names, comments): album, filename = name.split('/')[-2:] cur_photos = iphoto_albums.get(album, []) if (album, filename) in all_photos: dup_photos.append(name) continue all_photos.add((album, filename)) iphoto_albums[album] = cur_photos + [(name, comment, filename)] for album in iphoto_albums.keys(): if len(iphoto_albums[album]) > 1000: print album, "too big" del iphoto_albums[album] content_map = {'JPG':'image/jpeg', 'jpg':'image/jpeg', 'tiff':'image/tiff', 'png':'image/png'} while True: print "Restart" try: gd_client = gdata.photos.service.PhotosService() gd_client.email = 'thouis' gd_client.password = mypass gd_client.source = 'thouis-uploader' gd_client.ProgrammaticLogin() change = False for album in iphoto_albums: # I might have two copies running on different computers, so after each album, update the list. picasa_albums = dict((album.title.text, album) for album in gd_client.GetUserFeed(user='thouis').entry) existing_albums = set(picasa_albums.keys()) album_shortname = strip_last_digit(album) if album_shortname not in existing_albums: dest_album = gd_client.InsertAlbum(title=album_shortname, summary=album_shortname, access='private') change = True else: dest_album = picasa_albums[album_shortname] album_url = '/data/feed/api/user/default/albumid/%s' % (dest_album.gphoto_id.text) existing_photos = set(p.title.text for p in gd_client.GetFeed(album_url).entry) print album for name, comment, filename in iphoto_albums[album]: if filename in existing_photos: continue if os.path.exists(name): gd_client.InsertPhotoSimple(album_url, filename, comment, name, content_type=content_map[filename.split('.')[-1]]) print " ", name change = True if not change: sys.exit(0) except: import traceback traceback.print_exc() pass