Skip to content

Commit

Permalink
Fix Stream.fetch_game erroring for streams without a set game (#478)
Browse files Browse the repository at this point in the history
* Fix Stream.fetch_game erroring for streams without a set game

Signed-off-by: Lilly Rose Berner <[email protected]>

* Address review comments

Signed-off-by: Lilly Rose Berner <[email protected]>

---------

Signed-off-by: Lilly Rose Berner <[email protected]>
  • Loading branch information
LostLuma authored Dec 23, 2024
1 parent c4d22d2 commit 23828bc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
26 changes: 16 additions & 10 deletions twitchio/models/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand All @@ -115,19 +115,25 @@ def __init__(self, data: StreamsResponseData, *, http: HTTPClient) -> None:
def __repr__(self) -> str:
return f"<Stream id={self.id} user={self.user} title={self.title} started_at={self.started_at}>"

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:
Expand Down
4 changes: 2 additions & 2 deletions twitchio/types_/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 23828bc

Please sign in to comment.