fix and add export html

This commit is contained in:
xaoyaoo committed 2024-08-13 22:59:46 +08:00
1 parent 37293a5af2
commit 36fcb2ac44
8 files changed
+68 -59

No files matched your search

+2 -4
View File
@@ -5,7 +5,5 @@
# Author: xaoyaoo
# Date: 2024/04/20
# -------------------------------------------------------------------------------
if __name__ == '__main__':
pass
from .exportCSV import export_csv
from .exportJSON import export_json
+10 -6
View File
@@ -8,18 +8,19 @@
import csv
import json
import os
from ..dbMSG import MsgHandler
from pywxdump import DBHandler
def export_csv(wxid, outpath, db_config, page_size=5000):
def export_csv(wxid, outpath, db_config, my_wxid="我", page_size=5000):
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 = MsgHandler(db_config)
db = DBHandler(db_config, my_wxid)
count = pmsg.get_msg_count(wxid)
count = db.get_msgs_count(wxid)
chatCount = count.get(wxid, 0)
if chatCount == 0:
return False, "没有聊天记录"
@@ -27,9 +28,11 @@ def export_csv(wxid, outpath, db_config, page_size=5000):
if page_size > chatCount:
page_size = chatCount + 1
users = {}
for i in range(0, chatCount, page_size):
start_index = i
data, wxid_list = pmsg.get_msg_list(wxid, start_index, page_size)
data, users_t = db.get_msg_list(wxid, start_index, page_size)
users.update(users_t)
if len(data) == 0:
return False, "没有聊天记录"
@@ -52,7 +55,8 @@ def export_csv(wxid, outpath, db_config, page_size=5000):
src = row.get("src", "")
CreateTime = row.get("CreateTime", "")
csv_writer.writerow([id, MsgSvrID, type_name, is_sender, talker, room_name, msg, src, CreateTime])
with open(os.path.join(outpath, "users.json"), "w", encoding="utf-8") as f:
json.dump(users, f, ensure_ascii=False, indent=4)
return True, f"导出成功: {outpath}"
+11 -7
View File
@@ -7,31 +7,35 @@
# -------------------------------------------------------------------------------
import json
import os
from ..dbMSG import MsgHandler
from ..__init__ import DBHandler
def export_json(wxid, outpath, db_config):
def export_json(wxid, outpath, db_config, my_wxid="我", indent=4):
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 = MsgHandler(db_config)
db = DBHandler(db_config, my_wxid)
count = pmsg.get_msg_count(wxid)
count = db.get_msgs_count(wxid)
chatCount = count.get(wxid, 0)
if chatCount == 0:
return False, "没有聊天记录"
users = {}
page_size = chatCount + 1
for i in range(0, chatCount, page_size):
start_index = i
data, wxid_list = pmsg.get_msg_list(wxid, start_index, page_size)
data, users_t = db.get_msgs(wxid, start_index, page_size)
users.update(users_t)
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)
json.dump(data, f, ensure_ascii=False, indent=indent)
with open(os.path.join(outpath, "users.json"), "w", encoding="utf-8") as f:
json.dump(users, f, ensure_ascii=False, indent=indent)
return True, f"导出成功: {outpath}"