From bd571e15773511c4979004c2c09edfe1ee1d3beb Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Mon, 3 Jun 2024 05:51:44 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8F=90=E4=BE=9B=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E8=BD=AC=E5=9B=BE=E7=89=87=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/command/command.py | 45 ++--- model/platform/qq_gocq.py | 4 +- model/platform/qq_official.py | 4 +- type/config.py | 2 +- util/general_utils.py | 17 +- util/image_render/helper.py | 43 +++++ util/image_render/template/base.html | 247 +++++++++++++++++++++++++++ 7 files changed, 322 insertions(+), 40 deletions(-) create mode 100644 util/image_render/helper.py create mode 100644 util/image_render/template/base.html diff --git a/model/command/command.py b/model/command/command.py index 750593cf5..88e5c39ff 100644 --- a/model/command/command.py +++ b/model/command/command.py @@ -11,6 +11,7 @@ from nakuru.entities.components import ( Image ) from util import general_utils as gu +from util.image_render.helper import text_to_image_base from model.provider.provider import Provider from util.cmd_config import CmdConfig as cc from type.message import * @@ -97,14 +98,14 @@ class Command: if self.command_start_with(message, "nick"): return True, self.set_nick(message, platform, role) if self.command_start_with(message, "plugin"): - return True, self.plugin_oper(message, role, self.global_object, platform) + return True, await self.plugin_oper(message, role, self.global_object, platform) if self.command_start_with(message, "myid") or self.command_start_with(message, "!myid"): return True, self.get_my_id(message_obj, platform) if self.command_start_with(message, "web"): # 网页搜索 return True, self.web_search(message) if self.command_start_with(message, "update"): return True, self.update(message, role) - if not self.provider and self.command_start_with(message, "help"): + if not self.provider and message == "help": return True, await self.help() return False, None @@ -141,12 +142,12 @@ class Command: 插件指令 ''' - def plugin_oper(self, message: str, role: str, ctx: GlobalObject, platform: str): + async def plugin_oper(self, message: str, role: str, ctx: GlobalObject, platform: str): l = message.split(" ") if len(l) < 2: - p = gu.create_text_image( - "【插件指令面板】", "安装插件: \nplugin i 插件Github地址\n卸载插件: \nplugin d 插件名 \n重载插件: \nplugin reload\n查看插件列表:\nplugin l\n更新插件: plugin u 插件名\n") - return True, [Image.fromFileSystem(p)], "plugin" + p = await text_to_image_base("# 插件指令面板 \n- 安装插件: `plugin i 插件Github地址`\n- 卸载插件: `plugin d 插件名`\n- 重载插件: `plugin reload`\n- 查看插件列表:`plugin l`\n - 更新插件: `plugin u 插件名`\n") + with open(p, 'rb') as f: + return True, [Image.fromBytes(f.read())], "plugin" else: if l[1] == "i": if role != "admin": @@ -174,10 +175,10 @@ class Command: try: plugin_list_info = "" for plugin in ctx.cached_plugins: - plugin_list_info += f"{plugin.metadata.plugin_name}: \n名称: {plugin.metadata.plugin_name}\n简介: {plugin.metadata.plugin_desc}\n版本: {plugin.metadata.version}\n作者: {plugin.metadata.author}\n" - p = gu.create_text_image( - "【已激活插件列表】", plugin_list_info + "\n使用plugin v 插件名 查看插件帮助\n") - return True, [Image.fromFileSystem(p)], "plugin" + plugin_list_info += f"### {plugin.metadata.plugin_name} \n- 名称: {plugin.metadata.plugin_name}\n- 简介: {plugin.metadata.desc}\n- 版本: {plugin.metadata.version}\n- 作者: {plugin.metadata.author}\n" + p = await text_to_image_base(f"# 已激活的插件\n{plugin_list_info}\n> 使用plugin v 插件名 查看插件帮助\n") + with open(p, 'rb') as f: + return True, [Image.fromBytes(f.read())], "plugin" except BaseException as e: return False, f"获取插件列表失败,原因: {str(e)}", "plugin" elif l[1] == "v": @@ -188,9 +189,9 @@ class Command: info = i.metadata break if info: - p = gu.create_text_image( - f"【插件信息】", f"名称: {info.plugin_name}\n类型: {info.plugin_type}\n{info.desc}\n版本: {info.version}\n作者: {info.author}") - return True, [Image.fromFileSystem(p)], "plugin" + p = await text_to_image_base(f"# `{info.plugin_name}` 插件信息\n- 类型: {info.plugin_type}\n- 简介{info.desc}\n- 版本: {info.version}\n- 作者: {info.author}") + with open(p, 'rb') as f: + return True, [Image.fromBytes(f.read())], "plugin" else: return False, "未找到该插件", "plugin" except BaseException as e: @@ -232,22 +233,24 @@ class Command: notice = (await resp.json())["notice"] except BaseException as e: notice = "" - msg = "# Help Center\n## 指令列表\n" + msg = "## 指令列表\n" for key, value in commands.items(): - msg += f"`{key}` - {value}\n" + msg += f"- `{key}`: {value}\n" # plugins - if cached_plugins != None: + print(cached_plugins) + if cached_plugins: plugin_list_info = "" for plugin in cached_plugins: - plugin_list_info += f"`{plugin.metadata.plugin_name}` {plugin.metadata.desc}\n" - if plugin_list_info.strip() != "": - msg += "\n## 插件列表\n> 使用plugin v 插件名 查看插件帮助\n" + plugin_list_info += f"- `{plugin.metadata.plugin_name}`: {plugin.metadata.desc}\n" + if plugin_list_info.strip(): + msg += "\n## 插件列表\n> 使用 plugin v 插件名 查看插件帮助\n" msg += plugin_list_info msg += notice try: - p = gu.create_markdown_image(msg) - return [Image.fromFileSystem(p),] + p = await text_to_image_base(msg) + with open(p, 'rb') as f: + return [Image.fromBytes(f.read()),] except BaseException as e: logger.error(str(e)) return msg diff --git a/model/platform/qq_gocq.py b/model/platform/qq_gocq.py index c7dc7dea8..779e7f2aa 100644 --- a/model/platform/qq_gocq.py +++ b/model/platform/qq_gocq.py @@ -1,5 +1,6 @@ from nakuru.entities.components import Plain, At, Image, Node from util import general_utils as gu +from util.image_render.helper import text_to_image_base from util.cmd_config import CmdConfig import asyncio from nakuru import ( @@ -213,7 +214,8 @@ class QQGOCQ(Platform): news.append(i) plains_str = "".join(plains).strip() if plains_str != "" and len(plains_str) > 50: - p = gu.create_markdown_image("".join(plains)) + # p = gu.create_markdown_image("".join(plains)) + p = await text_to_image_base(plains_str) news.append(Image.fromFileSystem(p)) res = news diff --git a/model/platform/qq_official.py b/model/platform/qq_official.py index d7e754412..4b01f6026 100644 --- a/model/platform/qq_official.py +++ b/model/platform/qq_official.py @@ -20,6 +20,7 @@ from ._message_parse import ( from type.message import * from typing import Union, List from nakuru.entities.components import BaseMessageComponent +from util.image_render.helper import text_to_image_base from SparkleLogging.utils.core import LogManager from logging import Logger @@ -183,7 +184,8 @@ class QQOfficial(Platform): else: plain_text += "\n\n" + \ "![](file:///" + image_path + ")" - image_path = gu.create_markdown_image("".join(plain_text)) + # image_path = gu.create_markdown_image("".join(plain_text)) + image_path = await text_to_image_base("".join(plain_text)) plain_text = "" else: diff --git a/type/config.py b/type/config.py index fc25b2026..261a1a1b3 100644 --- a/type/config.py +++ b/type/config.py @@ -1 +1 @@ -VERSION = '3.2.3' +VERSION = '3.2.4' diff --git a/util/general_utils.py b/util/general_utils.py index 33a632a57..824b79193 100644 --- a/util/general_utils.py +++ b/util/general_utils.py @@ -10,6 +10,7 @@ import json import sys import psutil import ssl +import base64 from PIL import Image, ImageDraw, ImageFont from type.types import GlobalObject @@ -443,21 +444,6 @@ def get_local_ip_addresses(): return ip -def get_sys_info(global_object: GlobalObject): - mem = None - stats = global_object.dashboard_data.stats - os_name = platform.system() - os_version = platform.version() - - if 'sys_perf' in stats and 'memory' in stats['sys_perf']: - mem = stats['sys_perf']['memory'] - return { - 'mem': mem, - 'os': os_name + '_' + os_version, - 'py': platform.python_version(), - } - - def upload(_global_object: GlobalObject): ''' 上传相关非敏感统计数据 @@ -520,7 +506,6 @@ def retry(n: int = 3): return wrapper return decorator - def run_monitor(global_object: GlobalObject): ''' 监测机器性能 diff --git a/util/image_render/helper.py b/util/image_render/helper.py new file mode 100644 index 000000000..27f55c971 --- /dev/null +++ b/util/image_render/helper.py @@ -0,0 +1,43 @@ +import aiohttp, os +from util.general_utils import download_image_by_url, create_markdown_image +from type.config import VERSION + +BASE_RENDER_URL = "https://t2i.soulter.top/text2img" +TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), "template") + +async def text_to_image_base(text: str, return_url: bool = False) -> str: + ''' + 返回图像的文件路径 + ''' + with open(os.path.join(TEMPLATE_PATH, "base.html"), "r") as f: + tmpl_str = f.read() + + assert(tmpl_str) + + text = text.replace("`", "\`") + + post_data = { + "tmpl": tmpl_str, + "json": return_url, + "tmpldata": { + "text": text, + "version": f"v{VERSION}" + }, + "options": { + "full_page": True + } + } + + if return_url: + async with aiohttp.ClientSession() as session: + async with session.post(f"{BASE_RENDER_URL}/generate", json=post_data) as resp: + ret = await resp.json() + return f"{BASE_RENDER_URL}/{ret['data']['id']}" + else: + image_path = "" + try: + image_path = await download_image_by_url(f"{BASE_RENDER_URL}/generate", post=True, post_data=post_data) + except Exception as e: + print(f"调用 markdown 渲染 API 失败,错误信息:{e},将使用本地渲染方式。") + image_path = create_markdown_image(text) + return image_path \ No newline at end of file diff --git a/util/image_render/template/base.html b/util/image_render/template/base.html new file mode 100644 index 000000000..9d871ae72 --- /dev/null +++ b/util/image_render/template/base.html @@ -0,0 +1,247 @@ + + + + + + + + + + + + +
+ # AstrBot + {{ version }} +
+
+ + + + + + \ No newline at end of file