diff --git a/pywxdump/api/api.py b/pywxdump/api/api.py index ef03c7c..6610cef 100644 --- a/pywxdump/api/api.py +++ b/pywxdump/api/api.py @@ -23,7 +23,7 @@ from pywxdump import read_info, VERSION_LIST, batch_decrypt, BiasAddr, merge_db, import pywxdump from pywxdump.dbpreprocess import wxid2userinfo, ParsingMSG, get_user_list, get_recent_user_list, ParsingMediaMSG, \ download_file -from pywxdump.dbpreprocess import export_csv +from pywxdump.dbpreprocess import export_csv,export_json # app = Flask(__name__, static_folder='../ui/web/dist', static_url_path='/') @@ -513,14 +513,44 @@ def get_export_csv(): if not my_wxid: return ReJson(1001, body="my_wxid is required") wxid = request.json.get("wxid") + # st_ed_time = request.json.get("datetime", [0, 0]) if not wxid: return ReJson(1002, body=f"username is required: {wxid}") + # if not isinstance(st_ed_time, list) or len(st_ed_time) != 2: + # return ReJson(1002, body=f"datetime is required: {st_ed_time}") + # start, end = st_ed_time + # if not isinstance(start, int) or not isinstance(end, int) or start >= end: + # return ReJson(1002, body=f"datetime is required: {st_ed_time}") outpath = os.path.join(g.tmp_path, "export", my_wxid, "csv", wxid) if not os.path.exists(outpath): os.makedirs(outpath) - code, ret = export_csv(wxid, outpath, read_session(g.sf, my_wxid, "msg_path")) + code, ret = export_csv(wxid, outpath, read_session(g.sf, my_wxid, "merge_path")) + if code: + return ReJson(0, ret) + else: + return ReJson(2001, body=ret) + + +@api.route('/api/export_json', methods=["GET", 'POST']) +def get_export_json(): + """ + 导出json + :return: + """ + my_wxid = read_session(g.sf, "test", "last") + if not my_wxid: return ReJson(1001, body="my_wxid is required") + + wxid = request.json.get("wxid") + if not wxid: + return ReJson(1002, body=f"username is required: {wxid}") + + outpath = os.path.join(g.tmp_path, "export", my_wxid, "json", wxid) + if not os.path.exists(outpath): + os.makedirs(outpath) + + code, ret = export_json(wxid, outpath, read_session(g.sf, my_wxid, "merge_path")) if code: return ReJson(0, ret) else: diff --git a/pywxdump/dbpreprocess/__init__.py b/pywxdump/dbpreprocess/__init__.py index c247efd..2a57ca6 100644 --- a/pywxdump/dbpreprocess/__init__.py +++ b/pywxdump/dbpreprocess/__init__.py @@ -14,6 +14,7 @@ from .parsingOpenIMContact import ParsingOpenIMContact from .utils import download_file from .export.exportCSV import export_csv +from .export.exportJSON import export_json def get_user_list(MicroMsg_db_path, OpenIMContact_db_path=None, word=None): diff --git a/pywxdump/dbpreprocess/export/exportJSON.py b/pywxdump/dbpreprocess/export/exportJSON.py new file mode 100644 index 0000000..f0a4015 --- /dev/null +++ b/pywxdump/dbpreprocess/export/exportJSON.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*-# +# ------------------------------------------------------------------------------- +# Name: exportCSV.py +# Description: +# Author: xaoyaoo +# Date: 2024/04/20 +# ------------------------------------------------------------------------------- +import json +import os +from ..parsingMSG import ParsingMSG + + +def export_json(wxid, outpath, msg_path): + if not os.path.exists(outpath): + outpath = os.path.join(os.getcwd(), "export" + os.sep + wxid) + if not os.path.exists(outpath): + os.makedirs(outpath) + + pmsg = ParsingMSG(msg_path) + + count = pmsg.msg_count(wxid) + chatCount = count.get(wxid, 0) + if chatCount == 0: + return False, "没有聊天记录" + + page_size = chatCount + 1 + for i in range(0, chatCount, page_size): + start_index = i + data, wxid_list = pmsg.msg_list(wxid, start_index, page_size) + if len(data) == 0: + return False, "没有聊天记录" + save_path = os.path.join(outpath, f"{wxid}_{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}" + + +if __name__ == '__main__': + pass