diff --git a/src/bot.py b/src/bot.py index fed0965..cf180e2 100644 --- a/src/bot.py +++ b/src/bot.py @@ -18,7 +18,7 @@ logging.config.dictConfig(settings["logging"]) logger = logging.getLogger("rcgcdb.bot") logger.debug("Current settings: {settings}".format(settings=settings)) -# Log Fail states with structure wiki_id: number of fail states +# Log Fail states with structure wiki_url: number of fail states all_wikis: dict = {} mw_msgs: dict = {} # will have the type of id: tuple @@ -43,10 +43,9 @@ def calculate_delay() -> float: def generate_targets(wiki_url: str) -> defaultdict: combinations = defaultdict(list) - for webhook in db_cursor.execute('SELECT ROWID, * FROM rcgcdw WHERE wiki = ?', (wiki_url,)): - # rowid, guild, configid, webhook, wiki, lang, display, rcid, wikiid, postid - combination = (webhook[5], webhook[6]) # lang, display - combinations[combination].append(webhook[3]) + for webhook in db_cursor.execute('SELECT webhook, lang, display FROM rcgcdw WHERE wiki = ?', (wiki_url,)): + combination = (webhook[1], webhook[2]) # lang, display + combinations[combination].append(webhook[0]) return combinations @@ -54,21 +53,22 @@ async def wiki_scanner(): try: while True: calc_delay = calculate_delay() - fetch_all = db_cursor.execute('SELECT * FROM rcgcdw GROUP BY wiki') + fetch_all = db_cursor.execute('SELECT webhook, wiki, lang, display, wikiid, rcid, postid FROM rcgcdw GROUP BY wiki') + # webhook, wiki, lang, display, wikiid, rcid, postid for db_wiki in fetch_all.fetchall(): - logger.debug("Wiki {}".format(db_wiki[3])) + logger.debug("Wiki {}".format(db_wiki[1])) extended = False - if db_wiki[3] not in all_wikis: - logger.debug("New wiki: {}".format(db_wiki[3])) - all_wikis[db_wiki[3]] = Wiki() - local_wiki = all_wikis[db_wiki[3]] # set a reference to a wiki object from memory + if db_wiki[1] not in all_wikis: + logger.debug("New wiki: {}".format(db_wiki[1])) + all_wikis[db_wiki[1]] = Wiki() + local_wiki = all_wikis[db_wiki[1]] # set a reference to a wiki object from memory if local_wiki.mw_messages is None: extended = True async with aiohttp.ClientSession(headers=settings["header"], timeout=aiohttp.ClientTimeout(2.0)) as session: try: - wiki_response = await local_wiki.fetch_wiki(extended, db_wiki[3], session) - await local_wiki.check_status(db_wiki[3], wiki_response.status) + wiki_response = await local_wiki.fetch_wiki(extended, db_wiki[1], session) + await local_wiki.check_status(db_wiki[1], wiki_response.status) except (WikiServerError, WikiError): logger.exception("Exeption when fetching the wiki") continue # ignore this wiki if it throws errors @@ -77,39 +77,39 @@ async def wiki_scanner(): if "error" in recent_changes_resp or "errors" in recent_changes_resp: error = recent_changes_resp.get("error", recent_changes_resp["errors"]) if error["code"] == "readapidenied": - await local_wiki.fail_add(db_wiki[3], 410) + await local_wiki.fail_add(db_wiki[1], 410) continue raise WikiError recent_changes = recent_changes_resp['query']['recentchanges'] recent_changes.reverse() except aiohttp.ContentTypeError: logger.exception("Wiki seems to be resulting in non-json content.") - await local_wiki.fail_add(db_wiki[3], 410) + await local_wiki.fail_add(db_wiki[1], 410) continue except: logger.exception("On loading json of response.") continue if extended: await process_mwmsgs(recent_changes_resp, local_wiki, mw_msgs) - if db_wiki[6] is None: # new wiki, just get the last rc to not spam the channel + if db_wiki[5] is None: # new wiki, just get the last rc to not spam the channel if len(recent_changes) > 0: - DBHandler.add(db_wiki[3], recent_changes[-1]["rcid"]) + DBHandler.add(db_wiki[1], recent_changes[-1]["rcid"]) continue else: - DBHandler.add(db_wiki[3], 0) + DBHandler.add(db_wiki[1], 0) continue categorize_events = {} - targets = generate_targets(db_wiki[3]) - paths = get_paths(db_wiki[3], recent_changes_resp) + targets = generate_targets(db_wiki[1]) + paths = get_paths(db_wiki[1], recent_changes_resp) for change in recent_changes: await process_cats(change, local_wiki, mw_msgs, categorize_events) for change in recent_changes: # Yeah, second loop since the categories require to be all loaded up - if change["rcid"] > db_wiki[6]: + if change["rcid"] > db_wiki[5]: for target in targets.items(): await essential_info(change, categorize_events, local_wiki, db_wiki, target, paths, recent_changes_resp) if recent_changes: - DBHandler.add(db_wiki[3], change["rcid"]) + DBHandler.add(db_wiki[1], change["rcid"]) DBHandler.update_db() await asyncio.sleep(delay=calc_delay) except asyncio.CancelledError: diff --git a/src/discord.py b/src/discord.py index dbd9d64..101a605 100644 --- a/src/discord.py +++ b/src/discord.py @@ -15,20 +15,20 @@ logger = logging.getLogger("rcgcdb.discord") # User facing webhook functions -async def wiki_removal(wiki_id, status): - for observer in db_cursor.execute('SELECT * FROM rcgcdw WHERE wiki = ?', (wiki_id,)): +async def wiki_removal(wiki_url, status): + for observer in db_cursor.execute('SELECT webhook, lang FROM rcgcdw WHERE wiki = ?', (wiki_url,)): def _(string: str) -> str: """Our own translation string to make it compatible with async""" - return langs[observer[4]].gettext(string) + return langs[observer[1]].gettext(string) reasons = {410: _("wiki deletion"), 404: _("wiki deletion"), 401: _("wiki becoming inaccessible"), 402: _("wiki becoming inaccessible"), 403: _("wiki becoming inaccessible"), 410: _("wiki becoming inaccessible")} reason = reasons.get(status, _("unknown error")) - await send_to_discord_webhook(DiscordMessage("compact", "webhook/remove", webhook_url=[observer[2]], content=_("The webhook for {} has been removed due to {}.".format(wiki_id, reason)), wiki=None)) + await send_to_discord_webhook(DiscordMessage("compact", "webhook/remove", webhook_url=[observer[0]], content=_("The webhook for {} has been removed due to {}.".format(wiki_url, reason)), wiki=None)) header = settings["header"] header['Content-Type'] = 'application/json' header['X-Audit-Log-Reason'] = "Wiki becoming unavailable" async with aiohttp.ClientSession(headers=header, timeout=aiohttp.ClientTimeout(5.0)) as session: - await session.delete("https://discord.com/api/webhooks/"+observer[2]) + await session.delete("https://discord.com/api/webhooks/"+observer[0]) async def webhook_removal_monitor(webhook_url: list, reason: int): @@ -102,8 +102,8 @@ class DiscordMessage: # Monitoring webhook functions -async def wiki_removal_monitor(wiki_id, status): - await send_to_discord_webhook_monitoring(DiscordMessage("compact", "webhook/remove", content="Removing {} because {}.".format(wiki_id, status), webhook_url=[None], wiki=None)) +async def wiki_removal_monitor(wiki_url, status): + await send_to_discord_webhook_monitoring(DiscordMessage("compact", "webhook/remove", content="Removing {} because {}.".format(wiki_url, status), webhook_url=[None], wiki=None)) async def send_to_discord_webhook_monitoring(data: DiscordMessage): diff --git a/src/wiki.py b/src/wiki.py index cf077bf..bff6d03 100644 --- a/src/wiki.py +++ b/src/wiki.py @@ -158,6 +158,7 @@ async def process_mwmsgs(wiki_response: dict, local_wiki: Wiki, mw_msgs: dict): mw_msgs[key] = msgs # it may be a little bit messy for sure, however I don't expect any reason to remove mw_msgs entries by one local_wiki.mw_messages = key +# db_wiki: webhook, wiki, lang, display, wikiid, rcid, postid async def essential_info(change: dict, changed_categories, local_wiki: Wiki, db_wiki: tuple, target: tuple, paths: tuple, request: dict): """Prepares essential information for both embed and compact message format.""" def _(string: str) -> str: