refactor(clover_image): 使用 aiohttp 替代 requests 获取 QQ头像

- 将同步函数改为异步函数,提高性能- 引入 aiohttp 库,替换 requests 库以支持异步请求
- 优化了文件写入逻辑,使用 chunk 方式写入
- 更新了相关插件中的调用方式,使其支持异步
This commit is contained in:
SlyAimer 2025-03-14 13:39:18 +08:00
parent 8f521145bb
commit ce18d17f4b
3 changed files with 27 additions and 15 deletions

View file

@ -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) # 将响应内容写入文件
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

View file

@ -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)),
])

View file

@ -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)