2024-03-21 01:20:18 +08:00
|
|
|
|
import os.path
|
2024-03-21 12:10:24 +08:00
|
|
|
|
import sys
|
|
|
|
|
from io import StringIO
|
2024-03-21 01:20:18 +08:00
|
|
|
|
|
2024-03-21 14:52:02 +08:00
|
|
|
|
import aiohttp
|
2024-03-21 01:20:18 +08:00
|
|
|
|
import nonebot
|
2024-03-21 14:52:02 +08:00
|
|
|
|
import pip
|
2024-03-21 01:20:18 +08:00
|
|
|
|
from arclet.alconna import Arparma, MultiVar
|
2024-03-24 22:04:51 +08:00
|
|
|
|
from nonebot import require
|
2024-03-21 01:20:18 +08:00
|
|
|
|
from nonebot.permission import SUPERUSER
|
2024-03-24 09:43:34 +08:00
|
|
|
|
from liteyuki.utils.language import get_user_lang
|
|
|
|
|
from liteyuki.utils.ly_typing import T_Bot
|
2024-03-31 06:22:53 +08:00
|
|
|
|
from liteyuki.utils.message import Markdown as md
|
2024-03-21 12:10:24 +08:00
|
|
|
|
from .common import *
|
|
|
|
|
|
2024-03-24 22:04:51 +08:00
|
|
|
|
require("nonebot_plugin_alconna")
|
|
|
|
|
from nonebot_plugin_alconna import Alconna, Args, Subcommand, on_alconna
|
|
|
|
|
|
2024-03-21 01:20:18 +08:00
|
|
|
|
npm_alc = on_alconna(
|
|
|
|
|
Alconna(
|
2024-03-22 07:44:41 +08:00
|
|
|
|
["npm", "插件"],
|
2024-03-21 01:20:18 +08:00
|
|
|
|
Subcommand(
|
|
|
|
|
"update",
|
|
|
|
|
alias=["u"],
|
|
|
|
|
),
|
|
|
|
|
Subcommand(
|
|
|
|
|
"search",
|
|
|
|
|
Args["keywords", MultiVar(str)]["page", int, 1],
|
2024-03-21 12:10:24 +08:00
|
|
|
|
alias=["s", "搜索"],
|
2024-03-21 01:20:18 +08:00
|
|
|
|
),
|
|
|
|
|
Subcommand(
|
|
|
|
|
"install",
|
|
|
|
|
Args["plugin_name", str],
|
2024-03-21 12:10:24 +08:00
|
|
|
|
alias=["i", "安装"],
|
2024-03-21 01:20:18 +08:00
|
|
|
|
),
|
|
|
|
|
Subcommand(
|
2024-03-23 19:55:12 +08:00
|
|
|
|
"uninstall",
|
2024-03-21 01:20:18 +08:00
|
|
|
|
Args["plugin_name", str],
|
2024-03-21 12:10:24 +08:00
|
|
|
|
alias=["rm", "移除", "卸载"],
|
2024-03-21 01:20:18 +08:00
|
|
|
|
),
|
2024-03-22 07:44:41 +08:00
|
|
|
|
Subcommand(
|
|
|
|
|
"list",
|
|
|
|
|
alias=["l", "ls", "列表"],
|
|
|
|
|
)
|
2024-03-21 01:20:18 +08:00
|
|
|
|
),
|
|
|
|
|
permission=SUPERUSER
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@npm_alc.handle()
|
|
|
|
|
async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
|
|
|
|
|
ulang = get_user_lang(str(event.user_id))
|
|
|
|
|
|
|
|
|
|
if not os.path.exists("data/liteyuki/plugins.json"):
|
2024-03-22 12:41:38 +08:00
|
|
|
|
await npm_update()
|
2024-03-21 01:20:18 +08:00
|
|
|
|
|
|
|
|
|
if result.subcommands.get("update"):
|
|
|
|
|
r = await npm_update()
|
|
|
|
|
if r:
|
|
|
|
|
await npm_alc.finish(ulang.get("npm.store_update_success"))
|
|
|
|
|
else:
|
|
|
|
|
await npm_alc.finish(ulang.get("npm.store_update_failed"))
|
|
|
|
|
|
|
|
|
|
elif result.subcommands.get("search"):
|
|
|
|
|
keywords: list[str] = result.subcommands["search"].args.get("keywords")
|
|
|
|
|
rs = await npm_search(keywords)
|
2024-03-21 12:18:15 +08:00
|
|
|
|
max_show = 10
|
2024-03-21 01:20:18 +08:00
|
|
|
|
if len(rs):
|
|
|
|
|
reply = f"{ulang.get('npm.search_result')} | {ulang.get('npm.total', TOTAL=len(rs))}\n***"
|
2024-03-21 12:10:24 +08:00
|
|
|
|
for plugin in rs[:min(max_show, len(rs))]:
|
2024-03-26 17:14:41 +08:00
|
|
|
|
btn_install = md.button(ulang.get("npm.install"), "npm install %s" % plugin.module_name)
|
|
|
|
|
link_page = md.link(ulang.get("npm.homepage"), plugin.homepage)
|
|
|
|
|
link_pypi = md.link(ulang.get("npm.pypi"), plugin.homepage)
|
2024-03-21 12:10:24 +08:00
|
|
|
|
|
2024-03-22 07:44:41 +08:00
|
|
|
|
reply += (f"\n# **{plugin.name}**\n"
|
|
|
|
|
f"\n> **{plugin.desc}**\n"
|
|
|
|
|
f"\n> {ulang.get('npm.author')}: {plugin.author}"
|
|
|
|
|
f"\n> *{md.escape(plugin.module_name)}*"
|
|
|
|
|
f"\n> {btn_install} {link_page} {link_pypi}\n\n***\n")
|
2024-03-21 12:10:24 +08:00
|
|
|
|
if len(rs) > max_show:
|
2024-03-21 14:52:02 +08:00
|
|
|
|
reply += f"\n{ulang.get('npm.too_many_results', HIDE_NUM=len(rs) - max_show)}"
|
2024-03-21 01:20:18 +08:00
|
|
|
|
else:
|
|
|
|
|
reply = ulang.get("npm.search_no_result")
|
2024-03-31 06:22:53 +08:00
|
|
|
|
await md.send_md(reply, bot, event=event)
|
2024-03-21 01:20:18 +08:00
|
|
|
|
|
2024-03-21 12:10:24 +08:00
|
|
|
|
elif result.subcommands.get("install"):
|
2024-03-21 14:52:02 +08:00
|
|
|
|
plugin_module_name: str = result.subcommands["install"].args.get("plugin_name")
|
2024-03-22 07:44:41 +08:00
|
|
|
|
store_plugin = await get_store_plugin(plugin_module_name)
|
2024-03-21 14:52:02 +08:00
|
|
|
|
await npm_alc.send(ulang.get("npm.installing", NAME=plugin_module_name))
|
|
|
|
|
r, log = npm_install(plugin_module_name)
|
|
|
|
|
log = log.replace("\\", "/")
|
2024-03-22 07:44:41 +08:00
|
|
|
|
|
|
|
|
|
if not store_plugin:
|
|
|
|
|
await npm_alc.finish(ulang.get("npm.plugin_not_found", NAME=plugin_module_name))
|
|
|
|
|
|
2024-03-26 17:14:41 +08:00
|
|
|
|
homepage_btn = md.button(ulang.get("npm.homepage"), store_plugin.homepage)
|
2024-03-21 12:10:24 +08:00
|
|
|
|
if r:
|
2024-03-22 07:44:41 +08:00
|
|
|
|
|
|
|
|
|
r_load = nonebot.load_plugin(plugin_module_name) # 加载插件
|
2024-03-21 14:52:02 +08:00
|
|
|
|
installed_plugin = InstalledPlugin(module_name=plugin_module_name) # 构造插件信息模型
|
2024-03-26 21:33:40 +08:00
|
|
|
|
found_in_db_plugin = plugin_db.first(InstalledPlugin(), "module_name = ?", plugin_module_name) # 查询数据库中是否已经安装
|
2024-03-22 07:44:41 +08:00
|
|
|
|
|
|
|
|
|
if r_load:
|
|
|
|
|
if found_in_db_plugin is None:
|
2024-03-24 09:43:34 +08:00
|
|
|
|
plugin_db.upsert(installed_plugin)
|
2024-03-26 17:14:41 +08:00
|
|
|
|
info = md.escape(ulang.get("npm.install_success", NAME=store_plugin.name)) # markdown转义
|
2024-03-31 06:22:53 +08:00
|
|
|
|
await md.send_md(
|
2024-03-22 07:44:41 +08:00
|
|
|
|
f"{info}\n\n"
|
|
|
|
|
f"```\n{log}\n```",
|
|
|
|
|
bot,
|
|
|
|
|
event=event
|
|
|
|
|
)
|
|
|
|
|
else:
|
2024-03-26 17:14:41 +08:00
|
|
|
|
await npm_alc.finish(ulang.get("npm.plugin_already_installed", NAME=store_plugin.name))
|
2024-03-22 07:44:41 +08:00
|
|
|
|
else:
|
2024-03-26 17:14:41 +08:00
|
|
|
|
info = ulang.get("npm.load_failed", NAME=plugin_module_name, HOMEPAGE=homepage_btn).replace("_", r"\\_")
|
2024-03-31 06:22:53 +08:00
|
|
|
|
await md.send_md(
|
2024-03-21 14:52:02 +08:00
|
|
|
|
f"{info}\n\n"
|
2024-03-22 07:44:41 +08:00
|
|
|
|
f"```\n{log}\n```\n",
|
2024-03-21 14:52:02 +08:00
|
|
|
|
bot,
|
|
|
|
|
event=event
|
|
|
|
|
)
|
2024-03-21 12:10:24 +08:00
|
|
|
|
else:
|
2024-03-26 17:14:41 +08:00
|
|
|
|
info = ulang.get("npm.install_failed", NAME=plugin_module_name, HOMEPAGE=homepage_btn).replace("_", r"\\_")
|
2024-03-31 06:22:53 +08:00
|
|
|
|
await md.send_md(
|
2024-03-21 14:52:02 +08:00
|
|
|
|
f"{info}\n\n"
|
2024-03-21 12:10:24 +08:00
|
|
|
|
f"```\n{log}\n```",
|
|
|
|
|
bot,
|
|
|
|
|
event=event
|
|
|
|
|
)
|
|
|
|
|
|
2024-03-23 19:55:12 +08:00
|
|
|
|
elif result.subcommands.get("uninstall"):
|
|
|
|
|
plugin_module_name: str = result.subcommands["uninstall"].args.get("plugin_name")
|
2024-03-26 21:33:40 +08:00
|
|
|
|
found_installed_plugin: InstalledPlugin = plugin_db.first(InstalledPlugin(), "module_name = ?", plugin_module_name)
|
2024-03-21 14:52:02 +08:00
|
|
|
|
if found_installed_plugin:
|
2024-03-29 00:05:46 +08:00
|
|
|
|
plugin_db.delete(InstalledPlugin(), "module_name = ?", plugin_module_name)
|
2024-03-23 19:55:12 +08:00
|
|
|
|
reply = f"{ulang.get('npm.uninstall_success', NAME=found_installed_plugin.module_name)}"
|
2024-03-21 14:52:02 +08:00
|
|
|
|
await npm_alc.finish(reply)
|
|
|
|
|
else:
|
|
|
|
|
await npm_alc.finish(ulang.get("npm.plugin_not_installed", NAME=plugin_module_name))
|
2024-03-21 12:10:24 +08:00
|
|
|
|
|
2024-03-21 01:20:18 +08:00
|
|
|
|
|
|
|
|
|
async def npm_update() -> bool:
|
|
|
|
|
"""
|
|
|
|
|
更新本地插件json缓存
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
bool: 是否成功更新
|
|
|
|
|
"""
|
|
|
|
|
url_list = [
|
|
|
|
|
"https://registry.nonebot.dev/plugins.json",
|
|
|
|
|
]
|
|
|
|
|
for url in url_list:
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
|
async with session.get(url) as resp:
|
|
|
|
|
if resp.status == 200:
|
|
|
|
|
async with aiofiles.open("data/liteyuki/plugins.json", "wb") as f:
|
|
|
|
|
data = await resp.read()
|
|
|
|
|
await f.write(data)
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def npm_search(keywords: list[str]) -> list[StorePlugin]:
|
|
|
|
|
"""
|
|
|
|
|
搜索插件
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
keywords (list[str]): 关键词列表
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
list[StorePlugin]: 插件列表
|
|
|
|
|
"""
|
|
|
|
|
results = []
|
|
|
|
|
async with aiofiles.open("data/liteyuki/plugins.json", "r", encoding="utf-8") as f:
|
|
|
|
|
plugins: list[StorePlugin] = [StorePlugin(**pobj) for pobj in json.loads(await f.read())]
|
|
|
|
|
for plugin in plugins:
|
|
|
|
|
plugin_text = ' '.join(
|
|
|
|
|
[
|
|
|
|
|
plugin.name,
|
|
|
|
|
plugin.desc,
|
|
|
|
|
plugin.author,
|
|
|
|
|
plugin.module_name,
|
|
|
|
|
' '.join([tag.label for tag in plugin.tags])
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
if all([keyword in plugin_text for keyword in keywords]):
|
|
|
|
|
results.append(plugin)
|
|
|
|
|
return results
|
|
|
|
|
|
2024-03-20 22:30:52 +08:00
|
|
|
|
|
2024-03-21 12:10:24 +08:00
|
|
|
|
def npm_install(plugin_module_name) -> tuple[bool, str]:
|
|
|
|
|
"""
|
|
|
|
|
Args:
|
|
|
|
|
plugin_module_name:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
tuple[bool, str]:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
buffer = StringIO()
|
|
|
|
|
sys.stdout = buffer
|
|
|
|
|
sys.stderr = buffer
|
|
|
|
|
|
|
|
|
|
mirrors = [
|
2024-03-21 14:52:02 +08:00
|
|
|
|
"https://pypi.tuna.tsinghua.edu.cn/simple", # 清华大学
|
2024-03-24 09:43:34 +08:00
|
|
|
|
"https://pypi.mirrors.cqupt.edu.cn/simple", # 重庆邮电大学
|
2024-03-21 14:52:02 +08:00
|
|
|
|
"https://pypi.liteyuki.icu/simple", # 轻雪镜像
|
|
|
|
|
"https://pypi.org/simple", # 官方源
|
2024-03-21 12:10:24 +08:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# 使用pip安装包,对每个镜像尝试一次,成功后返回值
|
|
|
|
|
success = False
|
|
|
|
|
for mirror in mirrors:
|
|
|
|
|
try:
|
2024-03-21 14:52:02 +08:00
|
|
|
|
nonebot.logger.info(f"npm_install try mirror: {mirror}")
|
2024-03-26 17:14:41 +08:00
|
|
|
|
result = pip.main(["install", plugin_module_name, "-i", mirror])
|
2024-03-21 12:10:24 +08:00
|
|
|
|
success = result == 0
|
2024-03-21 14:52:02 +08:00
|
|
|
|
if success:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
nonebot.logger.warning(f"npm_install failed, try next mirror.")
|
2024-03-21 12:10:24 +08:00
|
|
|
|
except Exception as e:
|
2024-03-21 14:52:02 +08:00
|
|
|
|
|
2024-03-21 12:10:24 +08:00
|
|
|
|
success = False
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
sys.stdout = sys.__stdout__
|
|
|
|
|
sys.stderr = sys.__stderr__
|
|
|
|
|
|
|
|
|
|
return success, buffer.getvalue()
|