SIMNet/simnet/client/components/diary/base.py

98 lines
3.6 KiB
Python
Raw Normal View History

2023-06-09 03:48:56 +00:00
from datetime import timedelta, timezone, datetime
2023-06-23 14:54:42 +00:00
from typing import Optional, Any, Dict, Union
2023-06-09 03:48:56 +00:00
from simnet.client.base import BaseClient
from simnet.client.routes import DETAIL_LEDGER_URL, INFO_LEDGER_URL
from simnet.models.diary import DiaryType
from simnet.models.genshin.diary import DiaryPage
2023-12-27 07:09:24 +00:00
from simnet.utils.enums import Region, Game
2023-06-09 03:48:56 +00:00
from simnet.utils.player import recognize_server
__all__ = ("BaseDiaryClient",)
CN_TIMEZONE = timezone(timedelta(hours=8))
class BaseDiaryClient(BaseClient):
"""Base diary component."""
async def request_ledger(
self,
player_id: Optional[int] = None,
*,
game: Optional[Game] = None,
detail: bool = False,
2023-06-23 14:54:42 +00:00
month: Union[int, str, None] = None,
2023-06-09 03:48:56 +00:00
lang: Optional[str] = None,
params: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""Make a request towards the ys ledger endpoint.
Args:
player_id (Optional[int], optional): The player ID to get the ledger for.
game (Optional[Game], optional): The game to get the ledger for.
detail (bool, optional): Whether to get the detailed ledger.
month (Optional[int], optional): The month to get the ledger for.
lang (Optional[str], optional): The language code to use for the request.
params (Optional[Dict[str, Any]], optional): The query parameters to use for the request.
Returns:
Dict[str, Any]: The response data.
"""
game = game or self.game
player_id = player_id or self.player_id
params = params or {}
2023-06-23 14:54:42 +00:00
base_url = DETAIL_LEDGER_URL if detail else INFO_LEDGER_URL
url = base_url.get_url(self.region, game)
2023-06-09 03:48:56 +00:00
if self.region == Region.OVERSEAS or game == Game.STARRAIL:
params["uid"] = player_id
params["region"] = recognize_server(player_id, game)
elif self.region == Region.CHINESE:
params["bind_uid"] = player_id
params["bind_region"] = recognize_server(player_id, game)
else:
raise TypeError(f"{self.region!r} is not a valid region.")
2023-06-23 14:54:42 +00:00
if game == Game.STARRAIL:
month = month or datetime.now(CN_TIMEZONE).strftime("%Y%m")
elif game == Game.GENSHIN:
month = month or str(datetime.now(CN_TIMEZONE).month)
params["month"] = month
2023-06-09 03:48:56 +00:00
params["lang"] = lang or self.lang
return await self.request_lab(url, params=params)
async def _get_diary_page(
self,
page: int,
*,
game: Optional[Game] = None,
player_id: Optional[int] = None,
diary_type: int = DiaryType.PRIMOGEMS,
month: Optional[int] = None,
lang: Optional[str] = None,
) -> DiaryPage:
"""Get a diary page.
Args:
page (int): The page number to get.
game (Optional[Game], optional): The game to get the diary page for.
player_id (Optional[int], optional): The player ID to get the diary page for.
diary_type (int, optional): The diary type to get the diary page for.
month (Optional[int], optional): The month to get the diary page for.
lang (Optional[str], optional): The language code to use for the request.
Returns:
DiaryPage: The diary page.
"""
data = await self.request_ledger(
player_id,
game=game,
detail=True,
month=month,
lang=lang,
params=dict(type=diary_type, current_page=page, page_size=100),
)
return DiaryPage(**data)