reencode notes and custom fields

This commit is contained in:
diegantobass 2025-05-08 09:59:54 +02:00
parent c3496a499c
commit c481fb90f6

View file

@ -1,9 +1,11 @@
import os import os
from re import escape
import sys import sys
import quopri import quopri
import base64 import base64
import pickle import pickle
import phonenumbers import phonenumbers
import pythonvCard4
from pythonvCard4.vcard import Contact from pythonvCard4.vcard import Contact
# input should be a valid .vcf file, output in current dir # 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: with open("pictures/" + contact.fn + "/profile.jpg", "wb") as f:
f.write(base64.decodebytes(str.encode(contact.custom["PHOTO"][0]))) f.write(base64.decodebytes(str.encode(contact.custom["PHOTO"][0])))
contact.photo_path = "pictures/" + contact.fn + "/profile.jpg" contact.photo_path = "pictures/" + contact.fn + "/profile.jpg"
contact.custom = {} contact.custom.pop("PHOTO", None)
# reformat phone numbers to international # reformat phone numbers to international
# TODO : distinguish phone numbers in final vcard (hint : "type" ?!) # TODO : distinguish phone numbers in final vcard (hint : "type" ?!)
@ -55,28 +57,34 @@ for line in input_file:
continue continue
contact.tel = [{"value": x, "type": []} for x in clean_tel] contact.tel = [{"value": x, "type": []} for x in clean_tel]
# handle the horrible quoted-printable string format # full-name reencoding
# TODO : doesn't work for list of strings in vobject
# full-name OK
contact.fn = quopri.decodestring(contact.fn).decode() contact.fn = quopri.decodestring(contact.fn).decode()
contact.custom.pop("FN", None)
# name list NOT # name list reencoding
clean_n = [] clean_n = []
for name in contact.n: for name in contact.n:
clean = quopri.decodestring(name).decode() clean = quopri.decodestring(name).decode()
clean_n.append(clean) clean_n.append(clean)
contact.name = clean_n contact.n = clean_n
contact.custom.pop("N", None)
# nickname NOT USED # nickname reencoding
clean_nickname = [] clean_nickname = []
for nick in contact.nickname: for nick in contact.nickname:
nick = quopri.decodestring(nick).decode() nick = quopri.decodestring(nick).decode()
clean_n.append(nick) clean_n.append(nick)
contact.nickname = clean_nickname 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 # rewrite contact as vcard
# TODO : override the function that encode lists of strings
vcf_text = contact.to_vcard() vcf_text = contact.to_vcard()
output.write(vcf_text) output.write(vcf_text)
current_card = "" current_card = ""