Handle the wording of time unit in calls

This commit is contained in:
KnugiHK
2024-09-07 21:26:36 +08:00
parent 8fcd50d21b
commit 8ffa8cfcac
2 changed files with 25 additions and 8 deletions

View File

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

View File

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