✨ 资源下载分离 (#1814)

* 🚀 资源文件单独下载,分离被动任务初始化

* 🔥 移除resources文件夹

* 🚀 优化版本更新逻辑,增加资源下载异常处理

* 🚀 增加资源下载失败异常处理及提示信息

* 🚀 增加资源下载选项及异常处理优化

* 🚀 移除空ID检查及资源更新条件处理

* ✨ 简易配置中未加载的配置将被移除

* 🐛 测试更新

* ✨ 修复更新完成消息格式

---------

Co-authored-by: Flern <cb56ec362bbbfb5272eb941281299d8d-qq_connect@git.osc>
This commit is contained in:
HibiKier
2025-01-06 11:32:56 +08:00
committed by GitHub
co-authored by Flern
parent 91dce56bf8
commit ec70144d7e
308 changed files with 397 additions and 38881 deletions
+11 -3
View File
@@ -16,6 +16,7 @@ from zhenxun.models.sign_user import SignUser
from zhenxun.models.user_console import UserConsole
from zhenxun.services.log import logger
from zhenxun.utils.decorator.shop import shop_register
from zhenxun.utils.manager.resource_manager import ResourceManager
from zhenxun.utils.platform import PlatformUtils
driver: Driver = nonebot.get_driver()
@@ -36,9 +37,15 @@ async def _(bot: Bot):
@driver.on_bot_disconnect
async def _(bot: Bot):
logger.debug(f"Bot: {bot.self_id} 断开连接...")
await BotConnectLog.create(
bot_id=bot.self_id, platform=bot.adapter, connect_time=datetime.now(), type=0
)
try:
await BotConnectLog.create(
bot_id=bot.self_id,
platform=bot.adapter,
connect_time=datetime.now(),
type=0,
)
except Exception as e:
logger.warning(f"记录bot: {bot.self_id} 断开连接失败", e=e)
SIGN_SQL = """
@@ -65,6 +72,7 @@ from public.bag_users t1
@driver.on_startup
async def _():
await ResourceManager.init_resources()
"""签到与用户的数据迁移"""
if goods_list := await GoodsInfo.filter(uuid__isnull=True).all():
for goods in goods_list:
+57 -23
View File
@@ -2,12 +2,24 @@ from nonebot.adapters import Bot
from nonebot.permission import SUPERUSER
from nonebot.plugin import PluginMetadata
from nonebot.rule import to_me
from nonebot_plugin_alconna import Alconna, Args, Match, on_alconna
from nonebot_plugin_alconna import (
Alconna,
Args,
Match,
Option,
Query,
on_alconna,
store_true,
)
from nonebot_plugin_session import EventSession
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
from zhenxun.configs.utils import PluginExtraData
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType
from zhenxun.utils.manager.resource_manager import (
DownloadResourceException,
ResourceManager,
)
from zhenxun.utils.message import MessageUtils
from ._data_source import UpdateManage
@@ -18,26 +30,32 @@ __plugin_meta__ = PluginMetadata(
usage="""
usage:
检查更新真寻最新版本,包括了自动更新
资源文件大小一般在130mb左右,除非必须更新一般仅更新代码文件
指令:
检查更新真寻
检查更新 [main|release|resource] ?[-r]
main: main分支
release: 最新release
resource: 资源文件
-r: 下载资源文件,一般在更新main或release时使用
示例:
检查更新 main
检查更新 main -r
检查更新 release -r
检查更新 resource
""".strip(),
extra=PluginExtraData(
author="HibiKier",
version="0.1",
plugin_type=PluginType.SUPERUSER,
configs=[
RegisterConfig(
key="UPDATE_REMIND",
value=True,
help="是否检测更新版本",
default_value=True,
),
],
).dict(),
)
_matcher = on_alconna(
Alconna("检查更新", Args["ver_type?", ["main", "dev", "release"]]),
Alconna(
"检查更新",
Args["ver_type?", ["main", "release", "resource"]],
Option("-r|--resource", action=store_true, help_text="下载资源文件"),
),
priority=1,
block=True,
permission=SUPERUSER,
@@ -46,18 +64,34 @@ _matcher = on_alconna(
@_matcher.handle()
async def _(bot: Bot, session: EventSession, ver_type: Match[str]):
async def _(
bot: Bot,
session: EventSession,
ver_type: Match[str],
resource: Query[bool] = Query("resource", False),
):
if not session.id1:
await MessageUtils.build_message("用户id为空...").finish()
if not ver_type.available:
result = await UpdateManage.check_version()
logger.info("查看当前版本...", "检查更新", session=session)
await MessageUtils.build_message(result).finish()
try:
result = await UpdateManage.update(bot, session.id1, ver_type.result)
except Exception as e:
logger.error("版本更新失败...", "检查更新", session=session, e=e)
await MessageUtils.build_message(f"更新版本失败...e: {e}").finish()
result = ""
if ver_type.result in {"main", "release"}:
if not ver_type.available:
result = await UpdateManage.check_version()
logger.info("查看当前版本...", "检查更新", session=session)
await MessageUtils.build_message(result).finish()
try:
result = await UpdateManage.update(bot, session.id1, ver_type.result)
except Exception as e:
logger.error("版本更新失败...", "检查更新", session=session, e=e)
await MessageUtils.build_message(f"更新版本失败...e: {e}").finish()
if resource.result or ver_type.result == "resource":
try:
await ResourceManager.init_resources(True)
result += "\n资源文件更新成功!"
except DownloadResourceException:
result += "\n资源更新下载失败..."
except Exception as e:
logger.error("资源更新下载失败...", "检查更新", session=session, e=e)
result += "\n资源更新未知错误..."
if result:
await MessageUtils.build_message(result).finish()
await MessageUtils.build_message(result.strip()).finish()
await MessageUtils.build_message("更新版本失败...").finish()
@@ -155,7 +155,7 @@ class UpdateManage:
)
@classmethod
async def update(cls, bot: Bot, user_id: str, version_type: str) -> str | None:
async def update(cls, bot: Bot, user_id: str, version_type: str) -> str:
"""更新操作
参数:
@@ -171,7 +171,7 @@ class UpdateManage:
url = None
new_version = None
repo_info = GithubUtils.parse_github_url(DEFAULT_GITHUB_URL)
if version_type in {"dev", "main"}:
if version_type in {"main"}:
repo_info.branch = version_type
new_version = await cls.__get_version_from_repo(repo_info)
if new_version:
@@ -203,14 +203,15 @@ class UpdateManage:
if await AsyncHttpx.download_file(url, download_file, stream=True):
logger.debug("下载真寻最新版文件完成...", "检查更新")
await _file_handle(new_version)
result = "版本更新完成"
return (
f"版本更新完成\n"
f"{result}\n"
f"版本: {cur_version} -> {new_version}\n"
"请重新启动真寻以完成更新!"
)
else:
logger.debug("下载真寻最新版文件失败...", "检查更新")
return None
return ""
@classmethod
def __get_version(cls) -> str:
+10 -5
View File
@@ -25,7 +25,7 @@ if old_config_file.exists():
old_config_file.rename(SIMPLE_CONFIG_FILE)
def _handle_config(plugin: Plugin):
def _handle_config(plugin: Plugin, exists_module: list[str]):
"""处理配置项
参数:
@@ -49,9 +49,10 @@ def _handle_config(plugin: Plugin):
arg_parser=reg_config.arg_parser,
_override=False,
)
exists_module.append(f"{module}:{reg_config.key}")
def _generate_simple_config():
def _generate_simple_config(exists_module: list[str]):
"""
生成简易配置
@@ -70,9 +71,12 @@ def _generate_simple_config():
try:
if _data.get(module) and k in _data[module].keys():
Config.set_config(module, k, _data[module][k])
_tmp_data[module][k] = Config.get_config(module, k)
if f"{module}:{k}" in exists_module:
_tmp_data[module][k] = Config.get_config(module, k)
except AttributeError as e:
raise AttributeError(f"{e}\n可能为config.yaml配置文件填写不规范") from e
if not _tmp_data[module]:
_tmp_data.pop(module)
Config.save()
temp_file = DATA_PATH / "temp_config.yaml"
# 重新生成简易配置文件
@@ -103,9 +107,10 @@ def _():
初始化插件数据配置
"""
plugins2config_file = DATA_PATH / "configs" / "plugins2config.yaml"
exists_module = []
for plugin in get_loaded_plugins():
if plugin.metadata:
_handle_config(plugin)
_handle_config(plugin, exists_module)
if not Config.is_empty():
Config.save()
_data: CommentedMap = _yaml.load(plugins2config_file.open(encoding="utf8"))
@@ -118,4 +123,4 @@ def _():
# 存完插件基本设置
with plugins2config_file.open("w", encoding="utf8") as wf:
_yaml.dump(_data, wf)
_generate_simple_config()
_generate_simple_config(exists_module)
+1 -44
View File
@@ -34,7 +34,6 @@ async def _handle_setting(
plugin: Plugin,
plugin_list: list[PluginInfo],
limit_list: list[PluginLimit],
task_list: list[tuple[bool, TaskInfo]],
):
"""处理插件设置
@@ -91,20 +90,6 @@ async def _handle_setting(
)
for limit in extra_data.limits
)
if extra_data.tasks:
task_list.extend(
(
task.create_status,
TaskInfo(
module=task.module,
name=task.name,
status=task.status,
run_time=task.run_time,
default_status=task.default_status,
),
)
for task in extra_data.tasks
)
@driver.on_startup
@@ -114,14 +99,13 @@ async def _():
"""
plugin_list: list[PluginInfo] = []
limit_list: list[PluginLimit] = []
task_list = []
module2id = {}
load_plugin = []
if module_list := await PluginInfo.all().values("id", "module_path"):
module2id = {m["module_path"]: m["id"] for m in module_list}
for plugin in get_loaded_plugins():
load_plugin.append(plugin.module_name)
await _handle_setting(plugin, plugin_list, limit_list, task_list)
await _handle_setting(plugin, plugin_list, limit_list)
create_list = []
update_list = []
for plugin in plugin_list:
@@ -170,33 +154,6 @@ async def _():
# limit_create.append(limit)
# if limit_create:
# await PluginLimit.bulk_create(limit_create, 10)
if task_list:
module_dict = {
t[1]: t[0] for t in await TaskInfo.all().values_list("id", "module")
}
create_list = []
update_list = []
for status, task in task_list:
if task.module not in module_dict:
create_list.append((status, task))
else:
task.id = module_dict[task.module]
update_list.append(task)
if create_list:
_create_list = [t[1] for t in create_list]
await TaskInfo.bulk_create(_create_list, 10)
if block := [t[1].module for t in create_list if not t[0]]:
block_task = ",".join(block) + ","
if group_list := await GroupConsole.all():
for group in group_list:
group.block_task += block_task
await GroupConsole.bulk_update(group_list, ["block_task"], 10)
if update_list:
await TaskInfo.bulk_update(
update_list,
["run_time", "name"],
10,
)
await data_migration()
await PluginInfo.filter(module_path__in=load_plugin).update(load_status=True)
await PluginInfo.filter(module_path__not_in=load_plugin).update(load_status=False)
+164
View File
@@ -0,0 +1,164 @@
import nonebot
from nonebot import get_loaded_plugins
from nonebot.drivers import Driver
from nonebot.plugin import Plugin
from nonebot.utils import is_coroutine_callable
from nonebot_plugin_apscheduler import scheduler
from zhenxun.configs.utils import PluginExtraData, Task
from zhenxun.models.group_console import GroupConsole
from zhenxun.models.task_info import TaskInfo
from zhenxun.services.log import logger
from zhenxun.utils.common_utils import CommonUtils
driver: Driver = nonebot.get_driver()
async def _handle_setting(
plugin: Plugin,
task_info_list: list[tuple[bool, TaskInfo]],
task_list: list[Task],
):
"""处理插件设置
参数:
plugin: Plugin
task_info_list: 被动技能db数据列表
task_list: 被动技能列表
"""
metadata = plugin.metadata
if not metadata:
return
extra = metadata.extra
extra_data = PluginExtraData(**extra)
if extra_data.tasks:
task_info_list.extend(
(
task.create_status,
TaskInfo(
module=task.module,
name=task.name,
status=task.status,
default_status=task.default_status,
),
)
for task in extra_data.tasks
)
task_list.extend(extra_data.tasks)
async def update_to_group(create_list: list[tuple[bool, TaskInfo]]):
"""根据创建时状态对群组进行被动技能更新
参数:
create_list: 被动技能创建列表
"""
if blocks := [t[1].module for t in create_list if not t[0]]:
if group_list := await GroupConsole.all():
for group in group_list:
block_tasks = list(
set(CommonUtils.convert_module_format(group.block_task) + blocks)
)
group.block_task = CommonUtils.convert_module_format(block_tasks)
await GroupConsole.bulk_update(group_list, ["block_task"], 10)
async def to_db(
load_task: list[str],
create_list: list[tuple[bool, TaskInfo]],
update_list: list[TaskInfo],
):
"""将被动技能保存至数据库
参数:
load_task: 已加载的被动技能模块
create_list: 被动技能创建列表
update_list: 被动技能更新列表
"""
if create_list:
_create_list = [t[1] for t in create_list]
await TaskInfo.bulk_create(_create_list, 10)
await update_to_group(create_list)
if update_list:
await TaskInfo.bulk_update(
update_list,
["run_time", "name"],
10,
)
if load_task:
await TaskInfo.filter(module__in=load_task).update(load_status=True)
await TaskInfo.filter(module__not_in=load_task).update(load_status=False)
async def get_run_task(task: Task, *args, **kwargs):
is_run = False
if task.check:
if is_coroutine_callable(task.check):
if await task.check(*task.check_args):
is_run = True
elif task.check(*task.check_args):
is_run = True
else:
bot = task.check_args[0]
group_id = task.check_args[1]
if not await CommonUtils.task_is_block(bot, task.module, group_id):
is_run = True
if is_run and task.run_func:
if is_coroutine_callable(task.run_func):
await task.run_func(*args, **kwargs)
else:
task.run_func(*args, **kwargs)
async def create_schedule(task: Task):
scheduler_model = task.scheduler
if not scheduler_model or not task.run_func:
return
try:
scheduler.add_job(
get_run_task,
scheduler_model.trigger,
run_date=scheduler_model.run_date,
hour=scheduler_model.hour,
minute=scheduler_model.minute,
second=scheduler_model.second,
id=scheduler_model.id,
max_instances=scheduler_model.max_instances,
args=scheduler_model.args,
kwargs=scheduler_model.kwargs,
)
logger.debug(f"成功动态创建定时任务: {task.name}({task.module})")
except Exception as e:
logger.error(f"动态创建定时任务 {task.name}({task.module}) 失败", e=e)
@driver.on_startup
async def _():
"""
初始化插件数据配置
"""
task_list: list[Task] = []
task_info_list: list[tuple[bool, TaskInfo]] = []
for plugin in get_loaded_plugins():
await _handle_setting(plugin, task_info_list, task_list)
if not task_info_list:
await TaskInfo.all().update(load_status=False)
return
module_dict = {t[1]: t[0] for t in await TaskInfo.all().values_list("id", "module")}
load_task = []
create_list = []
update_list = []
for status, task in task_info_list:
if task.module not in module_dict:
create_list.append((status, task))
else:
task.id = module_dict[task.module]
update_list.append(task)
load_task.append(task.module)
await to_db(load_task, create_list, update_list)
# db_task = await TaskInfo.filter(load_status=True, status=True).values_list(
# "module", flat=True
# )
# task_list = [t for t in task_list if t.module in db_task]
# for task in task_list:
# create_schedule(task)
+7 -2
View File
@@ -57,7 +57,7 @@ LG_MESSAGE = [
async def init_image():
SIGN_RESOURCE_PATH.mkdir(parents=True, exist_ok=True)
SIGN_TODAY_CARD_PATH.mkdir(exist_ok=True, parents=True)
await generate_progress_bar_pic()
# await generate_progress_bar_pic()
clear_sign_data_pic()
@@ -86,6 +86,7 @@ async def get_card(
返回:
Path: 卡片路径
"""
await generate_progress_bar_pic()
user_id = user.user_id
date = datetime.now().date()
_type = "view" if is_card_view else "sign"
@@ -296,6 +297,10 @@ async def generate_progress_bar_pic():
"""
初始化进度条图片
"""
bar_white_file = SIGN_RESOURCE_PATH / "bar_white.png"
if bar_white_file.exists():
return
bg_2 = (254, 1, 254)
bg_1 = (0, 245, 246)
@@ -335,7 +340,7 @@ async def generate_progress_bar_pic():
await bk.paste(img_y, (0, 0))
await bk.paste(A, (25, 0))
await bk.paste(img_x, (975, 0))
await bk.save(SIGN_RESOURCE_PATH / "bar_white.png")
await bk.save(bar_white_file)
def get_level_and_next_impression(impression: float) -> tuple[str, int | float, int]:
@@ -16,7 +16,7 @@ router = APIRouter(prefix="/menu")
dependencies=[authentication()],
response_model=Result[list[MenuData]],
response_class=JSONResponse,
deprecated="获取菜单列表", # type: ignore
description="获取菜单列表",
)
async def _() -> Result[list[MenuData]]:
try:
@@ -23,7 +23,7 @@ driver = nonebot.get_driver()
dependencies=[authentication()],
response_model=Result[list[BotInfo]],
response_class=JSONResponse,
deprecated="获取bot列表", # type: ignore
description="获取bot列表", # type: ignore
)
async def _() -> Result[list[BotInfo]]:
try:
@@ -74,7 +74,7 @@ async def _(bot_id: str | None = None) -> Result[AllChatAndCallCount]:
dependencies=[authentication()],
response_model=Result[ChatCallMonthCount],
response_class=JSONResponse,
deprecated="获取聊天/调用记录的一个月数量", # type: ignore
description="获取聊天/调用记录的一个月数量", # type: ignore
)
async def _(bot_id: str | None = None) -> Result[ChatCallMonthCount]:
try:
@@ -91,7 +91,7 @@ async def _(bot_id: str | None = None) -> Result[ChatCallMonthCount]:
dependencies=[authentication()],
response_model=Result[BaseResultModel],
response_class=JSONResponse,
deprecated="获取Bot连接记录", # type: ignore
description="获取Bot连接记录", # type: ignore
)
async def _(query: QueryModel) -> Result[BaseResultModel]:
try:
@@ -106,7 +106,7 @@ async def _(query: QueryModel) -> Result[BaseResultModel]:
dependencies=[authentication()],
response_model=Result[Config],
response_class=JSONResponse,
deprecated="获取nb配置", # type: ignore
description="获取nb配置", # type: ignore
)
async def _() -> Result[Config]:
return Result.ok(driver.config)
@@ -85,9 +85,6 @@ async def _(group: UpdateGroup) -> Result[str]:
description="获取好友列表",
)
async def _(bot_id: str) -> Result[list[Friend]]:
"""
获取群信息
"""
try:
bot = nonebot.get_bot(bot_id)
friend_list, _ = await PlatformUtils.get_friend_list(bot)
@@ -95,7 +92,7 @@ async def _(bot_id: str) -> Result[list[Friend]]:
for f in friend_list:
ava_url = AVA_URL.format(f.user_id)
result_list.append(
Friend(user_id=f.user_id, nickname=f.nickname, ava_url=ava_url)
Friend(user_id=f.user_id, nickname=f.user_name or "", ava_url=ava_url)
)
return Result.ok(
result_list,
@@ -24,7 +24,7 @@ router = APIRouter(prefix="/plugin")
dependencies=[authentication()],
response_model=Result[list[PluginInfo]],
response_class=JSONResponse,
deprecated="获取插件列表", # type: ignore
description="获取插件列表", # type: ignore
)
async def _(
plugin_type: list[PluginType] = Query(None), menu_type: str | None = None
@@ -43,7 +43,7 @@ async def _(
dependencies=[authentication()],
response_model=Result[int],
response_class=JSONResponse,
deprecated="获取插件数量", # type: ignore
description="获取插件数量", # type: ignore
)
async def _() -> Result[int]:
try:
@@ -16,7 +16,7 @@ router = APIRouter(prefix="/store")
dependencies=[authentication()],
response_model=Result[dict],
response_class=JSONResponse,
deprecated="获取插件商店插件信息", # type: ignore
description="获取插件商店插件信息", # type: ignore
)
async def _() -> Result[dict]:
try:
@@ -41,7 +41,7 @@ async def _() -> Result[dict]:
dependencies=[authentication()],
response_model=Result,
response_class=JSONResponse,
deprecated="安装插件", # type: ignore
description="安装插件", # type: ignore
)
async def _(param: PluginIr) -> Result:
try:
@@ -59,7 +59,7 @@ async def _(param: PluginIr) -> Result:
dependencies=[authentication()],
response_model=Result,
response_class=JSONResponse,
deprecated="更新插件", # type: ignore
description="更新插件", # type: ignore
)
async def _(param: PluginIr) -> Result:
try:
@@ -77,7 +77,7 @@ async def _(param: PluginIr) -> Result:
dependencies=[authentication()],
response_model=Result,
response_class=JSONResponse,
deprecated="移除插件", # type: ignore
description="移除插件", # type: ignore
)
async def _(param: PluginIr) -> Result:
try: