diff --git a/extensions/base/mediawiki.py b/extensions/base/mediawiki.py index db0362a..50d6ecc 100644 --- a/extensions/base/mediawiki.py +++ b/extensions/base/mediawiki.py @@ -18,7 +18,6 @@ import math from src.discord.message import DiscordMessage from src.api import formatter from src.i18n import rc_formatters -from src.api.client import Client from src.api.context import Context from src.configloader import settings from src.exceptions import * diff --git a/src/api/client.py b/src/api/client.py index f311029..f72f124 100644 --- a/src/api/client.py +++ b/src/api/client.py @@ -13,18 +13,19 @@ # You should have received a copy of the GNU General Public License # along with RcGcDw. If not, see . -import src.rcgcdw + import src.misc from typing import Union from collections import OrderedDict + class Client: """ A client for interacting with RcGcDw when creating formatters or hooks. """ - def __init__(self): - self._formatters = src.rcgcdw.formatter_hooks - self.__recent_changes = src.rcgcdw.wiki + def __init__(self, hooks, wiki): + self._formatters = hooks + self.__recent_changes = wiki self.WIKI_API_PATH = src.misc.WIKI_API_PATH self.WIKI_ARTICLE_PATH = src.misc.WIKI_ARTICLE_PATH self.WIKI_SCRIPT_PATH = src.misc.WIKI_SCRIPT_PATH @@ -60,7 +61,4 @@ class Client: return self.__recent_changes.api_request(params, *json_path, timeout, allow_redirects) def get_formatters(self): - return self._formatters - - -client = Client() + return self._formatters \ No newline at end of file diff --git a/src/api/util.py b/src/api/util.py new file mode 100644 index 0000000..01aab94 --- /dev/null +++ b/src/api/util.py @@ -0,0 +1,40 @@ +# This file is part of Recent changes Goat compatible Discord webhook (RcGcDw). +# +# RcGcDw is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# RcGcDw is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with RcGcDw. If not, see . +import re +from urllib.parse import quote +from typing import Optional, Callable +from src.discord.message import DiscordMessage +from src.configloader import settings +import src.misc + + +def default_message(event: str, formatter_hooks: dict) -> Callable: + """Returns a method of a formatter responsible for the event or None if such does not exist.""" + return formatter_hooks.get(event, formatter_hooks.get("generic", formatter_hooks["no_formatter"])) + + +def link_formatter(link: str) -> str: + """Formats a link to not embed it""" + return "<" + quote(link.replace(" ", "_"), "/:?=&") + ">" + + +def escape_formatting(data: str) -> str: + """Escape Discord formatting""" + return re.sub(r"([`_*~<>{}@/|\\])", "\\\\\\1", data, 0) + + +def create_article_path(article: str) -> str: + """Takes the string and creates an URL with it as the article name""" + return src.misc.WIKI_ARTICLE_PATH.replace("$1", article) \ No newline at end of file diff --git a/src/rcgcdw.py b/src/rcgcdw.py index 95e0acb..f3da4d7 100644 --- a/src/rcgcdw.py +++ b/src/rcgcdw.py @@ -24,12 +24,12 @@ import time, logging.config, requests, datetime, gettext, math, os.path, schedul import src.misc from collections import defaultdict, Counter -import src.api.client from typing import Optional +import src.api.client from src.api.context import Context from src.configloader import settings from src.misc import add_to_dict, datafile, \ - WIKI_API_PATH, LinkParser + WIKI_API_PATH from src.api.util import create_article_path, default_message from src.discord.queue import send_to_discord from src.discord.message import DiscordMessage, DiscordMessageMetadata @@ -60,6 +60,7 @@ def load_extensions(): except ImportError: logger.critical("No extensions module found. What's going on?") sys.exit(1) + storage = datafile # Remove previous data holding file if exists and limitfetch allows @@ -229,11 +230,12 @@ def day_overview(): def rc_processor(change, changed_categories): """Prepares essential information for both embed and compact message format.""" + from src.misc import LinkParser LinkParser = LinkParser() metadata = DiscordMessageMetadata("POST", rev_id=change.get("revid", None), log_id=change.get("logid", None), page_id=change.get("pageid", None)) logger.debug(change) - context = Context(settings["appearance"]["mode"], settings["webhookURL"], src.api.client.client) + context = Context(settings["appearance"]["mode"], settings["webhookURL"], client) if ("actionhidden" in change or "suppressed" in change) and "suppressed" not in settings["ignored"]: # if event is hidden using suppression context.event = "suppressed" discord_message: Optional[DiscordMessage] = default_message("suppressed", formatter_hooks)(context, change) @@ -269,6 +271,7 @@ def abuselog_processing(entry, recent_changes): # Log in and download wiki information wiki = Wiki(rc_processor, abuselog_processing) +client = src.api.client.Client(formatter_hooks, wiki) try: if settings["wiki_bot_login"] and settings["wiki_bot_password"]: wiki.log_in()