From 65dc67c6ac4fa258aef1b2e1fa9655d45fa4abe8 Mon Sep 17 00:00:00 2001 From: Chenric <91937041+ChenXu233@users.noreply.github.com> Date: Sat, 21 Sep 2024 18:53:41 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E5=BC=82=E6=AD=A5=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 还有个httpx的client启动优化没解决,准备后期用uninfo解决了。 --- nonebot_plugin_dialectlist/__init__.py | 41 ++--------------- nonebot_plugin_dialectlist/model.py | 1 + nonebot_plugin_dialectlist/utils.py | 62 +++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 39 deletions(-) diff --git a/nonebot_plugin_dialectlist/__init__.py b/nonebot_plugin_dialectlist/__init__.py index 2849a91..1e4c3ee 100644 --- a/nonebot_plugin_dialectlist/__init__.py +++ b/nonebot_plugin_dialectlist/__init__.py @@ -216,7 +216,7 @@ async def handle_rank( start: datetime = Arg(), stop: datetime = Arg(), ): - + if id := state["group_id"]: id = str(id) logger.debug(f"group_id: {id}") @@ -248,47 +248,14 @@ async def handle_rank( rank = got_rank(raw_rank) logger.debug(rank) - rank2: List[UserRankInfo] = [] ids = await persist_id2user_id([int(i[0]) for i in rank]) for i in range(len(rank)): rank[i][0] = str(ids[i]) logger.debug(rank[i]) - total = sum([i[1] for i in rank]) - index = 1 - user_infos = await get_user_infos(bot, event, user_ids=[str(i[0]) for i in rank]) - for user_info,i in zip(user_infos,rank): - if user_info: - logger.debug(user_info) - user_nickname = ( - user_info.user_displayname - if user_info.user_displayname - else user_info.user_name if user_info.user_name else user_info.user_id - ) - user_avatar = ( - await user_info.user_avatar.get_image() - if user_info.user_avatar - else open( - os.path.dirname(os.path.abspath(__file__)) - + "/template/avatar/default.jpg", - "rb", - ).read() - ) - user = UserRankInfo( - **model_dump(user_info), - user_bnum=i[1], - user_proportion=round(i[1] / total * 100, 2), - user_index=index, - user_nickname=user_nickname, - user_avatar_bytes=user_avatar, - ) - user.user_gender = ( - "♀" - if user_info.user_gender == "female" - else "♂" if user_info.user_gender == "male" else "" - ) - rank2.append(user) - index += 1 + t1 = t.time() + rank2 = await get_user_infos(bot, event, rank) + logger.debug(f"获取用户信息花费时间:{t.time() - t1}") string: str = "" for i in rank2: diff --git a/nonebot_plugin_dialectlist/model.py b/nonebot_plugin_dialectlist/model.py index 745b231..4d5a7d7 100644 --- a/nonebot_plugin_dialectlist/model.py +++ b/nonebot_plugin_dialectlist/model.py @@ -18,6 +18,7 @@ class UserRankInfo(UserInfo): # class MsgCountDayData(BaseModel): # session_id: str # session_bnum: int + class MessageCountCache(Model): __table_args__ = {"extend_existing": True} id: Mapped[int] = mapped_column(primary_key=True) diff --git a/nonebot_plugin_dialectlist/utils.py b/nonebot_plugin_dialectlist/utils.py index 932c808..c9c869c 100644 --- a/nonebot_plugin_dialectlist/utils.py +++ b/nonebot_plugin_dialectlist/utils.py @@ -8,6 +8,7 @@ from sqlalchemy.sql import ColumnElement from nonebot.log import logger from nonebot.params import Depends +from nonebot.compat import model_dump from nonebot.matcher import Matcher from nonebot.adapters import Bot, Event @@ -183,7 +184,64 @@ async def get_rank_image(rank: List[UserRankInfo]) -> bytes: }, pages={"viewport": {"width": 1000, "height": 10}}, ) +def _get_user_nickname(user_info:UserInfo)->str: + user_nickname = ( + user_info.user_displayname + if user_info.user_displayname + else user_info.user_name if user_info.user_name else user_info.user_id + ) + return user_nickname -async def get_user_infos(bot:Bot,event:Event,user_ids:List[str],use_cache: bool = True)-> List[Optional[UserInfo]]: +async def _get_user_default_avatar()->bytes: + img = open( + os.path.dirname(os.path.abspath(__file__)) + + "/template/avatar/default.jpg", + "rb", + ).read() + return img + +def get_default_user_info()->UserInfo: + user_info = UserInfo( + user_id="114514", + user_name="鬼知道这谁,bot获取不了", + ) + 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] - return (await asyncio.gather(*pool)) + user_infos = (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()) + + total = sum([i[1] for i in rank]) + rank2 = [] + for i in range(len(rank)): + + user_info = user_infos[i] + if not user_info: + user_info = get_default_user_info() + + user = UserRankInfo( + **model_dump(user_info), + user_bnum=rank[i][1], + user_proportion=round(rank[i][1] / total * 100, 2), + user_index=i+1, + user_nickname=_get_user_nickname(user_info), + user_avatar_bytes=user_avatars[i], + ) + rank2.append(user) + + return rank2 +