Пример #1
0
    async def fetch_channel(self, broadcaster: str):
        """|coro|

        Retrieve channel information from the API.

        Parameters
        -----------
        broadcaster: str
            The channel name or ID to request from API. Returns empty dict if no channel was found.

        Returns
        --------
            :class:`twitchio.ChannelInfo`
        """

        if not broadcaster.isdigit():
            get_id = await self.fetch_users(names=[broadcaster.lower()])
            if not get_id:
                raise IndexError("Invalid channel name.")
            broadcaster = get_id[0].id
        try:
            data = await self._http.get_channels(broadcaster)

            from .models import ChannelInfo

            return ChannelInfo(self._http, data=data[0])

        except HTTPException:
            raise HTTPException("Incorrect channel ID.")
Пример #2
0
    async def get_chatters(self, channel: str):
        """|coro|

        Method which retrieves the currently active chatters on the given stream.

        Parameters
        ------------
        channel: str
            The channel name to retrieve data for.

        Returns
        ---------
        Chatters
            Namedtuple containing active chatter data.

        Raises
        --------
        HTTPException
            Bad request while fetching stream chatters.
        """

        url = f'http://tmi.twitch.tv/group/user/{channel.lower()}/chatters'

        async with self.http._session.get(url) as resp:
            if 200 <= resp.status < 300:
                data = await resp.json()
            else:
                raise HTTPException(f'Fetching chatters failed: {resp.status}', resp.reason)

            all_ = []
            for x in data['chatters'].values():
                all_ += x

            return Chatters(data['chatter_count'], all_, *data['chatters'].values())
Пример #3
0
    async def handle_callback(self, request) -> web.Response:
        query = request.query

        try:
            if query['hub.mode'] == 'denied':
                asyncio.run_coroutine_threadsafe(self._bot._ws.event_error(
                    HTTPException(
                        f'Webhook subscription denied | {query["hub.reason"]}')
                ),
                                                 loop=self.loop)
                return web.Response(text='200: OK', status=200)

            if query['hub.challenge']:
                asyncio.run_coroutine_threadsafe(
                    self._bot.event_webhook(query), loop=self.loop)
                return web.Response(body=query['hub.challenge'],
                                    content_type='application/json')
        except KeyError:
            web.Response(text='Bad Request', status=400)

        return web.Response(text='200: OK', status=200)