From cd31a1a8bfb17a3d130d6cf33a058cf2d0e3d18c Mon Sep 17 00:00:00 2001 From: Laszlo Zeke Date: Fri, 27 Jan 2017 23:11:29 +0100 Subject: [PATCH] Added mechanism retry for url fetch --- TwitchRSS/twitchrss.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/TwitchRSS/twitchrss.py b/TwitchRSS/twitchrss.py index 0361e9a..4bcbc90 100644 --- a/TwitchRSS/twitchrss.py +++ b/TwitchRSS/twitchrss.py @@ -112,13 +112,16 @@ class RSSVoDServer(webapp2.RequestHandler): 'Client-ID': TWITCH_CLIENT_ID } request = urllib2.Request(url, headers=headers) - try: - result = urllib2.urlopen(request) - logging.debug('Fetch from twitch for %s with code %s' % (id, result.getcode())) - return result.read() - except urllib2.URLError as e: - logging.warning("Fetch exception caught: %s" % e) - return '' + retries = 0 + while retries < 3: + try: + result = urllib2.urlopen(request, timeout=3) + logging.debug('Fetch from twitch for %s with code %s' % (id, result.getcode())) + return result.read() + except BaseException as e: + logging.warning("Fetch exception caught: %s" % e) + retries += 1 + return '' def extract_userid(self, user_info): userlist = user_info.get('users')