diff --git a/vcf_cleaner.py b/vcf_cleaner.py index 8697984..3a31c69 100644 --- a/vcf_cleaner.py +++ b/vcf_cleaner.py @@ -1,9 +1,11 @@ import os +from re import escape import sys import quopri import base64 import pickle import phonenumbers +import pythonvCard4 from pythonvCard4.vcard import Contact # input should be a valid .vcf file, output in current dir @@ -38,7 +40,7 @@ for line in input_file: with open("pictures/" + contact.fn + "/profile.jpg", "wb") as f: f.write(base64.decodebytes(str.encode(contact.custom["PHOTO"][0]))) contact.photo_path = "pictures/" + contact.fn + "/profile.jpg" - contact.custom = {} + contact.custom.pop("PHOTO", None) # reformat phone numbers to international # TODO : distinguish phone numbers in final vcard (hint : "type" ?!) @@ -55,28 +57,34 @@ for line in input_file: 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 + # full-name reencoding contact.fn = quopri.decodestring(contact.fn).decode() + contact.custom.pop("FN", None) - # name list NOT + # name list reencoding clean_n = [] for name in contact.n: clean = quopri.decodestring(name).decode() clean_n.append(clean) - contact.name = clean_n + contact.n = clean_n + contact.custom.pop("N", None) - # nickname NOT USED + # nickname reencoding clean_nickname = [] for nick in contact.nickname: nick = quopri.decodestring(nick).decode() clean_n.append(nick) contact.nickname = clean_nickname + # custom field reencoding + for field in contact.custom: + for value in contact.custom[field]: + contact.custom[field] = quopri.decodestring(value).decode() + + if contact.note: + contact.note = quopri.decodestring(contact.note).decode() + # rewrite contact as vcard - # TODO : override the function that encode lists of strings vcf_text = contact.to_vcard() output.write(vcf_text) current_card = ""