-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
53 lines (39 loc) · 1.37 KB
/
main.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
import vobject as v
with open('Contacts.vcf', 'r') as contacts:
contacts_string = contacts.read()
contacts = list(v.readComponents(contacts_string))
new_contacts = ""
def vcard(family, given, tel):
"""Creates a vCard object with the provided elements
"""
c = v.vCard()
c.add('n')
# switch family and given to have GIVEN FAMILY in that order,
# since Nokia S30+ ignores FN and takes N (which is in FAMILY
# GIVEN order)
c.n.value = v.vcard.Name(family=given,
given=family)
c.add('fn')
c.fn.value = given + " " + family
c.add('tel')
c.tel.value = tel
return c
for elem in contacts:
# Check if contact has more phone numbers
try:
fam = elem.n.value.family
giv = elem.n.value.given
tel = elem.tel.value
new_contact = vcard(fam, giv, tel)
new_contacts += new_contact.serialize() + "\n"
extra_tel_numbers = elem.contents['tel']
if len(extra_tel_numbers) > 1:
cnt = 2
for extra_tel in range(1, len(extra_tel_numbers)):
extra_contact = vcard(fam + " " + str(cnt), giv,
elem.contents['tel'][extra_tel].value)
cnt += 1
new_contacts += extra_contact.serialize() + "\n"
except (KeyError, AttributeError):
pass
print(new_contacts)