-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
74 lines (48 loc) · 1.72 KB
/
utils.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
"""Utility functions for getting and processing contact names."""
import os
CONTACTS_FILES_PATH = 'contacts_files/'
def get_path(file_name):
return '{}{}'.format(CONTACTS_FILES_PATH, file_name)
def does_file_exist(file_name):
return os.path.exists(get_path(file_name))
def get_contacts_source(file_name):
filename, extension = os.path.splitext(file_name)
if extension == '.vcf':
return 'apple'
elif 'facebook' in filename:
return 'facebook'
else:
return 'txt/csv'
def is_contact(previous_line, source):
if source == 'apple':
return previous_line[0:2] == 'N:'
elif source == 'facebook':
return previous_line == 'Friends\n'
else:
return True
def process_contact(line, source):
if source == 'apple':
return line[3:].strip()
return line.strip()
def get_contacts_set_from_file(file_name):
if not does_file_exist(file_name):
return set()
source = get_contacts_source(file_name)
contacts = set()
with open(get_path(file_name), 'r') as contacts_file:
previous_line = ''
for line in contacts_file:
if is_contact(previous_line, source):
contact = process_contact(line, source)
contacts.add(contact)
previous_line = line
return contacts
def combine_and_convert(contacts):
if not does_file_exist('combine_and_convert.csv'):
return
with open(get_path('combine_and_convert.csv'), 'r') as combine_and_convert_file:
for line in combine_and_convert_file:
names = line.strip().split(',')
name_to_keep = names[0]
contacts.difference_update(set(names))
contacts.add(name_to_keep)