#!/usr/bin/python # # Let's say you imported a lot of contacts into your google account, and now # all the phone numbers are saved as "other", and not home or mobile. # If you use zindus thunderbird extension to synchronise the thunderbird # addressbook to google, you will not see those numbers. # # This script looks all phone numbers, and modifies them using a simple criterion, # setting them either to home or mobile import sys import getopt import getpass import atom import gdata.contacts import gdata.contacts.service import gdata.contacts.client # TODO: TOKEN = """INSERT AUTH TOKEN HERE""" # if you don't have a token, uncomment the triple ### commented lines REL_OTHER = "http://schemas.google.com/g/2005#other" REL_HOME = "http://schemas.google.com/g/2005#home" REL_MOBILE = "http://schemas.google.com/g/2005#mobile" class ContactRead(object): def __init__(self, email, password): self.gd_client = gdata.contacts.service.ContactsService() self.gd_client.email = email ###self.gd_client.password = password self.gd_client.source = "mpb.li-ContactRead" self.gd_client.SetClientLoginToken(TOKEN) # Comment this if no TOKEN ###self.gd_client.ProgrammaticLogin() def PrintFeed(self, feed, ctr=0): if not feed.entry: print '\nNo entries in feed.\n' return 0 print "%s entries in feed " % len(feed.entry) for i, entry in enumerate(feed.entry): if entry.phone_number: newnum = [] updated = 0 for num in entry.phone_number: if num.rel == REL_OTHER: # we have to do something ! if num.text.startswith("+417"): # swiss mobile phone newcontact = gdata.contacts.PhoneNumber(rel=REL_MOBILE, text=num.text, uri=num.uri, primary=num.primary) else: # Home phone newcontact = gdata.contacts.PhoneNumber(rel=REL_HOME, text=num.text, uri=num.uri, primary=num.primary) newnum.append(newcontact) updated += 1 print '\n%s %s' % (ctr+i+1, entry.title.text) print "label %s \t->\t %s" % (num.label, newcontact.label) print "rel %s \t->\t %s" % (num.rel, newcontact.rel) print "uri %s \t->\t %s" % (num.uri, newcontact.uri) print "text %s \t->\t %s" % (num.text, newcontact.text) # number print "primary %s \t->\t %s" % (num.primary, newcontact.primary) print else: print '\n%s - %s is ok' % (entry.title.text, num.text) newnum.append(num) assert len(entry.phone_number) == len(newnum) if updated > 0: # replace phone number list by newnum entry.phone_number = newnum print("updating {0} out of {1} entries".format(updated, len(entry.phone_number))) self.gd_client.UpdateContact(entry.GetEditLink().href, entry) return len(feed.entry) + ctr def ListAllContacts(self): #self.gd_client.max_results = 250 #self.gd_client.start_index = ctr feed = self.gd_client.GetContactsFeed() while feed: self.PrintFeed(feed) next = feed.GetNextLink() feed = None if next: feed = self.gd_client.GetContactsFeed(next.href) else: break def main(): # Parse command line options try: opts, args = getopt.getopt(sys.argv[1:], '', ['user=', 'pw=']) except getopt.error, msg: print './%s --user [username] --pw [password]' % sys.argv[0] sys.exit(2) user = '' pw = '' # Process options for option, arg in opts: if option == '--user': user = arg elif option == '--pw': pw = arg ###while not user: ###user = raw_input('Please enter your username: ') ###while not pw: ###pw = getpass.getpass() ###if not pw: ###print 'Password cannot be blank.' try: sample = ContactRead(user, pw) except gdata.service.BadAuthentication: print 'Invalid user credentials given.' return sample.ListAllContacts() if __name__ == '__main__': main()