/
contacts-sync
executable file
·974 lines (808 loc) · 32.9 KB
/
contacts-sync
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
#!/usr/bin/env python
"""Synchronize contacts among a set of GMail accounts."""
__author__ = "Michael Adler"
__copyright__ = "Copyright 2021 Michael Adler"
__license__ = "GPL"
__version__ = "0.2"
##
## Copyright 2021 Michael Adler
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
import argparse
import os
import sys
import re
import uuid
import copy
import pickle
import time
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client import tools
from oauth2client.tools import run_flow
SECRET_JSON = '.google/authdata/client_secret.json'
SCOPES = ['https://www.googleapis.com/auth/contacts']
MAX_RESULTS = 10000
SYNC_ID_TAG = 'csync-uid'
PERSON_FIELDS = 'addresses,ageRanges,biographies,birthdays,calendarUrls,clientData,coverPhotos,emailAddresses,events,externalIds,genders,imClients,interests,locales,locations,memberships,metadata,miscKeywords,names,nicknames,occupations,organizations,phoneNumbers,photos,relations,sipAddresses,skills,urls,userDefined'
PERSON_FIELDS_UPDATE = 'addresses,biographies,birthdays,clientData,emailAddresses,events,externalIds,genders,imClients,interests,locales,locations,memberships,miscKeywords,names,nicknames,occupations,organizations,phoneNumbers,relations,sipAddresses,skills,urls,userDefined'
GROUP_FIELDS = 'metadata,groupType,memberCount,name,clientData'
enableUpdates = True
debug = False
################################################################################
#
# UserContacts class: manage Google server I/O and a single user's copy of
# contacts and groups.
#
################################################################################
class UserContacts(object):
"""UserContacts object loads Google contacts from an account"""
def __init__(self, user, flags = None, createuid = True):
"""Constructor for the UserContacts object.
Takes a user name that will be used to find an OAuth2 stored credential.
"""
self.user = user
##
## Get authorization.
##
if user == None:
print('Must supply a user name!')
sys.exit(1)
# Try to read existing credentials
user_dir = os.path.expanduser('~')
token_fn = user_dir + '/.google/authdata/contacts-credentials-' + user + '.json'
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(token_fn):
creds = Credentials.from_authorized_user_file(token_fn, SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
# Save the credentials for the next run
with open(token_fn, 'w') as token:
token.write(creds.to_json())
else:
print('Need new token for ' + user + ':')
flow = flow_from_clientsecrets(os.path.join(user_dir, SECRET_JSON),
scope=SCOPES,
redirect_uri = 'urn:ietf:wg:oauth:2.0:oob')
auth_flags = tools.argparser.parse_args(args=['--noauth_local_webserver',
'--logging_level=DEBUG'])
creds = run_flow(flow, Storage(token_fn), auth_flags)
self.__service = build('people', 'v1', credentials=creds)
self.people = self.__service.people()
##
## Load all groups into a dictionary, indexed by resourceName
##
self.__groups = dict()
contactGroups = self.__service.contactGroups()
req = contactGroups.list(groupFields=GROUP_FIELDS, pageSize=500)
while True:
resp = req.execute()
if not resp:
break
groups = resp.get('contactGroups', [])
for g in groups:
self.__groups[g['resourceName']] = g
req = contactGroups.list_next(req, resp)
if not req:
break
##
## Load all contacts
##
self.__contacts = dict()
self.nouids = []
req = self.people.connections().list(
resourceName='people/me',
pageSize=500,
sortOrder='FIRST_NAME_ASCENDING',
sources='READ_SOURCE_TYPE_CONTACT',
personFields=PERSON_FIELDS)
while True:
resp = req.execute()
if not resp:
break
connections = resp.get('connections', [])
for person in connections:
# Ignore contacts not belonging to any group
if not person.get('memberships', []):
continue
# Ignore contacts without a name
if not ContactGetPrintName(person):
continue
# Check that contact has one and only one UID. The
# test for multiple UIDs is in case Google's merge contacts
# combines UIDs from multiple contacts.
uid_obj = None
num_uids_found = 0
for obj in person.get('clientData', []):
if obj['key'] == SYNC_ID_TAG:
uid_obj = obj
num_uids_found += 1;
if not uid_obj:
# When syncing a new account for the first time we
# don't want to create uid without first checking
# duplicate names. so just store these entries.
if not createuid:
self.nouids.append(person)
continue
uid_obj = EntryAddUID(person, str(uuid.uuid1()))
if debug:
print('Add UID {0} to "{1}" ({2})'.format(uid_obj['value'],
ContactGetPrintName(person),
user))
if enableUpdates:
person = self.people.updateContact(
resourceName = person['resourceName'],
body = person,
personFields = PERSON_FIELDS,
sources='READ_SOURCE_TYPE_CONTACT',
updatePersonFields = 'clientData').execute()
if num_uids_found > 1:
# Drop extra UIDs
d = [obj for obj in person['clientData'] if obj['key'] != SYNC_ID_TAG]
d.append(uid_obj)
person['clientData'] = d
if debug:
print('Merge UIDs using {0} for "{1}" ({2})'.format(uid_obj['value'],
ContactGetPrintName(person),
user))
if enableUpdates:
person = self.people.updateContact(
resourceName = person['resourceName'],
body = person,
personFields = PERSON_FIELDS,
sources='READ_SOURCE_TYPE_CONTACT',
updatePersonFields = 'clientData').execute()
self.__contacts[uid_obj['value']] = person
req = self.people.connections().list_next(req, resp)
if not req:
break
##
## GetContact --
## Fetch a single contact from local cache.
##
def GetContact(self, uid):
if uid in self.__contacts:
return self.__contacts[uid]
else:
return None
##
## ContactAdd --
## Add a new contact on the Google server
##
def ContactAdd(self, entry):
if enableUpdates:
entry = self.people.createContact(
body = entry,
personFields = PERSON_FIELDS,
sources='READ_SOURCE_TYPE_CONTACT').execute()
self.__contacts[ContactGetUID(entry)] = entry
return entry
##
## ContactUpdate --
## Update an existing contact on the Google server
##
def ContactUpdate(self, entry):
if enableUpdates:
# Update the server. Impose a delay because the Google
# API will fail with too high a request rate.
time.sleep(1)
entry = self.people.updateContact(
resourceName = entry['resourceName'],
body = entry,
personFields = PERSON_FIELDS,
sources='READ_SOURCE_TYPE_CONTACT',
updatePersonFields = PERSON_FIELDS_UPDATE).execute()
self.__contacts[ContactGetUID(entry)] = entry
return entry
##
## ContactDelete --
## Delete a contact from the Google server
##
def ContactDelete(self, entry):
del self.__contacts[ContactGetUID(entry)]
if enableUpdates:
entry = self.people.deleteContact(
resourceName = entry['resourceName']).execute()
##
## ContactIterItems --
## Contact object iterator
##
def ContactIterItems(self):
return iter(self.__contacts.items())
##
## ContactIterValues --
## Contact object iterator
##
def ContactIterValues(self):
return iter(self.__contacts.values())
##
## DumpContacts --
## Dump contact data and IDs.
##
def DumpContacts(self):
for id, entry in self.__contacts.items():
print(' %s: %s' % (id, ContactDebugDump(entry)))
##
## GetGroup --
## Fetch a single group from local cache. Groups are indexed by
## resource name.
##
def GetGroup(self, rsrc_name):
if rsrc_name in self.__groups:
return self.__groups[rsrc_name]
else:
return None
##
## GroupChangeID --
## Change the ID of a group
##
def GroupChangeID(self, rsrc_name, newID):
group = self.__groups[rsrc_name]
if GroupIsSystemGroup(group):
sys.exit("Illegal attempt to change a system group ID")
# Update the SYNC_ID_TAG in extended properties
d = [obj for obj in group.get('clientData', []) if obj['key'] != SYNC_ID_TAG]
uid_obj = dict()
uid_obj['key'] = SYNC_ID_TAG
uid_obj['value'] = newID
d.append(uid_obj)
group['clientData'] = d
if enableUpdates:
upd = dict()
upd['contactGroup'] = group
upd['readGroupFields'] = GROUP_FIELDS
upd['updateGroupFields'] = 'clientData'
self.__groups[rsrc_name] = \
self.__service.contactGroups().update(resourceName = group['resourceName'],
body = upd).execute()
##
## GroupRename --
## Change the name of a group
##
def GroupRename(self, rsrc_name, newName):
group = self.__groups[rsrc_name]
if GroupIsSystemGroup(group):
sys.exit("Illegal attempt to change a system group ID")
group['name'] = newName
# Update the groups dictionary
self.__groups[rsrc_name] = group
if enableUpdates:
upd = dict()
upd['contactGroup'] = group
upd['readGroupFields'] = GROUP_FIELDS
upd['updateGroupFields'] = 'name'
self.__groups[rsrc_name] = \
self.__service.contactGroups().update(resourceName = group['resourceName'],
body = upd).execute()
##
## GroupAdd --
## Add a new group on the Google server
##
def GroupAdd(self, uid, name):
group = dict()
group['name'] = name
EntryAddUID(group, uid)
if not enableUpdates:
self.__groups[uid] = group
else:
group = self.__service.contactGroups().create(
body={'contactGroup': group,
'readGroupFields': GROUP_FIELDS}).execute()
self.__groups[group['resourceName']] = group
##
## GroupIterItems --
## Group iterator
##
def GroupIterItems(self):
return iter(self.__groups.items())
##
## GroupIterValues --
## Group object iterator
##
def GroupIterValues(self):
return iter(self.__groups.values())
##
## InitGroupByUID --
## Once groups have UIDs, following MergeGroups(), build a map from UID
## to group.
##
def InitGroupByUID(self):
self.__groups_by_uid = dict()
for grp in self.GroupIterValues():
self.__groups_by_uid[GroupGetUID(grp)] = grp
##
## GetGroupByUID --
## Lookup a group from its UID. InitGroupByUID() must be run first.
##
def GetGroupByUID(self, uid):
if uid in self.__groups_by_uid:
return self.__groups_by_uid[uid]
else:
return None
##
## DumpGroups --
## Dump group data and IDs.
##
def DumpGroups(self):
for id, grp in self.__groups.items():
print(' %s: %s' % (id, GroupDebugDump(grp)))
##
## Add uid to all entries, possibly using uid from entries with same name
##
def AddUids(contacts):
# map name to (uid, account email address) for all accounts
name2uid = dict([(ContactGetPrintName(person), (uid, c.user)) for c in contacts for uid, person in c.ContactIterItems()])
for c in contacts:
for e in c.nouids:
# use already existing uid if possible
print_name = ContactGetPrintName(e)
if print_name in name2uid:
(uid, user) = name2uid[print_name]
if debug: print('Add UID {0} to "{1}" ({2}) from pre-existing user in account {3}'.format(
uid, print_name, c.user, user))
else:
uid = str(uuid.uuid1())
if debug: print('Add UID {0} to "{1}" ({2})'.format(uid, print_name, c.user))
EntryAddUID(e, uid)
e = c.people.updateContact(
resourceName = e['resourceName'],
body = e,
personFields = PERSON_FIELDS,
sources='READ_SOURCE_TYPE_CONTACT',
updatePersonFields = 'clientData').execute()
################################################################################
#
# Operations on either contacts or groups
#
################################################################################
##
## Return the UID of a contact/group or None if it doesn't have one.
##
def EntryGetUID(entry):
client_data = entry.get('clientData', [])
for obj in client_data:
if obj['key'] == SYNC_ID_TAG:
return obj['value']
return None
def EntryAddUID(entry, uid):
uid_obj = dict()
uid_obj['key'] = SYNC_ID_TAG
uid_obj['value'] = uid
if 'clientData' in entry:
entry['clientData'].append(uid_obj)
else:
entry['clientData'] = [uid_obj]
return uid_obj
################################################################################
#
# Operations on contacts
#
################################################################################
##
## Return the printable name of a contact
##
def ContactGetPrintName(person):
if ('names' in person) and ('displayName' in person['names'][0]):
return person['names'][0]['displayName']
if ('organizations' in person) and ('name' in person['organizations'][0]):
return person['organizations'][0]['name']
return None
def ContactGetUID(person):
return EntryGetUID(person)
def ContactGetUpdateTime(person):
upd_time = ''
if ('metadata' in person):
for src in person['metadata'].get('sources', []):
if src['updateTime'] > upd_time:
upd_time = src['updateTime']
return upd_time
def ContactDebugDump(person):
r = ContactGetPrintName(person) + ':\n'
if ('metadata' in person) and ('sources' in person['metadata']):
for src in person['metadata']['sources']:
r += ' Updated: %s\n' % (src['updateTime'])
r += ' Source Type: %s\n' % (src['type'])
r += ' Resource name: %s\n' % (person['resourceName'])
r += ' ETag: %s\n' % (person.get('etag', ''))
for e in person.get('memberships', []):
if 'contactGroupMembership' in e:
r += ' Group: %s\n' % (e['contactGroupMembership']['contactGroupResourceName'])
for e in person.get('calendarUrls', []):
r += ' Calendar: %s (%s)\n' % (e['url'], e['type'])
for e in person.get('externalIds', []):
r += ' External ID: %s (%s)\n' % (e['value'], e['type'])
for e in person.get('relations', []):
r += ' Relation: %s (%s)\n' % (e['person'], e['type'])
for e in person.get('userDefined', []):
r += ' User Defined: %s (%s)\n' % (e['value'], e['key'])
for e in person.get('urls', []):
r += ' Website: %s (%s)\n' % (e['value'], e['type'])
for e in person.get('phoneNumbers', []):
r += ' Phone: %s (%s)\n' % (e['value'], e['type'])
for e in person.get('emailAddress', []):
r += ' Phone: %s (%s)\n' % (e['value'], e['type'])
for e in person.get('organizations', []):
if 'name' in e:
org_name = e['name']
else:
org_name = 'NO NAME'
r += ' Org name: %s\n' % (org_name)
for e in person.get('imClients', []):
r += ' IM: %s at %s (%s)\n' % (e['username'], e['protocol'], e['type'])
for e in person.get('addresses', []):
## Drop trailing whitespace
addr = e['formattedValue'].rstrip('\n ')
## Move ZIP up
addr = re.sub('\n([0-9]+)$', r' \1', addr)
## Indent
addr = re.sub('\n', '\n ', addr)
r += ' Addr (%s):\n %s\n' % (e['type'], addr)
for e in person.get('photos', []):
r += ' Photo: %s\n' % (e['url'])
return r
################################################################################
#
# Operations on groups
#
################################################################################
def GroupIsSystemGroup(group):
return ('groupType' in group) and (group['groupType'] == 'SYSTEM_CONTACT_GROUP')
##
## Return the UID of a group or None if it doesn't have one.
##
def GroupGetUID(group):
# System group? Use the system ID.
if GroupIsSystemGroup(group):
return group['resourceName']
else:
return EntryGetUID(group)
##
## Return some unique tag for a group. If a UID exists, use that. If
## not, use the group name. Later program phases will guarantee all
## groups have UIDs.
##
def GroupGetTag(group):
tag = GroupGetUID(group)
if not tag:
tag = group['resourceName']
return tag
##
## Return updated time or empty string if not set. System groups may not
## have update times.
##
def GroupGetUpdateTime(group):
if ('metadata' in group) and ('updateTime' in group['metadata']):
return group['metadata']['updateTime']
else:
return ''
def GroupDebugDump(group):
r = group['name'] + '\n'
upd = GroupGetUpdateTime(group)
if upd:
r += ' Updated: %s\n' % (upd)
if GroupIsSystemGroup(group):
r += ' System Group: %s\n' % (group['resourceName'])
else:
r += ' Group: %s\n' % (group['resourceName'])
if 'clientData' in group:
for obj in group['clientData']:
if obj['key'] == SYNC_ID_TAG:
r += ' ID: %s\n' % (obj['value'])
return r
################################################################################
#
# Combine groups from multiple accounts
#
################################################################################
def MergeGroups(users, contacts):
##
## First collect all groups that already have UIDs
##
all_groups_by_uid = dict()
for i in range(len(users)):
for grp in contacts[i].GroupIterValues():
uid = GroupGetUID(grp)
upd_time = GroupGetUpdateTime(grp)
if uid and ((not uid in all_groups_by_uid) or (upd_time > all_groups_by_uid[uid]['updated'])):
# First time UID is seen or this instance was updated more
# recently.
all_groups_by_uid[uid] = { 'name': grp['name'],
'updated': upd_time }
##
## Have any groups changed names?
##
for i in range(len(users)):
for rsrc_name, grp in contacts[i].GroupIterItems():
uid = GroupGetUID(grp)
if uid and (grp['name'] != all_groups_by_uid[uid]['name']):
print('Group: renaming from %s to %s (%s)' % (grp['name'], all_groups_by_uid[uid]['name'], users[i]))
contacts[i].GroupRename(rsrc_name, all_groups_by_uid[uid]['name'])
##
## Build an inverse index from name to uid
##
group_name_to_uid = dict()
for uid, g in all_groups_by_uid.items():
group_name_to_uid[g['name']] = uid
##
## Collect both new and old groups, indexed by user, in preparation for making
# groups global.
##
all_groups_by_name = dict()
for i in range(len(users)):
for grp in contacts[i].GroupIterValues():
name = grp['name']
# First time name is seen? Initialize a list across all users.
if not name in all_groups_by_name:
all_groups_by_name[name] = [None] * len(users)
all_groups_by_name[name][i] = grp
##
## Assign UIDs to new groups across all users.
##
for name in all_groups_by_name.keys():
# Is there already a UID?
if name in group_name_to_uid:
uid = group_name_to_uid[name]
else:
uid = str(uuid.uuid1())
for i in range(len(users)):
if all_groups_by_name[name][i]:
old_uid = GroupGetUID(all_groups_by_name[name][i])
if old_uid != uid:
print('Group: setting %s (%s) to UID %s' % (name, users[i], uid))
contacts[i].GroupChangeID(all_groups_by_name[name][i]['resourceName'], uid)
else:
print('Group: adding %s (%s) with UID %s' % (name, users[i], uid))
contacts[i].GroupAdd(uid, name)
# Build the map from group UID to group for each user
for i in range(len(users)):
contacts[i].InitGroupByUID()
################################################################################
#
# Combine contacts from multiple accounts
#
################################################################################
##
## MergeContacts --
## Combine contacts from all users.
##
def MergeContacts(users, contacts, pvtGroups):
global groupIDtoGroup
##
## Load previous state from last time the merge was run. This is used
## to detect user changes to contacts.
##
prev_state = [LoadPrevContactState(users[i]) for i in range(len(users))]
##
## Find all modified contacts and replicate them
##
for i in range(len(users)):
for uid, person in contacts[i].ContactIterItems():
# Was the contact modified since the last sync?
name = ContactGetPrintName(person)
if not uid in prev_state[i]:
print("Contact: \"{0}\" ({1}) is new".format(name, users[i]))
modified = True
elif ContactGetUpdateTime(person) > prev_state[i][uid]['updated']:
print("Contact: \"{0}\" ({1}) is modified".format(name, users[i]))
modified = True
if debug:
print(' UID: %s' % (uid))
print(' Old: date %s' % (prev_state[i][uid]['updated']))
print(' New: date %s' % (ContactGetUpdateTime(person)))
else:
modified = False
# Is this contact private?
is_private = False
for grp in person.get('memberships', []):
if 'contactGroupMembership' in grp:
if grp['contactGroupMembership']['contactGroupResourceName'] in pvtGroups:
is_private = True
break
for j in range(len(users)):
if j != i:
c = contacts[j].GetContact(uid)
if c:
if is_private:
print("Contact: delete \"{0}\" ({1}) marked private by {2}".format(name, users[j], users[i]))
contacts[j].ContactDelete(c)
del prev_state[j][uid]
elif modified:
print("Contact: update \"{0}\" ({1})".format(name, users[j]))
new_person = UpdateContactInUser(contacts[j], contacts[i], person, c)
prev_state[j][uid] = {'updated': ContactGetUpdateTime(new_person)}
elif not is_private and (modified or not uid in prev_state[j]):
print("Contact: add \"{0}\" ({1})".format(name, users[j]))
new_person = AddContactToUser(contacts[j], contacts[i], person)
prev_state[j][uid] = {'updated': ContactGetUpdateTime(new_person)}
##
## Look for contacts deleted by any user and delete them in all users
##
for i in range(len(users)):
for uid in prev_state[i]:
if not contacts[i].GetContact(uid):
for j in range(len(users)):
c = contacts[j].GetContact(uid)
if c:
name = ContactGetPrintName(c)
print("Contact: delete \"{0}\" ({1}) deleted by {2}".format(name, users[j], users[i]))
contacts[j].ContactDelete(c)
##
## Save the last modified times of the contacts for use in the next run
##
for i in range(len(users)):
state = dict()
for uid, person in contacts[i].ContactIterItems():
state[uid] = {'updated': ContactGetUpdateTime(person)}
if enableUpdates:
SaveContactState(users[i], state)
##
## AddContactToUser --
## Take an entry from one user and add it to another user's contact list.
##
def AddContactToUser(contacts_tgt, contacts_src, src_person):
# Make a copy so the original isn't modified
entry = copy.copy(src_person)
# Update the group membership to the target user's equivalent groups
MapContactGroupsToUser(contacts_tgt, contacts_src, entry, src_person)
ContactDropSourceIdFields(entry)
# Drop system-generated tags
drop_keys = ['resourceName', 'etag', 'photos']
for dk in drop_keys:
if dk in entry:
del entry[dk]
# Add the new contact
return contacts_tgt.ContactAdd(entry)
##
## UpdateContactInUser --
## Take an entry from one user and update another user's existing equivalent
## entry.
##
def UpdateContactInUser(contacts_tgt, contacts_src, src_person, prev_person):
# Make a copy so the original isn't modified
entry = copy.copy(src_person)
# Update the group membership to the target user's equivalent groups
MapContactGroupsToUser(contacts_tgt, contacts_src, entry, src_person)
ContactDropSourceIdFields(entry)
# Drop system-generated tags
drop_keys = ['resourceName', 'etag', 'photos']
for dk in drop_keys:
if dk in entry:
del entry[dk]
# Use the original resource name
entry['resourceName'] = prev_person['resourceName']
entry['etag'] = prev_person['etag']
return contacts_tgt.ContactUpdate(entry)
##
## MapContactGroupsToUser --
## Map the groups associated with src_person to tgt_person, where tgt_person
## is owned by a different account. The target account uses different resource
## names for the equivalent groups.
##
def MapContactGroupsToUser(contacts_tgt, contacts_src, tgt_person, src_person):
tgt_person['memberships'] = []
for grp in src_person.get('memberships', []):
src_rsrc = grp.get('contactGroupMembership', {}).get('contactGroupResourceName', {})
if src_rsrc:
src_grp = contacts_src.GetGroup(src_rsrc)
new_grp = contacts_tgt.GetGroupByUID(GroupGetUID(src_grp))
if new_grp:
rsrc = new_grp['resourceName']
id = re.sub('^[^/]*/', '', rsrc)
tgt_person['memberships'].append({'contactGroupMembership':
{'contactGroupId': id,
'contactGroupResourceName': rsrc}})
##
## ContactDropSourceIdFields --
## When submitting an update, no fields with external sources are
## permitted. Drop them from a person entry.
##
def ContactDropSourceIdFields(person):
for key in list(person):
list_entry = person[key]
if isinstance(list_entry, list):
for entry in list_entry:
if ('metadata' in entry) and ('source' in entry['metadata']):
del entry['metadata']['source']
##
## LoadPrevContactState --
## Load the state (last modified times) of contacts from the last run of
## this program. These times will tell us whether the user has modified
## a contact.
##
def LoadPrevContactState(user):
fn = os.path.expanduser('~') + '/.google/contacts-sync-state-' + user
try:
f = open(fn, 'rb')
state = pickle.load(f)
f.close()
except IOError:
state = dict()
return state
##
## SaveContactState --
## Save the contact state for use in the next run. See LoadPrevContactState().
##
def SaveContactState(user, state):
fn = os.path.expanduser('~') + '/.google/contacts-sync-state-' + user
f = open(fn, 'wb')
pickle.dump(state, f)
f.close()
################################################################################
#
# Main
#
################################################################################
def main():
global enableUpdates
global debug
addingnewaccount = False
users = []
pvt_grp_names = set()
# Parse Google tools flags
parser = argparse.ArgumentParser(description='Synchronize Google contacts between accounts',
parents=[tools.argparser])
parser.add_argument('--user', type=str, required=True, action='append',
help='Name of a Google account.')
parser.add_argument('--private', type=str, action='append',
help='Mark a group that should not be synchronized across accounts. ' +
'Marking an existing shared contact private in one account will ' +
'cause it to be removed from all other accounts.')
parser.add_argument('--adding-new-account', action='store_true',
help='Adding a new account not previously synchronized.')
parser.add_argument('--dry-run', action='store_true',
help='Prevents writes to the Google servers.')
parser.add_argument('--debug', action='store_true',
help='Print debugging messages.')
flags = parser.parse_args()
users = flags.user
pvt_grp_names = flags.private
enableUpdates = not flags.dry_run
debug = flags.debug
##
## If adding a new account use the name of contact to set uid
##
if flags.adding_new_account:
contacts = []
for i in range(len(users)):
contacts.append(UserContacts(users[i],
flags = flags,
createuid = False))
AddUids(contacts)
##
## Open connection to Google server and load current state for each user.
##
contacts = []
for i in range(len(users)):
contacts.append(UserContacts(users[i], flags = flags))
## Merge groups across all users
MergeGroups(users, contacts)
## Compute the set of all private groups (indexed by HREF)
pvt_groups = set()
for i in range(len(users)):
for rsrc_name, grp in contacts[i].GroupIterItems():
if grp['name'] in pvt_grp_names:
pvt_groups.add(rsrc_name)
## Merge contacts across all users
MergeContacts(users, contacts, pvt_groups)
if __name__ == '__main__':
main()