Merge pull request #222 from aaayun/feature-add-other-type-chat-info

添加html部分文件导出的支持
This commit is contained in:
SiYuan 2023-12-24 19:33:51 +08:00 committed by GitHub
commit feff156c6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 150 additions and 4 deletions

View File

@ -18,6 +18,7 @@ import shutil
from ..util.compress_content import parser_reply
from ..util.emoji import get_emoji, get_emoji_path, get_emoji_url
from ..util.image import get_image_path, get_image
from ..util.file import get_file
os.makedirs('./data/聊天记录', exist_ok=True)
@ -360,6 +361,37 @@ class ChildThread(QThread):
def wx_file(self, doc, isSend, content, status):
return
def file(self, doc, message):
origin_docx_path = f"{os.path.abspath('.')}/data/聊天记录/{self.contact.remark}"
bytesExtra = message[10]
str_time = message[8]
is_send = message[4]
timestamp = message[5]
is_chatroom = 1 if self.contact.is_chatroom else 0
if is_chatroom:
avatar = f"./avatar/{message[12].wxid}.png"
else:
avatar = f"./avatar/{MePC().wxid if is_send else self.contact.wxid}.png"
if is_chatroom:
if is_send:
displayname = MePC().name
else:
displayname = message[12].remark
else:
displayname = MePC().name if is_send else self.contact.remark
displayname = escape_js_and_html(displayname)
if self.output_type == Output.HTML:
link = get_file(bytesExtra, thumb=True, output_path=origin_docx_path + '/file')
file_name = ''
shutil.copy(f"{os.path.abspath('.')}/app/resources/icons/file.png", origin_docx_path + '/file/file.png')
file_path = './file/file.png'
if link != "":
file_name = os.path.basename(link)
link = './file/' + file_name
doc.write(
f'''{{ type:49, text: '{file_path}',is_send:{is_send},avatar_path:'{avatar}',timestamp:{timestamp},is_chatroom:{is_chatroom},displayname:'{displayname}',link: '{link}',sub_type:6,file_name: '{file_name}'}},'''
)
def retract_message(self, doc, isSend, content, status):
return
@ -570,6 +602,8 @@ class ChildThread(QThread):
self.system_msg(f, message)
elif type_ == 49 and sub_type == 57 and self.message_types.get(1):
self.refermsg(f, message)
elif type_ == 49 and sub_type == 6 and self.message_types.get(4906):
self.file(f, message)
f.write(html_end)
f.close()
self.okSignal.emit(1)

Binary file not shown.

After

Width:  |  Height:  |  Size: 568 B

View File

@ -161,7 +161,7 @@ body{
color: darkgray;
}
.chat-image img{
.chat-image img,.chat-file img{
margin-right: 18px;
margin-left: 18px;
max-width: 300px;
@ -184,6 +184,29 @@ audio{
margin-left: 9px;
margin-right: 9px;
}
.chat-file {
width: 300px;
background-color: #fff;
margin-right: 20px;
}
.chat-file a ,.chat-file div{
display: flex;
color: #000;
outline: none;
text-decoration: none;
margin: 0 20px 20px 20px;
}
.chat-file div{
margin: 20px;
}
.chat-file a span ,.chat-file div span{
/* flex-grow: 1; */
width: 200px;
}
.chat-file a img,.chat-file div img{
width: 100px;
}
.input-area{
border-top:0.5px solid #e0e0e0;
height: 150px;
@ -361,9 +384,9 @@ input {
// 计算当前页应该显示的元素范围
const startIndex = (page - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
function replaceEmoji(text){
// 定义替换规则,可以根据需要添加更多规则
var replacementRules = [
{ pattern: /\[微笑\]/g, replacement: '<img src="https://res.wx.qq.com/t/wx_fed/we-emoji/res/v1.2.8/assets/Expression/Expression_1@2x.png" id="微笑" class="emoji_img">' },
@ -534,8 +557,19 @@ input {
messageAudioTag.innerHTML = `<audio src="${message.text}" controls></audio>`;
return messageAudioTag;
}
function messageFileBox(message) {
const messageFileTag = document.createElement('div');
messageFileTag.className = `chat-file`;
if (message.link !== ''){
messageFileTag.innerHTML = `
<a href="${message.link}" target="_blank"><span>${message.file_name}</span><img src="${message.text}"/></a>`
}else{
messageFileTag.innerHTML = `<div><span>文件已丢失</span><img src="${message.text}"/></div>`;
}
return messageFileTag;
}
// 从数据列表中取出对应范围的元素并添加到容器中
// 从数据列表中取出对应范围的元素并添加到容器中
for (let i = startIndex; i < endIndex && i < chatMessages.length; i++) {
const message = chatMessages[i];
if (i == startIndex) { // 判断一下在页面顶部多加一个时间
@ -619,6 +653,19 @@ input {
messageContent.appendChild(messageElementReferText(message, side));
}
// 整合
messageElement.className = `item item-${side}`;
messageElement.appendChild(message.is_send ? messageContent : avatarTag);
messageElement.appendChild(message.is_send ? avatarTag : messageContent);
}
if (message.sub_type == 6) {
// displayname 和 file
messageContent.className = `content-wrapper content-wrapper-${side}`;
if (message.is_chatroom && !message.is_send) {
messageContent.appendChild(displayNameBox(message));
}
messageContent.appendChild(messageFileBox(message));
// 整合
messageElement.className = `item item-${side}`;
messageElement.appendChild(message.is_send ? messageContent : avatarTag);

View File

@ -12,6 +12,7 @@ types = {
'语音': 34,
'视频': 43,
'表情包': 47,
'文件': 4906,
'拍一拍等系统消息': 10000
}
Stylesheet = """
@ -32,6 +33,7 @@ class ExportDialog(QDialog):
if file_type == 'html':
self.export_type = Output.HTML
self.export_choices = {"文本": True, "图片": True, "语音": False, "视频": False, "表情包": False,
'文件': True,
'拍一拍等系统消息': True} # 定义导出的数据类型,默认全部选择
elif file_type == 'csv':
self.export_type = Output.CSV

63
app/util/file.py Normal file
View File

@ -0,0 +1,63 @@
import os
import traceback
import shutil
import requests
from app.log import log, logger
from app.util.protocbuf.msg_pb2 import MessageBytesExtra
from ..person import MePC
root_path = './data/files/'
if not os.path.exists('./data'):
os.mkdir('./data')
if not os.path.exists(root_path):
os.mkdir(root_path)
class File:
def __init__(self):
self.open_flag = False
def get_file(bytes_extra, thumb=False, output_path=root_path) -> str:
try:
msg_bytes = MessageBytesExtra()
msg_bytes.ParseFromString(bytes_extra)
file_original_path = ''
file_path = ''
file_name = ''
real_path = ''
if len(msg_bytes.message2) > 0:
file_field = msg_bytes.message2[-1].field2
if file_field.find('sec_msg_node') == -1:
file_original_path = file_field
file_name = os.path.basename(file_original_path)
if file_name != '' and file_name != MePC().wxid:
file_path = os.path.join(output_path, file_name)
if os.path.exists(file_path):
print('文件' + file_path + '已存在')
return file_path
if os.path.isabs(file_original_path):
if os.path.exists(file_original_path):
real_path = file_original_path
else: # 如果没找到再判断一次是否是迁移了目录
if file_original_path.find(r"FileStorage") != -1:
real_path = MePC().wx_dir + file_original_path[
file_original_path.find("FileStorage") - 1:]
else:
if file_original_path.find(MePC().wxid) != -1:
real_path = MePC().wx_dir + file_original_path.replace(MePC().wxid, '')
else:
real_path = MePC().wx_dir + file_original_path
if real_path != "":
if os.path.exists(real_path):
print('开始获取文件' + real_path)
shutil.copy2(real_path, file_path)
else:
print('文件' + file_original_path + '已丢失')
file_path = ''
return file_path
except:
logger.error(traceback.format_exc())
return ""