mirror of
https://github.com/LifeArchiveProject/WeChatDataAnalysis.git
synced 2026-02-20 06:40:49 +08:00
feat(chat-export): 支持 HTML 导出(合并消息/远程缩略图可选下载)
- 导出格式新增 html:生成 index.html + 会话 messages.html,离线浏览 - 支持 chatHistory(合并消息)解析/渲染与弹窗查看 - 图片资源解析增强:MessageResourceInfo 优先 + md5/hdmd5 兜底 - HTML 导出可选下载远程缩略图(仅公网主机/图片类型/5MB 限制) - 修复拍一拍误判、公众号封面样式识别;转账过期状态与前端展示
This commit is contained in:
50
tests/test_chat_app_message_type4_patmsg_regression.py
Normal file
50
tests/test_chat_app_message_type4_patmsg_regression.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
from wechat_decrypt_tool.chat_helpers import _parse_app_message
|
||||
|
||||
|
||||
class TestChatAppMessageType4PatMsgRegression(unittest.TestCase):
|
||||
def test_type4_link_with_patmsg_metadata_is_not_misclassified_as_pat(self):
|
||||
raw_text = (
|
||||
"<msg>"
|
||||
'<appmsg appid="wxcb8d4298c6a09bcb" sdkver="0">'
|
||||
"<title>【中配】抽象可能让你的代码变差 - CodeAesthetic</title>"
|
||||
"<des>UP主:黑纹白斑马</des>"
|
||||
"<type>4</type>"
|
||||
"<url>https://b23.tv/au68guF</url>"
|
||||
"<appname>哔哩哔哩</appname>"
|
||||
"<appattach><cdnthumburl>3057020100044b30</cdnthumburl></appattach>"
|
||||
"<patMsg><chatUser /></patMsg>"
|
||||
"</appmsg>"
|
||||
"</msg>"
|
||||
)
|
||||
|
||||
parsed = _parse_app_message(raw_text)
|
||||
self.assertEqual(parsed.get("renderType"), "link")
|
||||
self.assertEqual(parsed.get("url"), "https://b23.tv/au68guF")
|
||||
self.assertEqual(parsed.get("title"), "【中配】抽象可能让你的代码变差 - CodeAesthetic")
|
||||
self.assertEqual(parsed.get("from"), "哔哩哔哩")
|
||||
self.assertNotEqual(parsed.get("content"), "[拍一拍]")
|
||||
|
||||
def test_type62_is_still_pat(self):
|
||||
raw_text = '<msg><appmsg><title>"A" 拍了拍 "B"</title><type>62</type></appmsg></msg>'
|
||||
parsed = _parse_app_message(raw_text)
|
||||
self.assertEqual(parsed.get("renderType"), "system")
|
||||
self.assertEqual(parsed.get("content"), "[拍一拍]")
|
||||
|
||||
def test_sysmsg_type_patmsg_attr_is_still_pat(self):
|
||||
raw_text = '<sysmsg type="patmsg"><foo>bar</foo></sysmsg>'
|
||||
parsed = _parse_app_message(raw_text)
|
||||
self.assertEqual(parsed.get("renderType"), "system")
|
||||
self.assertEqual(parsed.get("content"), "[拍一拍]")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
218
tests/test_chat_export_chat_history_modal.py
Normal file
218
tests/test_chat_export_chat_history_modal.py
Normal file
@@ -0,0 +1,218 @@
|
||||
import os
|
||||
import hashlib
|
||||
import sqlite3
|
||||
import sys
|
||||
import unittest
|
||||
import zipfile
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
|
||||
class TestChatExportChatHistoryModal(unittest.TestCase):
|
||||
_MD5 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
|
||||
def _reload_export_modules(self):
|
||||
import wechat_decrypt_tool.app_paths as app_paths
|
||||
import wechat_decrypt_tool.chat_helpers as chat_helpers
|
||||
import wechat_decrypt_tool.media_helpers as media_helpers
|
||||
import wechat_decrypt_tool.chat_export_service as chat_export_service
|
||||
|
||||
importlib.reload(app_paths)
|
||||
importlib.reload(chat_helpers)
|
||||
importlib.reload(media_helpers)
|
||||
importlib.reload(chat_export_service)
|
||||
return chat_export_service
|
||||
|
||||
def _seed_contact_db(self, path: Path, *, account: str, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE contact (
|
||||
username TEXT,
|
||||
remark TEXT,
|
||||
nick_name TEXT,
|
||||
alias TEXT,
|
||||
local_type INTEGER,
|
||||
verify_flag INTEGER,
|
||||
big_head_url TEXT,
|
||||
small_head_url TEXT
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE stranger (
|
||||
username TEXT,
|
||||
remark TEXT,
|
||||
nick_name TEXT,
|
||||
alias TEXT,
|
||||
local_type INTEGER,
|
||||
verify_flag INTEGER,
|
||||
big_head_url TEXT,
|
||||
small_head_url TEXT
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO contact VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(account, "", "我", "", 1, 0, "", ""),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO contact VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(username, "", "测试好友", "", 1, 0, "", ""),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_session_db(self, path: Path, *, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE SessionTable (
|
||||
username TEXT,
|
||||
is_hidden INTEGER,
|
||||
sort_timestamp INTEGER
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO SessionTable VALUES (?, ?, ?)",
|
||||
(username, 0, 1735689600),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_message_db(self, path: Path, *, account: str, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute("CREATE TABLE Name2Id (rowid INTEGER PRIMARY KEY, user_name TEXT)")
|
||||
conn.execute("INSERT INTO Name2Id(rowid, user_name) VALUES (?, ?)", (1, account))
|
||||
conn.execute("INSERT INTO Name2Id(rowid, user_name) VALUES (?, ?)", (2, username))
|
||||
|
||||
table_name = f"msg_{hashlib.md5(username.encode('utf-8')).hexdigest()}"
|
||||
conn.execute(
|
||||
f"""
|
||||
CREATE TABLE {table_name} (
|
||||
local_id INTEGER,
|
||||
server_id INTEGER,
|
||||
local_type INTEGER,
|
||||
sort_seq INTEGER,
|
||||
real_sender_id INTEGER,
|
||||
create_time INTEGER,
|
||||
message_content TEXT,
|
||||
compress_content BLOB
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
record_item = (
|
||||
"<recorditem>"
|
||||
"<datalist>"
|
||||
"<dataitem>"
|
||||
"<datatype>2</datatype>"
|
||||
f"<fullmd5>{self._MD5}</fullmd5>"
|
||||
"</dataitem>"
|
||||
"</datalist>"
|
||||
"</recorditem>"
|
||||
)
|
||||
chat_history_xml = (
|
||||
"<msg><appmsg>"
|
||||
"<type>19</type>"
|
||||
"<title>聊天记录</title>"
|
||||
"<des>记录预览</des>"
|
||||
f"<recorditem><![CDATA[{record_item}]]></recorditem>"
|
||||
"</appmsg></msg>"
|
||||
)
|
||||
|
||||
conn.execute(
|
||||
f"INSERT INTO {table_name} (local_id, server_id, local_type, sort_seq, real_sender_id, create_time, message_content, compress_content) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(1, 1001, 49, 1, 2, 1735689601, chat_history_xml, None),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_media_files(self, account_dir: Path) -> None:
|
||||
resource_root = account_dir / "resource"
|
||||
(resource_root / "aa").mkdir(parents=True, exist_ok=True)
|
||||
(resource_root / "aa" / f"{self._MD5}.jpg").write_bytes(b"\xff\xd8\xff\xd9")
|
||||
|
||||
def _prepare_account(self, root: Path, *, account: str, username: str) -> Path:
|
||||
account_dir = root / "output" / "databases" / account
|
||||
account_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
self._seed_contact_db(account_dir / "contact.db", account=account, username=username)
|
||||
self._seed_session_db(account_dir / "session.db", username=username)
|
||||
self._seed_message_db(account_dir / "message_0.db", account=account, username=username)
|
||||
self._seed_media_files(account_dir)
|
||||
return account_dir
|
||||
|
||||
def _create_job(self, manager, *, account: str, username: str):
|
||||
job = manager.create_job(
|
||||
account=account,
|
||||
scope="selected",
|
||||
usernames=[username],
|
||||
export_format="html",
|
||||
start_time=None,
|
||||
end_time=None,
|
||||
include_hidden=False,
|
||||
include_official=False,
|
||||
include_media=True,
|
||||
media_kinds=["image"],
|
||||
message_types=["chatHistory", "image"],
|
||||
output_dir=None,
|
||||
allow_process_key_extract=False,
|
||||
download_remote_media=False,
|
||||
privacy_mode=False,
|
||||
file_name=None,
|
||||
)
|
||||
|
||||
for _ in range(200):
|
||||
latest = manager.get_job(job.export_id)
|
||||
if latest and latest.status in {"done", "error", "cancelled"}:
|
||||
return latest
|
||||
import time as _time
|
||||
|
||||
_time.sleep(0.05)
|
||||
self.fail("export job did not finish in time")
|
||||
|
||||
def test_chat_history_modal_has_media_index_and_record_item(self):
|
||||
with TemporaryDirectory() as td:
|
||||
root = Path(td)
|
||||
account = "wxid_test"
|
||||
username = "wxid_friend"
|
||||
self._prepare_account(root, account=account, username=username)
|
||||
|
||||
prev_data = os.environ.get("WECHAT_TOOL_DATA_DIR")
|
||||
try:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = str(root)
|
||||
svc = self._reload_export_modules()
|
||||
job = self._create_job(svc.CHAT_EXPORT_MANAGER, account=account, username=username)
|
||||
self.assertEqual(job.status, "done", msg=job.error)
|
||||
|
||||
with zipfile.ZipFile(job.zip_path, "r") as zf:
|
||||
names = set(zf.namelist())
|
||||
self.assertIn(f"media/images/{self._MD5}.jpg", names)
|
||||
|
||||
html_path = next((n for n in names if n.endswith("/messages.html")), "")
|
||||
self.assertTrue(html_path)
|
||||
html_text = zf.read(html_path).decode("utf-8")
|
||||
self.assertIn('id="chatHistoryModal"', html_text)
|
||||
self.assertIn('data-wce-chat-history="1"', html_text)
|
||||
self.assertIn('data-record-item-b64="', html_text)
|
||||
self.assertIn('id="wceMediaIndex"', html_text)
|
||||
self.assertIn(self._MD5, html_text)
|
||||
finally:
|
||||
if prev_data is None:
|
||||
os.environ.pop("WECHAT_TOOL_DATA_DIR", None)
|
||||
else:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = prev_data
|
||||
353
tests/test_chat_export_html_format.py
Normal file
353
tests/test_chat_export_html_format.py
Normal file
@@ -0,0 +1,353 @@
|
||||
import os
|
||||
import json
|
||||
import hashlib
|
||||
import sqlite3
|
||||
import sys
|
||||
import unittest
|
||||
import zipfile
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
|
||||
class TestChatExportHtmlFormat(unittest.TestCase):
|
||||
_FILE_MD5 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
_VOICE_SERVER_ID = 2001
|
||||
|
||||
def _reload_export_modules(self):
|
||||
import wechat_decrypt_tool.app_paths as app_paths
|
||||
import wechat_decrypt_tool.chat_helpers as chat_helpers
|
||||
import wechat_decrypt_tool.media_helpers as media_helpers
|
||||
import wechat_decrypt_tool.chat_export_service as chat_export_service
|
||||
|
||||
importlib.reload(app_paths)
|
||||
importlib.reload(chat_helpers)
|
||||
importlib.reload(media_helpers)
|
||||
importlib.reload(chat_export_service)
|
||||
return chat_export_service
|
||||
|
||||
def _seed_contact_db(self, path: Path, *, account: str, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE contact (
|
||||
username TEXT,
|
||||
remark TEXT,
|
||||
nick_name TEXT,
|
||||
alias TEXT,
|
||||
local_type INTEGER,
|
||||
verify_flag INTEGER,
|
||||
big_head_url TEXT,
|
||||
small_head_url TEXT
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE stranger (
|
||||
username TEXT,
|
||||
remark TEXT,
|
||||
nick_name TEXT,
|
||||
alias TEXT,
|
||||
local_type INTEGER,
|
||||
verify_flag INTEGER,
|
||||
big_head_url TEXT,
|
||||
small_head_url TEXT
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO contact VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(account, "", "我", "", 1, 0, "", ""),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO contact VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(username, "", "测试好友", "", 1, 0, "", ""),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_session_db(self, path: Path, *, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE SessionTable (
|
||||
username TEXT,
|
||||
is_hidden INTEGER,
|
||||
sort_timestamp INTEGER
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO SessionTable VALUES (?, ?, ?)",
|
||||
(username, 0, 1735689600),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_message_db(self, path: Path, *, account: str, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute("CREATE TABLE Name2Id (rowid INTEGER PRIMARY KEY, user_name TEXT)")
|
||||
conn.execute("INSERT INTO Name2Id(rowid, user_name) VALUES (?, ?)", (1, account))
|
||||
conn.execute("INSERT INTO Name2Id(rowid, user_name) VALUES (?, ?)", (2, username))
|
||||
|
||||
table_name = f"msg_{hashlib.md5(username.encode('utf-8')).hexdigest()}"
|
||||
conn.execute(
|
||||
f"""
|
||||
CREATE TABLE {table_name} (
|
||||
local_id INTEGER,
|
||||
server_id INTEGER,
|
||||
local_type INTEGER,
|
||||
sort_seq INTEGER,
|
||||
real_sender_id INTEGER,
|
||||
create_time INTEGER,
|
||||
message_content TEXT,
|
||||
compress_content BLOB
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
image_xml = '<msg><img md5="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" cdnthumburl="img_file_id_1" /></msg>'
|
||||
voice_xml = '<msg><voicemsg voicelength="3000" /></msg>'
|
||||
file_md5 = self._FILE_MD5
|
||||
file_xml = (
|
||||
"<msg><appmsg>"
|
||||
"<type>6</type>"
|
||||
"<title>demo.pdf</title>"
|
||||
"<totallen>2048</totallen>"
|
||||
f"<md5>{file_md5}</md5>"
|
||||
"</appmsg></msg>"
|
||||
)
|
||||
link_xml = (
|
||||
"<msg><appmsg>"
|
||||
"<type>5</type>"
|
||||
"<title>示例链接</title>"
|
||||
"<des>这是描述</des>"
|
||||
"<url>https://example.com/</url>"
|
||||
"<thumburl>https://example.com/thumb.jpg</thumburl>"
|
||||
"<sourceusername>gh_test</sourceusername>"
|
||||
"<sourcedisplayname>测试公众号</sourcedisplayname>"
|
||||
"</appmsg></msg>"
|
||||
)
|
||||
chat_history_xml = (
|
||||
"<msg><appmsg>"
|
||||
"<type>19</type>"
|
||||
"<title>聊天记录</title>"
|
||||
"<des>记录预览</des>"
|
||||
"<recorditem><desc>张三: hi\n李四: ok</desc></recorditem>"
|
||||
"</appmsg></msg>"
|
||||
)
|
||||
transfer_xml = (
|
||||
"<msg><appmsg>"
|
||||
"<type>2000</type>"
|
||||
"<title>微信转账</title>"
|
||||
"<wcpayinfo>"
|
||||
"<pay_memo>转账备注</pay_memo>"
|
||||
"<feedesc>¥1.23</feedesc>"
|
||||
"<paysubtype>3</paysubtype>"
|
||||
"<transferid>transfer_123</transferid>"
|
||||
"</wcpayinfo>"
|
||||
"</appmsg></msg>"
|
||||
)
|
||||
red_packet_xml = (
|
||||
"<msg><appmsg>"
|
||||
"<type>2001</type>"
|
||||
"<title>红包</title>"
|
||||
"<wcpayinfo>"
|
||||
"<sendertitle>恭喜发财,大吉大利</sendertitle>"
|
||||
"<senderdes>微信红包</senderdes>"
|
||||
"</wcpayinfo>"
|
||||
"</appmsg></msg>"
|
||||
)
|
||||
voip_xml = (
|
||||
"<msg><VoIPBubbleMsg>"
|
||||
"<room_type>1</room_type>"
|
||||
"<msg>语音通话</msg>"
|
||||
"</VoIPBubbleMsg></msg>"
|
||||
)
|
||||
quote_voice_xml = (
|
||||
"<msg><appmsg>"
|
||||
"<type>57</type>"
|
||||
"<title>回复语音</title>"
|
||||
"<refermsg>"
|
||||
"<type>34</type>"
|
||||
f"<svrid>{self._VOICE_SERVER_ID}</svrid>"
|
||||
"<fromusr>wxid_friend</fromusr>"
|
||||
"<displayname>测试好友</displayname>"
|
||||
"<content>wxid_friend:3000:1:</content>"
|
||||
"</refermsg>"
|
||||
"</appmsg></msg>"
|
||||
)
|
||||
rows = [
|
||||
(1, 1001, 3, 1, 2, 1735689601, image_xml, None),
|
||||
(2, 1002, 1, 2, 2, 1735689602, "普通文本消息[微笑]", None),
|
||||
(3, 1003, 49, 3, 1, 1735689603, transfer_xml, None),
|
||||
(4, 1004, 49, 4, 2, 1735689604, red_packet_xml, None),
|
||||
(5, 1005, 49, 5, 1, 1735689605, file_xml, None),
|
||||
(6, 1006, 49, 6, 2, 1735689606, link_xml, None),
|
||||
(7, 1007, 49, 7, 2, 1735689607, chat_history_xml, None),
|
||||
(8, 1008, 50, 8, 2, 1735689608, voip_xml, None),
|
||||
(9, self._VOICE_SERVER_ID, 34, 9, 1, 1735689609, voice_xml, None),
|
||||
(10, 1010, 49, 10, 1, 1735689610, quote_voice_xml, None),
|
||||
]
|
||||
conn.executemany(
|
||||
f"INSERT INTO {table_name} (local_id, server_id, local_type, sort_seq, real_sender_id, create_time, message_content, compress_content) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
rows,
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_media_files(self, account_dir: Path) -> None:
|
||||
resource_root = account_dir / "resource"
|
||||
(resource_root / "aa").mkdir(parents=True, exist_ok=True)
|
||||
(resource_root / "aa" / "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jpg").write_bytes(b"\xff\xd8\xff\xd9")
|
||||
(resource_root / "bb").mkdir(parents=True, exist_ok=True)
|
||||
(resource_root / "bb" / f"{self._FILE_MD5}.dat").write_bytes(b"dummy")
|
||||
|
||||
conn = sqlite3.connect(str(account_dir / "media_0.db"))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE VoiceInfo (
|
||||
svr_id INTEGER,
|
||||
create_time INTEGER,
|
||||
voice_data BLOB
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO VoiceInfo VALUES (?, ?, ?)",
|
||||
(self._VOICE_SERVER_ID, 1735689609, b"SILK_VOICE_DATA"),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _prepare_account(self, root: Path, *, account: str, username: str) -> Path:
|
||||
account_dir = root / "output" / "databases" / account
|
||||
account_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
self._seed_contact_db(account_dir / "contact.db", account=account, username=username)
|
||||
self._seed_session_db(account_dir / "session.db", username=username)
|
||||
self._seed_message_db(account_dir / "message_0.db", account=account, username=username)
|
||||
self._seed_media_files(account_dir)
|
||||
return account_dir
|
||||
|
||||
def _create_job(self, manager, *, account: str, username: str):
|
||||
job = manager.create_job(
|
||||
account=account,
|
||||
scope="selected",
|
||||
usernames=[username],
|
||||
export_format="html",
|
||||
start_time=None,
|
||||
end_time=None,
|
||||
include_hidden=False,
|
||||
include_official=False,
|
||||
include_media=True,
|
||||
media_kinds=["image", "emoji", "video", "video_thumb", "voice", "file"],
|
||||
message_types=[],
|
||||
output_dir=None,
|
||||
allow_process_key_extract=False,
|
||||
download_remote_media=False,
|
||||
privacy_mode=False,
|
||||
file_name=None,
|
||||
)
|
||||
|
||||
for _ in range(200):
|
||||
latest = manager.get_job(job.export_id)
|
||||
if latest and latest.status in {"done", "error", "cancelled"}:
|
||||
return latest
|
||||
import time as _time
|
||||
|
||||
_time.sleep(0.05)
|
||||
self.fail("export job did not finish in time")
|
||||
|
||||
def test_html_export_contains_index_and_conversation_page(self):
|
||||
with TemporaryDirectory() as td:
|
||||
root = Path(td)
|
||||
account = "wxid_test"
|
||||
username = "wxid_friend"
|
||||
self._prepare_account(root, account=account, username=username)
|
||||
|
||||
prev_data = os.environ.get("WECHAT_TOOL_DATA_DIR")
|
||||
try:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = str(root)
|
||||
svc = self._reload_export_modules()
|
||||
job = self._create_job(svc.CHAT_EXPORT_MANAGER, account=account, username=username)
|
||||
self.assertEqual(job.status, "done", msg=job.error)
|
||||
|
||||
self.assertTrue(job.zip_path and job.zip_path.exists())
|
||||
with zipfile.ZipFile(job.zip_path, "r") as zf:
|
||||
names = set(zf.namelist())
|
||||
|
||||
self.assertIn("index.html", names)
|
||||
self.assertIn("assets/wechat-chat-export.css", names)
|
||||
self.assertIn("assets/wechat-chat-export.js", names)
|
||||
|
||||
manifest = json.loads(zf.read("manifest.json").decode("utf-8"))
|
||||
self.assertEqual(manifest.get("format"), "html")
|
||||
|
||||
html_path = next((n for n in names if n.endswith("/messages.html")), "")
|
||||
self.assertTrue(html_path)
|
||||
|
||||
html_text = zf.read(html_path).decode("utf-8")
|
||||
self.assertIn('data-wce-rail-avatar="1"', html_text)
|
||||
self.assertIn('data-wce-session-list="1"', html_text)
|
||||
self.assertIn('id="sessionSearchInput"', html_text)
|
||||
self.assertIn('data-wce-time-divider="1"', html_text)
|
||||
self.assertIn('id="messageTypeFilter"', html_text)
|
||||
self.assertIn('value="chatHistory"', html_text)
|
||||
self.assertIn('id="chatHistoryModal"', html_text)
|
||||
self.assertIn('data-wce-chat-history="1"', html_text)
|
||||
self.assertIn('data-record-item-b64="', html_text)
|
||||
self.assertIn('id="wceMediaIndex"', html_text)
|
||||
self.assertIn('data-wce-quote-voice-btn="1"', html_text)
|
||||
self.assertNotIn('title="刷新消息"', html_text)
|
||||
self.assertNotIn('title="导出聊天记录"', html_text)
|
||||
self.assertNotIn("搜索聊天记录", html_text)
|
||||
self.assertNotIn("朋友圈", html_text)
|
||||
self.assertNotIn("年度总结", html_text)
|
||||
self.assertNotIn("设置", html_text)
|
||||
self.assertNotIn("隐私模式", html_text)
|
||||
|
||||
self.assertTrue(any(n.startswith("media/images/") for n in names))
|
||||
self.assertIn("../../media/images/", html_text)
|
||||
|
||||
self.assertIn("wechat-transfer-card", html_text)
|
||||
self.assertIn("wechat-redpacket-card", html_text)
|
||||
self.assertIn("wechat-chat-history-card", html_text)
|
||||
self.assertIn("wechat-voip-bubble", html_text)
|
||||
self.assertIn("wechat-link-card", html_text)
|
||||
self.assertIn("wechat-file-card", html_text)
|
||||
self.assertIn("wechat-voice-wrapper", html_text)
|
||||
|
||||
css_text = zf.read("assets/wechat-chat-export.css").decode("utf-8", errors="ignore")
|
||||
self.assertIn("wechat-transfer-card", css_text)
|
||||
self.assertNotIn("wechat-transfer-card[data-v-", css_text)
|
||||
|
||||
js_text = zf.read("assets/wechat-chat-export.js").decode("utf-8", errors="ignore")
|
||||
self.assertIn("wechat-voice-bubble", js_text)
|
||||
self.assertIn("voice-playing", js_text)
|
||||
self.assertIn("data-wce-quote-voice-btn", js_text)
|
||||
|
||||
self.assertIn("assets/images/wechat/wechat-trans-icon1.png", names)
|
||||
self.assertIn("assets/images/wechat/zip.png", names)
|
||||
self.assertIn("assets/images/wechat/WeChat-Icon-Logo.wine.svg", names)
|
||||
self.assertTrue(any(n.startswith("fonts/") and n.endswith(".woff2") for n in names))
|
||||
self.assertIn("wxemoji/Expression_1@2x.png", names)
|
||||
self.assertIn("../../wxemoji/Expression_1@2x.png", html_text)
|
||||
finally:
|
||||
if prev_data is None:
|
||||
os.environ.pop("WECHAT_TOOL_DATA_DIR", None)
|
||||
else:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = prev_data
|
||||
199
tests/test_chat_export_image_md5_candidate_fallback.py
Normal file
199
tests/test_chat_export_image_md5_candidate_fallback.py
Normal file
@@ -0,0 +1,199 @@
|
||||
import os
|
||||
import hashlib
|
||||
import sqlite3
|
||||
import sys
|
||||
import unittest
|
||||
import zipfile
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
|
||||
class TestChatExportImageMd5CandidateFallback(unittest.TestCase):
|
||||
def _reload_export_modules(self):
|
||||
import wechat_decrypt_tool.app_paths as app_paths
|
||||
import wechat_decrypt_tool.chat_helpers as chat_helpers
|
||||
import wechat_decrypt_tool.media_helpers as media_helpers
|
||||
import wechat_decrypt_tool.chat_export_service as chat_export_service
|
||||
|
||||
importlib.reload(app_paths)
|
||||
importlib.reload(chat_helpers)
|
||||
importlib.reload(media_helpers)
|
||||
importlib.reload(chat_export_service)
|
||||
return chat_export_service
|
||||
|
||||
def _seed_contact_db(self, path: Path, *, account: str, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE contact (
|
||||
username TEXT,
|
||||
remark TEXT,
|
||||
nick_name TEXT,
|
||||
alias TEXT,
|
||||
local_type INTEGER,
|
||||
verify_flag INTEGER,
|
||||
big_head_url TEXT,
|
||||
small_head_url TEXT
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE stranger (
|
||||
username TEXT,
|
||||
remark TEXT,
|
||||
nick_name TEXT,
|
||||
alias TEXT,
|
||||
local_type INTEGER,
|
||||
verify_flag INTEGER,
|
||||
big_head_url TEXT,
|
||||
small_head_url TEXT
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO contact VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(account, "", "我", "", 1, 0, "", ""),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO contact VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(username, "", "测试好友", "", 1, 0, "", ""),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_session_db(self, path: Path, *, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE SessionTable (
|
||||
username TEXT,
|
||||
is_hidden INTEGER,
|
||||
sort_timestamp INTEGER
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO SessionTable VALUES (?, ?, ?)",
|
||||
(username, 0, 1735689600),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_message_db(self, path: Path, *, account: str, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute("CREATE TABLE Name2Id (rowid INTEGER PRIMARY KEY, user_name TEXT)")
|
||||
conn.execute("INSERT INTO Name2Id(rowid, user_name) VALUES (?, ?)", (1, account))
|
||||
conn.execute("INSERT INTO Name2Id(rowid, user_name) VALUES (?, ?)", (2, username))
|
||||
|
||||
table_name = f"msg_{hashlib.md5(username.encode('utf-8')).hexdigest()}"
|
||||
conn.execute(
|
||||
f"""
|
||||
CREATE TABLE {table_name} (
|
||||
local_id INTEGER,
|
||||
server_id INTEGER,
|
||||
local_type INTEGER,
|
||||
sort_seq INTEGER,
|
||||
real_sender_id INTEGER,
|
||||
create_time INTEGER,
|
||||
message_content TEXT,
|
||||
compress_content BLOB
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
good_md5 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
bad_md5 = "ffffffffffffffffffffffffffffffff"
|
||||
image_xml = f'<msg><img md5="{bad_md5}" hdmd5="{good_md5}" cdnthumburl="img_file_id_1" /></msg>'
|
||||
|
||||
conn.execute(
|
||||
f"INSERT INTO {table_name} (local_id, server_id, local_type, sort_seq, real_sender_id, create_time, message_content, compress_content) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(1, 1001, 3, 1, 2, 1735689601, image_xml, None),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_decrypted_resource(self, account_dir: Path) -> None:
|
||||
resource_root = account_dir / "resource"
|
||||
(resource_root / "aa").mkdir(parents=True, exist_ok=True)
|
||||
(resource_root / "aa" / "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jpg").write_bytes(b"\xff\xd8\xff\xd9")
|
||||
|
||||
def _prepare_account(self, root: Path, *, account: str, username: str) -> Path:
|
||||
account_dir = root / "output" / "databases" / account
|
||||
account_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
self._seed_contact_db(account_dir / "contact.db", account=account, username=username)
|
||||
self._seed_session_db(account_dir / "session.db", username=username)
|
||||
self._seed_message_db(account_dir / "message_0.db", account=account, username=username)
|
||||
self._seed_decrypted_resource(account_dir)
|
||||
return account_dir
|
||||
|
||||
def _create_job(self, manager, *, account: str, username: str):
|
||||
job = manager.create_job(
|
||||
account=account,
|
||||
scope="selected",
|
||||
usernames=[username],
|
||||
export_format="html",
|
||||
start_time=None,
|
||||
end_time=None,
|
||||
include_hidden=False,
|
||||
include_official=False,
|
||||
include_media=True,
|
||||
media_kinds=["image"],
|
||||
message_types=[],
|
||||
output_dir=None,
|
||||
allow_process_key_extract=False,
|
||||
download_remote_media=False,
|
||||
privacy_mode=False,
|
||||
file_name=None,
|
||||
)
|
||||
|
||||
for _ in range(200):
|
||||
latest = manager.get_job(job.export_id)
|
||||
if latest and latest.status in {"done", "error", "cancelled"}:
|
||||
return latest
|
||||
import time as _time
|
||||
|
||||
_time.sleep(0.05)
|
||||
self.fail("export job did not finish in time")
|
||||
|
||||
def test_falls_back_to_secondary_md5_candidate(self):
|
||||
with TemporaryDirectory() as td:
|
||||
root = Path(td)
|
||||
account = "wxid_test"
|
||||
username = "wxid_friend"
|
||||
self._prepare_account(root, account=account, username=username)
|
||||
|
||||
prev_data = os.environ.get("WECHAT_TOOL_DATA_DIR")
|
||||
try:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = str(root)
|
||||
svc = self._reload_export_modules()
|
||||
job = self._create_job(svc.CHAT_EXPORT_MANAGER, account=account, username=username)
|
||||
self.assertEqual(job.status, "done", msg=job.error)
|
||||
|
||||
with zipfile.ZipFile(job.zip_path, "r") as zf:
|
||||
names = set(zf.namelist())
|
||||
self.assertIn("media/images/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jpg", names)
|
||||
self.assertFalse(any("ffffffffffffffffffffffffffffffff" in n for n in names if n.startswith("media/images/")))
|
||||
|
||||
html_path = next((n for n in names if n.endswith("/messages.html")), "")
|
||||
self.assertTrue(html_path)
|
||||
html_text = zf.read(html_path).decode("utf-8", errors="ignore")
|
||||
self.assertIn("../../media/images/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jpg", html_text)
|
||||
finally:
|
||||
if prev_data is None:
|
||||
os.environ.pop("WECHAT_TOOL_DATA_DIR", None)
|
||||
else:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = prev_data
|
||||
|
||||
235
tests/test_chat_export_image_md5_prefers_message_resource.py
Normal file
235
tests/test_chat_export_image_md5_prefers_message_resource.py
Normal file
@@ -0,0 +1,235 @@
|
||||
import os
|
||||
import hashlib
|
||||
import sqlite3
|
||||
import sys
|
||||
import unittest
|
||||
import zipfile
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
|
||||
class TestChatExportImageMd5PrefersMessageResource(unittest.TestCase):
|
||||
def _reload_export_modules(self):
|
||||
import wechat_decrypt_tool.app_paths as app_paths
|
||||
import wechat_decrypt_tool.chat_helpers as chat_helpers
|
||||
import wechat_decrypt_tool.media_helpers as media_helpers
|
||||
import wechat_decrypt_tool.chat_export_service as chat_export_service
|
||||
|
||||
importlib.reload(app_paths)
|
||||
importlib.reload(chat_helpers)
|
||||
importlib.reload(media_helpers)
|
||||
importlib.reload(chat_export_service)
|
||||
return chat_export_service
|
||||
|
||||
def _seed_source_info(self, account_dir: Path) -> None:
|
||||
wxid_dir = account_dir / "_wxid_dummy"
|
||||
db_storage_dir = account_dir / "_db_storage_dummy"
|
||||
wxid_dir.mkdir(parents=True, exist_ok=True)
|
||||
db_storage_dir.mkdir(parents=True, exist_ok=True)
|
||||
(account_dir / "_source.json").write_text(
|
||||
'{"wxid_dir": "' + str(wxid_dir).replace("\\", "\\\\") + '", "db_storage_path": "' + str(db_storage_dir).replace("\\", "\\\\") + '"}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
def _seed_contact_db(self, path: Path, *, account: str, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE contact (
|
||||
username TEXT,
|
||||
remark TEXT,
|
||||
nick_name TEXT,
|
||||
alias TEXT,
|
||||
local_type INTEGER,
|
||||
verify_flag INTEGER,
|
||||
big_head_url TEXT,
|
||||
small_head_url TEXT
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE stranger (
|
||||
username TEXT,
|
||||
remark TEXT,
|
||||
nick_name TEXT,
|
||||
alias TEXT,
|
||||
local_type INTEGER,
|
||||
verify_flag INTEGER,
|
||||
big_head_url TEXT,
|
||||
small_head_url TEXT
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO contact VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(account, "", "我", "", 1, 0, "", ""),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO contact VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(username, "", "测试好友", "", 1, 0, "", ""),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_session_db(self, path: Path, *, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE SessionTable (
|
||||
username TEXT,
|
||||
is_hidden INTEGER,
|
||||
sort_timestamp INTEGER
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO SessionTable VALUES (?, ?, ?)",
|
||||
(username, 0, 1735689600),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_message_db(self, path: Path, *, account: str, username: str, bad_md5: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute("CREATE TABLE Name2Id (rowid INTEGER PRIMARY KEY, user_name TEXT)")
|
||||
conn.execute("INSERT INTO Name2Id(rowid, user_name) VALUES (?, ?)", (1, account))
|
||||
conn.execute("INSERT INTO Name2Id(rowid, user_name) VALUES (?, ?)", (2, username))
|
||||
|
||||
table_name = f"msg_{hashlib.md5(username.encode('utf-8')).hexdigest()}"
|
||||
conn.execute(
|
||||
f"""
|
||||
CREATE TABLE {table_name} (
|
||||
local_id INTEGER,
|
||||
server_id INTEGER,
|
||||
local_type INTEGER,
|
||||
sort_seq INTEGER,
|
||||
real_sender_id INTEGER,
|
||||
create_time INTEGER,
|
||||
message_content TEXT,
|
||||
compress_content BLOB
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
image_xml = f'<msg><img md5="{bad_md5}" /></msg>'
|
||||
conn.execute(
|
||||
f"INSERT INTO {table_name} (local_id, server_id, local_type, sort_seq, real_sender_id, create_time, message_content, compress_content) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(1, 1001, 3, 1, 2, 1735689601, image_xml, None),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_message_resource_db(self, path: Path, *, good_md5: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE MessageResourceInfo (
|
||||
message_id INTEGER,
|
||||
message_svr_id INTEGER,
|
||||
message_local_type INTEGER,
|
||||
chat_id INTEGER,
|
||||
message_local_id INTEGER,
|
||||
message_create_time INTEGER,
|
||||
packed_info BLOB
|
||||
)
|
||||
"""
|
||||
)
|
||||
# packed_info may contain multiple tokens; include a realistic *.dat reference so the extractor prefers it.
|
||||
packed_info = f"{good_md5}_t.dat".encode("ascii")
|
||||
conn.execute(
|
||||
"INSERT INTO MessageResourceInfo VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
(1, 1001, 3, 0, 1, 1735689601, packed_info),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_decrypted_resource(self, account_dir: Path, *, good_md5: str) -> None:
|
||||
resource_root = account_dir / "resource"
|
||||
(resource_root / good_md5[:2]).mkdir(parents=True, exist_ok=True)
|
||||
# Minimal JPEG payload (valid SOI/EOI).
|
||||
(resource_root / good_md5[:2] / f"{good_md5}.jpg").write_bytes(b"\xff\xd8\xff\xd9")
|
||||
|
||||
def _prepare_account(self, root: Path, *, account: str, username: str, bad_md5: str, good_md5: str) -> Path:
|
||||
account_dir = root / "output" / "databases" / account
|
||||
account_dir.mkdir(parents=True, exist_ok=True)
|
||||
self._seed_source_info(account_dir)
|
||||
self._seed_contact_db(account_dir / "contact.db", account=account, username=username)
|
||||
self._seed_session_db(account_dir / "session.db", username=username)
|
||||
self._seed_message_db(account_dir / "message_0.db", account=account, username=username, bad_md5=bad_md5)
|
||||
self._seed_message_resource_db(account_dir / "message_resource.db", good_md5=good_md5)
|
||||
self._seed_decrypted_resource(account_dir, good_md5=good_md5)
|
||||
return account_dir
|
||||
|
||||
def _create_job(self, manager, *, account: str, username: str):
|
||||
job = manager.create_job(
|
||||
account=account,
|
||||
scope="selected",
|
||||
usernames=[username],
|
||||
export_format="html",
|
||||
start_time=None,
|
||||
end_time=None,
|
||||
include_hidden=False,
|
||||
include_official=False,
|
||||
include_media=True,
|
||||
media_kinds=["image"],
|
||||
message_types=["image"],
|
||||
output_dir=None,
|
||||
allow_process_key_extract=False,
|
||||
download_remote_media=False,
|
||||
privacy_mode=False,
|
||||
file_name=None,
|
||||
)
|
||||
|
||||
for _ in range(200):
|
||||
latest = manager.get_job(job.export_id)
|
||||
if latest and latest.status in {"done", "error", "cancelled"}:
|
||||
return latest
|
||||
import time as _time
|
||||
|
||||
_time.sleep(0.05)
|
||||
self.fail("export job did not finish in time")
|
||||
|
||||
def test_prefers_message_resource_md5_over_xml_md5(self):
|
||||
with TemporaryDirectory() as td:
|
||||
root = Path(td)
|
||||
account = "wxid_test"
|
||||
username = "wxid_friend"
|
||||
bad_md5 = "ffffffffffffffffffffffffffffffff"
|
||||
good_md5 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
self._prepare_account(root, account=account, username=username, bad_md5=bad_md5, good_md5=good_md5)
|
||||
|
||||
prev_data = os.environ.get("WECHAT_TOOL_DATA_DIR")
|
||||
try:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = str(root)
|
||||
svc = self._reload_export_modules()
|
||||
job = self._create_job(svc.CHAT_EXPORT_MANAGER, account=account, username=username)
|
||||
self.assertEqual(job.status, "done", msg=job.error)
|
||||
|
||||
with zipfile.ZipFile(job.zip_path, "r") as zf:
|
||||
names = set(zf.namelist())
|
||||
self.assertIn(f"media/images/{good_md5}.jpg", names)
|
||||
|
||||
html_path = next((n for n in names if n.endswith("/messages.html")), "")
|
||||
self.assertTrue(html_path)
|
||||
html_text = zf.read(html_path).decode("utf-8", errors="ignore")
|
||||
self.assertIn(f"../../media/images/{good_md5}.jpg", html_text)
|
||||
finally:
|
||||
if prev_data is None:
|
||||
os.environ.pop("WECHAT_TOOL_DATA_DIR", None)
|
||||
else:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = prev_data
|
||||
|
||||
@@ -198,6 +198,7 @@ class TestChatExportMessageTypesSemantics(unittest.TestCase):
|
||||
message_types=message_types,
|
||||
output_dir=None,
|
||||
allow_process_key_extract=False,
|
||||
download_remote_media=False,
|
||||
privacy_mode=privacy_mode,
|
||||
file_name=None,
|
||||
)
|
||||
|
||||
304
tests/test_chat_export_remote_thumb_option.py
Normal file
304
tests/test_chat_export_remote_thumb_option.py
Normal file
@@ -0,0 +1,304 @@
|
||||
import os
|
||||
import hashlib
|
||||
import sqlite3
|
||||
import sys
|
||||
import unittest
|
||||
import zipfile
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest import mock
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
|
||||
class _FakeResponse:
|
||||
def __init__(self, body: bytes, *, content_type: str) -> None:
|
||||
self.status_code = 200
|
||||
self.headers = {
|
||||
"Content-Type": str(content_type or "").strip(),
|
||||
"Content-Length": str(len(body)),
|
||||
}
|
||||
self._body = body
|
||||
|
||||
def iter_content(self, chunk_size=65536):
|
||||
data = self._body or b""
|
||||
for i in range(0, len(data), int(chunk_size or 65536)):
|
||||
yield data[i : i + int(chunk_size or 65536)]
|
||||
|
||||
def close(self):
|
||||
return None
|
||||
|
||||
|
||||
class TestChatExportRemoteThumbOption(unittest.TestCase):
|
||||
def _reload_export_modules(self):
|
||||
import wechat_decrypt_tool.app_paths as app_paths
|
||||
import wechat_decrypt_tool.chat_helpers as chat_helpers
|
||||
import wechat_decrypt_tool.media_helpers as media_helpers
|
||||
import wechat_decrypt_tool.chat_export_service as chat_export_service
|
||||
|
||||
importlib.reload(app_paths)
|
||||
importlib.reload(chat_helpers)
|
||||
importlib.reload(media_helpers)
|
||||
importlib.reload(chat_export_service)
|
||||
return chat_export_service
|
||||
|
||||
def _seed_contact_db(self, path: Path, *, account: str, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE contact (
|
||||
username TEXT,
|
||||
remark TEXT,
|
||||
nick_name TEXT,
|
||||
alias TEXT,
|
||||
local_type INTEGER,
|
||||
verify_flag INTEGER,
|
||||
big_head_url TEXT,
|
||||
small_head_url TEXT
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE stranger (
|
||||
username TEXT,
|
||||
remark TEXT,
|
||||
nick_name TEXT,
|
||||
alias TEXT,
|
||||
local_type INTEGER,
|
||||
verify_flag INTEGER,
|
||||
big_head_url TEXT,
|
||||
small_head_url TEXT
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO contact VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(account, "", "我", "", 1, 0, "", ""),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO contact VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(username, "", "测试好友", "", 1, 0, "", ""),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_session_db(self, path: Path, *, username: str) -> None:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE SessionTable (
|
||||
username TEXT,
|
||||
is_hidden INTEGER,
|
||||
sort_timestamp INTEGER
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO SessionTable VALUES (?, ?, ?)",
|
||||
(username, 0, 1735689600),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _seed_message_db(self, path: Path, *, account: str, username: str) -> tuple[str, str]:
|
||||
conn = sqlite3.connect(str(path))
|
||||
try:
|
||||
conn.execute("CREATE TABLE Name2Id (rowid INTEGER PRIMARY KEY, user_name TEXT)")
|
||||
conn.execute("INSERT INTO Name2Id(rowid, user_name) VALUES (?, ?)", (1, account))
|
||||
conn.execute("INSERT INTO Name2Id(rowid, user_name) VALUES (?, ?)", (2, username))
|
||||
|
||||
table_name = f"msg_{hashlib.md5(username.encode('utf-8')).hexdigest()}"
|
||||
conn.execute(
|
||||
f"""
|
||||
CREATE TABLE {table_name} (
|
||||
local_id INTEGER,
|
||||
server_id INTEGER,
|
||||
local_type INTEGER,
|
||||
sort_seq INTEGER,
|
||||
real_sender_id INTEGER,
|
||||
create_time INTEGER,
|
||||
message_content TEXT,
|
||||
compress_content BLOB
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
link_thumb = "https://1.1.1.1/thumb.png"
|
||||
quote_thumb = "https://1.1.1.1/quote.png"
|
||||
|
||||
link_xml = (
|
||||
"<msg><appmsg>"
|
||||
"<type>5</type>"
|
||||
"<title>示例链接</title>"
|
||||
"<des>这是描述</des>"
|
||||
"<url>https://example.com/</url>"
|
||||
f"<thumburl>{link_thumb}</thumburl>"
|
||||
"</appmsg></msg>"
|
||||
)
|
||||
quote_xml = (
|
||||
"<msg><appmsg>"
|
||||
"<type>57</type>"
|
||||
"<title>回复</title>"
|
||||
"<refermsg>"
|
||||
"<type>49</type>"
|
||||
"<svrid>8888</svrid>"
|
||||
"<fromusr>wxid_other</fromusr>"
|
||||
"<displayname>对方</displayname>"
|
||||
"<content>"
|
||||
"<msg><appmsg><type>5</type><title>被引用链接</title><url>https://example.com/</url>"
|
||||
f"<thumburl>{quote_thumb}</thumburl>"
|
||||
"</appmsg></msg>"
|
||||
"</content>"
|
||||
"</refermsg>"
|
||||
"</appmsg></msg>"
|
||||
)
|
||||
|
||||
rows = [
|
||||
(1, 1001, 49, 1, 2, 1735689601, link_xml, None),
|
||||
(2, 1002, 49, 2, 2, 1735689602, quote_xml, None),
|
||||
]
|
||||
conn.executemany(
|
||||
f"INSERT INTO {table_name} (local_id, server_id, local_type, sort_seq, real_sender_id, create_time, message_content, compress_content) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
rows,
|
||||
)
|
||||
conn.commit()
|
||||
return link_thumb, quote_thumb
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _prepare_account(self, root: Path, *, account: str, username: str) -> tuple[Path, str, str]:
|
||||
account_dir = root / "output" / "databases" / account
|
||||
account_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
self._seed_contact_db(account_dir / "contact.db", account=account, username=username)
|
||||
self._seed_session_db(account_dir / "session.db", username=username)
|
||||
link_thumb, quote_thumb = self._seed_message_db(account_dir / "message_0.db", account=account, username=username)
|
||||
return account_dir, link_thumb, quote_thumb
|
||||
|
||||
def _create_job(self, manager, *, account: str, username: str, download_remote_media: bool):
|
||||
job = manager.create_job(
|
||||
account=account,
|
||||
scope="selected",
|
||||
usernames=[username],
|
||||
export_format="html",
|
||||
start_time=None,
|
||||
end_time=None,
|
||||
include_hidden=False,
|
||||
include_official=False,
|
||||
include_media=True,
|
||||
media_kinds=["image", "emoji", "video", "video_thumb", "voice", "file"],
|
||||
message_types=["link", "quote", "image"],
|
||||
output_dir=None,
|
||||
allow_process_key_extract=False,
|
||||
download_remote_media=download_remote_media,
|
||||
privacy_mode=False,
|
||||
file_name=None,
|
||||
)
|
||||
|
||||
for _ in range(200):
|
||||
latest = manager.get_job(job.export_id)
|
||||
if latest and latest.status in {"done", "error", "cancelled"}:
|
||||
return latest
|
||||
import time as _time
|
||||
|
||||
_time.sleep(0.05)
|
||||
self.fail("export job did not finish in time")
|
||||
|
||||
def test_remote_thumb_disabled_does_not_download(self):
|
||||
with TemporaryDirectory() as td:
|
||||
root = Path(td)
|
||||
account = "wxid_test"
|
||||
username = "wxid_friend"
|
||||
_, link_thumb, quote_thumb = self._prepare_account(root, account=account, username=username)
|
||||
|
||||
prev_data = os.environ.get("WECHAT_TOOL_DATA_DIR")
|
||||
try:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = str(root)
|
||||
svc = self._reload_export_modules()
|
||||
|
||||
with mock.patch.object(
|
||||
svc.requests,
|
||||
"get",
|
||||
side_effect=AssertionError("requests.get should not be called when download_remote_media=False"),
|
||||
) as m_get:
|
||||
job = self._create_job(
|
||||
svc.CHAT_EXPORT_MANAGER,
|
||||
account=account,
|
||||
username=username,
|
||||
download_remote_media=False,
|
||||
)
|
||||
self.assertEqual(job.status, "done", msg=job.error)
|
||||
self.assertEqual(m_get.call_count, 0)
|
||||
|
||||
with zipfile.ZipFile(job.zip_path, "r") as zf:
|
||||
names = set(zf.namelist())
|
||||
html_path = next((n for n in names if n.endswith("/messages.html")), "")
|
||||
self.assertTrue(html_path)
|
||||
html_text = zf.read(html_path).decode("utf-8")
|
||||
self.assertIn(f'src="{link_thumb}"', html_text)
|
||||
self.assertIn(f'src="{quote_thumb}"', html_text)
|
||||
self.assertFalse(any(n.startswith("media/remote/") for n in names))
|
||||
finally:
|
||||
if prev_data is None:
|
||||
os.environ.pop("WECHAT_TOOL_DATA_DIR", None)
|
||||
else:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = prev_data
|
||||
|
||||
def test_remote_thumb_enabled_downloads_and_rewrites(self):
|
||||
with TemporaryDirectory() as td:
|
||||
root = Path(td)
|
||||
account = "wxid_test"
|
||||
username = "wxid_friend"
|
||||
_, link_thumb, quote_thumb = self._prepare_account(root, account=account, username=username)
|
||||
|
||||
prev_data = os.environ.get("WECHAT_TOOL_DATA_DIR")
|
||||
try:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = str(root)
|
||||
svc = self._reload_export_modules()
|
||||
|
||||
fake_png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde"
|
||||
|
||||
def _fake_get(url, **_kwargs):
|
||||
return _FakeResponse(fake_png, content_type="image/png")
|
||||
|
||||
with mock.patch.object(svc.requests, "get", side_effect=_fake_get) as m_get:
|
||||
job = self._create_job(
|
||||
svc.CHAT_EXPORT_MANAGER,
|
||||
account=account,
|
||||
username=username,
|
||||
download_remote_media=True,
|
||||
)
|
||||
self.assertEqual(job.status, "done", msg=job.error)
|
||||
self.assertGreaterEqual(m_get.call_count, 1)
|
||||
|
||||
with zipfile.ZipFile(job.zip_path, "r") as zf:
|
||||
names = set(zf.namelist())
|
||||
html_path = next((n for n in names if n.endswith("/messages.html")), "")
|
||||
self.assertTrue(html_path)
|
||||
html_text = zf.read(html_path).decode("utf-8")
|
||||
|
||||
h1 = hashlib.sha256(link_thumb.encode("utf-8", errors="ignore")).hexdigest()
|
||||
arc1 = f"media/remote/{h1[:32]}.png"
|
||||
self.assertIn(arc1, names)
|
||||
self.assertIn(f"../../{arc1}", html_text)
|
||||
self.assertNotIn(f'src="{link_thumb}"', html_text)
|
||||
|
||||
h2 = hashlib.sha256(quote_thumb.encode("utf-8", errors="ignore")).hexdigest()
|
||||
arc2 = f"media/remote/{h2[:32]}.png"
|
||||
self.assertIn(arc2, names)
|
||||
self.assertIn(f"../../{arc2}", html_text)
|
||||
self.assertNotIn(f'src="{quote_thumb}"', html_text)
|
||||
finally:
|
||||
if prev_data is None:
|
||||
os.environ.pop("WECHAT_TOOL_DATA_DIR", None)
|
||||
else:
|
||||
os.environ["WECHAT_TOOL_DATA_DIR"] = prev_data
|
||||
|
||||
58
tests/test_chat_official_article_cover_style.py
Normal file
58
tests/test_chat_official_article_cover_style.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
from wechat_decrypt_tool.chat_helpers import _parse_app_message
|
||||
|
||||
|
||||
class TestChatOfficialArticleCoverStyle(unittest.TestCase):
|
||||
def test_mp_weixin_feed_url_is_cover_style(self):
|
||||
raw_text = (
|
||||
"<msg>"
|
||||
"<appmsg>"
|
||||
"<title>时尚穿搭:「这样的jk你喜欢吗」</title>"
|
||||
"<des>这样的jk你喜欢吗?</des>"
|
||||
"<type>5</type>"
|
||||
"<url>"
|
||||
"http://mp.weixin.qq.com/s?__biz=MzkxOTY4MjIxOA==&mid=2247508015&idx=1&sn=931dce677c6e70b4365792b14e7e8ff0"
|
||||
"&exptype=masonry_feed_brief_content_elite_for_pcfeeds_u2i&ranksessionid=1770868256_1&req_id=1770867949535989#rd"
|
||||
"</url>"
|
||||
"<thumburl>https://mmbiz.qpic.cn/sz_mmbiz_jpg/foo/640?wx_fmt=jpeg&wxfrom=401</thumburl>"
|
||||
"<sourcedisplayname>甜图社</sourcedisplayname>"
|
||||
"<sourceusername>gh_abc123</sourceusername>"
|
||||
"</appmsg>"
|
||||
"</msg>"
|
||||
)
|
||||
|
||||
parsed = _parse_app_message(raw_text)
|
||||
self.assertEqual(parsed.get("renderType"), "link")
|
||||
self.assertEqual(parsed.get("linkType"), "official_article")
|
||||
self.assertEqual(parsed.get("linkStyle"), "cover")
|
||||
|
||||
def test_mp_weixin_non_feed_url_keeps_default_style(self):
|
||||
raw_text = (
|
||||
"<msg>"
|
||||
"<appmsg>"
|
||||
"<title>普通分享</title>"
|
||||
"<des>这样的jk你喜欢吗?</des>"
|
||||
"<type>5</type>"
|
||||
"<url>http://mp.weixin.qq.com/s?__biz=foo&mid=1&idx=1&sn=bar#rd</url>"
|
||||
"<sourcedisplayname>甜图社</sourcedisplayname>"
|
||||
"<sourceusername>gh_abc123</sourceusername>"
|
||||
"</appmsg>"
|
||||
"</msg>"
|
||||
)
|
||||
|
||||
parsed = _parse_app_message(raw_text)
|
||||
self.assertEqual(parsed.get("renderType"), "link")
|
||||
self.assertEqual(parsed.get("linkType"), "official_article")
|
||||
self.assertEqual(parsed.get("linkStyle"), "default")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -62,7 +62,68 @@ class TestTransferPostprocess(unittest.TestCase):
|
||||
|
||||
self.assertEqual(merged[0].get("transferStatus"), "已被接收")
|
||||
|
||||
def test_pending_transfer_marked_expired_by_system_message(self):
|
||||
merged = [
|
||||
{
|
||||
"id": "message_0:Msg_x:100",
|
||||
"renderType": "transfer",
|
||||
"paySubType": "1",
|
||||
"transferId": "t-expired-1",
|
||||
"amount": "¥500.00",
|
||||
"createTime": 1770742598,
|
||||
"isSent": True,
|
||||
"transferStatus": "转账",
|
||||
},
|
||||
{
|
||||
"id": "message_0:Msg_x:101",
|
||||
"renderType": "system",
|
||||
"type": 10000,
|
||||
"createTime": 1770829000,
|
||||
"content": "收款方24小时内未接收你的转账,已过期",
|
||||
},
|
||||
]
|
||||
|
||||
chat_router._postprocess_transfer_messages(merged)
|
||||
|
||||
self.assertEqual(merged[0].get("paySubType"), "10")
|
||||
self.assertEqual(merged[0].get("transferStatus"), "已过期")
|
||||
|
||||
def test_expired_matching_wins_over_amount_time_received_fallback(self):
|
||||
merged = [
|
||||
{
|
||||
"id": "message_0:Msg_x:200",
|
||||
"renderType": "transfer",
|
||||
"paySubType": "1",
|
||||
"transferId": "t-expired-2",
|
||||
"amount": "¥500.00",
|
||||
"createTime": 1770742598,
|
||||
"isSent": True,
|
||||
"transferStatus": "",
|
||||
},
|
||||
{
|
||||
"id": "message_0:Msg_x:201",
|
||||
"renderType": "transfer",
|
||||
"paySubType": "3",
|
||||
"transferId": "t-other",
|
||||
"amount": "¥500.00",
|
||||
"createTime": 1770828800,
|
||||
"isSent": False,
|
||||
"transferStatus": "已收款",
|
||||
},
|
||||
{
|
||||
"id": "message_0:Msg_x:202",
|
||||
"renderType": "system",
|
||||
"type": 10000,
|
||||
"createTime": 1770829000,
|
||||
"content": "收款方24小时内未接收你的转账,已过期",
|
||||
},
|
||||
]
|
||||
|
||||
chat_router._postprocess_transfer_messages(merged)
|
||||
|
||||
self.assertEqual(merged[0].get("paySubType"), "10")
|
||||
self.assertEqual(merged[0].get("transferStatus"), "已过期")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user