WeChatMsg/app/DataBase/exporter_csv.py

33 lines
1.3 KiB
Python
Raw Normal View History

2024-01-02 22:33:46 +08:00
import csv
import os
from app.DataBase import msg_db
2024-01-29 20:42:58 +08:00
from app.DataBase.exporter import ExporterBase
2024-02-04 23:11:43 +08:00
from app.config import output_dir
2024-01-02 22:33:46 +08:00
class CSVExporter(ExporterBase):
def to_csv(self):
2024-01-15 19:51:08 +08:00
print(f"【开始导出 CSV {self.contact.remark}")
2024-02-04 23:11:43 +08:00
origin_path = os.path.join(os.path.abspath('.'),output_dir,'聊天记录',self.contact.remark)
os.makedirs(origin_path, exist_ok=True)
filename = os.path.join(origin_path,f"{self.contact.remark}_utf8.csv")
2024-01-02 22:33:46 +08:00
columns = ['localId', 'TalkerId', 'Type', 'SubType',
'IsSender', 'CreateTime', 'Status', 'StrContent',
'StrTime', 'Remark', 'NickName', 'Sender']
messages = msg_db.get_messages(self.contact.wxid, time_range=self.time_range)
2024-01-02 22:33:46 +08:00
# 写入CSV文件
with open(filename, mode='w', newline='', encoding='utf-8-sig') as file:
writer = csv.writer(file)
writer.writerow(columns)
# 写入数据
2024-01-04 20:28:08 +08:00
# writer.writerows(messages)
for msg in messages:
other_data = [msg[13].remark, msg[13].nickName, msg[13].wxid] if self.contact.is_chatroom else []
2024-01-04 20:28:08 +08:00
writer.writerow([*msg[:9], *other_data])
2024-01-11 23:25:42 +08:00
print(f"【完成导出 CSV {self.contact.remark}")
self.okSignal.emit(1)
2024-01-04 20:28:08 +08:00
2024-01-02 22:33:46 +08:00
def run(self):
2024-01-04 20:28:08 +08:00
self.to_csv()