From 2d4d934a91a101e6987a867e3b313175bfa492e0 Mon Sep 17 00:00:00 2001 From: jonx Date: Fri, 3 May 2024 02:36:17 +0200 Subject: [PATCH 1/2] Handle groups of VCards correctly --- Whatsapp_Chat_Exporter/extract_iphone.py | 33 ++++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/Whatsapp_Chat_Exporter/extract_iphone.py b/Whatsapp_Chat_Exporter/extract_iphone.py index dadd695..34d4078 100644 --- a/Whatsapp_Chat_Exporter/extract_iphone.py +++ b/Whatsapp_Chat_Exporter/extract_iphone.py @@ -261,19 +261,30 @@ def vcard(db, data, media_folder): total_row_number = len(contents) print(f"\nProcessing vCards...(0/{total_row_number})", end="\r") path = f'{media_folder}/Message/vCards' - if not os.path.isdir(path): - Path(path).mkdir(parents=True, exist_ok=True) + Path(path).mkdir(parents=True, exist_ok=True) + for index, content in enumerate(contents): - file_name = "".join(x for x in content["ZVCARDNAME"] if x.isalnum()) - file_name = file_name.encode('utf-8')[:230].decode('utf-8', 'ignore') - file_path = os.path.join(path, f"{file_name}.vcf") - if not os.path.isfile(file_path): - with open(file_path, "w", encoding="utf-8") as f: - f.write(content["ZVCARDSTRING"]) + file_paths = [] + vcard_names = content["ZVCARDNAME"].split("_$!!$_") + vcard_strings = content["ZVCARDSTRING"].split("_$!!$_") + + # If this is a list of contacts + if len(vcard_names) > len(vcard_strings): + vcard_names.pop(0) # Dismiss the first element, which is the group name + + for name, vcard_string in zip(vcard_names, vcard_strings): + file_name = "".join(x for x in name if x.isalnum()) + file_name = file_name.encode('utf-8')[:230].decode('utf-8', 'ignore') + file_path = os.path.join(path, f"{file_name}.vcf") + file_paths.append(file_path) + + if not os.path.isfile(file_path): + with open(file_path, "w", encoding="utf-8") as f: + f.write(vcard_string) + + vcard_summary = " + ".join([f"{name} (See file: {os.path.basename(fp)})" for name, fp in zip(vcard_names, file_paths)]) message = data[content["_id"]].messages[content["ZMESSAGE"]] - message.data = content["ZVCARDNAME"] + \ - "The vCard file cannot be displayed here, " \ - f"however it should be located at {file_path}" + message.data = vcard_summary message.mime = "text/x-vcard" message.media = True message.meta = True From d57ff29e711a64525f90e565132a196b988a7e40 Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Sat, 8 Jun 2024 17:29:10 +0800 Subject: [PATCH 2/2] Add link to vcard entry --- Whatsapp_Chat_Exporter/extract_iphone.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Whatsapp_Chat_Exporter/extract_iphone.py b/Whatsapp_Chat_Exporter/extract_iphone.py index 34d4078..85b2e70 100644 --- a/Whatsapp_Chat_Exporter/extract_iphone.py +++ b/Whatsapp_Chat_Exporter/extract_iphone.py @@ -4,6 +4,7 @@ import os from glob import glob from pathlib import Path from mimetypes import MimeTypes +from markupsafe import escape as htmle from Whatsapp_Chat_Exporter.data_model import ChatStore, Message from Whatsapp_Chat_Exporter.utility import APPLE_TIME, Device @@ -282,10 +283,12 @@ def vcard(db, data, media_folder): with open(file_path, "w", encoding="utf-8") as f: f.write(vcard_string) - vcard_summary = " + ".join([f"{name} (See file: {os.path.basename(fp)})" for name, fp in zip(vcard_names, file_paths)]) + vcard_summary = "This media include the following vCard file(s):
" + vcard_summary += " | ".join([f'{htmle(name)}' for name, fp in zip(vcard_names, file_paths)]) message = data[content["_id"]].messages[content["ZMESSAGE"]] message.data = vcard_summary message.mime = "text/x-vcard" message.media = True message.meta = True + message.safe = True print(f"Processing vCards...({index + 1}/{total_row_number})", end="\r")