PaiGram/core/services/game/services.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
2.1 KiB
Python
Raw Normal View History

2022-07-26 10:07:31 +00:00
from typing import List, Optional
from core.base_service import BaseService
2023-04-04 14:02:14 +00:00
from core.services.game.cache import GameCacheForStrategy
2022-12-10 12:37:43 +00:00
from modules.apihelper.client.components.hyperion import Hyperion
2022-07-26 10:07:31 +00:00
2023-04-04 14:02:14 +00:00
__all__ = "GameStrategyService"
2022-07-26 10:07:31 +00:00
class GameStrategyService(BaseService):
def __init__(self, cache: GameCacheForStrategy, collections: Optional[List[int]] = None):
2022-07-26 10:07:31 +00:00
self._cache = cache
self._hyperion = Hyperion()
if collections is None:
self._collections = [2319292, 2319293, 2319294, 2319295, 2319296, 2319298, 2319299, 642956]
2022-07-26 10:07:31 +00:00
else:
self._collections = collections
2022-11-20 05:59:16 +00:00
self._special_posts = {"达达利亚": "21272578"}
2022-07-26 10:07:31 +00:00
async def _get_strategy_from_hyperion(self, collection_id: int, character_name: str) -> int:
2022-11-20 05:59:16 +00:00
if character_name in self._special_posts:
return self._special_posts[character_name]
2022-07-26 10:07:31 +00:00
post_id: int = -1
post_full_in_collection = await self._hyperion.get_post_full_in_collection(collection_id)
2022-10-08 00:59:08 +00:00
for post_data in post_full_in_collection["posts"]:
title = post_data["post"]["subject"]
2022-07-26 10:07:31 +00:00
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 and title and character_name in title:
post_id = int(post_data["post"]["post_id"])
2022-07-26 10:07:31 +00:00
if post_id != -1:
break
return post_id
async def get_strategy(self, character_name: str) -> str:
cache = await self._cache.get_url_list(character_name)
if len(cache) >= 1:
2022-11-20 05:59:16 +00:00
return cache[0]
2022-07-26 10:07:31 +00:00
for collection_id in self._collections:
post_id = await self._get_strategy_from_hyperion(collection_id, character_name)
if post_id != -1:
break
else:
return ""
2022-10-08 00:59:08 +00:00
artwork_info = await self._hyperion.get_post_info(2, post_id)
await self._cache.set_url_list(character_name, artwork_info.image_urls)
return artwork_info.image_urls[0]