Skip to content

Commit

Permalink
⚡ 获取用户数据非常好优化
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenXu233 committed Sep 28, 2024
1 parent 4194097 commit 677ec56
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions nonebot_plugin_dialectlist/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import httpx
import asyncio
import unicodedata

Expand All @@ -15,6 +16,7 @@
from nonebot_plugin_orm import get_session
from nonebot_plugin_session import Session, SessionLevel, extract_session
from nonebot_plugin_userinfo import get_user_info, UserInfo
from nonebot_plugin_userinfo.exception import NetworkError
from nonebot_plugin_localstore import get_cache_dir
from nonebot_plugin_htmlrender import template_to_pic
from nonebot_plugin_session_orm import SessionModel
Expand Down Expand Up @@ -192,6 +194,19 @@ async def _get_user_default_avatar() -> bytes:
).read()
return img

async def _get_user_avatar(user: UserInfo, client: httpx.AsyncClient) -> bytes:
if not user.user_avatar:
return await _get_user_default_avatar()
url = user.user_avatar.get_url()
for i in range(3):
try:
resp = await client.get(url, timeout=10)
resp.raise_for_status()
return resp.content
except Exception as e:
logger.warning(f"Error downloading {url}, retry {i}/3: {e}")
await asyncio.sleep(3)
raise NetworkError(f"{url} 下载失败!")

def get_default_user_info() -> UserInfo:
user_info = UserInfo(
Expand All @@ -200,23 +215,24 @@ def get_default_user_info() -> UserInfo:
)
return user_info


async def get_user_infos(
bot: Bot, event: Event, rank: List, use_cache: bool = True
) -> List[UserRankInfo]:

user_ids = [i[0] for i in rank]
pool = [get_user_info(bot, event, id, use_cache) for id in user_ids]
user_infos = await asyncio.gather(*pool)

async with httpx.AsyncClient() as client:
pool = []
for i in user_infos:
if not i:
pool.append(_get_user_default_avatar())
continue
if i.user_avatar:
pool.append(_get_user_avatar(i, client))
user_avatars = await asyncio.gather(*pool)

pool = []
for i in user_infos:
if not i:
pool.append(_get_user_default_avatar())
continue
if i.user_avatar:
pool.append(i.user_avatar.get_image())
user_avatars = await asyncio.gather(*pool)
for i in user_avatars:
if not i:
user_avatars[user_avatars.index(i)] = await _get_user_default_avatar()
Expand Down

0 comments on commit 677ec56

Please sign in to comment.