From c9f8efc6cca4556b04adca2894cdd1b61c87d421 Mon Sep 17 00:00:00 2001 From: D0rs4n <41237606+D0rs4n@users.noreply.github.com> Date: Mon, 2 Aug 2021 19:13:03 +0200 Subject: Handle Wikipedia API errors and warnings in the Wikipedia Cog - Add an additional check to handle errors and warnings coming from the side of the Wikipedia API, as the response code sent by the Wikipedia API remains 200 even if there is an error. --- bot/exts/evergreen/wikipedia.py | 55 ++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 22 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 83937438..8af22737 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -39,31 +39,42 @@ class WikipediaSearch(commands.Cog): async with self.bot.http_session.get(url=url) as resp: if resp.status == 200: raw_data = await resp.json() - number_of_results = raw_data["query"]["searchinfo"]["totalhits"] - - if number_of_results: - results = raw_data["query"]["search"] - lines = [] - - for article in results: - line = WIKI_SEARCH_RESULT.format( - name=article["title"], - description=unescape( - re.sub( - WIKI_SNIPPET_REGEX, "", article["snippet"] - ) - ), - url=f"https://en.wikipedia.org/?curid={article['pageid']}" - ) - lines.append(line) - - return lines + if raw_data.get("query", None) is None: + if raw_data.get("errors", None) is not None: + log.info("There was an error regarding the Wikipedia API query.") + else: + log.info("There was an issue when trying to communicate with the Wikipedia API") - else: await channel.send( - "Sorry, we could not find a wikipedia article using that search term." - ) + "There was an issue processing your Wikipedia request, please try again later.") return + else: + + number_of_results = raw_data["query"]["searchinfo"]["totalhits"] + + if number_of_results: + results = raw_data["query"]["search"] + lines = [] + + for article in results: + line = WIKI_SEARCH_RESULT.format( + name=article["title"], + description=unescape( + re.sub( + WIKI_SNIPPET_REGEX, "", article["snippet"] + ) + ), + url=f"https://en.wikipedia.org/?curid={article['pageid']}" + ) + lines.append(line) + + return lines + + else: + await channel.send( + "Sorry, we could not find a wikipedia article using that search term." + ) + return else: log.info(f"Unexpected response `{resp.status}` while searching wikipedia for `{search}`") await channel.send( -- cgit v1.2.3