From 683c3ce4aafeaa3cfd2bcc895d5d4b28284a4208 Mon Sep 17 00:00:00 2001 From: omg-xtao <100690902+omg-xtao@users.noreply.github.com> Date: Wed, 31 Aug 2022 19:24:02 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E7=B4=A0=E6=9D=90=E9=83=A8=E5=88=86=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E7=BC=BA=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/game/services.py | 36 ++++++++++++++++++++++++++++++++---- plugins/genshin/material.py | 2 +- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/core/game/services.py b/core/game/services.py index 9a925597..8834fcca 100644 --- a/core/game/services.py +++ b/core/game/services.py @@ -45,19 +45,40 @@ class GameStrategyService: return artwork_info.results.image_url_list[0] -class GameMaterialService(GameStrategyService): +class GameMaterialService: def __init__(self, cache: GameCache, collections: Optional[List[int]] = None): - super().__init__(cache, collections) + self._cache = cache + self._hyperion = Hyperion() self._collections = [428421, 1164644] if collections is None else collections self._special = ['雷电将军', '珊瑚宫心海', '菲谢尔', '托马', '八重神子', '九条裟罗', '辛焱', '神里绫华'] + async def _get_material_from_hyperion(self, collection_id: int, character_name: str) -> int: + post_id: int = -1 + post_full_in_collection = await self._hyperion.get_post_full_in_collection(collection_id) + if post_full_in_collection.error: + return post_id + for post_data in post_full_in_collection.data["posts"]: + topics = post_data["topics"] + for topic in topics: + if character_name == topic["name"]: + post_id = int(post_data["post"]["post_id"]) + break + if post_id != -1: + break + subject = post_data["post"]["subject"] + if character_name in subject: + post_id = int(post_data["post"]["post_id"]) + if post_id != -1: + break + return post_id + async def get_material(self, character_name: str) -> str: cache = await self._cache.get_url_list(character_name) if len(cache) >= 1: return cache[-1] for collection_id in self._collections: - post_id = await self._get_strategy_from_hyperion(collection_id, character_name) + post_id = await self._get_material_from_hyperion(collection_id, character_name) if post_id != -1: break else: @@ -66,4 +87,11 @@ class GameMaterialService(GameStrategyService): artwork_info = await self._hyperion.get_artwork_info(2, post_id) await self._cache.set_url_list(character_name, artwork_info.results.image_url_list) image_url_list = artwork_info.results.image_url_list - return image_url_list[2] if character_name in self._special else image_url_list[1] + if len(image_url_list) == 0: + return "" + elif len(image_url_list) == 1: + return image_url_list[0] + elif character_name in self._special: + return image_url_list[2] + else: + return image_url_list[1] diff --git a/plugins/genshin/material.py b/plugins/genshin/material.py index c4a1858b..45524396 100644 --- a/plugins/genshin/material.py +++ b/plugins/genshin/material.py @@ -59,7 +59,7 @@ class Material(BasePlugins): Log.info(f"用户 {user.full_name}[{user.id}] 查询角色培养素材命令请求 || 参数 {character_name}") await message.reply_chat_action(ChatAction.UPLOAD_PHOTO) file_path = await url_to_file(url, "") - caption = "From 米游社 友人A " \ + caption = "From 米游社 " \ f"查看 [原图]({url})" await message.reply_photo(photo=open(file_path, "rb"), caption=caption, filename=f"{character_name}.png", allow_sending_without_reply=True, parse_mode=ParseMode.MARKDOWN_V2)