This commit is contained in:
KnugiHK
2025-05-05 15:20:14 +08:00
parent d0fc620ba6
commit 534aea924d
2 changed files with 19 additions and 5 deletions

View File

@@ -215,7 +215,8 @@ class ChatStore:
} }
@classmethod @classmethod
def from_json(cls, data): def from_json(cls, data: Dict) -> 'ChatStore':
"""Create a chat store from JSON data."""
chat = cls(data.get("type"), data.get("name")) chat = cls(data.get("type"), data.get("name"))
chat.my_avatar = data.get("my_avatar") chat.my_avatar = data.get("my_avatar")
chat.their_avatar = data.get("their_avatar") chat.their_avatar = data.get("their_avatar")
@@ -242,7 +243,13 @@ class ChatStore:
"""Get all message keys in the chat.""" """Get all message keys in the chat."""
return self._messages.keys() return self._messages.keys()
def merge_with(self, other): def merge_with(self, other: 'ChatStore'):
"""Merge another ChatStore into this one.
Args:
other (ChatStore): The ChatStore to merge with
"""
if not isinstance(other, ChatStore): if not isinstance(other, ChatStore):
raise TypeError("Can only merge with another ChatStore object") raise TypeError("Can only merge with another ChatStore object")
@@ -339,7 +346,7 @@ class Message:
} }
@classmethod @classmethod
def from_json(cls, data): def from_json(cls, data: Dict) -> 'Message':
message = cls( message = cls(
from_me = data["from_me"], from_me = data["from_me"],
timestamp = data["timestamp"], timestamp = data["timestamp"],

View File

@@ -5,18 +5,18 @@ import os
import unicodedata import unicodedata
import re import re
import math import math
import shutil
from bleach import clean as sanitize 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 ChatStore from Whatsapp_Chat_Exporter.data_model import ChatStore
import shutil
from typing import Dict, List, Optional, Tuple from typing import Dict, List, Optional, Tuple
try: try:
from enum import StrEnum, IntEnum from enum import StrEnum, IntEnum
except ImportError: except ImportError:
# < Python 3.11 # < Python 3.11
# This should be removed when the support for Python 3.10 ends. # This should be removed when the support for Python 3.10 ends. (31 Oct 2026)
from enum import Enum from enum import Enum
class StrEnum(str, Enum): class StrEnum(str, Enum):
pass pass
@@ -260,6 +260,13 @@ def import_from_json(json_file: str, data: Dict[str, ChatStore]):
def incremental_merge(source_dir: str, target_dir: str, media_dir: str): def incremental_merge(source_dir: str, target_dir: str, media_dir: str):
"""Merges JSON files from the source directory into the target directory.
Args:
source_dir (str): The path to the source directory containing JSON files.
target_dir (str): The path to the target directory to merge into.
media_dir (str): The path to the media directory.
"""
json_files = [f for f in os.listdir(source_dir) if f.endswith('.json')] json_files = [f for f in os.listdir(source_dir) if f.endswith('.json')]
print("JSON files found:", json_files) print("JSON files found:", json_files)