Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Created March 18, 2011 10:21
Show Gist options
  • Save dongyuwei/875861 to your computer and use it in GitHub Desktop.
Save dongyuwei/875861 to your computer and use it in GitHub Desktop.
添加部门通讯录到google contacts,继而可以通过google sync同步到手机通讯录。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#by [email protected] | yuwei@staff
import atom
import gdata.contacts
import gdata.contacts.service
import csv
gd_client = gdata.contacts.service.ContactsService()
gd_client.email = '[email protected]'
gd_client.password = 'xxxxxx'
gd_client.source = ''
gd_client.ProgrammaticLogin()
def add_contact(group_entry,name,extension_number='',mobile='',email='',msn='',weibo_nick='',weibo_uri=''):
notes = "name:%s\nmobile:%s\nextension_number:%s\nemail:%s\nmsn:%s\nweibo_nick:%s\nweibo_uri:%s" % (name.decode('utf8'),mobile,extension_number,email,msn,weibo_nick.decode('utf8'),weibo_uri)
print notes
new_contact = gdata.contacts.ContactEntry(title=atom.Title(text=name))
new_contact.phone_number.append(gdata.contacts.PhoneNumber(rel=gdata.contacts.PHONE_MOBILE, text=mobile))
new_contact.phone_number.append(gdata.contacts.PhoneNumber(rel=gdata.contacts.PHONE_WORK, text=extension_number))
new_contact.email.append(gdata.contacts.Email(address=email+"@staff.sina.com.cn",primary='true', rel=gdata.contacts.REL_WORK))
new_contact.email.append(gdata.contacts.Email(address=msn,rel=gdata.contacts.REL_WORK))
new_contact.extended_property.append(gdata.ExtendedProperty(name='微博昵称'.decode('utf8'), value=weibo_nick))
new_contact.extended_property.append(gdata.ExtendedProperty(name='微博域名'.decode('utf8'), value=weibo_uri))
new_contact.content = atom.Content(text=notes)
contact_entry = gd_client.CreateContact(new_contact)
contact_entry.group_membership_info.append(gdata.contacts.GroupMembershipInfo(href=group_entry.id.text))
gd_client.UpdateContact(contact_entry.GetEditLink().href, contact_entry)
contacts = csv.reader(open("contacts.csv", "r"))
contact_list = []
#部门||姓名|分机|手机|邮箱|MSN|微博昵称|微博域名
for data in contacts:
contact_list.append(data)
group_entry = gd_client.CreateGroup(gdata.contacts.GroupEntry(title=atom.Title(text='weibo')))
for data in contact_list[2:]:
try:
add_contact(group_entry,data[2],data[3],data[4],data[5],data[6],data[7],data[8])
except Exception as err:
print 'Error:',err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment