comment all code
This commit is contained in:
parent
988f662265
commit
c3496a499c
1 changed files with 17 additions and 3 deletions
|
|
@ -6,18 +6,24 @@ import pickle
|
|||
import phonenumbers
|
||||
from pythonvCard4.vcard import Contact
|
||||
|
||||
# input should be a valid .vcf file, output in current dir
|
||||
input_file = open(sys.argv[1], 'r', encoding="latin-1").readlines()
|
||||
output = open('output.vcf', 'w')
|
||||
|
||||
# this only outputs pictures of contacts with multiples in a dir for manual check
|
||||
# current code stage ignores the problem and chooses the first picture anyway
|
||||
check_for_multiple_pictures = False
|
||||
|
||||
# a contact is defined by its END:VCARD markup
|
||||
current_card = ""
|
||||
for line in input_file:
|
||||
current_card += line
|
||||
|
||||
if "END:VCARD" in line:
|
||||
# the pythonvCard4 parsing assumes a LOT of things
|
||||
contact = Contact.from_vcard(current_card)
|
||||
|
||||
# TODO : isolate contacts with multiple pictures
|
||||
if check_for_multiple_pictures:
|
||||
if "PHOTO" in contact.custom and len(contact.custom["PHOTO"]) > 1:
|
||||
os.makedirs("multiple_pictures/" + contact.fn, exist_ok=True)
|
||||
|
|
@ -26,6 +32,7 @@ for line in input_file:
|
|||
with open("photo/" + contact.fn + "/" + str(image) + ".jpg", "wb") as f:
|
||||
f.write(base64.decodebytes(str.encode(contact.custom["PHOTO"][image])))
|
||||
|
||||
# choose the first pictures and ignores multiples
|
||||
if "PHOTO" in contact.custom:
|
||||
os.makedirs("pictures/" + contact.fn, exist_ok=True)
|
||||
with open("pictures/" + contact.fn + "/profile.jpg", "wb") as f:
|
||||
|
|
@ -33,6 +40,8 @@ for line in input_file:
|
|||
contact.photo_path = "pictures/" + contact.fn + "/profile.jpg"
|
||||
contact.custom = {}
|
||||
|
||||
# reformat phone numbers to international
|
||||
# TODO : distinguish phone numbers in final vcard (hint : "type" ?!)
|
||||
clean_tel = []
|
||||
for number in contact.tel:
|
||||
try:
|
||||
|
|
@ -44,25 +53,30 @@ for line in input_file:
|
|||
clean_tel.append(number)
|
||||
except phonenumbers.phonenumberutil.NumberParseException:
|
||||
continue
|
||||
|
||||
contact.tel = [{"value": x, "type": []} for x in clean_tel]
|
||||
|
||||
# handle the horrible quoted-printable string format
|
||||
# TODO : doesn't work for list of strings in vobject
|
||||
|
||||
# full-name OK
|
||||
contact.fn = quopri.decodestring(contact.fn).decode()
|
||||
|
||||
# name list NOT
|
||||
clean_n = []
|
||||
for name in contact.n:
|
||||
clean = quopri.decodestring(name).decode()
|
||||
clean_n.append(clean)
|
||||
contact.name = clean_n
|
||||
print(contact.name)
|
||||
|
||||
# nickname NOT USED
|
||||
clean_nickname = []
|
||||
for nick in contact.nickname:
|
||||
nick = quopri.decodestring(nick).decode()
|
||||
clean_n.append(nick)
|
||||
contact.nickname = clean_nickname
|
||||
|
||||
# rewrite contact as vcard
|
||||
# TODO : override the function that encode lists of strings
|
||||
vcf_text = contact.to_vcard()
|
||||
output.write(vcf_text)
|
||||
current_card = ""
|
||||
continue
|
||||
|
|
|
|||
Loading…
Reference in a new issue