Added Cargo formatters, included LinkParser in the client

This commit is contained in:
Frisk 2021-05-06 13:48:11 +02:00
parent 0f6cf9da4d
commit d42eed4e20
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC
5 changed files with 135 additions and 41 deletions

View file

@ -14,3 +14,4 @@
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
import extensions.base.mediawiki
import extensions.base.abusefilter

115
extensions/base/cargo.py Normal file
View file

@ -0,0 +1,115 @@
# 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/>.
import logging
import re
from src.discord.message import DiscordMessage
from src.api import formatter
from src.i18n import rc_formatters
from src.api.context import Context
from src.api.util import embed_helper, compact_author, create_article_path, sanitize_to_markdown
_ = rc_formatters.gettext
ngettext = rc_formatters.ngettext
# Cargo - https://www.mediawiki.org/wiki/Extension:Cargo
# cargo/createtable - Creation of Cargo table
@formatter.embed(event="cargo/createtable")
def embed_cargo_createtable(ctx: Context, change: dict):
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
embed_helper(ctx, embed, change)
table = re.search(r"\[(.*?)]\(<(.*?)>\)", ctx.client.parse_links(change["logparams"]["0"]))
embed["url"] = table.group(2)
embed["title"] = _("Created the Cargo table \"{table}\"").format(table=table.group(1))
return embed
@formatter.compact(event="cargo/createtable")
def compact_cargo_createtable(ctx: Context, change: dict):
author, author_url = compact_author(ctx, change)
table = re.search(r"\[(.*?)]\(<(.*?)>\)", ctx.client.parse_links(change["logparams"]["0"]))
content = _("[{author}]({author_url}) created the Cargo table \"{table}\"").format(author=author,
author_url=author_url,
table=table)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# cargo/recreatetable
@formatter.embed(event="cargo/recreatetable")
def embed_cargo_recreatetable(ctx: Context, change: dict):
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
embed_helper(ctx, embed, change)
table = re.search(r"\[(.*?)]\(<(.*?)>\)", ctx.client.parse_links(change["logparams"]["0"]))
embed["url"] = table.group(2)
embed["title"] = _("Recreated the Cargo table \"{table}\"").format(table=table.group(1))
return embed
@formatter.compact(event="cargo/recreatetable")
def compact_cargo_recreatetable(ctx: Context, change: dict):
author, author_url = compact_author(ctx, change)
table = re.search(r"\[(.*?)]\(<(.*?)>\)", ctx.client.parse_links(change["logparams"]["0"]))
content = _("[{author}]({author_url}) recreated the Cargo table \"{table}\"").format(author=author,
author_url=author_url,
table=table)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# cargo/replacetable
@formatter.embed(event="cargo/replacetable")
def embed_cargo_replacetable(ctx: Context, change: dict):
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
embed_helper(ctx, embed, change)
table = re.search(r"\[(.*?)]\(<(.*?)>\)", ctx.client.parse_links(change["logparams"]["0"]))
embed["url"] = table.group(2)
embed["title"] = _("Replaced the Cargo table \"{table}\"").format(table=table.group(1))
return embed
@formatter.compact(event="cargo/recreatetable")
def compact_cargo_replacetable(ctx: Context, change: dict):
author, author_url = compact_author(ctx, change)
table = re.search(r"\[(.*?)]\(<(.*?)>\)", ctx.client.parse_links(change["logparams"]["0"]))
content = _("[{author}]({author_url}) replaced the Cargo table \"{table}\"").format(author=author,
author_url=author_url,
table=table)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# cargo/deletetable
@formatter.embed(event="cargo/deletetable")
def embed_cargo_deletetable(ctx: Context, change: dict):
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
embed_helper(ctx, embed, change)
embed["url"] = create_article_path("Special:CargoTables")
embed["title"] = _("Deleted the Cargo table \"{table}\"").format(table=sanitize_to_markdown(change["logparams"]["0"]))
return embed
@formatter.compact(event="cargo/recreatetable")
def compact_cargo_deletetable(ctx: Context, change: dict):
author, author_url = compact_author(ctx, change)
content = _("[{author}]({author_url}) deleted the Cargo table \"{table}\"").format(author=author,
author_url=author_url,
table=sanitize_to_markdown(change["logparams"]["0"]))
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)

View file

@ -23,7 +23,6 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING:
from src.wiki import Wiki
class Client:
"""
A client for interacting with RcGcDw when creating formatters or hooks.
@ -31,19 +30,25 @@ class Client:
def __init__(self, hooks, wiki):
self._formatters = hooks
self.__recent_changes: Wiki = 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
self.WIKI_JUST_DOMAIN = src.misc.WIKI_JUST_DOMAIN
self.WIKI_API_PATH: str = src.misc.WIKI_API_PATH
self.WIKI_ARTICLE_PATH: str = src.misc.WIKI_ARTICLE_PATH
self.WIKI_SCRIPT_PATH: str = src.misc.WIKI_SCRIPT_PATH
self.WIKI_JUST_DOMAIN: str = src.misc.WIKI_JUST_DOMAIN
self.content_parser = src.misc.ContentParser
self.tags = self.__recent_changes.tags
self.namespaces = self.__recent_changes.namespaces
self.LinkParser: type(src.misc.LinkParser) = src.misc.LinkParser
#self.make_api_request: src.rc.wiki.__recent_changes.api_request = self.__recent_changes.api_request
def refresh_internal_data(self):
"""Refreshes internal storage data for wiki tags and MediaWiki messages."""
self.__recent_changes.init_info()
def parse_links(self, summary: str):
link_parser = self.LinkParser()
link_parser.feed(summary)
return link_parser.new_string
def make_api_request(self, params: Union[str, OrderedDict], *json_path: str, timeout: int = 10, allow_redirects: bool = False):
"""Method to GET request data from the wiki's API with error handling including recognition of MediaWiki errors.

View file

@ -229,22 +229,13 @@ def compact_formatter(action, change, parsed_comment, categories, recent_changes
link = link_formatter(create_article_path(change["title"]))
content = _("[{author}]({author_url}) edited the slice for [{article}]({article_url})").format(author=author, author_url=author_url, article=change["title"], article_url=link)
elif action == "cargo/createtable":
LinkParser.feed(change["logparams"]["0"])
table = LinkParser.new_string
LinkParser.new_string = ""
content = _("[{author}]({author_url}) created the Cargo table \"{table}\"").format(author=author, author_url=author_url, table=table)
elif action == "cargo/deletetable":
content = _("[{author}]({author_url}) deleted the Cargo table \"{table}\"").format(author=author, author_url=author_url, table=change["logparams"]["0"])
elif action == "cargo/recreatetable":
LinkParser.feed(change["logparams"]["0"])
table = LinkParser.new_string
LinkParser.new_string = ""
content = _("[{author}]({author_url}) recreated the Cargo table \"{table}\"").format(author=author, author_url=author_url, table=table)
elif action == "cargo/replacetable":
LinkParser.feed(change["logparams"]["0"])
table = LinkParser.new_string
LinkParser.new_string = ""
content = _("[{author}]({author_url}) replaced the Cargo table \"{table}\"").format(author=author, author_url=author_url, table=table)
elif action == "managetags/create":
elif action == "managetags/delete":
@ -614,30 +605,13 @@ def embed_formatter(action, change, parsed_comment, categories, recent_changes):
link = create_article_path(change["title"])
embed["title"] = _("Edited the slice for {article}").format(article=change["title"])
elif action == "cargo/createtable":
LinkParser.feed(change["logparams"]["0"])
table = re.search(r"\[(.*?)\]\(<(.*?)>\)", LinkParser.new_string)
LinkParser.new_string = ""
link = table.group(2)
embed["title"] = _("Created the Cargo table \"{table}\"").format(table=table.group(1))
parsed_comment = None
elif action == "cargo/deletetable":
link = create_article_path("Special:CargoTables")
embed["title"] = _("Deleted the Cargo table \"{table}\"").format(table=change["logparams"]["0"])
parsed_comment = None
elif action == "cargo/recreatetable":
LinkParser.feed(change["logparams"]["0"])
table = re.search(r"\[(.*?)\]\(<(.*?)>\)", LinkParser.new_string)
LinkParser.new_string = ""
link = table.group(2)
embed["title"] = _("Recreated the Cargo table \"{table}\"").format(table=table.group(1))
parsed_comment = None
elif action == "cargo/replacetable":
LinkParser.feed(change["logparams"]["0"])
table = re.search(r"\[(.*?)\]\(<(.*?)>\)", LinkParser.new_string)
LinkParser.new_string = ""
link = table.group(2)
embed["title"] = _("Replaced the Cargo table \"{table}\"").format(table=table.group(1))
parsed_comment = None
elif action == "managetags/create":
elif action == "managetags/delete":

View file

@ -29,8 +29,7 @@ import src.api.client
from src.api.context import Context
from src.api.hooks import formatter_hooks, pre_hooks, post_hooks
from src.configloader import settings
from src.misc import add_to_dict, datafile, \
WIKI_API_PATH
from src.misc import add_to_dict, datafile, WIKI_API_PATH, LinkParser
from src.api.util import create_article_path, default_message, sanitize_to_markdown
from src.discord.queue import send_to_discord
from src.discord.message import DiscordMessage, DiscordMessageMetadata