From f62bef8938c41323c8979ff7e586c86ac5c4a4d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Fri, 11 May 2018 11:01:13 +0200 Subject: [PATCH] Implement getting info about current live stream --- twitch.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/twitch.py b/twitch.py index 06c705d..6a12ae6 100644 --- a/twitch.py +++ b/twitch.py @@ -46,6 +46,21 @@ class Twitch(object): break return result + def _get_stream_info(self, user_id): + def request(): + url = '{0}/streams/{1}'.format(self.api_url, user_id) + params = dict(client_id=self.client_id) + r = requests.get(url, params=params) + r.raise_for_status() + return r.json() + data = request() + if data['stream'] is None: + return None + return dict( + id=int(data['stream']['_id']), + game=data['stream']['game'], + viewers=int(data['stream']['viewers'])) + def get_messages(self, user_id, since): try: videos = self._get_videos(user_id) @@ -61,3 +76,11 @@ class Twitch(object): return None else: return result + + def get_stream_info(self, user_id): + try: + return self._get_stream_info(user_id) + except requests.exceptions.HTTPError as e: + if self.log: + self.log.error('Failed to get stream info: %s', str(e)) + return None