Compare commits

...

8 commits

Author SHA1 Message Date
diegantobass
01ebe8b545 handle multiple tel numbers + ignore invalid numbers 2025-05-07 21:59:54 +02:00
diegantobass
55c2e0bfd9 parse number with phonenumbers to clean it 2025-05-07 21:58:19 +02:00
diegantobass
ad30bee143 clean duplicate pictures 2025-05-07 21:42:33 +02:00
diegantobass
a334cbeee1 ignore stuff 2025-05-07 21:09:55 +02:00
diegantobass
3f3b3d948e add possible check for multiple pictures 2025-05-07 14:58:26 +02:00
diegantobass
98fb0b13f4 refine input/output 2025-05-07 14:43:55 +02:00
diegantobass
ef47ee5773 prepare external output normalise script 2025-05-07 14:43:22 +02:00
diegantobass
27aa4a4c1a ignore external output file 2025-05-07 14:42:50 +02:00
3 changed files with 46 additions and 21 deletions

4
.gitignore vendored
View file

@ -1,3 +1,5 @@
*.vcf *.vcf
/vcf /vcf
/photo /pictures
/multiple_pictures
/whatsapp

1
csv_to_vcf.py Normal file
View file

@ -0,0 +1 @@

View file

@ -1,33 +1,55 @@
import pickle
from pythonvCard4.vcard import Contact
import base64
import os import os
import sys
import base64
import pickle
import phonenumbers
from pythonvCard4.vcard import Contact
file = open("contacts.vcf", 'r').readlines() input_file = open(sys.argv[1], 'r').readlines()
check_for_multiple_pictures = False
cards = [] cards = []
current_card = "" current_card = ""
for line in file:
for line in input_file:
current_card += line current_card += line
if "END:VCARD" in line: if "END:VCARD" in line:
contact = Contact.from_vcard(current_card) contact = Contact.from_vcard(current_card)
if check_for_multiple_pictures:
if "PHOTO" in contact.custom and len(contact.custom["PHOTO"]) > 1: if "PHOTO" in contact.custom and len(contact.custom["PHOTO"]) > 1:
os.makedirs("photo/" + contact.fn, exist_ok=True) os.makedirs("multiple_pictures/" + contact.fn, exist_ok=True)
for image in range(len(contact.custom["PHOTO"])): for image in range(len(contact.custom["PHOTO"])):
print(contact.custom["PHOTO"][image])
with open("photo/" + contact.fn + "/" + str(image) + ".jpg", "wb") as f: with open("photo/" + contact.fn + "/" + str(image) + ".jpg", "wb") as f:
f.write(base64.decodebytes(str.encode(contact.custom["PHOTO"][image]))) 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)
clean_tel.append({"value": number, "type": []})
except phonenumbers.phonenumberutil.NumberParseException:
continue
contact.tel = clean_tel
cards.append(contact) cards.append(contact)
current_card = "" current_card = ""
continue continue
output = open('output.vcf', 'w')
# contact = Contact.from_vcard(cards[156]) for card in cards:
# print(contact.custom["PHOTO"]) vcf_text = card.to_vcard()
output.write(vcf_text)
# vcf_text = contact.to_vcard()
# print(vcf_text)
# open('test.vcf', 'w').write(vcf_text)
print(len(cards))