From 7bb2fb242090b93b74c3b92ac183ba94026a31a4 Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Sat, 13 Jul 2024 12:26:54 +0800 Subject: [PATCH] Refactoring --pretty-print-json and --avoid_encoding_json options --- Whatsapp_Chat_Exporter/__main__.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Whatsapp_Chat_Exporter/__main__.py b/Whatsapp_Chat_Exporter/__main__.py index 228a644..bc61eb8 100644 --- a/Whatsapp_Chat_Exporter/__main__.py +++ b/Whatsapp_Chat_Exporter/__main__.py @@ -93,17 +93,19 @@ def main(): const="result.json", help="Save the result to a single JSON file (default if present: result.json)") parser.add_argument( - '--avoidJSONEnsureAscii', - dest='avoid_json_ensure_ascii', + '--avoid_encoding_json', + dest='avoid_encoding_json', default=False, action='store_true', - help="Don't encode non-ascii chars in the output json files") + help="Don't encode non-ascii characters in the output JSON files") parser.add_argument( - '--prettyPrintJson', + '--pretty-print-json', dest='pretty_print_json', - default=False, - action='store_true', - help="Pretty print the output json") + default=None, + nargs='?', + const=2, + type=int, + help="Pretty print the output JSON.") parser.add_argument( '-d', '--db', @@ -554,7 +556,11 @@ def main(): if not args.json_per_chat: with open(args.json, "w") as f: - data = json.dumps(data, ensure_ascii=not args.avoid_json_ensure_ascii, indent=2 if args.pretty_print_json else None) + data = json.dumps( + data, + ensure_ascii=not args.avoid_encoding_json, + indent=args.pretty_print_json + ) print(f"\nWriting JSON file...({int(len(data)/1024/1024)}MB)") f.write(data) else: @@ -569,7 +575,7 @@ def main(): else: contact = jik.replace('+', '') with open(f"{args.json}/{contact}.json", "w") as f: - file_content_to_write = json.dumps(data[jik], ensure_ascii=not args.avoid_json_ensure_ascii, indent=2 if args.pretty_print_json else None) + file_content_to_write = json.dumps(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") print()