/
gcup.py
86 lines (62 loc) · 1.97 KB
/
gcup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import sys
import gdata.data
import gdata.contacts.client
import gdata.contacts.data
import glob
import vobject
import time
import os
#Class Connection to invoke everything
class Connection(object):
def __init__(self,emailID,Password):
self.gd_client = gdata.contacts.client.ContactsClient(source='GContactsUploader')
print self.gd_client.ClientLogin(emailID,Password,self.gd_client.source)
print "\nWhoo Hoo! We're connected to the Google Server..."
def Sync(self):
flag = raw_input("Enter '0' to exit program or any other key to continue: ")
if flag == '0':
exit()
vcf_path = os.path.dirname(os.path.abspath(__file__))
vcf_list = glob.glob(vcf_path + "\*.vcf")
count = 0
for people in vcf_list:
fp = open(people,"r")
content = fp.read()
fp.close()
v = vobject.readOne(content)
name=v.n.value
number=v.tel.value
new_contact = gdata.contacts.data.ContactEntry()
new_contact.name = gdata.data.Name(full_name=gdata.data.FullName(text=name))
new_contact.phone_number.append(gdata.data.PhoneNumber(text=number,rel=gdata.data.WORK_REL,primary='true'))
contact_entry = self.gd_client.CreateContact(new_contact)
print "Contact's ID: ", contact_entry.id.text
name =""
number = ""
count = count + 1
print "\nUploaded Contacts: " + str(count)
time.sleep(1)
#Main Function here, triggers the Connection class
def main():
''' REVISION FOR LATER
This part would require further cleaning
Use GETOPT instead of simple SYS
'''
if (len(sys.argv) < 3):
print '\nINVAID INPUT FORMAT'
print '\n\t Usage: python gcup.py [email] [password]'
print '\n'
else:
email = sys.argv[1].replace(" ","")
passw = sys.argv[2].replace(" ","")
try:
instance = Connection(email,passw)
print '\nEmail is: ', email,' \nPassword is: ', passw
print '\n'
#Main function call!
instance.Sync()
except gdata.client.BadAuthentication:
print 'Invalid user credentials given'
return
if __name__ == '__main__':
main()