-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvCard_data_cruncher.py
328 lines (278 loc) · 16.3 KB
/
vCard_data_cruncher.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
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
import os
import vobject
class vCardDataCruncher:
version_supported = [3.0]
contact_categories_lookup = {'001': "only_name",
'010': "only_email",
'100': "only_tele",
'101': "tele_and_name",
'110': "tele_and_email",
'011': "email_and_name",
'111': "name_email_and_tele"}
required_fields = {3.0: ["fn", "n"]}
def __init__(self,
file_path: str):
file_path = os.path.abspath(file_path)
self.file_location = '/'.join(file_path.split('/')[:-1])
self.file_name = file_path.split('/')[-1]
self.contact_categories = dict()
for _, entry in self.contact_categories_lookup.items():
self.contact_categories[entry] = list()
self.final_contacts_list = list()
self.raw_contacts_list = list()
self.raw_contacts_modified_atleast_once = False
with open(f"{self.file_location}/{self.file_name}") as vcf_file:
individual_contacts = []
append_to_current_entry = False
current_entry = ""
for line in vcf_file:
if line.startswith("BEGIN:VCARD"):
append_to_current_entry = True
elif line.startswith("END:VCARD"):
current_entry += line
append_to_current_entry = False
if append_to_current_entry:
current_entry += line
else:
individual_contacts.append(current_entry)
current_entry = ""
for contact in individual_contacts:
v_contact = vobject.readOne(contact)
self.raw_contacts_list.append(v_contact)
# self.pretty_print(v_contact)
@staticmethod
def pretty_print(vcard_object: vobject.vCard()):
print("\n---------------------------")
for field in vcard_object.contents:
field_contents = vcard_object.contents[field]
for data in field_contents:
print(f"{data.name}")
print(f"\tValue - {data.value}")
print("---------------------------")
def sort_contacts_into_categories(self):
for contact in self.intermediate_contacts_list:
no_name = False
no_tele = False
no_email = False
if 'fn' not in contact.contents or 'n' not in contact.contents:
no_name = True
if 'tel' not in contact.contents:
no_tele = True
if 'email' not in contact.contents:
no_email = True
category = f"{int(not no_tele)}{int(not no_email)}{int(not no_name)}"
self.contact_categories[self.contact_categories_lookup[category]].append(contact)
def merge_contacts_with_same_tele(self):
print("\nMerging contacts with same telephone number")
count = 0
# Use this dict to keep a track of the number stored in the processed_list
numbers_used = dict()
if self.raw_contacts_modified_atleast_once:
copy_of_latest_contacts_list = self.intermediate_contacts_list
else:
self.raw_contacts_modified_atleast_once = True
copy_of_latest_contacts_list = self.raw_contacts_list
processed_list = list()
for contact in copy_of_latest_contacts_list:
# Check if there is an entry for telephone numbers in the contact.
if contact.contents.get('tel'):
contact_number = contact.contents['tel'][0].value
# If the number doesnt exists in the dict, then insert the contact into the processed list.
if contact_number not in numbers_used:
processed_list.append(contact)
numbers_used[contact_number] = len(processed_list) - 1
# Else, if there is a contact with the same number present,
# copy those values from the current contact,
# that are missing from the contact stored in the processed list.
else:
count += 1
# print(f"Number {contact_number} already exists!")
# Get the contact from the processed_list that matches the current contact.
existing_entry_in_processed_list = processed_list[numbers_used[contact_number]]
# self.pretty_print(existing_entry_in_processed_list)
# If the current contact has email's that are not in the contact stored in the processed_list,
# add them to the contact stored.
if 'email' in contact.contents:
for email in contact.contents['email']:
if 'email' in existing_entry_in_processed_list.contents:
for email_stored in existing_entry_in_processed_list.contents['email']:
if email_stored.value != email.value:
existing_entry_in_processed_list.add('email').value = email.value
else:
existing_entry_in_processed_list.add('email').value = email.value
# If the current contact has tele's that are not in the contact stored in the processed_list,
# add them to the contact stored.
if 'tel' in contact.contents:
for tel in contact.contents['tel']:
if 'tel' in existing_entry_in_processed_list.contents:
for tel_stored in existing_entry_in_processed_list.contents['tel']:
if tel_stored.value != tel.value:
existing_entry_in_processed_list.add('tel').value = tel.value
else:
existing_entry_in_processed_list.add('tel').value = tel.value
# self.pretty_print(existing_entry_in_processed_list)
# The contact doesn't have a phone number.
# So just copy it and process it in some other function.
else:
processed_list.append(contact)
print(f"\t#contacts before processing - {len(copy_of_latest_contacts_list)}")
print(f"\t#contacts deleted - {count}")
print(f"\t#contacts after processing - {len(processed_list)}")
# Copy the contacts into the instance variable
self.intermediate_contacts_list = processed_list
def merge_contacts_with_same_fn(self):
print("\nMerging contacts with same name")
count = 0
# Use this dict to keep a track of the number stored in the processed_list
names_used = dict()
if self.raw_contacts_modified_atleast_once:
copy_of_latest_contacts_list = self.intermediate_contacts_list
else:
self.raw_contacts_modified_atleast_once = True
copy_of_latest_contacts_list = self.raw_contacts_list
processed_list = list()
for contact in copy_of_latest_contacts_list:
# Check if there is an entry for telephone numbers in the contact.
if contact.contents.get('fn', None):
contact_name = contact.contents['fn'][0].value
if contact_name != "":
# If the number doesnt exists in the dict, then insert the contact into the processed list.
if contact_name not in names_used:
processed_list.append(contact)
names_used[contact_name] = len(processed_list) - 1
# Else, if there is a contact with the same number present,
# copy those values from the current contact,
# that are missing from the contact stored in the processed list.
else:
count += 1
# print(f"Contact named {contact_name} already exists!")
# Get the contact from the processed_list that matches the current contact.
existing_entry_in_processed_list = processed_list[names_used[contact_name]]
# self.pretty_print(existing_entry_in_processed_list)
# If the current contact has email's that are not in the contact stored in the processed_list,
# add them to the contact stored.
if 'email' in contact.contents:
for email in contact.contents['email']:
if 'email' in existing_entry_in_processed_list.contents:
for email_stored in existing_entry_in_processed_list.contents['email']:
if email_stored.value != email.value:
existing_entry_in_processed_list.add('email').value = email.value
else:
existing_entry_in_processed_list.add('email').value = email.value
# If the current contact has tele's that are not in the contact stored in the processed_list,
# add them to the contact stored.
if 'tel' in contact.contents:
for tel in contact.contents['tel']:
if 'tel' in existing_entry_in_processed_list.contents:
for tel_stored in existing_entry_in_processed_list.contents['tel']:
if tel_stored.value != tel.value:
existing_entry_in_processed_list.add('tel').value = tel.value
else:
existing_entry_in_processed_list.add('tel').value = tel.value
# self.pretty_print(existing_entry_in_processed_list)
else:
processed_list.append(contact)
# The contact doesn't have a name.
# So just copy it and process it in some other function.
else:
processed_list.append(contact)
print(f"\t#contacts before processing - {len(copy_of_latest_contacts_list)}")
print(f"\t#contacts deleted - {count}")
print(f"\t#contacts after processing - {len(processed_list)}")
# Copy the contacts into the instance variable
self.intermediate_contacts_list = processed_list
def merge_contacts_with_same_email(self):
print("\nMerging contacts with same email")
count = 0
# Use this dict to keep a track of the number stored in the processed_list
emails_used = dict()
if self.raw_contacts_modified_atleast_once:
copy_of_latest_contacts_list = self.intermediate_contacts_list
else:
self.raw_contacts_modified_atleast_once = True
copy_of_latest_contacts_list = self.raw_contacts_list
processed_list = list()
for contact in copy_of_latest_contacts_list:
# Check if there is an entry for telephone numbers in the contact.
if contact.contents.get('email', None):
contact_email = contact.contents['email'][0].value
# If the number doesnt exists in the dict, then insert the contact into the processed list.
if contact_email not in emails_used:
processed_list.append(contact)
emails_used[contact_email] = len(processed_list) - 1
# Else, if there is a contact with the same number present,
# copy those values from the current contact,
# that are missing from the contact stored in the processed list.
else:
count += 1
# print(f"Contact with email {contact_email} already exists!")
# Get the contact from the processed_list that matches the current contact.
existing_entry_in_processed_list = processed_list[emails_used[contact_email]]
# self.pretty_print(existing_entry_in_processed_list)
# If the current contact has email's that are not in the contact stored in the processed_list,
# add them to the contact stored.
if 'email' in contact.contents:
for email in contact.contents['email']:
if 'email' in existing_entry_in_processed_list.contents:
for email_stored in existing_entry_in_processed_list.contents['email']:
if email_stored.value != email.value:
existing_entry_in_processed_list.add('email').value = email.value
else:
existing_entry_in_processed_list.add('email').value = email.value
# If the current contact has tele's that are not in the contact stored in the processed_list,
# add them to the contact stored.
if 'tel' in contact.contents:
for tel in contact.contents['tel']:
if 'tel' in existing_entry_in_processed_list.contents:
for tel_stored in existing_entry_in_processed_list.contents['tel']:
if tel_stored.value != tel.value:
existing_entry_in_processed_list.add('tel').value = tel.value
else:
existing_entry_in_processed_list.add('tel').value = tel.value
# self.pretty_print(existing_entry_in_processed_list)
# The contact doesn't have an email.
# So just copy it and process it in some other function.
else:
processed_list.append(contact)
print(f"\t#contacts before processing - {len(copy_of_latest_contacts_list)}")
print(f"\t#contacts deleted - {count}")
print(f"\t#contacts after processing - {len(processed_list)}")
# Copy the contacts into the instance variable
self.intermediate_contacts_list = processed_list
def interactively_filter_contacts(self):
get_rid_of_all_contacts_with = dict()
get_rid_of_all_contacts_with['only_email'] = True
get_rid_of_all_contacts_with['only_name'] = True
get_rid_of_all_contacts_with['only_tele'] = False
get_rid_of_all_contacts_with['email_and_name'] = True
write_without_asking_contacts_with = dict()
write_without_asking_contacts_with['tele_and_name'] = True
write_without_asking_contacts_with['name_email_and_tele'] = True
for category in self.contact_categories:
if not get_rid_of_all_contacts_with.get(category, False):
if not write_without_asking_contacts_with.get(category, False):
for entry in self.contact_categories[category]:
self.pretty_print(entry)
choice = input("\nDo you want to get rid of this contact ? (Y/N): ")
if choice.upper() == 'N':
self.final_contacts_list.append(entry)
else:
for entry in self.contact_categories[category]:
self.final_contacts_list.append(entry)
def write_final_list_to_file(self):
count = 0
print(f"\n--> Processed file path - {self.file_location}/processed_{self.file_name}")
with open(f"{self.file_location}/processed_{self.file_name}", 'w') as output_file:
for entry in self.final_contacts_list:
output_file.write(entry.serialize())
count += 1
print(f"--> Total number of contact - {count}")
if __name__ == '__main__':
contacts_db1 = vCardDataCruncher("contacts.vcf")
contacts_db1.merge_contacts_with_same_tele()
contacts_db1.merge_contacts_with_same_fn()
contacts_db1.merge_contacts_with_same_email()
contacts_db1.sort_contacts_into_categories()
contacts_db1.interactively_filter_contacts()
contacts_db1.write_final_list_to_file()
print()