Skip to content

Commit dc76c2d

Browse files
authored
Add client fetch streams (#183)
* implemented a fetch_streams method for the Client class * put 1lines <120 back as they were * removed print, corrected docstring * changing per IAmTomahawkx's feedback for the PR * changed user_ids and game_ids to be List[int] per pr feedback
1 parent dc01b21 commit dc76c2d

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

twitchio/client.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ def __init__(
9191

9292
@classmethod
9393
def from_client_credentials(
94-
cls, client_id: str, client_secret: str, *, loop: asyncio.AbstractEventLoop = None
94+
cls,
95+
client_id: str,
96+
client_secret: str,
97+
*,
98+
loop: asyncio.AbstractEventLoop = None,
9599
) -> "Client":
96100
"""
97101
creates a client application token from your client credentials.
@@ -121,7 +125,7 @@ def from_client_credentials(
121125
self.loop = loop or asyncio.get_event_loop()
122126
self._http = TwitchHTTP(self, client_id=client_id, client_secret=client_secret)
123127
self._connection = WSConnection(
124-
client=self, loop=self.loop, initial_channels=None, heartbeat=self._heartbeat
128+
client=self, loop=self.loop, initial_channels=None
125129
) # The only reason we're even creating this is to avoid attribute errors
126130
self._events = {}
127131
self._waiting = []
@@ -480,6 +484,46 @@ async def fetch_tags(self, ids: List[str] = None):
480484
data = await self._http.get_stream_tags(ids)
481485
return [models.Tag(x) for x in data]
482486

487+
async def fetch_streams(
488+
self,
489+
user_ids: List[int] = None,
490+
game_ids: List[int] = None,
491+
user_logins: List[str] = None,
492+
languages: List[str] = None,
493+
token: str = None,
494+
):
495+
"""|coro|
496+
Fetches live streams from the helix API
497+
498+
Parameters
499+
-----------
500+
user_ids: Optional[List[:class:`int`]]
501+
user ids of people whose streams to fetch
502+
game_ids: Optional[List[:class:`int`]]
503+
game ids of streams to fetch
504+
user_logins: Optional[List[:class:`str`]]
505+
user login names of people whose streams to fetch
506+
languages: Optional[List:class:`str]]
507+
language for the stream(s). ISO 639-1 or two letter code for supported stream language
508+
token: Optional[:class:`str`]
509+
An optional OAuth token to use instead of the bot OAuth token
510+
511+
Returns
512+
--------
513+
List[:class:`twitchio.Stream`]
514+
"""
515+
from .models import Stream
516+
517+
assert user_ids or game_ids or user_logins
518+
data = await self._http.get_streams(
519+
game_ids=game_ids,
520+
user_ids=user_ids,
521+
user_logins=user_logins,
522+
languages=languages,
523+
token=token,
524+
)
525+
return [Stream(self._http, x) for x in data]
526+
483527
async def search_categories(self, query: str):
484528
"""|coro|
485529
Searches twitches categories

twitchio/models.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,38 @@ def __init__(self, data: dict):
530530

531531
def __repr__(self):
532532
return f"<WebhookSubscription callback={self.callback} topic={self.topic} expires_at={self.expires_at}>"
533+
534+
535+
class Stream:
536+
537+
__slots__ = (
538+
"id",
539+
"user",
540+
"game_id",
541+
"game_name",
542+
"type",
543+
"title",
544+
"viewer_count",
545+
"started_at",
546+
"language",
547+
"thumbnail_url",
548+
"tag_ids",
549+
"is_mature",
550+
)
551+
552+
def __init__(self, http: "TwitchHTTP", data: dict):
553+
self.id: int = data["id"]
554+
self.user = PartialUser(http, data["user_id"], data["user_name"], data["user_login"])
555+
self.game_id: int = data["game_id"]
556+
self.game_name: str = data["game_name"]
557+
self.type: str = data["type"]
558+
self.title: str = data["title"]
559+
self.viewer_count: int = data["viewer_count"]
560+
self.started_at = datetime.datetime.strptime(data["started_at"], "%Y-%m-%dT%H:%M:%SZ")
561+
self.language: str = data["language"]
562+
self.thumbnail_url: str = data["thumbnail_url"]
563+
self.tag_ids: List[str] = data["tag_ids"]
564+
self.is_mature: bool = data["is_mature"]
565+
566+
def __repr__(self):
567+
return f"<Stream id={self.id} user={self.user} title={self.title} started_at={self.started_at}>"

0 commit comments

Comments
 (0)