Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- ## Librairies import ##
- from re import sub
- from os import chdir
- ## Require gdata-python-client library
- # wget http://gdata-python-client.googlecode.com/files/gdata-2.0.17.zip
- # tar -xzvf gdata-2.0.17.tar.gz
- # cd gdata-2.0.17/
- # ./setup.py install
- import gdata.contacts.client
- ## Credentials ##
- login = 'example.address' # For example.address@gmail.com
- password = 'YourTopSecretPassword'
- ## Connection ##
- gd_client = gdata.contacts.client.ContactsClient(source='test_python_import')
- gd_client.ClientLogin(login + '@gmail.com', password, gd_client.source)
- ## Contact data request ##
- query = gdata.contacts.client.ContactsQuery()
- # Assuming you have less than 1000 contacts...
- query.max_results = 1000
- # Filter for contact group 'My Contacts'
- query.group = 'http://www.google.com/m8/feeds/groups/' + login + '%40gmail.com/base/6'
- feed = gd_client.GetContacts(q = query)
- # Change working directory
- chdir('/home/username')
- # Open output file
- out_file = open('gContacts.vcf', 'w')
- ## Write contact data in vCard format in the output file ##
- for i, entry in enumerate(feed.entry):
- out_file.write('BEGIN:VCARD\nVERSION:3.0\n')
- # Get last name and firt name
- if entry.name.family_name and entry.name.given_name:
- out_file.write('N:%s;%s\n' % (entry.name.family_name.text.encode('utf-8'), entry.name.given_name.text.encode('utf-8')))
- # Get full name
- out_file.write('FN:%s\n' % (entry.name.full_name.text.encode('utf-8')))
- # Get all email addresses of the contact
- for email in entry.email:
- if email:
- out_file.write('EMAIL;TYPE=INTERNET:%s\n' % (email.address.encode('utf-8')))
- # Get all phone numbers
- for phone in entry.phone_number:
- if phone:
- phone.text = sub(' ', '', phone.text) # Strip space
- out_file.write('TEL:%s\n' % (phone.text.encode('utf-8')))
- out_file.write('END:VCARD\n')
- # Close output file
- out_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement