From 7c6526d1ea6904c65a3135570f8c8626faea8d2b Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Thu, 21 Dec 2023 16:48:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=B9=E4=B8=BA=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 6 +++--- pkg/qqbot/cmds/system/update.py | 10 +++------- pkg/utils/center/apigroup.py | 21 ++++++++++----------- pkg/utils/center/groups/main.py | 8 ++++---- pkg/utils/center/groups/plugin.py | 12 ++++++------ pkg/utils/center/groups/usage.py | 12 ++++++------ pkg/utils/updater.py | 4 ++-- 7 files changed, 34 insertions(+), 39 deletions(-) diff --git a/main.py b/main.py index ebb98e92..5fe42013 100644 --- a/main.py +++ b/main.py @@ -422,7 +422,7 @@ def stop(): raise e -async def main(): +def main(): global use_override # 检查是否携带了 --override 或 -r 参数 if '--override' in sys.argv or '-r' in sys.argv: @@ -450,7 +450,7 @@ async def main(): elif len(sys.argv) > 1 and sys.argv[1] == 'update': print("正在进行程序更新...") import pkg.utils.updater as updater - await updater.update_all(cli=True) + updater.update_all(cli=True) sys.exit(0) # 关闭urllib的http警告 @@ -486,5 +486,5 @@ def run_wrapper(): if __name__ == '__main__': - asyncio.run(main()) + main() diff --git a/pkg/qqbot/cmds/system/update.py b/pkg/qqbot/cmds/system/update.py index 6d97cd9e..d4cca3f3 100644 --- a/pkg/qqbot/cmds/system/update.py +++ b/pkg/qqbot/cmds/system/update.py @@ -1,5 +1,4 @@ import threading -import asyncio import traceback from .. import aamgr @@ -21,9 +20,9 @@ def process(cls, ctx: aamgr.Context) -> tuple[bool, list]: import pkg.utils.reloader import pkg.utils.context - async def update_task(): + def update_task(): try: - if await pkg.utils.updater.update_all(): + if pkg.utils.updater.update_all(): pkg.utils.context.get_qqbot_manager().notify_admin("更新完成, 请手动重启程序。") else: pkg.utils.context.get_qqbot_manager().notify_admin("无新版本") @@ -32,10 +31,7 @@ async def update_task(): pkg.utils.context.get_qqbot_manager().notify_admin("更新失败:{}".format(e0)) return - def wrapper(): - asyncio.run(update_task()) - - threading.Thread(target=wrapper).start() + threading.Thread(target=update_task, daemon=True).start() reply = ["[bot]正在更新,请耐心等待,请勿重复发起更新..."] diff --git a/pkg/utils/center/apigroup.py b/pkg/utils/center/apigroup.py index 5938fd60..1324ab76 100644 --- a/pkg/utils/center/apigroup.py +++ b/pkg/utils/center/apigroup.py @@ -2,7 +2,7 @@ import uuid import json -import aiohttp +import requests class APIGroup(metaclass=abc.ABCMeta): @@ -28,16 +28,15 @@ async def do( url = self.prefix + path data = json.dumps(data) headers['Content-Type'] = 'application/json' - async with aiohttp.ClientSession() as session: - async with session.request( - method, - url, - data=data, - params=params, - headers=headers, - **kwargs - ) as resp: - return await resp.json() + + return requests.request( + method, + url, + data=data, + params=params, + headers=headers, + **kwargs + ) def gen_rid( self diff --git a/pkg/utils/center/groups/main.py b/pkg/utils/center/groups/main.py index 71bef9b2..f158cd79 100644 --- a/pkg/utils/center/groups/main.py +++ b/pkg/utils/center/groups/main.py @@ -9,7 +9,7 @@ class V2MainDataAPI(apigroup.APIGroup): def __init__(self, prefix: str): super().__init__(prefix+"/main") - async def post_update_record( + def post_update_record( self, spent_seconds: int, infer_reason: str, @@ -17,7 +17,7 @@ async def post_update_record( new_version: str, ): """提交更新记录""" - return await self.do( + return self.do( "POST", "/update", data={ @@ -31,12 +31,12 @@ async def post_update_record( } ) - async def post_announcement_showed( + def post_announcement_showed( self, ids: list[int], ): """提交公告已阅""" - return await self.do( + return self.do( "POST", "/announcement", data={ diff --git a/pkg/utils/center/groups/plugin.py b/pkg/utils/center/groups/plugin.py index beff8714..b3ac423c 100644 --- a/pkg/utils/center/groups/plugin.py +++ b/pkg/utils/center/groups/plugin.py @@ -9,12 +9,12 @@ class V2PluginDataAPI(apigroup.APIGroup): def __init__(self, prefix: str): super().__init__(prefix+"/plugin") - async def post_install_record( + def post_install_record( self, plugin: dict ): """提交插件安装记录""" - return await self.do( + return self.do( "POST", "/install", data={ @@ -23,12 +23,12 @@ async def post_install_record( } ) - async def post_remove_record( + def post_remove_record( self, plugin: dict ): """提交插件卸载记录""" - return await self.do( + return self.do( "POST", "/remove", data={ @@ -37,14 +37,14 @@ async def post_remove_record( } ) - async def post_update_record( + def post_update_record( self, plugin: dict, old_version: str, new_version: str, ): """提交插件更新记录""" - return await self.do( + return self.do( "POST", "/update", data={ diff --git a/pkg/utils/center/groups/usage.py b/pkg/utils/center/groups/usage.py index 338b869a..6e383a35 100644 --- a/pkg/utils/center/groups/usage.py +++ b/pkg/utils/center/groups/usage.py @@ -9,7 +9,7 @@ class V2UsageDataAPI(apigroup.APIGroup): def __init__(self, prefix: str): super().__init__(prefix+"/usage") - async def post_query_record( + def post_query_record( self, session_type: str, session_id: str, @@ -20,7 +20,7 @@ async def post_query_record( retry_times: int, ): """提交请求记录""" - return await self.do( + return self.do( "POST", "/query", data={ @@ -40,13 +40,13 @@ async def post_query_record( } ) - async def post_event_record( + def post_event_record( self, plugins: list[dict], event_name: str, ): """提交事件触发记录""" - return await self.do( + return self.do( "POST", "/event", data={ @@ -59,14 +59,14 @@ async def post_event_record( } ) - async def post_function_record( + def post_function_record( self, plugin: dict, function_name: str, function_description: str, ): """提交内容函数使用记录""" - return await self.do( + return self.do( "POST", "/function", data={ diff --git a/pkg/utils/updater.py b/pkg/utils/updater.py index abbc696a..3881298c 100644 --- a/pkg/utils/updater.py +++ b/pkg/utils/updater.py @@ -109,7 +109,7 @@ def compare_version_str(v0: str, v1: str) -> int: return 0 -async def update_all(cli: bool = False) -> bool: +def update_all(cli: bool = False) -> bool: """检查更新并下载源码""" start_time = time.time() @@ -207,7 +207,7 @@ async def update_all(cli: bool = False) -> bool: with open("current_tag", "w") as f: f.write(current_tag) - await context.get_center_v2_api().main.post_update_record( + context.get_center_v2_api().main.post_update_record( spent_seconds=int(time.time()-start_time), infer_reason="update", old_version=old_tag,