Skip to content
This repository has been archived by the owner on Jul 21, 2022. It is now read-only.

Commit

Permalink
Handle the URLError properly #162
Browse files Browse the repository at this point in the history
  • Loading branch information
iblancasa committed Dec 6, 2017
1 parent a8245fa commit a15f322
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/githubcity/ghcity.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,22 +504,28 @@ def __readAPI(self, url):
self.__logger.debug("Getting " + url)
response = urlopen(req)
code = response.code
except HTTPError as e:
if hasattr(e, "headers"):
reset = int(e.headers("X-RateLimit-Reset"))
if reset < 0:
log_message = "Error when reading response. Wait: 30 secs"
sleep_duration = 30
else:
utcAux = datetime.datetime.utcnow()
utcAux = utcAux.utctimetuple()
now_sec = timegm(utcAux)
sleep_duration = reset - now_sec
log_message = "Limit of API. Wait: "
log_message += str(sleep_duration)
log_message += " secs"
self.__logger.warning(log_message)
sleep(sleep_duration)
except HTTPError as error:
headers = error.headers.items()

reset = -1

for header in headers:
if header[0] == "X-RateLimit-Reset":
reset = int(header[1])

if reset < 0:
log_message = "Error when reading response. Wait: 30 secs"
sleep_duration = 30
else:
utcAux = datetime.datetime.utcnow()
utcAux = utcAux.utctimetuple()
now_sec = timegm(utcAux)
sleep_duration = reset - now_sec
log_message = "Limit of API. Wait: "
log_message += str(sleep_duration)
log_message += " secs"
self.__logger.warning(log_message)
sleep(sleep_duration)
code = 0
# pylint: disable=W0703
except Exception as e:
Expand Down

0 comments on commit a15f322

Please sign in to comment.