summaryrefslogtreecommitdiff
path: root/ATRI/plugins/tex.py
diff options
context:
space:
mode:
authorKyomotoi <[email protected]>2021-02-20 08:19:41 +0800
committerKyomotoi <[email protected]>2021-02-20 08:19:41 +0800
commitae7ea2d379ec7fb0edb8e333145bb141106a2fd2 (patch)
tree3243cb1dd668d36f92f17e958ff2a38e7b2c450c /ATRI/plugins/tex.py
parent3d16960b487f8911fb3817c2abcdb252ad848f58 (diff)
downloadATRI-ae7ea2d379ec7fb0edb8e333145bb141106a2fd2.tar.gz
ATRI-ae7ea2d379ec7fb0edb8e333145bb141106a2fd2.tar.bz2
ATRI-ae7ea2d379ec7fb0edb8e333145bb141106a2fd2.zip
✨ 更新插件,埋下bug
- 更新插件: - call-owner - code-runner - status - anime-search - tex(待修复) - 埋下bug: - service中limit作为机器人服务中的开关,目前写入文件亟待修复
Diffstat (limited to 'ATRI/plugins/tex.py')
-rw-r--r--ATRI/plugins/tex.py52
1 files changed, 52 insertions, 0 deletions
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())
+
+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
+
[email protected]("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)
+ )
+ )