重构导出csv
This commit is contained in:
1 parent
aec195214c
commit
cc337391bc
6 files changed
+106
-10
No files matched your search
@@ -13,6 +13,8 @@ from .parsingMediaMSG import ParsingMediaMSG
|
||||
from .parsingOpenIMContact import ParsingOpenIMContact
|
||||
from .utils import download_file
|
||||
|
||||
from .export.exportCSV import export_csv
|
||||
|
||||
|
||||
def get_user_list(MicroMsg_db_path, OpenIMContact_db_path=None, word=None):
|
||||
"""
|
||||
@@ -65,7 +67,7 @@ def wxid2userinfo(MicroMsg_db_path, OpenIMContact_db_path, wxid):
|
||||
"""
|
||||
获取联系人信息
|
||||
:param MicroMsg_db_path: MicroMsg.db 文件路径
|
||||
:param wxid: 微信id
|
||||
:param wxid: 微信id,可以是单个id,也可以是多个id,使用list传入
|
||||
:return: 联系人信息 {wxid: {wxid: wxid, nickname: nickname, remark: remark, account: account, describe: describe, headImgUrl: headImgUrl}}
|
||||
"""
|
||||
# 连接 MicroMsg.db 数据库,并执行查询
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# -*- coding: utf-8 -*-#
|
||||
# -------------------------------------------------------------------------------
|
||||
# Name: exportCSV.py
|
||||
# Description:
|
||||
# Author: xaoyaoo
|
||||
# Date: 2024/04/20
|
||||
# -------------------------------------------------------------------------------
|
||||
import csv
|
||||
import json
|
||||
import os
|
||||
from ..parsingMSG import ParsingMSG
|
||||
|
||||
|
||||
def export_csv(wxid, outpath, msg_path, 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 = ParsingMSG(msg_path)
|
||||
|
||||
count = pmsg.msg_count(wxid)
|
||||
chatCount = count.get(wxid, 0)
|
||||
if chatCount == 0:
|
||||
return False, "没有聊天记录"
|
||||
|
||||
if page_size > chatCount:
|
||||
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}.csv")
|
||||
|
||||
with open(save_path, "w", encoding="utf-8", newline='') as f:
|
||||
csv_writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
|
||||
|
||||
csv_writer.writerow(["id", "MsgSvrID", "type_name", "is_sender", "talker", "room_name", "content",
|
||||
"CreateTime"])
|
||||
for row in data:
|
||||
id = row.get("id", "")
|
||||
MsgSvrID = row.get("MsgSvrID", "")
|
||||
type_name = row.get("type_name", "")
|
||||
is_sender = row.get("is_sender", "")
|
||||
talker = row.get("talker", "")
|
||||
room_name = row.get("room_name", "")
|
||||
content = row.get("content", "")
|
||||
CreateTime = row.get("CreateTime", "")
|
||||
|
||||
content = json.dumps(content, ensure_ascii=False)
|
||||
csv_writer.writerow([id, MsgSvrID, type_name, is_sender, talker, room_name, content, CreateTime])
|
||||
|
||||
return True, f"导出成功: {outpath}"
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pass
|
||||
@@ -50,7 +50,7 @@ class ParsingMSG(DatabaseBase):
|
||||
"""
|
||||
获取聊天记录数量,根据wxid获取单个联系人的聊天记录数量,不传wxid则获取所有联系人的聊天记录数量
|
||||
:param MSG_db_path: MSG.db 文件路径
|
||||
:return: 聊天记录数量列表
|
||||
:return: 聊天记录数量列表 {wxid: chat_count}
|
||||
"""
|
||||
if wxid:
|
||||
sql = f"SELECT StrTalker, COUNT(*) FROM MSG WHERE StrTalker='{wxid}';"
|
||||
|
||||
@@ -42,7 +42,7 @@ class ParsingMicroMsg(DatabaseBase):
|
||||
def wxid2userinfo(self, wxid):
|
||||
"""
|
||||
获取单个联系人信息
|
||||
:param wxid: 微信id
|
||||
:param wxid: 微信id,可以是单个id,也可以是id列表
|
||||
:return: 联系人信息
|
||||
"""
|
||||
if isinstance(wxid, str):
|
||||
|
||||
Reference in new issue
Block a user