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