#!/usr/bin/python # -*- coding: utf-8 -*- """ Script to upload photos from a VCF addressbook to Google Contacts Contacts must already exist in Google (import VCF in Google first). Photos are cropped to 96x96 in the middle using ImageMagic (via command line) to match Googles image limit Contacts are matched based on the Full name (FN in VCF) / Entry Title (in Google) Version: 1.1 """ # Configuration: vcf_filename = "addressbook-2009-11-10.vcf" # VCF file to read google_login = "mymail@gmail.com" # Your google Email address password = None # if none -> prompt max_results = 5000 # use low number for testing import vobject import StringIO import atom import gdata.contacts import gdata.contacts.service import sys,os,time # temporary files image_filename = 'photo.jpg' cropped_image_filename = "photoc.jpg" f = open(vcf_filename) gd_client = gdata.contacts.service.ContactsService() gd_client.email = google_login gd_client.password = None gd_client.source = 'exampleCo-exampleApp-1' if gd_client.password is None: import getpass gd_client.password = getpass.getpass("password:") gd_client.ProgrammaticLogin() query = gdata.contacts.service.ContactsQuery() query.max_results = max_results #print "query:", query.max_results, query.ToUri() feed = gd_client.GetContactsFeed(query.ToUri()) for v in vobject.readComponents( f ): #print v.contents if "photo" not in v.contents: continue #v.prettyPrint() name = unicode(v.fn.value) for i, entry in enumerate(feed.entry): if entry.title.text is None: continue title = unicode(entry.title.text, "utf-8") if title == name: print "Uploading Photo for", name.encode('utf-8'), "to", title.encode('utf-8') # Update foto! p = open(image_filename, 'w') p.write(v.photo.value) p.close() # Change size to 96x96 cropped_image_filename = "photoc.jpg" cmd = "convert "+image_filename+" -gravity Center -crop 96x96+0+0 "+cropped_image_filename os.system(cmd) try: photo_metadata = gd_client.ChangePhoto(cropped_image_filename, entry, content_type='image/jpeg') except gdata.service.RequestError: print "Didn't work!"