vcf_cleaner/vcf_cleaner.py
2025-05-07 23:17:52 +02:00

68 lines
2.4 KiB
Python

import os
import sys
import quopri
import base64
import pickle
import phonenumbers
from pythonvCard4.vcard import Contact
input_file = open(sys.argv[1], 'r', encoding="latin-1").readlines()
output = open('output.vcf', 'w')
check_for_multiple_pictures = False
current_card = ""
for line in input_file:
current_card += line
if "END:VCARD" in line:
contact = Contact.from_vcard(current_card)
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)
for image in range(len(contact.custom["PHOTO"])):
print(contact.custom["PHOTO"][image])
with open("photo/" + contact.fn + "/" + str(image) + ".jpg", "wb") as f:
f.write(base64.decodebytes(str.encode(contact.custom["PHOTO"][image])))
if "PHOTO" in contact.custom:
os.makedirs("pictures/" + contact.fn, exist_ok=True)
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 = {}
clean_tel = []
for number in contact.tel:
try:
number = number["value"]
number = number.replace("-", "")
number = phonenumbers.parse(number, region="FR")
number = phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
if number not in clean_tel:
clean_tel.append(number)
except phonenumbers.phonenumberutil.NumberParseException:
continue
contact.tel = [{"value": x, "type": []} for x in clean_tel]
contact.fn = quopri.decodestring(contact.fn).decode()
clean_n = []
for name in contact.n:
clean = quopri.decodestring(name).decode()
clean_n.append(clean)
contact.name = clean_n
print(contact.name)
clean_nickname = []
for nick in contact.nickname:
nick = quopri.decodestring(nick).decode()
clean_n.append(nick)
contact.nickname = clean_nickname
vcf_text = contact.to_vcard()
output.write(vcf_text)
current_card = ""
continue