mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-Nonebot.git
synced 2026-04-01 22:04:51 +00:00
refactor(clover_image): 使用 aiohttp 替代 requests 获取 QQ头像
- 将同步函数改为异步函数,提高性能- 引入 aiohttp 库,替换 requests 库以支持异步请求 - 优化了文件写入逻辑,使用 chunk 方式写入 - 更新了相关插件中的调用方式,使其支持异步
This commit is contained in:
parent
8c9d0380dd
commit
c09f4419b5
3 changed files with 27 additions and 15 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import os
|
import os
|
||||||
|
import aiohttp
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from src.configs.path_config import image_local_qq_image_path
|
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头像"""
|
"""获取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):
|
if not os.path.exists(image_local_qq_image_path):
|
||||||
os.makedirs(image_local_qq_image_path)
|
os.makedirs(image_local_qq_image_path)
|
||||||
|
|
||||||
save_path = image_local_qq_image_path + '/' + member_open_id + '.jpg'
|
save_path = image_local_qq_image_path + '/' + member_open_id + '.jpg'
|
||||||
size = 140 #尺寸 40、100、140、640
|
size = 140 #尺寸 40、100、140、640
|
||||||
url = f"https://q.qlogo.cn/qqapp/{app_id}/{member_open_id}/{size}"
|
url = f"https://q.qlogo.cn/qqapp/{app_id}/{member_open_id}/{size}"
|
||||||
response = requests.get(url) # 发送 GET 请求获取图片资源
|
async with aiohttp.ClientSession() as session:
|
||||||
if response.status_code == 200: # 判断请求是否成功
|
async with session.get(url) as response:
|
||||||
with open(save_path, 'wb') as file: # 以二进制写入模式打开文件
|
response.raise_for_status()
|
||||||
file.write(response.content) # 将响应内容写入文件
|
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
|
return save_path
|
||||||
|
|
||||||
"""获取QQ头像"""
|
"""获取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):
|
if not os.path.exists(image_local_qq_image_path):
|
||||||
os.makedirs(image_local_qq_image_path)
|
os.makedirs(image_local_qq_image_path)
|
||||||
if account is None:
|
if account is None:
|
||||||
|
|
@ -30,9 +35,14 @@ def download_qq_image_by_account(account):
|
||||||
save_path = image_local_qq_image_path + '/' + account + '.jpg'
|
save_path = image_local_qq_image_path + '/' + account + '.jpg'
|
||||||
size = 640 # 尺寸 40、100、140、640
|
size = 640 # 尺寸 40、100、140、640
|
||||||
url = f"https://q2.qlogo.cn/headimg_dl?dst_uin={account}&spec={size}"
|
url = f"https://q2.qlogo.cn/headimg_dl?dst_uin={account}&spec={size}"
|
||||||
response = requests.get(url) # 发送 GET 请求获取图片资源
|
async with aiohttp.ClientSession() as session:
|
||||||
if response.status_code == 200: # 判断请求是否成功
|
async with session.get(url) as response:
|
||||||
with open(save_path, 'wb') as file: # 以二进制写入模式打开文件
|
response.raise_for_status()
|
||||||
file.write(response.content) # 将响应内容写入文件
|
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
|
return save_path
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ async def handle_function(message: MessageEvent):
|
||||||
user_id = await UserList.get_user_id(member_openid,message.group_id)
|
user_id = await UserList.get_user_id(member_openid,message.group_id)
|
||||||
if user_id is None:
|
if user_id is None:
|
||||||
await today_group_wife.finish("潜在老婆太少了,快请群友多多使用吧")
|
await today_group_wife.finish("潜在老婆太少了,快请群友多多使用吧")
|
||||||
local_image_path = download_qq_image(user_id)
|
local_image_path = await download_qq_image(user_id)
|
||||||
msg = Message([
|
msg = Message([
|
||||||
MessageSegment.text("您的今日群老婆"),
|
MessageSegment.text("您的今日群老婆"),
|
||||||
MessageSegment.file_image(Path(local_image_path)),
|
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):
|
async def handle_function(message: MessageEvent):
|
||||||
member_openid = message.get_user_id()
|
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([
|
msg = Message([
|
||||||
MessageSegment.file_image(Path(local_image_path)),
|
MessageSegment.file_image(Path(local_image_path)),
|
||||||
])
|
])
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from lazy_object_proxy.utils import await_
|
||||||
from nonebot.rule import to_me
|
from nonebot.rule import to_me
|
||||||
from nonebot.plugin import on_command
|
from nonebot.plugin import on_command
|
||||||
from nonebot.adapters.qq import Message, MessageEvent, MessageSegment
|
from nonebot.adapters.qq import Message, MessageEvent, MessageSegment
|
||||||
|
|
@ -22,7 +24,7 @@ async def handle_touch(message: MessageEvent):
|
||||||
result = await QrTouch.touch(0)
|
result = await QrTouch.touch(0)
|
||||||
q.reply_touch_content = result.reply_touch_content
|
q.reply_touch_content = result.reply_touch_content
|
||||||
await QrTouchLog.insert_touch_log(q, member_openid)
|
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)),
|
msg = Message([MessageSegment.file_image(Path(local_gif)),
|
||||||
MessageSegment.text(result.reply_touch_content),])
|
MessageSegment.text(result.reply_touch_content),])
|
||||||
await delete_file(local_gif)
|
await delete_file(local_gif)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue