diff --git a/.gitignore b/.gitignore index 839a841..bcafc10 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,6 @@ node_modules .DS_Store dist dist-ssr -*.local \ No newline at end of file +*.local +/pywxdump/ui/web/* +/pywxdump/ui/web/assets/* diff --git a/pywxdump/api/remote_server.py b/pywxdump/api/remote_server.py index f1418e6..91baf21 100644 --- a/pywxdump/api/remote_server.py +++ b/pywxdump/api/remote_server.py @@ -25,7 +25,7 @@ from pywxdump.api.utils import get_conf, get_conf_wxids, set_conf, error9999, ge from pywxdump import get_wx_info, WX_OFFS, batch_decrypt, BiasAddr, merge_db, decrypt_merge, merge_real_time_db from pywxdump.db import DBHandler, download_file, dat2img -from pywxdump.db.export import export_csv, export_json +from pywxdump.db.export import export_csv, export_json, export_html # app = Flask(__name__, static_folder='../ui/web/dist', static_url_path='/') @@ -449,6 +449,40 @@ def get_export_json(): return ReJson(2001, body=ret) +@rs_api.route('/api/rs/export_html', methods=["GET", 'POST']) +def get_export_html(): + """ + 导出json + :return: + """ + my_wxid = get_conf(g.caf, g.at, "last") + if not my_wxid: return ReJson(1001, body="my_wxid is required") + db_config = get_conf(g.caf, my_wxid, "db_config") + + wxid = request.json.get("wxid") + if not wxid: + return ReJson(1002, body=f"username is required: {wxid}") + + html_outpath = os.path.join(g.work_path, "export", my_wxid, "html") + if not os.path.exists(html_outpath): + os.makedirs(html_outpath) + assert isinstance(html_outpath, str) + outpath = os.path.join(html_outpath, wxid) + if os.path.exists(outpath): + shutil.rmtree(outpath, ignore_errors=True) + # 复制pywxdump/ui/web/*到outpath + web_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "ui", "web") + shutil.copytree(web_path, outpath) + + + code, ret = export_html(wxid, outpath, db_config, my_wxid=my_wxid) + + if code: + return ReJson(0, ret) + else: + return ReJson(2001, body=ret) + + # end 导出聊天记录 ******************************************************************************************************* # start 聊天记录分析api ************************************************************************************************** diff --git a/pywxdump/db/export/__init__.py b/pywxdump/db/export/__init__.py index b5c7ad4..ef47993 100644 --- a/pywxdump/db/export/__init__.py +++ b/pywxdump/db/export/__init__.py @@ -6,4 +6,5 @@ # Date: 2024/04/20 # ------------------------------------------------------------------------------- from .exportCSV import export_csv -from .exportJSON import export_json \ No newline at end of file +from .exportJSON import export_json +from .exportHtml import export_html diff --git a/pywxdump/db/export/exportCSV.py b/pywxdump/db/export/exportCSV.py index 1bacc0c..baa05d5 100644 --- a/pywxdump/db/export/exportCSV.py +++ b/pywxdump/db/export/exportCSV.py @@ -8,8 +8,7 @@ import csv import json import os - -from pywxdump import DBHandler +from ..__init__ import DBHandler def export_csv(wxid, outpath, db_config, my_wxid="我", page_size=5000): diff --git a/pywxdump/db/export/exportHtml.py b/pywxdump/db/export/exportHtml.py new file mode 100644 index 0000000..88069af --- /dev/null +++ b/pywxdump/db/export/exportHtml.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*-# +# ------------------------------------------------------------------------------- +# Name: exportCSV.py +# Description: +# Author: xaoyaoo +# Date: 2024/04/20 +# ------------------------------------------------------------------------------- +import json +import os +from ..__init__ import DBHandler + + +def export_html(wxid, outpath, db_config, my_wxid="我"): + 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) + + db = DBHandler(db_config, my_wxid) + + count = db.get_msgs_count(wxid) + chatCount = count.get(wxid, 0) + if chatCount == 0: + return False, "没有聊天记录" + + msgs, users = db.get_msgs(wxid, 0, chatCount + 1) + if len(msgs) == 0: + return False, "没有聊天记录" + + data_js = ( + "localStorage.setItem('isUseLocalData', 't') // 't' : 'f' \n" + f"const local_msg_count = {chatCount}\n" + f"const local_mywxid = '{my_wxid}' \n" + f"const local_user_list = {json.dumps(users, ensure_ascii=False, indent=None )} \n" + f"const local_msg_list = {json.dumps(msgs, ensure_ascii=False, indent=None )} \n" + ) + + save_path = os.path.join(outpath, f"data.js") + with open(save_path, "w", encoding="utf-8") as f: + f.write(data_js) + + return True, f"导出成功: {outpath}" + + +if __name__ == '__main__': + pass