Skip to content

Commit

Permalink
Make wish record time timezone aware
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Sep 5, 2024
1 parent b510928 commit 5af9afd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
28 changes: 19 additions & 9 deletions genshin/client/components/gacha.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async def _get_gacha_page(
game: typing.Optional[types.Game] = None,
lang: typing.Optional[str] = None,
authkey: typing.Optional[str] = None,
) -> typing.Sequence[typing.Any]:
) -> typing.Tuple[typing.Sequence[typing.Any], int]:
"""Get a single page of wishes."""
data = await self.request_gacha_info(
"getGachaLog",
Expand All @@ -66,7 +66,18 @@ async def _get_gacha_page(
authkey=authkey,
params=dict(gacha_type=banner_type, real_gacha_type=banner_type, size=20, end_id=end_id),
)
return data["list"]

if game is types.Game.GENSHIN:
# Genshin doesn't return timezone data
# America: UTC-5, Europe: UTC+1, others are UTC+8
tz_offsets = {"os_usa": -13, "os_euro": -7}
tz_offset = tz_offsets.get(data["region"], 0)

tz_offset = data["region_time_zone"]
if game is types.Game.STARRAIL:
tz_offset -= 8 # Star rail returns UTC+n for this value

return data["list"], tz_offset

async def _get_wish_page(
self,
Expand All @@ -77,15 +88,14 @@ async def _get_wish_page(
authkey: typing.Optional[str] = None,
) -> typing.Sequence[models.Wish]:
"""Get a single page of wishes."""
data = await self._get_gacha_page(
data, tz_offset = await self._get_gacha_page(
end_id=end_id,
banner_type=banner_type,
lang=lang,
authkey=authkey,
game=types.Game.GENSHIN,
)

return [models.Wish(**i, banner_type=banner_type) for i in data]
return [models.Wish(**i, banner_type=banner_type, tz_offset=tz_offset) for i in data]

async def _get_warp_page(
self,
Expand All @@ -96,15 +106,15 @@ async def _get_warp_page(
authkey: typing.Optional[str] = None,
) -> typing.Sequence[models.Warp]:
"""Get a single page of warps."""
data = await self._get_gacha_page(
data, tz_offset = await self._get_gacha_page(
end_id=end_id,
banner_type=banner_type,
lang=lang,
authkey=authkey,
game=types.Game.STARRAIL,
)

return [models.Warp(**i, banner_type=banner_type) for i in data]
return [models.Warp(**i, banner_type=banner_type, tz_offset=tz_offset) for i in data]

async def _get_signal_page(
self,
Expand All @@ -115,15 +125,15 @@ async def _get_signal_page(
authkey: typing.Optional[str] = None,
) -> typing.Sequence[models.SignalSearch]:
"""Get a single page of warps."""
data = await self._get_gacha_page(
data, tz_offset = await self._get_gacha_page(
end_id=end_id,
banner_type=banner_type,
lang=lang,
authkey=authkey,
game=types.Game.ZZZ,
)

return [models.SignalSearch(**i, banner_type=banner_type) for i in data]
return [models.SignalSearch(**i, banner_type=banner_type, tz_offset=tz_offset) for i in data]

def wish_history(
self,
Expand Down
37 changes: 19 additions & 18 deletions genshin/models/genshin/gacha.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,42 @@ class ZZZBannerType(enum.IntEnum):
"""Bangboo banner."""


class Wish(APIModel, Unique):
"""Wish made on any banner."""
class BaseWish(APIModel, Unique):
"""Base wish model."""

uid: int

id: int
type: str = Aliased("item_type")
name: str
rarity: int = Aliased("rank_type")

tz_offset: int
"""Number of hours from UTC+8."""
time: datetime.datetime
"""Timezone-aware time of when the wish was made"""

@pydantic.validator("time", pre=True)
def __parse_time(cls, v: str, values: typing.Dict[str, typing.Any]) -> datetime.datetime:
return datetime.datetime.fromisoformat(v).replace(
tzinfo=datetime.timezone(datetime.timedelta(hours=8 + values["tz_offset"]))
)


class Wish(BaseWish):
"""Wish made on any banner."""

type: str = Aliased("item_type")
banner_type: GenshinBannerType

@pydantic.validator("banner_type", pre=True)
def __cast_banner_type(cls, v: typing.Any) -> int:
return int(v)


class Warp(APIModel, Unique):
class Warp(BaseWish):
"""Warp made on any banner."""

uid: int

id: int
item_id: int
type: str = Aliased("item_type")
name: str
rarity: int = Aliased("rank_type")
time: datetime.datetime

banner_type: StarRailBannerType
banner_id: int = Aliased("gacha_id")
Expand All @@ -123,17 +130,11 @@ def __cast_banner_type(cls, v: typing.Any) -> int:
return int(v)


class SignalSearch(APIModel, Unique):
class SignalSearch(BaseWish):
"""Signal Search made on any banner."""

uid: int

id: int
item_id: int
type: str = Aliased("item_type")
name: str
rarity: int = Aliased("rank_type")
time: datetime.datetime

banner_type: ZZZBannerType

Expand Down

0 comments on commit 5af9afd

Please sign in to comment.