import os import pandas as pd from PyQt5.QtCore import pyqtSignal, QThread from . import msg from ..log import log from ..person import MePC if not os.path.exists('./data/聊天记录'): os.mkdir('./data/聊天记录') class Output(QThread): """ 发送信息线程 """ progressSignal = pyqtSignal(int) rangeSignal = pyqtSignal(int) okSignal = pyqtSignal(int) i = 1 CSV = 0 DOCX = 1 HTML = 2 def __init__(self, contact, parent=None, type_=DOCX): super().__init__(parent) self.last_timestamp = 0 self.sec = 2 # 默认1000秒 self.contact = contact self.ta_username = contact.wxid self.msg_id = 0 self.output_type = type_ self.total_num = 0 self.num = 0 @log def to_csv(self, conRemark, path): origin_docx_path = f"{os.path.abspath('.')}/data/聊天记录/{self.contact.remark}" if not os.path.exists(origin_docx_path): os.mkdir(origin_docx_path) filename = f"{os.path.abspath('.')}/data/聊天记录/{self.contact.remark}/{self.contact.remark}.csv" # columns = ["用户名", "消息内容", "发送时间", "发送状态", "消息类型", "isSend", "msgId"] columns = ['localId', 'TalkerId', 'Type', 'SubType', 'IsSender', 'CreateTime', 'Status', 'StrContent', 'StrTime'] messages = msg.get_messages(self.contact.wxid) # print() df = pd.DataFrame( data=messages, columns=columns, ) df.to_csv(filename, encoding='utf-8') self.okSignal.emit('ok') def to_html(self): origin_docx_path = f"{os.path.abspath('.')}/data/聊天记录/{self.contact.remark}" if not os.path.exists(origin_docx_path): os.mkdir(origin_docx_path) messages = msg.get_messages(self.contact.wxid) filename = f"{os.path.abspath('.')}/data/聊天记录/{self.contact.remark}/{self.contact.remark}.html" f = open(filename, 'w', encoding='utf-8') html_head = ''' Title
''' f.write(html_head) MePC().avatar.save(os.path.join(origin_docx_path, 'myhead.png')) self.contact.avatar.save(os.path.join(origin_docx_path, 'tahead.png')) for message in messages: type_ = message[2] str_content = message[7] str_time = message[8] # print(type_, type(type_)) is_send = message[4] avatar = MePC().avatar_path if is_send else self.contact.avatar_path timestamp = message[5] if type_ == 1: if self.is_5_min(timestamp): f.write( f'''
{str_time}
''' ) if is_send: f.write( f'''
{str_content}
''' ) else: f.write( f'''
{str_content}
''' ) html_end = '''
''' f.write(html_end) f.close() self.okSignal.emit('ok') def is_5_min(self, timestamp): if abs(timestamp - self.last_timestamp) > 300: self.last_timestamp = timestamp return True return False def run(self): if self.output_type == self.DOCX: return elif self.output_type == self.CSV: # print("线程导出csv") self.to_csv(self.ta_username, "path") elif self.output_type == self.HTML: self.to_html()