From c09f4419b50537619c2904cbab7986215ffd9bbb Mon Sep 17 00:00:00 2001 From: SlyAimer <2289782085@qq.com> Date: Fri, 14 Mar 2025 13:39:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor(clover=5Fimage):=20=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=20aiohttp=20=E6=9B=BF=E4=BB=A3=20requests=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=20QQ=E5=A4=B4=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将同步函数改为异步函数,提高性能- 引入 aiohttp 库,替换 requests 库以支持异步请求 - 优化了文件写入逻辑,使用 chunk 方式写入 - 更新了相关插件中的调用方式,使其支持异步 --- src/clover_image/qq_image.py | 34 ++++++++++++++++++++++------------ src/plugins/today_wife.py | 4 ++-- src/plugins/touch.py | 4 +++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/clover_image/qq_image.py b/src/clover_image/qq_image.py index 2f54e6b..a2a6c0a 100644 --- a/src/clover_image/qq_image.py +++ b/src/clover_image/qq_image.py @@ -1,5 +1,5 @@ import os - +import aiohttp import requests from src.configs.path_config import image_local_qq_image_path @@ -8,21 +8,26 @@ from src.configs.api_config import app_id,bot_account """获取QQ头像""" -def download_qq_image(member_open_id): +async def download_qq_image(member_open_id): if not os.path.exists(image_local_qq_image_path): os.makedirs(image_local_qq_image_path) save_path = image_local_qq_image_path + '/' + member_open_id + '.jpg' size = 140 #尺寸 40、100、140、640 url = f"https://q.qlogo.cn/qqapp/{app_id}/{member_open_id}/{size}" - response = requests.get(url) # 发送 GET 请求获取图片资源 - if response.status_code == 200: # 判断请求是否成功 - with open(save_path, 'wb') as file: # 以二进制写入模式打开文件 - file.write(response.content) # 将响应内容写入文件 - return save_path + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + response.raise_for_status() + with open(save_path, 'wb') as file: + while True: + chunk = await response.content.read(8192) + if not chunk: + break + file.write(chunk) + return save_path """获取QQ头像""" -def download_qq_image_by_account(account): +async def download_qq_image_by_account(account): if not os.path.exists(image_local_qq_image_path): os.makedirs(image_local_qq_image_path) if account is None: @@ -30,9 +35,14 @@ def download_qq_image_by_account(account): save_path = image_local_qq_image_path + '/' + account + '.jpg' size = 640 # 尺寸 40、100、140、640 url = f"https://q2.qlogo.cn/headimg_dl?dst_uin={account}&spec={size}" - response = requests.get(url) # 发送 GET 请求获取图片资源 - if response.status_code == 200: # 判断请求是否成功 - with open(save_path, 'wb') as file: # 以二进制写入模式打开文件 - file.write(response.content) # 将响应内容写入文件 + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + response.raise_for_status() + with open(save_path, 'wb') as file: + while True: + chunk = await response.content.read(8192) + if not chunk: + break + file.write(chunk) return save_path diff --git a/src/plugins/today_wife.py b/src/plugins/today_wife.py index 7b4115c..8208b7c 100644 --- a/src/plugins/today_wife.py +++ b/src/plugins/today_wife.py @@ -15,7 +15,7 @@ async def handle_function(message: MessageEvent): user_id = await UserList.get_user_id(member_openid,message.group_id) if user_id is None: await today_group_wife.finish("潜在老婆太少了,快请群友多多使用吧") - local_image_path = download_qq_image(user_id) + local_image_path = await download_qq_image(user_id) msg = Message([ MessageSegment.text("您的今日群老婆"), MessageSegment.file_image(Path(local_image_path)), @@ -29,7 +29,7 @@ today_wife = on_command("今日老婆", rule=to_me(), priority=10) async def handle_function(message: MessageEvent): member_openid = message.get_user_id() - local_image_path = download_qq_image(member_openid) + local_image_path = await download_qq_image(member_openid) msg = Message([ MessageSegment.file_image(Path(local_image_path)), ]) diff --git a/src/plugins/touch.py b/src/plugins/touch.py index 4f57211..1406ada 100644 --- a/src/plugins/touch.py +++ b/src/plugins/touch.py @@ -1,4 +1,6 @@ from pathlib import Path + +from lazy_object_proxy.utils import await_ from nonebot.rule import to_me from nonebot.plugin import on_command from nonebot.adapters.qq import Message, MessageEvent, MessageSegment @@ -22,7 +24,7 @@ async def handle_touch(message: MessageEvent): result = await QrTouch.touch(0) q.reply_touch_content = result.reply_touch_content await QrTouchLog.insert_touch_log(q, member_openid) - local_gif = rua(download_qq_image_by_account(None)).add_gif() + local_gif = rua(await download_qq_image_by_account(None)).add_gif() msg = Message([MessageSegment.file_image(Path(local_gif)), MessageSegment.text(result.reply_touch_content),]) await delete_file(local_gif)