mirror of
https://github.com/KnugiHK/WhatsApp-Chat-Exporter.git
synced 2026-04-24 15:01:36 +00:00
Changes after code review
This commit is contained in:
@@ -690,12 +690,12 @@ def export_multiple_json(args, data: Dict) -> None:
|
|||||||
contact = jik.replace('+', '')
|
contact = jik.replace('+', '')
|
||||||
|
|
||||||
if args.telegram:
|
if args.telegram:
|
||||||
obj = telegram_json_format(jik, data[jik])
|
messages = telegram_json_format(jik, data[jik], args.timezone_offset)
|
||||||
else:
|
else:
|
||||||
obj = {jik: data[jik]}
|
messages = {jik: data[jik]}
|
||||||
with open(f"{json_path}/{safe_name(contact)}.json", "w") as f:
|
with open(f"{json_path}/{safe_name(contact)}.json", "w") as f:
|
||||||
file_content = json.dumps(
|
file_content = json.dumps(
|
||||||
obj,
|
messages,
|
||||||
ensure_ascii=not args.avoid_encoding_json,
|
ensure_ascii=not args.avoid_encoding_json,
|
||||||
indent=args.pretty_print_json
|
indent=args.pretty_print_json
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from bleach import clean as sanitize
|
|||||||
from markupsafe import Markup
|
from markupsafe import Markup
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from Whatsapp_Chat_Exporter.data_model import ChatCollection, ChatStore
|
from Whatsapp_Chat_Exporter.data_model import ChatCollection, ChatStore, Timing
|
||||||
from typing import Dict, List, Optional, Tuple, Union
|
from typing import Dict, List, Optional, Tuple, Union
|
||||||
try:
|
try:
|
||||||
from enum import StrEnum, IntEnum
|
from enum import StrEnum, IntEnum
|
||||||
@@ -636,6 +636,16 @@ def get_from_string(msg: Dict, chat_id: str) -> str:
|
|||||||
return str(chat_id)
|
return str(chat_id)
|
||||||
|
|
||||||
|
|
||||||
|
def get_chat_type(chat_id: str) -> str:
|
||||||
|
"""Return the chat type based on the whatsapp id"""
|
||||||
|
if chat_id.endswith("@s.whatsapp.net"):
|
||||||
|
return "personal_chat"
|
||||||
|
if chat_id.endswith("@g.us"):
|
||||||
|
return "private_group"
|
||||||
|
logger.warning("Unknown chat type for %s, defaulting to private_group", chat_id)
|
||||||
|
return "private_group"
|
||||||
|
|
||||||
|
|
||||||
def get_from_id(msg: Dict, chat_id: str) -> str:
|
def get_from_id(msg: Dict, chat_id: str) -> str:
|
||||||
"""Return the user id for the sender"""
|
"""Return the user id for the sender"""
|
||||||
if msg["from_me"]:
|
if msg["from_me"]:
|
||||||
@@ -645,18 +655,19 @@ def get_from_id(msg: Dict, chat_id: str) -> str:
|
|||||||
return f"user{chat_id}"
|
return f"user{chat_id}"
|
||||||
|
|
||||||
|
|
||||||
def get_reply_id(data: Dict, reply_key: str) -> Optional[str]:
|
def get_reply_id(data: Dict, reply_key: int) -> Optional[int]:
|
||||||
"""Get the id of the message corresponding to the reply"""
|
"""Get the id of the message corresponding to the reply"""
|
||||||
if not reply_key:
|
if not reply_key:
|
||||||
return None
|
return None
|
||||||
for msg_id, msg in data["messages"].items():
|
for msg_id, msg in data["messages"].items():
|
||||||
if msg["key_id"] == reply_key:
|
if msg["key_id"] == reply_key:
|
||||||
return int(msg_id)
|
return msg_id
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def telegram_json_format(jik: str, data: Dict) -> Dict:
|
def telegram_json_format(jik: str, data: Dict, timezone_offset) -> Dict:
|
||||||
"""Convert the data to the Telegram export format"""
|
"""Convert the data to the Telegram export format"""
|
||||||
|
timing = Timing(timezone_offset or CURRENT_TZ_OFFSET)
|
||||||
try:
|
try:
|
||||||
chat_id = int(''.join([c for c in jik if c.isdigit()]))
|
chat_id = int(''.join([c for c in jik if c.isdigit()]))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@@ -664,13 +675,12 @@ def telegram_json_format(jik: str, data: Dict) -> Dict:
|
|||||||
chat_id = 0
|
chat_id = 0
|
||||||
obj = {
|
obj = {
|
||||||
"name": data["name"] if data["name"] else jik,
|
"name": data["name"] if data["name"] else jik,
|
||||||
# TODO can we do better than this?
|
"type": get_chat_type(jik),
|
||||||
"type": "private_group" if data["name"] else "personal_chat",
|
|
||||||
"id": chat_id,
|
"id": chat_id,
|
||||||
"messages": [ {
|
"messages": [ {
|
||||||
"id": int(msgId),
|
"id": int(msgId),
|
||||||
"type": "message",
|
"type": "message",
|
||||||
"date": datetime.fromtimestamp(msg["timestamp"]).isoformat().split(".")[0],
|
"date": timing.format_timestamp(msg["timestamp"], "%Y-%m-%dT%H:%M:%S"),
|
||||||
"date_unixtime": int(msg["timestamp"]),
|
"date_unixtime": int(msg["timestamp"]),
|
||||||
"from": get_from_string(msg, chat_id),
|
"from": get_from_string(msg, chat_id),
|
||||||
"from_id": get_from_id(msg, chat_id),
|
"from_id": get_from_id(msg, chat_id),
|
||||||
|
|||||||
Reference in New Issue
Block a user