From deee8cfebc69e63ddc0060bc888e7f00b90efe6e Mon Sep 17 00:00:00 2001 From: Den4200 Date: Thu, 1 Oct 2020 00:15:14 -0400 Subject: Removed unnecessary GitHub API request and checked response message instead. This check is to see if a GitHub user is non-existent. We do not want to log this as an error. --- bot/exts/halloween/hacktoberstats.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'bot') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index a2261caf..ed1755e3 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -24,6 +24,11 @@ REQUEST_HEADERS = {"User-Agent": "Python Discord Hacktoberbot"} if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" +GITHUB_NONEXISTENT_USER_MESSAGE = ( + "The listed users cannot be searched either because the users do not exist " + "or you do not have permission to view the users." +) + class HacktoberStats(commands.Cog): """Hacktoberfest statistics Cog.""" @@ -247,19 +252,21 @@ class HacktoberStats(commands.Cog): ) async with aiohttp.ClientSession() as session: - async with session.get(f"https://api.github.com/users/{github_username}", headers=REQUEST_HEADERS) as resp: - if resp.status == 404: - logging.debug(f"No GitHub user found named '{github_username}'") - return - async with session.get(query_url, headers=REQUEST_HEADERS) as resp: jsonresp = await resp.json() if "message" in jsonresp.keys(): # One of the parameters is invalid, short circuit for now api_message = jsonresp["errors"][0]["message"] - logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}") + + # Ignore logging non-existent users or users we do not have permission to see + if api_message == GITHUB_NONEXISTENT_USER_MESSAGE: + logging.debug(f"No GitHub user found named '{github_username}'") + else: + logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}") + return + else: if jsonresp["total_count"] == 0: # Short circuit if there aren't any PRs -- cgit v1.2.3