mirror of
https://gitlab.com/chicken-riders/RcGcDw.git
synced 2025-02-23 00:24:09 +00:00
Some more work towards making the client work, added context class
This commit is contained in:
parent
d725b97c2a
commit
a9d6fa1830
|
@ -18,7 +18,8 @@ import math
|
||||||
from src.discord.message import DiscordMessage
|
from src.discord.message import DiscordMessage
|
||||||
from src.api import formatter
|
from src.api import formatter
|
||||||
from src.i18n import rc_formatters
|
from src.i18n import rc_formatters
|
||||||
from src.api.client import Client, client
|
from src.api.client import Client
|
||||||
|
from src.api.context import Context
|
||||||
from src.configloader import settings
|
from src.configloader import settings
|
||||||
from src.exceptions import *
|
from src.exceptions import *
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ class base():
|
||||||
super().__init__(api)
|
super().__init__(api)
|
||||||
|
|
||||||
@formatter.embed(event="edit", mode="embed")
|
@formatter.embed(event="edit", mode="embed")
|
||||||
def embed_edit(self, ctx: Client, change: dict) -> DiscordMessage:
|
def embed_edit(self, ctx: Context, change: dict) -> DiscordMessage:
|
||||||
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
|
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
|
||||||
action = ctx.event
|
action = ctx.event
|
||||||
editsize = change["newlen"] - change["oldlen"]
|
editsize = change["newlen"] - change["oldlen"]
|
||||||
|
@ -92,6 +93,7 @@ class base():
|
||||||
embed.add_field(_("Added"), "{data}".format(data=EditDiff.small_prev_ins), inline=True)
|
embed.add_field(_("Added"), "{data}".format(data=EditDiff.small_prev_ins), inline=True)
|
||||||
else:
|
else:
|
||||||
logger.warning("Unable to download data on the edit content!")
|
logger.warning("Unable to download data on the edit content!")
|
||||||
|
return embed
|
||||||
|
|
||||||
@formatter.compact(event="edit", mode="embed")
|
@formatter.compact(event="edit", mode="embed")
|
||||||
def compact_edit(self, change: dict):
|
def compact_edit(self, change: dict):
|
||||||
|
|
|
@ -60,5 +60,7 @@ class Client:
|
||||||
"""
|
"""
|
||||||
return self.__recent_changes.api_request(params, *json_path, timeout, allow_redirects)
|
return self.__recent_changes.api_request(params, *json_path, timeout, allow_redirects)
|
||||||
|
|
||||||
|
def get_formatters(self):
|
||||||
|
return self._formatters
|
||||||
|
|
||||||
client = Client()
|
client = Client()
|
||||||
|
|
23
src/api/context.py
Normal file
23
src/api/context.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
class Context:
|
||||||
|
"""Context object containing client and some metadata regarding specific formatter call"""
|
||||||
|
def __init__(self, message_type: str, webhook_url: str, client):
|
||||||
|
self.client = client
|
||||||
|
self.webhook_url = webhook_url
|
||||||
|
self.message_type = message_type
|
||||||
|
self.event = None
|
|
@ -14,11 +14,13 @@
|
||||||
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
|
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import src.rcgcdw
|
import src.rcgcdw
|
||||||
|
import logging
|
||||||
from src.configloader import settings
|
from src.configloader import settings
|
||||||
from src.exceptions import FormatterBreaksAPISpec
|
from src.exceptions import FormatterBreaksAPISpec
|
||||||
from src.discord.message import DiscordMessage
|
from src.discord.message import DiscordMessage
|
||||||
from typing import Optional, Callable
|
from typing import Optional, Callable
|
||||||
|
|
||||||
|
logger = logging.getLogger("src.api.formatter")
|
||||||
|
|
||||||
def _register_formatter(func: Callable[[dict], DiscordMessage], kwargs: dict[str, str], formatter_type: str,
|
def _register_formatter(func: Callable[[dict], DiscordMessage], kwargs: dict[str, str], formatter_type: str,
|
||||||
action_type: Optional[str]=None):
|
action_type: Optional[str]=None):
|
||||||
|
@ -35,6 +37,10 @@ def _register_formatter(func: Callable[[dict], DiscordMessage], kwargs: dict[str
|
||||||
if action_type is None:
|
if action_type is None:
|
||||||
raise FormatterBreaksAPISpec("event type")
|
raise FormatterBreaksAPISpec("event type")
|
||||||
if settings["appearance"]["mode"] == formatter_type:
|
if settings["appearance"]["mode"] == formatter_type:
|
||||||
|
if action_type in src.rcgcdw.formatter_hooks:
|
||||||
|
logger.warning(f"Action {action_type} is already defined inside of "
|
||||||
|
f"{src.rcgcdw.formatter_hooks[action_type].__module__}! "
|
||||||
|
f"Overwriting it with one from {func.__module__}")
|
||||||
src.rcgcdw.formatter_hooks[action_type] = func
|
src.rcgcdw.formatter_hooks[action_type] = func
|
||||||
|
|
||||||
|
|
||||||
|
|
11
src/rc.py
11
src/rc.py
|
@ -20,6 +20,7 @@ import sys
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
|
import src.api.client
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
from src.configloader import settings
|
from src.configloader import settings
|
||||||
|
@ -28,6 +29,7 @@ from src.misc import WIKI_SCRIPT_PATH, WIKI_API_PATH, datafile, send_simple, saf
|
||||||
from src.discord.queue import messagequeue
|
from src.discord.queue import messagequeue
|
||||||
from src.exceptions import MWError, BadRequest, ClientError, ServerError, MediaWikiError
|
from src.exceptions import MWError, BadRequest, ClientError, ServerError, MediaWikiError
|
||||||
from src.session import session
|
from src.session import session
|
||||||
|
from src.api.context import Context
|
||||||
from typing import Union
|
from typing import Union
|
||||||
# from src.rc_formatters import compact_formatter, embed_formatter, compact_abuselog_formatter, embed_abuselog_formatter
|
# from src.rc_formatters import compact_formatter, embed_formatter, compact_abuselog_formatter, embed_abuselog_formatter
|
||||||
from src.i18n import rc
|
from src.i18n import rc
|
||||||
|
@ -202,7 +204,7 @@ class Wiki(object):
|
||||||
logger.debug("Change ({}) is lower or equal to recent_id {}".format(change["rcid"], recent_id))
|
logger.debug("Change ({}) is lower or equal to recent_id {}".format(change["rcid"], recent_id))
|
||||||
continue
|
continue
|
||||||
logger.debug(recent_id)
|
logger.debug(recent_id)
|
||||||
essential_info(change, categorize_events.get(change.get("revid"), None))
|
rc_processor(change, categorize_events.get(change.get("revid"), None))
|
||||||
return highest_id
|
return highest_id
|
||||||
|
|
||||||
def prepare_abuse_log(self, abuse_log: list):
|
def prepare_abuse_log(self, abuse_log: list):
|
||||||
|
@ -454,12 +456,13 @@ class Wiki(object):
|
||||||
wiki = Wiki()
|
wiki = Wiki()
|
||||||
|
|
||||||
|
|
||||||
def essential_info(change, changed_categories):
|
def rc_processor(change, changed_categories):
|
||||||
"""Prepares essential information for both embed and compact message format."""
|
"""Prepares essential information for both embed and compact message format."""
|
||||||
|
formatters = src.api.client.client.get_formatters() # TODO Make it better? Importing might be a hell
|
||||||
logger.debug(change)
|
logger.debug(change)
|
||||||
|
context = Context(settings["appearance"]["mode"], settings["webhookURL"], src.api.client.client)
|
||||||
if ("actionhidden" in change or "suppressed" in change) and "suppressed" not in settings["ignored"]: # if event is hidden using suppression
|
if ("actionhidden" in change or "suppressed" in change) and "suppressed" not in settings["ignored"]: # if event is hidden using suppression
|
||||||
appearance_mode("suppressed", change, "", changed_categories, wiki)
|
context.event = "suppressed"
|
||||||
return
|
|
||||||
if "commenthidden" not in change:
|
if "commenthidden" not in change:
|
||||||
LinkParser.feed(change["parsedcomment"])
|
LinkParser.feed(change["parsedcomment"])
|
||||||
parsed_comment = LinkParser.new_string
|
parsed_comment = LinkParser.new_string
|
||||||
|
|
|
@ -39,19 +39,22 @@ if settings["fandom_discussions"]["enabled"]:
|
||||||
import src.discussions
|
import src.discussions
|
||||||
|
|
||||||
TESTING = True if "--test" in sys.argv else False # debug mode, pipeline testing
|
TESTING = True if "--test" in sys.argv else False # debug mode, pipeline testing
|
||||||
|
formatter_hooks = {}
|
||||||
# Prepare logging
|
# Prepare logging
|
||||||
|
|
||||||
logging.config.dictConfig(settings["logging"])
|
logging.config.dictConfig(settings["logging"])
|
||||||
logger = logging.getLogger("rcgcdw")
|
logger = logging.getLogger("rcgcdw")
|
||||||
logger.debug("Current settings: {settings}".format(settings=settings))
|
logger.debug("Current settings: {settings}".format(settings=settings))
|
||||||
from src.migrations import * # migrations after logging
|
from src.migrations import * # migrations after logging
|
||||||
try:
|
|
||||||
import extensions
|
|
||||||
except ImportError:
|
def load_extensions():
|
||||||
logger.critical("No extensions module found. What's going on?")
|
"""Loads all of the extensions, can be a local import because all we need is them to register"""
|
||||||
raise
|
try:
|
||||||
sys.exit(1)
|
import extensions
|
||||||
|
except ImportError:
|
||||||
|
logger.critical("No extensions module found. What's going on?")
|
||||||
|
sys.exit(1)
|
||||||
storage = datafile
|
storage = datafile
|
||||||
|
|
||||||
# Remove previous data holding file if exists and limitfetch allows
|
# Remove previous data holding file if exists and limitfetch allows
|
||||||
|
@ -63,8 +66,6 @@ if settings["limitrefetch"] != -1 and os.path.exists("lastchange.txt") is True:
|
||||||
datafile.save_datafile()
|
datafile.save_datafile()
|
||||||
os.remove("lastchange.txt")
|
os.remove("lastchange.txt")
|
||||||
|
|
||||||
formatter_hooks = {}
|
|
||||||
|
|
||||||
|
|
||||||
def day_overview_request():
|
def day_overview_request():
|
||||||
logger.info("Fetching daily overview... This may take up to 30 seconds!")
|
logger.info("Fetching daily overview... This may take up to 30 seconds!")
|
||||||
|
@ -251,6 +252,7 @@ if 1 == 2: # additional translation strings in unreachable code
|
||||||
_("autoreview"), _("autopatrol"), _("wiki_guardian"), ngettext("second", "seconds", 1), ngettext("minute", "minutes", 1), ngettext("hour", "hours", 1), ngettext("day", "days", 1), ngettext("week", "weeks", 1), ngettext("month", "months",1), ngettext("year", "years", 1), ngettext("millennium", "millennia", 1), ngettext("decade", "decades", 1), ngettext("century", "centuries", 1))
|
_("autoreview"), _("autopatrol"), _("wiki_guardian"), ngettext("second", "seconds", 1), ngettext("minute", "minutes", 1), ngettext("hour", "hours", 1), ngettext("day", "days", 1), ngettext("week", "weeks", 1), ngettext("month", "months",1), ngettext("year", "years", 1), ngettext("millennium", "millennia", 1), ngettext("decade", "decades", 1), ngettext("century", "centuries", 1))
|
||||||
# noinspection PyUnreachableCode
|
# noinspection PyUnreachableCode
|
||||||
|
|
||||||
|
load_extensions()
|
||||||
|
|
||||||
if TESTING:
|
if TESTING:
|
||||||
logger.debug("DEBUGGING ")
|
logger.debug("DEBUGGING ")
|
||||||
|
|
Loading…
Reference in a new issue