PaiGram/service/game.py

43 lines
1.7 KiB
Python
Raw Normal View History

2022-04-14 07:18:45 +00:00
from model.genshinhelper import Mihoyo
from service.repository import AsyncRepository
from service.cache import RedisCache
class GetGameInfo:
def __init__(self, repository: AsyncRepository, cache: RedisCache):
self.repository = repository
self.cache = cache
self.mihoyo = Mihoyo()
async def get_characters_cultivation_atlas(self, character_name: str) -> str:
qname = f"game:info:characters_cultivation_atlas:{character_name}"
url_info = await self.cache.get_str_list(qname)
if len(url_info) >= 1:
return url_info[-1]
async def get_post_id(collection_id: int) -> int:
post_full_in_collection = await self.mihoyo.get_post_full_in_collection(collection_id) # 642956
if post_full_in_collection.error:
await self.cache.set_str_list(qname, [""], 3600)
return -1
_post_id: int = -1
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
return _post_id
post_id = await get_post_id(642956)
if post_id == -1:
post_id = await get_post_id(307224)
if post_id == -1:
await self.cache.set_str_list(qname, [""], 3600)
return ""
artwork_info = await self.mihoyo.get_artwork_info(post_id)
await self.cache.set_str_list(qname, artwork_info.results.image_url_list, 3600)
return artwork_info.results.image_url_list[0]