2025-05-07 14:43:55 +02:00
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
import base64
|
2025-05-07 09:01:40 +02:00
|
|
|
import pickle
|
2025-05-07 21:58:19 +02:00
|
|
|
import phonenumbers
|
2025-05-07 09:01:40 +02:00
|
|
|
from pythonvCard4.vcard import Contact
|
|
|
|
|
|
2025-05-07 14:43:55 +02:00
|
|
|
input_file = open(sys.argv[1], 'r').readlines()
|
2025-05-07 14:58:26 +02:00
|
|
|
check_for_multiple_pictures = False
|
2025-05-07 09:01:40 +02:00
|
|
|
|
2025-05-07 10:00:29 +02:00
|
|
|
cards = []
|
|
|
|
|
current_card = ""
|
2025-05-07 21:42:33 +02:00
|
|
|
|
2025-05-07 14:43:55 +02:00
|
|
|
for line in input_file:
|
2025-05-07 21:42:33 +02:00
|
|
|
|
2025-05-07 10:00:29 +02:00
|
|
|
current_card += line
|
2025-05-07 21:42:33 +02:00
|
|
|
|
2025-05-07 10:00:29 +02:00
|
|
|
if "END:VCARD" in line:
|
|
|
|
|
contact = Contact.from_vcard(current_card)
|
2025-05-07 21:42:33 +02:00
|
|
|
|
2025-05-07 14:58:26 +02:00
|
|
|
if check_for_multiple_pictures:
|
|
|
|
|
if "PHOTO" in contact.custom and len(contact.custom["PHOTO"]) > 1:
|
2025-05-07 21:42:33 +02:00
|
|
|
os.makedirs("multiple_pictures/" + contact.fn, exist_ok=True)
|
2025-05-07 14:58:26 +02:00
|
|
|
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])))
|
2025-05-07 09:01:40 +02:00
|
|
|
|
2025-05-07 21:42:33 +02:00
|
|
|
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 = {}
|
|
|
|
|
|
2025-05-07 21:59:54 +02:00
|
|
|
clean_tel = []
|
2025-05-07 21:58:19 +02:00
|
|
|
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)
|
2025-05-07 21:59:54 +02:00
|
|
|
clean_tel.append({"value": number, "type": []})
|
2025-05-07 21:58:19 +02:00
|
|
|
except phonenumbers.phonenumberutil.NumberParseException:
|
|
|
|
|
continue
|
2025-05-07 21:59:54 +02:00
|
|
|
contact.tel = clean_tel
|
2025-05-07 21:58:19 +02:00
|
|
|
|
2025-05-07 10:00:29 +02:00
|
|
|
cards.append(contact)
|
|
|
|
|
current_card = ""
|
|
|
|
|
continue
|
2025-05-07 09:01:40 +02:00
|
|
|
|
2025-05-07 14:43:55 +02:00
|
|
|
output = open('output.vcf', 'w')
|
|
|
|
|
for card in cards:
|
|
|
|
|
vcf_text = card.to_vcard()
|
|
|
|
|
output.write(vcf_text)
|