Skip to content

Commit

Permalink
增加导出json方式
Browse files Browse the repository at this point in the history
  • Loading branch information
xaoyaoo committed Jan 20, 2024
1 parent 767b09c commit 2078619
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pywxdump/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pywxdump/analyzer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 23 additions & 1 deletion pywxdump/analyzer/export_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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", "")
Expand Down
12 changes: 10 additions & 2 deletions pywxdump/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2078619

Please sign in to comment.