From 415c2787783b6e2185d8bc82b27e923e5c952b90 Mon Sep 17 00:00:00 2001 From: Frisk Date: Fri, 10 Jul 2020 15:38:36 +0200 Subject: [PATCH] Added some code --- src/bot.py | 21 ++++++++++++++++++--- src/session.py | 4 ++++ src/wiki.py | 23 +++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/session.py diff --git a/src/bot.py b/src/bot.py index 10c721f..c0e349f 100644 --- a/src/bot.py +++ b/src/bot.py @@ -2,6 +2,7 @@ import logging.config from src.config import settings import sqlite3 from src.wiki import Wiki +import asyncio, aiohttp logging.config.dictConfig(settings["logging"]) logger = logging.getLogger("rcgcdb.bot") @@ -10,12 +11,26 @@ logger.debug("Current settings: {settings}".format(settings=settings)) conn = sqlite3.connect('rcgcdb.db') c = conn.cursor() -# Fetch basic information about all of the wikis in the database +# Log Fail states with structure wiki_id: number of fail states all_wikis = {} +mw_msgs = {} # will have the type of id: tuple -for wiki in c.execute('SELECT * FROM wikis'): - all_wikis[wiki[0]] = Wiki() # assign cached information +# First populate the all_wikis list with every wiki +# Reasons for this: 1. we require amount of wikis to calculate the cooldown between requests +# 2. Easier to code +for wiki in c.execute('SELECT ROWID, * FROM wikis'): + all_wikis[wiki[0]] = Wiki() # Start queueing logic +async def main_loop(): + for db_wiki in c.execute('SELECT ROWID, * FROM wikis'): + extended = False + if wiki[0] not in all_wikis: + logger.debug("New wiki: {}".format(wiki[1])) + all_wikis[wiki[0]] = Wiki() + 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 diff --git a/src/session.py b/src/session.py new file mode 100644 index 0000000..83780eb --- /dev/null +++ b/src/session.py @@ -0,0 +1,4 @@ +import aiohttp +from src.config import settings + +session = aiohttp.ClientSession(headers=settings["header"]) diff --git a/src/wiki.py b/src/wiki.py index 944ad26..9fd337c 100644 --- a/src/wiki.py +++ b/src/wiki.py @@ -1,5 +1,28 @@ from dataclasses import dataclass +from src.session import session + @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): + url_path = api_path + amount = 20 + if extended: + params = {"action": "query", "format": "json", "uselang": "content", "list": "tags|recentchanges", + "meta": "allmessages|siteinfo", + "utf8": 1, "tglimit": "max", "tgprop": "displayname", + "rcprop": "title|redirect|timestamp|ids|loginfo|parsedcomment|sizes|flags|tags|user", + "rclimit": amount, "rctype": "edit|new|log|external", + "ammessages": "recentchanges-page-added-to-category|recentchanges-page-removed-from-category|recentchanges-page-added-to-category-bundled|recentchanges-page-removed-from-category-bundled", + "amenableparser": 1, "amincludelocal": 1, "siprop": "namespaces"} + else: + params = {"action": "query", "format": "json", "uselang": "content", "list": "tags|recentchanges", + "utf8": 1, + "tglimit": "max", "tgprop": "displayname", + "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