diff --git a/src/bot.py b/src/bot.py index c0e349f..9d99596 100644 --- a/src/bot.py +++ b/src/bot.py @@ -33,4 +33,7 @@ async def main_loop(): local_wiki = all_wikis[wiki[0]] # set a reference to a wiki object from memory if all_wikis[wiki[0]].mw_messages is None: extended = True - wiki_response = await all_wikis[wiki[0]].fetch_wiki(extended) \ No newline at end of file + wiki_response = await local_wiki.fetch_wiki(extended) + try: + await local_wiki.check_status(wiki[0], wiki_response.status, db_wiki[1]) + except: \ No newline at end of file diff --git a/src/exceptions.py b/src/exceptions.py new file mode 100644 index 0000000..6fde0c1 --- /dev/null +++ b/src/exceptions.py @@ -0,0 +1,17 @@ +class WikiError(Exception): + pass + +class WikiServerError(Exception): + pass + +class WikiNotFoundError(Exception): + pass + +class WikiRemovedError(Exception): + pass + +class WikiUnauthorizedError(Exception): + pass + +class OtherWikiError(Exception): + pass \ No newline at end of file diff --git a/src/session.py b/src/session.py index 83780eb..2b3b2aa 100644 --- a/src/session.py +++ b/src/session.py @@ -1,4 +1,4 @@ import aiohttp from src.config import settings -session = aiohttp.ClientSession(headers=settings["header"]) +session = aiohttp.ClientSession(headers=settings["header"], timeout=aiohttp.ClientTimeout(5.0)) diff --git a/src/wiki.py b/src/wiki.py index 9fd337c..4d42e55 100644 --- a/src/wiki.py +++ b/src/wiki.py @@ -1,13 +1,16 @@ from dataclasses import dataclass from src.session import session +import logging, aiohttp +from src.exceptions import * +logger = logging.getLogger("rcgcdb.wiki") @dataclass class Wiki: mw_messages: int = None fail_times: int = 0 # corresponding to amount of times connection with wiki failed for client reasons (400-499) - async def fetch_wiki(self, extended, api_path): + async def fetch_wiki(self, extended, api_path) -> aiohttp.ClientResponse: url_path = api_path amount = 20 if extended: @@ -25,4 +28,23 @@ class Wiki: "rcprop": "title|redirect|timestamp|ids|loginfo|parsedcomment|sizes|flags|tags|user", "rclimit": amount, "rctype": "edit|new|log|external", "siprop": "namespaces"} try: - await session.get(url_path, params=params) \ No newline at end of file + response = await session.get(url_path, params=params) + except: + raise NotImplemented + return response + + async def check_status(self, wiki_id, status, name): + if 199 < status < 300: + self.fail_times = 0 + pass + elif 400 < status < 500: # ignore 400 error since this might be our fault + self.fail_times += 1 + logger.warning("Wiki {} responded with HTTP code {}, increased fail_times to {}, skipping...".format(name, status, self.fail_times)) + if self.fail_times > 3: + await self.remove(wiki_id) + raise WikiError + elif 499 < status < 600: + logger.warning("Wiki {} responded with HTTP code {}, skipping...".format(name, status, self.fail_times)) + raise WikiServerError + + async def remove(self, wiki_id): \ No newline at end of file