Merge pull request #157 from glemco/telegram_json

Add support for telegram JSON file format
This commit is contained in:
Knugi
2025-07-02 18:26:52 +08:00
committed by GitHub
2 changed files with 88 additions and 3 deletions

View File

@@ -15,6 +15,7 @@ from Whatsapp_Chat_Exporter.data_model import ChatCollection, ChatStore
from Whatsapp_Chat_Exporter.utility import APPLE_TIME, CLEAR_LINE, Crypt, check_update
from Whatsapp_Chat_Exporter.utility import readable_to_bytes, safe_name, bytes_to_readable
from Whatsapp_Chat_Exporter.utility import import_from_json, incremental_merge, DbType
from Whatsapp_Chat_Exporter.utility import telegram_json_format
from argparse import ArgumentParser, SUPPRESS
from datetime import datetime
from getpass import getpass
@@ -152,6 +153,10 @@ def setup_argument_parser() -> ArgumentParser:
'--pretty-print-json', dest='pretty_print_json', default=None, nargs='?', const=2, type=int,
help="Pretty print the output JSON."
)
json_group.add_argument(
"--telegram", dest="telegram", default=False, action='store_true',
help="Output the JSON in a format compatible with Telegram export (implies json-per-chat)"
)
json_group.add_argument(
"--per-chat", dest="json_per_chat", default=False, action='store_true',
help="Output the JSON file per chat"
@@ -652,7 +657,7 @@ def export_json(args, data: ChatCollection, contact_store=None) -> None:
data = {jik: chat.to_json() for jik, chat in data.items()}
# Export as a single file or per chat
if not args.json_per_chat:
if not args.json_per_chat and not args.telegram:
export_single_json(args, data)
else:
export_multiple_json(args, data)
@@ -688,9 +693,13 @@ def export_multiple_json(args, data: Dict) -> None:
else:
contact = jik.replace('+', '')
if args.telegram:
messages = telegram_json_format(jik, data[jik], args.timezone_offset)
else:
messages = {jik: data[jik]}
with open(f"{json_path}/{safe_name(contact)}.json", "w") as f:
file_content = json.dumps(
{jik: data[jik]},
messages,
ensure_ascii=not args.avoid_encoding_json,
indent=args.pretty_print_json
)