From 34b86c4227f5f8ecd088d782c2e10195a2dc2c21 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Tue, 22 Sep 2020 16:48:10 +0530 Subject: handled exceptiom of ContentTypeError at search_wikipedia --- bot/exts/evergreen/wikipedia.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index c1fff873..6cf8f592 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -3,6 +3,7 @@ import datetime import logging from typing import List +from aiohttp import client_exceptions from discord import Color, Embed, Message from discord.ext import commands @@ -28,18 +29,20 @@ class WikipediaCog(commands.Cog): async def search_wikipedia(self, search_term: str) -> List[str]: """Search wikipedia and return the first 10 pages found.""" - async with self.http_session.get(SEARCH_API.format(search_term=search_term)) as response: - data = await response.json() - pages = [] + async with self.http_session.get(SEARCH_API.format(search_term=search_term)) as response: + try: + data = await response.json() - search_results = data["query"]["search"] + search_results = data["query"]["search"] - # Ignore pages with "may refer to" - for search_result in search_results: - log.info("trying to append titles") - if "may refer to" not in search_result["snippet"]: - pages.append(search_result["title"]) + # Ignore pages with "may refer to" + for search_result in search_results: + log.info("trying to append titles") + if "may refer to" not in search_result["snippet"]: + pages.append(search_result["title"]) + except client_exceptions.ContentTypeError: + pages = None log.info("Finished appending titles") return pages -- cgit v1.2.3 From 3ec47214966f9547b104c416f5ff37e1415536b3 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Tue, 22 Sep 2020 17:24:11 +0530 Subject: changed List[str]->Optional[List[str]] --- bot/exts/evergreen/wikipedia.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 6cf8f592..f8711f90 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -1,7 +1,7 @@ import asyncio import datetime import logging -from typing import List +from typing import List, Optional from aiohttp import client_exceptions from discord import Color, Embed, Message @@ -27,7 +27,7 @@ class WikipediaCog(commands.Cog): """Formating wikipedia link with index and title.""" return f'`{index}` [{title}]({WIKIPEDIA_URL.format(title=title.replace(" ", "_"))})' - async def search_wikipedia(self, search_term: str) -> List[str]: + async def search_wikipedia(self, search_term: str) -> Optional[List[str]]: """Search wikipedia and return the first 10 pages found.""" pages = [] async with self.http_session.get(SEARCH_API.format(search_term=search_term)) as response: -- cgit v1.2.3 From a9916fd972a961be21f8ea641df7834840647a2e Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Tue, 22 Sep 2020 22:02:39 +0530 Subject: Changed WikipediaCog-> Wikipedia Change this becuase this causing issue in help command --- bot/exts/evergreen/wikipedia.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index f8711f90..f0fe494e 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -15,7 +15,7 @@ SEARCH_API = "https://en.wikipedia.org/w/api.php?action=query&list=search&srsear WIKIPEDIA_URL = "https://en.wikipedia.org/wiki/{title}" -class WikipediaCog(commands.Cog): +class Wikipedia(commands.Cog): """Get info from wikipedia.""" def __init__(self, bot: commands.Bot): @@ -111,4 +111,4 @@ class WikipediaCog(commands.Cog): def setup(bot: commands.Bot) -> None: """Wikipedia Cog load.""" - bot.add_cog(WikipediaCog(bot)) + bot.add_cog(Wikipedia(bot)) -- cgit v1.2.3 From fe899c1446a544adaa8078833f6dceecb6b11351 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Tue, 22 Sep 2020 22:10:25 +0530 Subject: Changed Wikipedia to WikipediaSearch --- bot/exts/evergreen/wikipedia.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index f0fe494e..be36e2c4 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -15,7 +15,7 @@ SEARCH_API = "https://en.wikipedia.org/w/api.php?action=query&list=search&srsear WIKIPEDIA_URL = "https://en.wikipedia.org/wiki/{title}" -class Wikipedia(commands.Cog): +class WikipediaSearch(commands.Cog): """Get info from wikipedia.""" def __init__(self, bot: commands.Bot): @@ -111,4 +111,4 @@ class Wikipedia(commands.Cog): def setup(bot: commands.Bot) -> None: """Wikipedia Cog load.""" - bot.add_cog(Wikipedia(bot)) + bot.add_cog(WikipediaSearch(bot)) -- cgit v1.2.3