From 2078619916bf015a51c18c3a06d9e9acaa55c808 Mon Sep 17 00:00:00 2001 From: xaoyaoo Date: Sat, 20 Jan 2024 22:13:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=BC=E5=87=BAjson?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pywxdump/__init__.py | 2 +- pywxdump/analyzer/__init__.py | 2 +- pywxdump/analyzer/export_chat.py | 24 +++++++++++++++++++++++- pywxdump/api/api.py | 12 ++++++++++-- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/pywxdump/__init__.py b/pywxdump/__init__.py index 262a74e6..eaf82f6f 100644 --- a/pywxdump/__init__.py +++ b/pywxdump/__init__.py @@ -9,7 +9,7 @@ from .wx_info import merge_copy_db, merge_msg_db, merge_media_msg_db, merge_db, decrypt_merge from .analyzer.db_parsing import read_img_dat, read_emoji, decompress_CompressContent, read_audio_buf, read_audio, \ parse_xml_string, read_BytesExtra -from .analyzer import export_csv +from .analyzer import export_csv,export_json from .ui import app_show_chat, get_user_list, export from .server import start_falsk diff --git a/pywxdump/analyzer/__init__.py b/pywxdump/analyzer/__init__.py index 11db128e..53536635 100644 --- a/pywxdump/analyzer/__init__.py +++ b/pywxdump/analyzer/__init__.py @@ -7,5 +7,5 @@ # ------------------------------------------------------------------------------- from .db_parsing import read_img_dat, read_emoji, decompress_CompressContent, read_audio_buf, read_audio, \ parse_xml_string, read_BytesExtra -from .export_chat import export_csv, get_contact_list, get_chatroom_list, get_msg_list, get_chat_count +from .export_chat import export_csv, get_contact_list, get_chatroom_list, get_msg_list, get_chat_count, export_json from .utils import get_type_name, get_name_typeid diff --git a/pywxdump/analyzer/export_chat.py b/pywxdump/analyzer/export_chat.py index c1e275b6..a997dee9 100644 --- a/pywxdump/analyzer/export_chat.py +++ b/pywxdump/analyzer/export_chat.py @@ -142,7 +142,8 @@ def get_msg_list(MSG_db_path, selected_talker="", start_index=0, page_size=500): voicelength = f"{voicelength:.2f}" content[ "msg"] = f"语音时长:{voicelength}秒\n翻译结果:{transtext}" if transtext else f"语音时长:{voicelength}秒" - content["src"] = os.path.join("audio", f"{StrTalker}", f"{CreateTime.replace(':', '-').replace(' ','_')}_{IsSender}_{MsgSvrID}.wav") + content["src"] = os.path.join("audio", f"{StrTalker}", + f"{CreateTime.replace(':', '-').replace(' ', '_')}_{IsSender}_{MsgSvrID}.wav") elif type_id == (43, 0): # 视频 BytesExtra = read_BytesExtra(BytesExtra) BytesExtra = str(BytesExtra) @@ -254,6 +255,27 @@ def export_csv(username, outpath, MSG_ALL_db_path, page_size=5000): return True, f"导出成功: {outpath}" +def export_json(username, outpath, MSG_ALL_db_path): + if not os.path.exists(outpath): + outpath = os.path.join(os.getcwd(), "export" + os.sep + username) + if not os.path.exists(outpath): + os.makedirs(outpath) + count = get_chat_count(MSG_ALL_db_path, username) + chatCount = count.get(username, 0) + if chatCount == 0: + return False, "没有聊天记录" + page_size = chatCount + 1 + for i in range(0, chatCount, page_size): + start_index = i + data = get_msg_list(MSG_ALL_db_path, username, start_index, page_size) + if len(data) == 0: + return False, "没有聊天记录" + save_path = os.path.join(outpath, f"{username}_{i}_{i + page_size}.json") + with open(save_path, "w", encoding="utf-8") as f: + json.dump(data, f, ensure_ascii=False, indent=4) + return True, f"导出成功: {outpath}" + + def export_html(user, outpath, MSG_ALL_db_path, MediaMSG_all_db_path, FileStorage_path, page_size=500): name_save = user.get("remark", user.get("nickname", user.get("username", ""))) username = user.get("username", "") diff --git a/pywxdump/api/api.py b/pywxdump/api/api.py index b4650df3..ae7d302c 100644 --- a/pywxdump/api/api.py +++ b/pywxdump/api/api.py @@ -335,15 +335,23 @@ def export(): return ReJson(1002, body={"start_time": start_time, "end_time": end_time}) elif export_type == "csv": + outpath = os.path.join(outpath, username) if not os.path.exists(outpath): os.makedirs(outpath) - code, ret = analyzer.export_csv(username, os.path.join(outpath, username), read_session(g.sf, "msg_path")) + code, ret = analyzer.export_csv(username, outpath, read_session(g.sf, "msg_path")) if code: return ReJson(0, ret) else: return ReJson(2001, body=ret) elif export_type == "json": - pass + outpath = os.path.join(outpath, username) + if not os.path.exists(outpath): + os.makedirs(outpath) + code, ret = analyzer.export_json(username, outpath, read_session(g.sf, "msg_path")) + if code: + return ReJson(0, ret) + else: + return ReJson(2001, body=ret) elif export_type == "html": chat_type_tups = [] for ct in chat_type: