From 80bdc4414a33784f180bbc0c41a087e822fa7932 Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Sat, 13 Jul 2024 14:12:21 +0800 Subject: [PATCH] Refactoring empty filtering --- Whatsapp_Chat_Exporter/__main__.py | 24 ++++++++++++----------- Whatsapp_Chat_Exporter/android_handler.py | 7 ++++--- Whatsapp_Chat_Exporter/utility.py | 2 +- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Whatsapp_Chat_Exporter/__main__.py b/Whatsapp_Chat_Exporter/__main__.py index 7f49e44..37f9b54 100644 --- a/Whatsapp_Chat_Exporter/__main__.py +++ b/Whatsapp_Chat_Exporter/__main__.py @@ -17,7 +17,7 @@ from Whatsapp_Chat_Exporter import exported_handler, android_handler from Whatsapp_Chat_Exporter import ios_handler, ios_media_handler from Whatsapp_Chat_Exporter.contacts_from_vcards import ContactsFromVCards from Whatsapp_Chat_Exporter.data_model import ChatStore -from Whatsapp_Chat_Exporter.utility import APPLE_TIME, Crypt, DbType, is_chat_empty +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 argparse import ArgumentParser, SUPPRESS from datetime import datetime @@ -261,11 +261,11 @@ def main(): help="Exclude chats that match the supplied phone number" ) parser.add_argument( - "--filter-empty", + "--dont-filter-empty", dest="filter_empty", default=False, action='store_true', - help="Exclude empty chats or with zero messages with content" + help="By default, the exporter will not render chats with no valid message. Setting this flag will cause the exporter to render those." ) parser.add_argument( "--per-chat", @@ -485,9 +485,6 @@ def main(): if not args.no_html: if contact_store.should_enrich_from_vcards(): contact_store.enrich_from_vcards(data) - - if (args.filter_empty): - data = {k: v for k, v in data.items() if not is_chat_empty(v)} create_html( data, @@ -496,7 +493,8 @@ def main(): args.embedded, args.offline, args.size, - args.no_avatar + args.no_avatar, + not args.filter_empty ) else: print( @@ -531,7 +529,9 @@ def main(): args.template, args.embedded, args.offline, - args.size + args.size, + args.no_avatar, + not args.filter_empty ) for file in glob.glob(r'*.*'): shutil.copy(file, args.output) @@ -543,12 +543,14 @@ def main(): args.template, args.embedded, args.offline, - args.size + args.size, + args.no_avatar, + not args.filter_empty ) if args.json and not args.import_json: - if (args.filter_empty): - data = {k: v for k, v in data.items() if not is_chat_empty(v)} + if not args.filter_empty: + data = {k: v for k, v in data.items() if not chat_is_empty(v)} if contact_store.should_enrich_from_vcards(): contact_store.enrich_from_vcards(data) diff --git a/Whatsapp_Chat_Exporter/android_handler.py b/Whatsapp_Chat_Exporter/android_handler.py index 094d3e1..32c2121 100644 --- a/Whatsapp_Chat_Exporter/android_handler.py +++ b/Whatsapp_Chat_Exporter/android_handler.py @@ -11,7 +11,7 @@ from markupsafe import escape as htmle from hashlib import sha256 from base64 import b64decode, b64encode from Whatsapp_Chat_Exporter.data_model import ChatStore, Message -from Whatsapp_Chat_Exporter.utility import MAX_SIZE, ROW_SIZE, DbType, determine_metadata, JidType +from Whatsapp_Chat_Exporter.utility import MAX_SIZE, ROW_SIZE, DbType, determine_metadata, JidType, chat_is_empty from Whatsapp_Chat_Exporter.utility import rendering, Crypt, Device, get_file_name, setup_template from Whatsapp_Chat_Exporter.utility import brute_force_offset, CRYPT14_OFFSETS, get_status_location from Whatsapp_Chat_Exporter.utility import get_chat_condition, slugify @@ -749,7 +749,8 @@ def create_html( embedded=False, offline_static=False, maximum_size=None, - no_avatar=False + no_avatar=False, + filter_empty=True ): template = setup_template(template, no_avatar) @@ -763,7 +764,7 @@ def create_html( for current, contact in enumerate(data): chat = data[contact] - if len(chat.messages) == 0: + if filter_empty and chat_is_empty(chat): continue safe_file_name, name = get_file_name(contact, chat) diff --git a/Whatsapp_Chat_Exporter/utility.py b/Whatsapp_Chat_Exporter/utility.py index 09e6671..1eb8b02 100644 --- a/Whatsapp_Chat_Exporter/utility.py +++ b/Whatsapp_Chat_Exporter/utility.py @@ -170,7 +170,7 @@ def get_chat_condition(filter, include, column): def _is_message_empty(message): return (message.data is None or message.data == "") and not message.media -def is_chat_empty(chat: ChatStore): +def chat_is_empty(chat: ChatStore): return len(chat.messages) == 0 or all(_is_message_empty(message) for message in chat.messages.values())