From ae7ea2d379ec7fb0edb8e333145bb141106a2fd2 Mon Sep 17 00:00:00 2001 From: Kyomotoi <1172294279@qq.com> Date: Sat, 20 Feb 2021 08:19:41 +0800 Subject: =?UTF-8?q?=E2=9C=A8=20=E6=9B=B4=E6=96=B0=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E5=9F=8B=E4=B8=8Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新插件: - call-owner - code-runner - status - anime-search - tex(待修复) - 埋下bug: - service中limit作为机器人服务中的开关,目前写入文件亟待修复 --- ATRI/plugins/tex.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 ATRI/plugins/tex.py (limited to 'ATRI/plugins/tex.py') diff --git a/ATRI/plugins/tex.py b/ATRI/plugins/tex.py new file mode 100644 index 0000000..01eabf7 --- /dev/null +++ b/ATRI/plugins/tex.py @@ -0,0 +1,52 @@ +import re +from urllib.parse import quote_plus +from nonebot.adapters.cqhttp.message import Message, MessageSegment + +from nonebot.plugin import on_command +from nonebot.adapters.cqhttp import Bot, MessageEvent +from nonebot.typing import T_State + +from ATRI.exceptions import RequestTimeOut +from ATRI.rule import is_in_banlist, is_in_dormant +from ATRI.utils.request import post_bytes + + +ZHIHU_TEX_SVG_URL_FORMAT = 'https://www.zhihu.com/equation?tex=' +LATEX2PNG_API_URL = 'http://latex2png.com/' +LATEX2PNG_IMAGE_URL_FORMAT = 'http://latex2png.com/output//' + + +tex = on_command("/tex", rule=is_in_banlist() & is_in_dormant()) + +@tex.handle() +async def _tex(bot: Bot, event: MessageEvent, state: T_State) -> None: + tex_code = str(event.message).strip() + if tex_code: + state["tex_code"] = tex_code + +@tex.got("tex_code", prompt="请告诉咱需要生成图片的的Tex公式") +async def __tex(bot: Bot, event: MessageEvent, state: T_State) -> None: + tex_code = state["tex_code"] + try: + req = await post_bytes( + LATEX2PNG_API_URL, + params={ + "latex": tex_code, + "res": "600", + "color": "000000" + } + ) + except RequestTimeOut: + raise RequestTimeOut("Failed to request!") + + html = str(req) + m = re.search(r"latex_[0-9a-z]+\.png", html) + if not m: + await tex.finish("生成失败,等会再试试吧...") + + await tex.finish( + Message( + MessageSegment.image(LATEX2PNG_IMAGE_URL_FORMAT + m.group(0)) + + "\n" + ZHIHU_TEX_SVG_URL_FORMAT + quote_plus(tex_code) + ) + ) -- cgit v1.2.3