From 23828bca1d4da6202b3eb2382d26b84c8e8b6c29 Mon Sep 17 00:00:00 2001 From: Lilly Rose Berner Date: Mon, 23 Dec 2024 13:19:22 +0100 Subject: [PATCH] Fix Stream.fetch_game erroring for streams without a set game (#478) * Fix Stream.fetch_game erroring for streams without a set game Signed-off-by: Lilly Rose Berner * Address review comments Signed-off-by: Lilly Rose Berner --------- Signed-off-by: Lilly Rose Berner --- twitchio/models/streams.py | 26 ++++++++++++++++---------- twitchio/types_/responses.py | 4 ++-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/twitchio/models/streams.py b/twitchio/models/streams.py index 3c93fc01..e6a7070a 100644 --- a/twitchio/models/streams.py +++ b/twitchio/models/streams.py @@ -58,10 +58,10 @@ class Stream: The current stream ID. user: twitchio.PartialUser The user who is streaming. - game_id: str - Current game ID being played on the channel. - game_name: str - Name of the game being played on the channel. + game_id: str | None + Current game ID being played on the channel. Could be `None` if no category / game has been set. + game_name: str | None + Name of the game being played on the channel. Could be `None` if no category / game has been set. type: str Whether the stream is "live" or not. title: str @@ -101,8 +101,8 @@ def __init__(self, data: StreamsResponseData, *, http: HTTPClient) -> None: self.id: str = data["id"] self.user = PartialUser(data["user_id"], data["user_login"], data["user_name"], http=http) - self.game_id: str = data["game_id"] - self.game_name: str = data["game_name"] + self.game_id: str | None = data["game_id"] + self.game_name: str | None = data["game_name"] self.type: str = data["type"] self.title: str = data["title"] self.viewer_count: int = data["viewer_count"] @@ -115,19 +115,25 @@ def __init__(self, data: StreamsResponseData, *, http: HTTPClient) -> None: def __repr__(self) -> str: return f"" - async def fetch_game(self) -> Game: + async def fetch_game(self) -> Game | None: """Fetches the :class:`~twitchio.Game` associated with this stream. The :class:`~twitchio.Game` returned is current from the time the :class:`~twitchio.Stream` instance was created. + Could be `None` if no category / game was set at the time the :class:`~twitchio.Stream` + instance was created. + Returns ------- - twitchio.Game - The game associated with this :class:`~twitchio.Stream` instance. + twitchio.Game | None + The game associated with this :class:`~twitchio.Stream` instance, or `None`. """ + if self.game_id is None: + return None + payload: GamesResponse = await self._http.get_games(ids=[self.game_id]) - return Game(payload["data"][0], http=self._http) + return Game(payload["data"][0], http=self._http) if payload["data"] else None class StreamMarker: diff --git a/twitchio/types_/responses.py b/twitchio/types_/responses.py index 2b9c8f62..4d4ddc27 100644 --- a/twitchio/types_/responses.py +++ b/twitchio/types_/responses.py @@ -1451,8 +1451,8 @@ class StreamsResponseData(TypedDict): user_id: str user_login: str user_name: str - game_id: str - game_name: str + game_id: str | None + game_name: str | None type: Literal["live", ""] title: str tags: list[str]