Skip to content

Commit 033d1e9

Browse files
committed
Add client.fetch_channels
Add fetch_channels method that follows Helix
1 parent e178fed commit 033d1e9

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Master
2222
- Added following new Client methods:
2323
- :func:`~twitchio.Client.fetch_chatters_colors`
2424
- :func:`~twitchio.Client.update_chatter_color`
25+
- :func:`~twitchio.Client.fetch_channels`
2526
- Add ``duration`` and ``vod_offset`` attributes to :class:`~twitchio.Clip`
2627
- Added repr for :class:`~twitchio.CustomReward`
2728
- Added repr for :class:`~twitchio.PredictionOutcome`

twitchio/client.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,15 +440,20 @@ async def fetch_clips(self, ids: List[str]):
440440
data = await self._http.get_clips(ids=ids)
441441
return [models.Clip(self._http, d) for d in data]
442442

443-
async def fetch_channel(self, broadcaster: str):
443+
async def fetch_channel(self, broadcaster: str, token: Optional[str] = None):
444444
"""|coro|
445445
446446
Retrieve channel information from the API.
447447
448+
.. note::
449+
This will be deprecated in 3.0. It's recommended to use :func:`~fetch_channels` instead.
450+
448451
Parameters
449452
-----------
450453
broadcaster: str
451454
The channel name or ID to request from API. Returns empty dict if no channel was found.
455+
token: Optional[:class:`str`]
456+
An optional OAuth token to use instead of the bot OAuth token.
452457
453458
Returns
454459
--------
@@ -459,9 +464,9 @@ async def fetch_channel(self, broadcaster: str):
459464
get_id = await self.fetch_users(names=[broadcaster.lower()])
460465
if not get_id:
461466
raise IndexError("Invalid channel name.")
462-
broadcaster = get_id[0].id
467+
broadcaster = str(get_id[0].id)
463468
try:
464-
data = await self._http.get_channels(broadcaster)
469+
data = await self._http.get_channels(broadcaster_id=broadcaster, token=token)
465470

466471
from .models import ChannelInfo
467472

@@ -470,6 +475,28 @@ async def fetch_channel(self, broadcaster: str):
470475
except HTTPException:
471476
raise HTTPException("Incorrect channel ID.")
472477

478+
async def fetch_channels(self, broadcaster_ids: List[int], token: Optional[str] = None):
479+
"""|coro|
480+
481+
Retrieve information for up to 100 channels from the API.
482+
483+
Parameters
484+
-----------
485+
broadcaster_ids: List[:class:`int`]
486+
The channel ids to request from API.
487+
token: Optional[:class:`str`]
488+
An optional OAuth token to use instead of the bot OAuth token
489+
490+
Returns
491+
--------
492+
List[:class:`twitchio.ChannelInfo`]
493+
"""
494+
from .models import ChannelInfo
495+
496+
data = await self._http.get_channels_new(broadcaster_ids=broadcaster_ids, token=token)
497+
return [ChannelInfo(self._http, data=d) for d in data]
498+
499+
473500
async def fetch_videos(
474501
self,
475502
ids: List[int] = None,

twitchio/http.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,16 @@ async def get_stream_markers(self, token: str, user_id: str = None, video_id: st
683683
)
684684
)
685685

686-
async def get_channels(self, broadcaster_id: str, token: str = None):
686+
async def get_channels(self, broadcaster_id: str, token: Optional[str]= None):
687687
return await self.request(Route("GET", "channels", query=[("broadcaster_id", broadcaster_id)], token=token))
688688

689+
690+
async def get_channels_new(self, broadcaster_ids: List[int], token: Optional[str] = None):
691+
if len(broadcaster_ids) > 100:
692+
raise ValueError("Maximum of 100 broadcaster_ids")
693+
q = [("broadcaster_id", str(broadcaster_id)) for broadcaster_id in broadcaster_ids]
694+
return await self.request(Route("GET", "channels", query=q, token=token))
695+
689696
async def patch_channel(
690697
self, token: str, broadcaster_id: str, game_id: str = None, language: str = None, title: str = None
691698
):

0 commit comments

Comments
 (0)