From 8ffa8cfcacd0ffe2e27d361f60339599fc052a92 Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Sat, 7 Sep 2024 21:26:36 +0800 Subject: [PATCH] Handle the wording of time unit in calls --- Whatsapp_Chat_Exporter/android_handler.py | 11 ++++------- Whatsapp_Chat_Exporter/utility.py | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Whatsapp_Chat_Exporter/android_handler.py b/Whatsapp_Chat_Exporter/android_handler.py index d6e2472..dc62477 100644 --- a/Whatsapp_Chat_Exporter/android_handler.py +++ b/Whatsapp_Chat_Exporter/android_handler.py @@ -1,6 +1,5 @@ #!/usr/bin/python3 -import datetime import sqlite3 import os import io @@ -12,8 +11,8 @@ 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 rendering, Crypt, Device, get_file_name, setup_template +from Whatsapp_Chat_Exporter.utility import MAX_SIZE, ROW_SIZE, DbType, convert_time_unit, determine_metadata +from Whatsapp_Chat_Exporter.utility import rendering, Crypt, Device, get_file_name, setup_template, JidType from Whatsapp_Chat_Exporter.utility import brute_force_offset, CRYPT14_OFFSETS, get_status_location from Whatsapp_Chat_Exporter.utility import get_chat_condition, slugify, convert_size, chat_is_empty @@ -734,13 +733,11 @@ def calls(db, data, timezone_offset, filter_chat): elif content['call_result'] == 3: call.data += "unavailable." elif content['call_result'] == 5: - call_time = str(datetime.timedelta(seconds=content['duration'])) - if "day" not in call_time and call_time.startswith("0:"): - call_time = call_time[2:] + call_time = convert_time_unit(content['duration']) call_bytes = convert_size(content['bytes_transferred']) call.data += ( f"initiated and lasted for {call_time} " - f"with {call_bytes} transferred." + f"with {call_bytes} data transferred." ) chat.add_message(content["_id"], call) content = c.fetchone() diff --git a/Whatsapp_Chat_Exporter/utility.py b/Whatsapp_Chat_Exporter/utility.py index dac89cf..eda32bb 100644 --- a/Whatsapp_Chat_Exporter/utility.py +++ b/Whatsapp_Chat_Exporter/utility.py @@ -6,7 +6,7 @@ import re import math from bleach import clean as sanitize from markupsafe import Markup -from datetime import datetime +from datetime import datetime, timedelta from enum import IntEnum from Whatsapp_Chat_Exporter.data_model import ChatStore try: @@ -24,6 +24,26 @@ MAX_SIZE = 4 * 1024 * 1024 # Default 4MB ROW_SIZE = 0x3D0 +def convert_time_unit(time_second: int): + time = str(timedelta(seconds=time_second)) + if "day" not in time: + if time_second < 1: + time = "less than a second" + elif time_second == 1: + time = "a second" + elif time_second < 60: + time = time[5:][1 if time_second < 10 else 0:] + " seconds" + elif time_second == 60: + time = "a minute" + elif time_second < 3600: + time = time[2:] + " minutes" + elif time_second == 3600: + time = "an hour" + else: + time += " hour" + return time + + def convert_size(size_bytes): """From https://stackoverflow.com/a/14822210/9478891 Authors: james-sapam & other contributors