SIMNet/simnet/client/components/chronicle/genshin.py

323 lines
12 KiB
Python
Raw Normal View History

2023-05-05 04:06:43 +00:00
import asyncio
2023-08-05 11:02:09 +00:00
from typing import Optional, Any, List, Dict, Union
2023-05-05 04:06:43 +00:00
from simnet.client.components.chronicle.base import BaseChronicleClient
2023-08-05 11:00:58 +00:00
from simnet.client.routes import RECORD_URL
from simnet.errors import DataNotPublic, BadRequest
2023-05-05 04:06:43 +00:00
from simnet.models.genshin.chronicle.abyss import SpiralAbyss, SpiralAbyssPair
from simnet.models.genshin.chronicle.characters import Character
from simnet.models.genshin.chronicle.img_theater import ImgTheater
2023-08-05 11:00:58 +00:00
from simnet.models.genshin.chronicle.notes import Notes, NotesWidget, NotesOverseaWidget
2023-05-05 04:06:43 +00:00
from simnet.models.genshin.chronicle.stats import (
PartialGenshinUserStats,
GenshinUserStats,
FullGenshinUserStats,
)
2023-05-28 09:32:21 +00:00
from simnet.models.lab.record import RecordCard
2023-12-27 07:09:24 +00:00
from simnet.utils.enums import Game, Region
2023-05-05 04:06:43 +00:00
from simnet.utils.player import recognize_genshin_server, recognize_region
__all__ = ("GenshinBattleChronicleClient",)
2023-05-05 04:06:43 +00:00
class GenshinBattleChronicleClient(BaseChronicleClient):
2023-05-05 04:06:43 +00:00
"""A client for retrieving data from Genshin's battle chronicle component.
This class is used to retrieve various data objects from StarRail's battle chronicle component,
including real-time notes, user statistics, and character information.
"""
async def _request_genshin_record(
self,
endpoint: str,
player_id: Optional[int] = None,
method: str = "GET",
2023-07-23 09:41:26 +00:00
endpoint_type: str = "api",
2023-05-05 04:06:43 +00:00
lang: Optional[str] = None,
payload: Optional[Dict[str, Any]] = None,
):
"""Get an arbitrary object from StarRail's battle chronicle.
Args:
endpoint (str): The endpoint of the object to retrieve.
2023-07-23 09:41:26 +00:00
endpoint_type (str, optional): The type of endpoint to send the request to.
2023-05-05 04:06:43 +00:00
player_id (Optional[int], optional): The player ID. Defaults to None.
method (str, optional): The HTTP method to use. Defaults to "GET".
lang (Optional[str], optional): The language of the data. Defaults to None.
payload (Optional[Dict[str, Any]], optional): The request payload. Defaults to None.
Returns:
Dict[str, Any]: The requested object.
Raises:
BadRequest: If the request is invalid.
DataNotPublic: If the requested data is not public.
ValueError: If the player ID is not specified.
"""
player_id = player_id or self.player_id
if player_id is None:
raise ValueError("Player ID is not specified.")
if payload is None:
2023-05-12 03:24:06 +00:00
payload = dict(role_id=player_id, server=recognize_genshin_server(player_id))
2023-05-05 04:06:43 +00:00
else:
2023-05-12 03:24:06 +00:00
payload = dict(role_id=player_id, server=recognize_genshin_server(player_id), **payload)
2023-05-05 04:06:43 +00:00
data, params = None, None
if method == "POST":
data = payload
else:
params = payload
return await self.request_game_record(
endpoint,
2023-07-23 09:41:26 +00:00
endpoint_type=endpoint_type,
2023-05-05 04:06:43 +00:00
lang=lang,
game=Game.GENSHIN,
region=recognize_region(player_id, game=Game.GENSHIN),
params=params,
data=data,
)
async def get_partial_genshin_user(
self,
player_id: Optional[int] = None,
*,
lang: Optional[str] = None,
) -> PartialGenshinUserStats:
"""Get partial genshin user without character equipment.
Args:
player_id (Optional[int], optional): The player ID. Defaults to None.
lang (Optional[str], optional): The language of the data. Defaults to None.
Returns:
PartialGenshinUserStats: The requested partial genshin user without character equipment.
"""
data = await self._request_genshin_record("index", player_id, lang=lang)
return PartialGenshinUserStats(**data)
async def get_genshin_characters(
self,
player_id: Optional[int] = None,
*,
lang: Optional[str] = None,
) -> List[Character]:
"""Get genshin user characters.
Args:
player_id (Optional[int], optional): The player ID. Defaults to None.
lang (Optional[str], optional): The language of the data. Defaults to None.
Returns:
Character: The requested genshin user characters.
"""
2023-05-12 03:24:06 +00:00
data = await self._request_genshin_record("character", player_id, lang=lang, method="POST")
2023-05-05 04:06:43 +00:00
return [Character(**i) for i in data["avatars"]]
async def get_genshin_user(
self,
player_id: Optional[int] = None,
*,
lang: Optional[str] = None,
) -> GenshinUserStats:
"""Get genshin user stats.
Args:
player_id (Optional[int], optional): The player ID. Defaults to None.
lang (Optional[str], optional): The language of the data. Defaults to None.
Returns:
GenshinUserStats: The requested genshin user stats.
"""
data, character_data = await asyncio.gather(
self._request_genshin_record("index", player_id, lang=lang),
2023-05-12 03:24:06 +00:00
self._request_genshin_record("character", player_id, lang=lang, method="POST"),
2023-05-05 04:06:43 +00:00
)
data = {**data, **character_data}
return GenshinUserStats(**data)
async def get_genshin_spiral_abyss(
self,
player_id: Optional[int] = None,
*,
previous: bool = False,
lang: Optional[str] = None,
) -> SpiralAbyss:
"""Get genshin spiral abyss runs.
Args:
player_id (Optional[int], optional): The player ID. Defaults to None.
previous (bool, optional): Whether to retrieve the data for the previous season of the Spiral Abyss.
Defaults to False.
lang (Optional[str], optional): The language of the data. Defaults to None.
Returns:
SpiralAbyss: genshin spiral abyss runs.
"""
payload = dict(schedule_type=2 if previous else 1)
2023-05-12 03:24:06 +00:00
data = await self._request_genshin_record("spiralAbyss", player_id, lang=lang, payload=payload)
2023-05-05 04:06:43 +00:00
return SpiralAbyss(**data)
async def get_genshin_imaginarium_theater(
self,
player_id: Optional[int] = None,
need_detail: Optional[bool] = True,
*,
lang: Optional[str] = None,
) -> ImgTheater:
"""Get genshin imaginarium theater runs.
Args:
player_id (Optional[int], optional): The player ID. Defaults to None.
need_detail (Optional[bool], optional): Whether to retrieve detailed data. Defaults to True.
lang (Optional[str], optional): The language of the data. Defaults to None.
Returns:
ImgTheater: genshin imaginarium theater runs.
"""
payload = {
"need_detail": need_detail,
}
data = await self._request_genshin_record("role_combat", player_id, lang=lang, payload=payload)
return ImgTheater(**data)
2023-05-05 04:06:43 +00:00
async def get_genshin_notes(
self,
player_id: Optional[int] = None,
*,
lang: Optional[str] = None,
autoauth: bool = True,
) -> Notes:
"""Get Genshin's real-time notes.
Args:
player_id (Optional[int], optional): The player ID. Defaults to None.
lang (Optional[str], optional): The language of the data. Defaults to None.
autoauth (bool, optional): Whether to automatically authenticate the user. Defaults to True.
Returns:
2023-07-23 09:41:26 +00:00
Notes: The requested real-time notes.
2023-05-05 04:06:43 +00:00
Raises:
BadRequest: If the request is invalid.
DataNotPublic: If the requested data is not public.
"""
try:
data = await self._request_genshin_record("dailyNote", player_id, lang=lang)
except DataNotPublic as e:
# error raised only when real-time notes are not enabled
if player_id and self.player_id != player_id:
2023-05-12 03:24:06 +00:00
raise BadRequest(e.response, "Cannot view real-time notes of other users.") from e
2023-05-05 04:06:43 +00:00
if not autoauth:
raise BadRequest(e.response, "Real-time notes are not enabled.") from e
await self.update_settings(3, True, game=Game.GENSHIN)
data = await self._request_genshin_record("dailyNote", lang=lang)
return Notes(**data)
async def get_full_genshin_user(
self,
player_id: Optional[int] = None,
*,
lang: Optional[str] = None,
) -> FullGenshinUserStats:
"""Get a genshin user with all their possible data.
Args:
player_id (Optional[int], optional): The player ID. Defaults to None.
lang (Optional[str], optional): The language of the data. Defaults to None.
Returns:
Character: The requested genshin user with all their possible data.
"""
index, character, abyss1, abyss2, activities = await asyncio.gather(
self._request_genshin_record("index", player_id, lang=lang),
2023-05-12 03:24:06 +00:00
self._request_genshin_record("character", player_id, lang=lang, method="POST"),
2023-05-05 04:06:43 +00:00
self.get_genshin_spiral_abyss(player_id, lang=lang, previous=False),
self.get_genshin_spiral_abyss(player_id, lang=lang, previous=True),
self.get_genshin_activities(player_id, lang=lang),
)
user = {**index, **character}
2023-05-05 04:06:43 +00:00
abyss = SpiralAbyssPair(current=abyss1, previous=abyss2)
return FullGenshinUserStats(**user, abyss=abyss, activities=activities)
2023-05-05 04:06:43 +00:00
2023-05-12 03:24:06 +00:00
async def get_genshin_activities(self, player_id: Optional[int] = None, *, lang: Optional[str] = None) -> Dict:
2023-05-05 04:06:43 +00:00
"""Get genshin activities.
Args:
player_id (Optional[int], optional): The player ID. Defaults to None.
lang (Optional[str], optional): The language of the data. Defaults to None.
Returns:
Dict: The requested get_genshin_activities.
"""
return await self._request_genshin_record("activities", player_id, lang=lang)
2023-05-28 09:32:21 +00:00
async def get_record_card(
self,
account_id: Optional[int] = None,
*,
lang: Optional[str] = None,
) -> Optional[RecordCard]:
"""Get a genshin player record cards.
Args:
account_id: Optional[int], the user's account ID, defaults to None
lang: Optional[str], the language version of the request, defaults to None
Returns:
Genshin player record cards.
Returns:
Optional[RecordCard]: RecordCard objects.
"""
account_id = account_id or self.account_id
record_cards = await self.get_record_cards(account_id, lang=lang)
for record_card in record_cards:
if record_card.game == Game.GENSHIN:
return record_card
return None
2023-07-23 09:41:26 +00:00
async def get_genshin_notes_by_stoken(
self,
lang: Optional[str] = None,
2023-08-05 11:02:09 +00:00
) -> Union[NotesWidget, NotesOverseaWidget]:
2023-07-23 09:41:26 +00:00
"""Get Genshin's real-time notes.
Args:
lang (Optional[str], optional): The language of the data. Defaults to None.
Returns:
2023-08-05 11:02:09 +00:00
NotesWidget (Union[NotesWidget, NotesOverseaWidget]): The requested real-time notes.
2023-07-23 09:41:26 +00:00
Raises:
BadRequest: If the request is invalid.
"""
stoken = self.cookies.get("stoken")
if stoken is None:
raise ValueError("stoken not found in cookies.")
stuid = self.cookies.get("stuid")
if stuid is None and self.account_id is None:
raise ValueError("account_id or stuid not found")
if self.account_id is not None and stuid is None:
self.cookies.set("stuid", str(self.account_id))
2023-08-05 11:00:58 +00:00
if self.region == Region.OVERSEAS:
2024-07-05 13:58:00 +00:00
route = RECORD_URL.get_url(self.region, self.game) / "../community/apihub/api/widget/data"
2023-08-05 11:00:58 +00:00
params = {"game_id": "2"}
data = await self.request_lab(route, params=params, lang=lang)
model = NotesOverseaWidget
else:
data = await self._request_genshin_record("widget/v2", endpoint_type="aapi", lang=lang)
model = NotesWidget
return model(**data)