Sanitize the file name for --per-chat options #86

This commit is contained in:
KnugiHK
2024-09-07 18:45:36 +08:00
parent bd4ccbb8ac
commit df3333f948
2 changed files with 7 additions and 3 deletions

View File

@@ -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")

View File

@@ -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):