summaryrefslogtreecommitdiff
path: root/ATRI/plugins/code_runner/data_source.py
diff options
context:
space:
mode:
authorKyomotoi <[email protected]>2021-07-08 22:09:00 +0800
committerKyomotoi <[email protected]>2021-07-08 22:09:00 +0800
commitbe2747e4d4b820ca0f1f988d3b77a628da26fe7b (patch)
treee1a59dd79ecd973a7d704568dcdc018f1f1b651a /ATRI/plugins/code_runner/data_source.py
parenta4e1b9d1581d756ef79ad063d1c0bd6b2fd13c1d (diff)
downloadATRI-be2747e4d4b820ca0f1f988d3b77a628da26fe7b.tar.gz
ATRI-be2747e4d4b820ca0f1f988d3b77a628da26fe7b.tar.bz2
ATRI-be2747e4d4b820ca0f1f988d3b77a628da26fe7b.zip
🔖♻️🐛🔧🔥📝 更新版本:YHN-001-A03
🔖 更新版本至:YHN-001-A03 ✨ 新增插件: - 涩图 - 闲聊(文爱 ♻️ 重构: - Service - 所有插件 🐛 修复部分小bug 🔧 暂时移除部分设置 🔥 删除: - 插件:nsfw、wife。日后加回 - 插件 essential 中部分内容 📝 更新README
Diffstat (limited to 'ATRI/plugins/code_runner/data_source.py')
-rw-r--r--ATRI/plugins/code_runner/data_source.py111
1 files changed, 111 insertions, 0 deletions
diff --git a/ATRI/plugins/code_runner/data_source.py b/ATRI/plugins/code_runner/data_source.py
new file mode 100644
index 0000000..bbe0d2e
--- /dev/null
+++ b/ATRI/plugins/code_runner/data_source.py
@@ -0,0 +1,111 @@
+from ATRI.rule import is_in_service
+from ATRI.service import Service
+from ATRI.utils import request
+from ATRI.exceptions import RequestError
+
+
+RUN_API_URL_FORMAT = "https://glot.io/run/{}?version=latest"
+SUPPORTED_LANGUAGES = {
+ "assembly": {"ext": "asm"},
+ "bash": {"ext": "sh"},
+ "c": {"ext": "c"},
+ "clojure": {"ext": "clj"},
+ "coffeescript": {"ext": "coffe"},
+ "cpp": {"ext": "cpp"},
+ "csharp": {"ext": "cs"},
+ "erlang": {"ext": "erl"},
+ "fsharp": {"ext": "fs"},
+ "go": {"ext": "go"},
+ "groovy": {"ext": "groovy"},
+ "haskell": {"ext": "hs"},
+ "java": {"ext": "java", "name": "Main"},
+ "javascript": {"ext": "js"},
+ "julia": {"ext": "jl"},
+ "kotlin": {"ext": "kt"},
+ "lua": {"ext": "lua"},
+ "perl": {"ext": "pl"},
+ "php": {"ext": "php"},
+ "python": {"ext": "py"},
+ "ruby": {"ext": "rb"},
+ "rust": {"ext": "rs"},
+ "scala": {"ext": "scala"},
+ "swift": {"ext": "swift"},
+ "typescript": {"ext": "ts"},
+}
+
+
+__doc__ = """
+在线跑代码
+"""
+
+
+class CodeRunner(Service):
+
+ def __init__(self):
+ Service.__init__(self, "在线跑代码", __doc__, rule=is_in_service("在线跑代码"))
+
+ @staticmethod
+ def help() -> str:
+ return (
+ "/code {语言}\n"
+ "{代码}\n"
+ "For example:\n"
+ "/code python\n"
+ "print('hello world')"
+ )
+
+ @staticmethod
+ def list_supp_lang() -> str:
+ msg0 = "咱现在支持的语言如下:\n"
+ msg0 += ", ".join(map(str, SUPPORTED_LANGUAGES.keys()))
+ return msg0
+
+ @staticmethod
+ async def runner(msg: str):
+ args = msg.split("\n")
+ if not args:
+ return "请检查键入内容..."
+
+ lang = args[0].replace("\r", "")
+ if lang not in SUPPORTED_LANGUAGES:
+ return "该语言暂不支持..."
+
+ del args[0]
+ code = "\n".join(map(str, args))
+ url = RUN_API_URL_FORMAT.format(lang)
+ js = {
+ "files": [
+ {
+ "name": (
+ SUPPORTED_LANGUAGES[lang].get("name", "main")
+ + f".{SUPPORTED_LANGUAGES[lang]['ext']}"
+ ),
+ "content": code,
+ }
+ ],
+ "stdin": "",
+ "command": ""
+ }
+
+ try:
+ res = await request.post(url, json=js)
+ except RequestError:
+ raise RequestError("Request failed!")
+
+ payload = await res.json()
+ sent = False
+ for k in ["stdout", "stderr", "error"]:
+ v = payload.get(k)
+ lines = v.splitlines()
+ lines, remained_lines = lines[:10], lines[10:]
+ out = "\n".join(lines)
+ out, remained_out = out[: 60 * 10], out[60 * 10 :]
+
+ if remained_lines or remained_out:
+ out += f"\n(太多了太多了...)"
+
+ if out:
+ return f"\n{k}:\n{out}"
+
+ if not sent:
+ return "\n运行完成,没任何输出呢..." \ No newline at end of file