调整目录,FIXME 待解决插件加载时,只读取目录下的main.py方法,其他过滤
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
import re
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def compress_chat_data(chat_data_str, time_threshold=5):
|
||||
"""
|
||||
压缩聊天数据,减少 token 使用,格式为时间,发信人,内容。
|
||||
|
||||
:param chat_data_str: 原始聊天记录的长字符串
|
||||
:param time_threshold: 同一发信人连续发言间隔小于该值(秒),则合并
|
||||
:return: 压缩后的聊天数据的长字符串
|
||||
"""
|
||||
# 如果字符串长度超过30000,则去除前面的聊天记录
|
||||
if len(chat_data_str) > 30000:
|
||||
lines = chat_data_str.splitlines()
|
||||
total_length = 0
|
||||
cut_index = 0
|
||||
|
||||
# 从后往前计算,找到保留哪些行
|
||||
for i in range(len(lines) - 1, -1, -1):
|
||||
line_length = len(lines[i]) + 1 # +1 是为了计入换行符
|
||||
total_length += line_length
|
||||
if total_length > 30000:
|
||||
cut_index = i + 1 # 保留这个索引之后的行
|
||||
break
|
||||
|
||||
# 只保留后面的聊天记录
|
||||
chat_data_str = '\n'.join(lines[cut_index:])
|
||||
|
||||
# 解析原始聊天数据为列表
|
||||
chat_data = []
|
||||
for line in chat_data_str.splitlines():
|
||||
# 跳过空行
|
||||
if not line.strip():
|
||||
continue
|
||||
|
||||
# 分割每一行,确保有3部分(时间, 发信人, 内容)
|
||||
parts = line.split(',', 2)
|
||||
if len(parts) < 3:
|
||||
continue # 如果没有完整的三部分,跳过该行
|
||||
|
||||
timestamp, sender, content = parts
|
||||
chat_data.append((timestamp, sender, content))
|
||||
|
||||
# 其余代码保持不变
|
||||
if not chat_data:
|
||||
return "" # 如果没有有效数据,返回空字符串
|
||||
|
||||
compressed_data = []
|
||||
current_date = None
|
||||
prev_time_obj = None
|
||||
prev_sender = None
|
||||
prev_content = None
|
||||
|
||||
for timestamp, sender, content in chat_data:
|
||||
try:
|
||||
time_obj = datetime.strptime(timestamp.strip(), "%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# 去除无意义的空格
|
||||
content = re.sub(r"\s+", " ", content).strip()
|
||||
if not content:
|
||||
continue
|
||||
|
||||
# 检查日期是否变化(不包含年份)
|
||||
date_str = time_obj.strftime('%m-%d')
|
||||
|
||||
# 如果是第一条消息或日期变化,添加日期标记
|
||||
if current_date is None or date_str != current_date:
|
||||
current_date = date_str
|
||||
compressed_data.append(f"{date_str}")
|
||||
|
||||
# 检查是否需要合并消息
|
||||
if (prev_sender == sender and prev_time_obj is not None and
|
||||
(time_obj - prev_time_obj).total_seconds() <= time_threshold):
|
||||
# 合并消息,更新最后一条消息
|
||||
compressed_data[-1] = f"{prev_time_obj.strftime('%H:%M:%S')},{sender},{prev_content} {content}"
|
||||
else:
|
||||
# 添加新消息
|
||||
compressed_data.append(f"{time_obj.strftime('%H:%M:%S')},{sender},{content}")
|
||||
|
||||
# 更新前一条消息的信息
|
||||
prev_time_obj = time_obj
|
||||
prev_sender = sender
|
||||
prev_content = content
|
||||
|
||||
except ValueError as e:
|
||||
# 处理日期格式错误
|
||||
print(f"日期格式错误: {timestamp}, 错误: {e}")
|
||||
continue
|
||||
|
||||
# 返回压缩后的数据长字符串
|
||||
return '\n'.join(compressed_data)
|
||||
@@ -4,10 +4,10 @@ from typing import Dict, Any, Tuple, Optional, List
|
||||
import requests
|
||||
|
||||
from message_storage.message_to_db import MessageStorage
|
||||
from compress_chat_data import compress_chat_data
|
||||
from utils.compress_chat_data import compress_chat_data
|
||||
from plugin_common.message_plugin_interface import MessagePluginInterface
|
||||
from plugin_common.plugin_interface import PluginStatus
|
||||
from plugins.message_summary.markdown_to_image import convert_md_str_to_image
|
||||
from utils.markdown_to_image import convert_md_str_to_image
|
||||
from plugins.stats_collector.decorators import plugin_stats_decorator
|
||||
from robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
|
||||
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
import markdown
|
||||
from playwright.sync_api import sync_playwright
|
||||
import os
|
||||
|
||||
|
||||
# 将 Markdown 字符串转换为 HTML
|
||||
def md_str_to_html(md_content, output_html):
|
||||
"""
|
||||
将 Markdown 字符串转换为 HTML 文件,并添加支持中文和 Emoji 的样式。
|
||||
|
||||
:param md_content: 输入的 Markdown 字符串
|
||||
:param output_html: 输出的 HTML 文件路径
|
||||
"""
|
||||
# 转换 Markdown 为 HTML,启用额外功能(如表格、代码高亮)
|
||||
html_content = markdown.markdown(md_content, extensions=['extra', 'codehilite'])
|
||||
|
||||
# 添加基本的 HTML 结构和样式,支持中文和 Emoji
|
||||
css = """
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', 'Noto Sans CJK SC', 'Microsoft YaHei', sans-serif;
|
||||
padding: 20px;
|
||||
line-height: 1.6;
|
||||
max-width: 700px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
pre, code {
|
||||
background-color: #f5f5f5;
|
||||
padding: 10px;
|
||||
border-radius: 5px;
|
||||
font-family: 'Courier New', Courier, monospace;
|
||||
}
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
width: 100%;
|
||||
}
|
||||
th, td {
|
||||
border: 1px solid #ddd;
|
||||
padding: 8px;
|
||||
text-align: left;
|
||||
}
|
||||
th {
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
/* 确保 Emoji 正确渲染 */
|
||||
span, p, li {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', 'Noto Sans CJK SC', 'Microsoft YaHei', sans-serif;
|
||||
}
|
||||
</style>
|
||||
"""
|
||||
|
||||
# 写入 HTML 文件
|
||||
with open(output_html, 'w', encoding='utf-8') as f:
|
||||
f.write('<html><head>')
|
||||
f.write('<meta charset="UTF-8">') # 确保 UTF-8 编码
|
||||
f.write(css)
|
||||
f.write('</head><body>')
|
||||
f.write(html_content)
|
||||
f.write('</body></html>')
|
||||
|
||||
|
||||
# 使用 Playwright 将 HTML 渲染并截图
|
||||
def html_to_image(html_file, output_image):
|
||||
"""
|
||||
使用 Playwright 加载 HTML 文件并截图。
|
||||
|
||||
:param html_file: 输入的 HTML 文件路径
|
||||
:param output_image: 输出的图片文件路径
|
||||
"""
|
||||
with sync_playwright() as p:
|
||||
# TODO 这里使用的比较呆的固定路径,后期优化
|
||||
browser = p.chromium.launch(executable_path=r"C:\Users\Liu_WIN10\AppData\Local\Google\Chrome\Application\chrome.exe")
|
||||
page = browser.new_page()
|
||||
|
||||
# 加载本地的 HTML 文件
|
||||
page.goto(f'file://{os.path.abspath(html_file)}')
|
||||
|
||||
# 设置 viewport(可选,根据需要调整)
|
||||
page.set_viewport_size({"width": 900, "height": 700})
|
||||
|
||||
# 截图
|
||||
page.screenshot(path=output_image, full_page=True)
|
||||
|
||||
browser.close()
|
||||
|
||||
|
||||
# 主函数:从字符串转换 Markdown 到图片
|
||||
def convert_md_str_to_image(md_content, output_image):
|
||||
"""
|
||||
将 Markdown 字符串转换为图片。
|
||||
|
||||
:param md_content: 输入的 Markdown 字符串
|
||||
:param output_image: 输出的图片文件路径
|
||||
"""
|
||||
# 中间生成的 HTML 文件
|
||||
temp_html = 'temp_output.html'
|
||||
|
||||
# 第一步:将 Markdown 字符串转换为 HTML
|
||||
md_str_to_html(md_content, temp_html)
|
||||
|
||||
# 第二步:将 HTML 渲染为图片
|
||||
html_to_image(temp_html, output_image)
|
||||
|
||||
# 可选:删除临时的 HTML 文件
|
||||
os.remove(temp_html)
|
||||
print(f"图片已生成:{output_image}")
|
||||
return os.path.abspath(output_image)
|
||||
|
||||
# 示例使用
|
||||
if __name__ == "__main__":
|
||||
# 示例 Markdown 字符串(包含中文和 Emoji)
|
||||
md_content = """
|
||||
# 示例标题
|
||||
这是一个示例 Markdown 字符串,包含中文和 Emoji 😊🚀。
|
||||
|
||||
## 子标题
|
||||
- 项目 1:学习 Python 🐍
|
||||
- 项目 2:编写代码 💻
|
||||
|
||||
### 代码示例
|
||||
```python
|
||||
print("你好,世界!🌍")
|
||||
"""
|
||||
spath = convert_md_str_to_image(md_content, "output.png")
|
||||
print(spath)
|
||||
Reference in New Issue
Block a user