diff --git a/Whatsapp_Chat_Exporter/__main__.py b/Whatsapp_Chat_Exporter/__main__.py index ecbdad3..f360de1 100644 --- a/Whatsapp_Chat_Exporter/__main__.py +++ b/Whatsapp_Chat_Exporter/__main__.py @@ -18,7 +18,7 @@ from Whatsapp_Chat_Exporter import ios_handler, ios_media_handler from Whatsapp_Chat_Exporter.vcards_contacts import ContactsFromVCards from Whatsapp_Chat_Exporter.data_model import ChatStore from Whatsapp_Chat_Exporter.utility import APPLE_TIME, Crypt, DbType, chat_is_empty -from Whatsapp_Chat_Exporter.utility import check_update, import_from_json +from Whatsapp_Chat_Exporter.utility import check_update, import_from_json, sanitize_filename from argparse import ArgumentParser, SUPPRESS from datetime import datetime from sys import exit @@ -578,7 +578,7 @@ def main(): contact = data[jik]["name"].replace('/', '') else: contact = jik.replace('+', '') - with open(f"{args.json}/{contact}.json", "w") as f: + with open(f"{args.json}/{sanitize_filename(contact)}.json", "w") as f: file_content_to_write = json.dumps({jik: data[jik]}, ensure_ascii=not args.avoid_encoding_json, indent=2 if args.pretty_print_json else None) f.write(file_content_to_write) print(f"Writing JSON file...({index + 1}/{total})", end="\r") diff --git a/Whatsapp_Chat_Exporter/utility.py b/Whatsapp_Chat_Exporter/utility.py index 75adaf3..4cff722 100644 --- a/Whatsapp_Chat_Exporter/utility.py +++ b/Whatsapp_Chat_Exporter/utility.py @@ -139,6 +139,10 @@ def import_from_json(json_file, data): print(f"Importing chats from JSON...({index + 1}/{total_row_number})", end="\r") +def sanitize_filename(file_name: str): + return "".join(x for x in file_name if x.isalnum() or x in "- ") + + def get_file_name(contact: str, chat: ChatStore): if "@" not in contact and contact not in ("000000000000000", "000000000000001", "ExportedChat"): raise ValueError("Unexpected contact format: " + contact) @@ -156,7 +160,7 @@ def get_file_name(contact: str, chat: ChatStore): else: name = phone_number - return "".join(x for x in file_name if x.isalnum() or x in "- "), name + return sanitize_filename(file_name), name def get_chat_condition(filter, include, column):