Advertisement
NiKaro127

Google Contacts to vCard

Oct 3rd, 2012
1,788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. ## Librairies import ##
  5. from re import sub
  6. from os import chdir
  7. ## Require gdata-python-client library
  8. # wget http://gdata-python-client.googlecode.com/files/gdata-2.0.17.zip
  9. # tar -xzvf gdata-2.0.17.tar.gz
  10. # cd gdata-2.0.17/
  11. # ./setup.py install
  12. import gdata.contacts.client
  13.  
  14. ## Credentials ##
  15. login = 'example.address' # For example.address@gmail.com
  16. password = 'YourTopSecretPassword'
  17.  
  18. ## Connection ##
  19. gd_client = gdata.contacts.client.ContactsClient(source='test_python_import')
  20. gd_client.ClientLogin(login + '@gmail.com', password, gd_client.source)
  21.  
  22. ## Contact data request ##
  23. query = gdata.contacts.client.ContactsQuery()
  24. # Assuming you have less than 1000 contacts...
  25. query.max_results = 1000
  26. # Filter for contact group 'My Contacts'
  27. query.group = 'http://www.google.com/m8/feeds/groups/' + login + '%40gmail.com/base/6'
  28. feed = gd_client.GetContacts(q = query)
  29.  
  30. # Change working directory
  31. chdir('/home/username')
  32. # Open output file
  33. out_file = open('gContacts.vcf', 'w')
  34. ## Write contact data in vCard format in the output file ##
  35. for i, entry in enumerate(feed.entry):
  36.         out_file.write('BEGIN:VCARD\nVERSION:3.0\n')
  37.     # Get last name and firt name
  38.         if entry.name.family_name and entry.name.given_name:
  39.                 out_file.write('N:%s;%s\n' % (entry.name.family_name.text.encode('utf-8'), entry.name.given_name.text.encode('utf-8')))
  40.     # Get full name
  41.         out_file.write('FN:%s\n' % (entry.name.full_name.text.encode('utf-8')))
  42.     # Get all email addresses of the contact
  43.         for email in entry.email:
  44.                 if email:
  45.                         out_file.write('EMAIL;TYPE=INTERNET:%s\n' % (email.address.encode('utf-8')))
  46.     # Get all phone numbers
  47.         for phone in entry.phone_number:
  48.                 if phone:
  49.             phone.text = sub(' ', '', phone.text) # Strip space
  50.                         out_file.write('TEL:%s\n' % (phone.text.encode('utf-8')))
  51.         out_file.write('END:VCARD\n')
  52. # Close output file
  53. out_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement