From 71b6558b9d94c14007dfc5676f824a80aa4378e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Sun, 29 Apr 2018 18:39:51 +0200 Subject: [PATCH] Handle HTTP errors in Twitch.get_messages() --- bot.py | 2 ++ twitch.py | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/bot.py b/bot.py index 512152a..cc0a411 100644 --- a/bot.py +++ b/bot.py @@ -153,6 +153,8 @@ class TwitchBot(irc.bot.SingleServerIRCBot): user_id = config['Twitch'].getint('target_user_id') since = dateutil.parser.parse(since).date() messages = Twitch(api_url, client_id, log).get_messages(user_id, since) + if not messages: + return for message in messages: for pattern, action in self.patterns: m = pattern.match(message) diff --git a/twitch.py b/twitch.py index 03e5f04..06c705d 100644 --- a/twitch.py +++ b/twitch.py @@ -47,11 +47,17 @@ class Twitch(object): return result def get_messages(self, user_id, since): - videos = self._get_videos(user_id) - result = [] - for video in [v for v in videos if v['date'] >= since]: + try: + videos = self._get_videos(user_id) + result = [] + for video in [v for v in videos if v['date'] >= since]: + if self.log: + self.log.info('Processing VOD %d (%s)', video['id'], video['title']) + comments = self._get_comments(video['id']) + result.extend(comments) + except requests.exceptions.HTTPError as e: if self.log: - self.log.info('Processing VOD %d (%s)', video['id'], video['title']) - comments = self._get_comments(video['id']) - result.extend(comments) - return result + self.log.error('Failed to retrieve VOD messages: %s', str(e)) + return None + else: + return result