From 95b693c4085c76576fdf78a862918a4bda1a2f2e Mon Sep 17 00:00:00 2001 From: Frisk Date: Wed, 17 Apr 2019 11:37:48 +0200 Subject: [PATCH] Fixed #64 --- rcgcdw.py | 129 ++++++++++++++++++++++++++---------------------------- 1 file changed, 62 insertions(+), 67 deletions(-) diff --git a/rcgcdw.py b/rcgcdw.py index e815fcd..64a2924 100644 --- a/rcgcdw.py +++ b/rcgcdw.py @@ -154,8 +154,11 @@ def send_to_discord(data): time.sleep(2.5) pass +def link_formatter(link): + return "<"+re.sub(r"([ \)])", "\\\\\\1", link)+">" + def compact_formatter(action, change, parsed_comment, categories): - author_url = "https://{wiki}.gamepedia.com/User:{user}".format(wiki=settings["wiki"], user=change["user"].replace(" ", "_")) + author_url = link_formatter("https://{wiki}.gamepedia.com/User:{user}".format(wiki=settings["wiki"], user=change["user"])) author = change["user"] parsed_comment = "" if parsed_comment is None else " (*"+parsed_comment+"*)" if action in ["edit", "new"]: @@ -172,51 +175,47 @@ def compact_formatter(action, change, parsed_comment, categories): else: content = _("[{author}]({author_url}) created [{article}]({edit_link}){comment} ({sign}{edit_size})").format(author=author, author_url=author_url, article=change["title"], edit_link=edit_link, comment=parsed_comment, edit_size=edit_size, sign=sign) elif action =="upload/upload": - file_link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + file_link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = _("[{author}]({author_url}) uploaded [{file}]({file_link}){comment}").format(author=author, author_url=author_url, file=change["title"], file_link=file_link, comment=parsed_comment) elif action == "upload/overwrite": - file_link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + file_link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = _("[{author}]({author_url}) uploaded a new version of [{file}]({file_link}){comment}").format(author=author, author_url=author_url, file=change["title"], file_link=file_link, comment=parsed_comment) elif action == "delete/delete": - page_link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + page_link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = _("[{author}]({author_url}) deleted [{page}]({page_link}){comment}").format(author=author, author_url=author_url, page=change["title"], page_link=page_link, comment=parsed_comment) elif action == "delete/delete_redir": - page_link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + page_link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = _("[{author}]({author_url}) deleted redirect by overwriting [{page}]({page_link}){comment}").format(author=author, author_url=author_url, page=change["title"], page_link=page_link, comment=parsed_comment) elif action == "move/move": - link = "".format(wiki=settings["wiki"], - article=change["logparams"]['target_title'].replace(" ", - "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["logparams"]['target_title'])) redirect_status = _("without making a redirect") if "suppressredirect" in change["logparams"] else _("with a redirect") content = _("[{author}]({author_url}) moved {redirect}*{article}* to [{target}]({target_url}) {made_a_redirect}{comment}").format(author=author, author_url=author_url, redirect="⤷ " if "redirect" in change else "", article=change["title"], target=change["logparams"]['target_title'], target_url=link, comment=parsed_comment, made_a_redirect=redirect_status) elif action == "move/move_redir": - link = "".format(wiki=settings["wiki"], - article=change["logparams"]["target_title"].replace(" ", - "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["logparams"]["target_title"])) content = _("[{author}]({author_url}) moved {redirect}*{article}* over redirect to [{target}]({target_url}){comment}").format(author=author, author_url=author_url, redirect="⤷ " if "redirect" in change else "", article=change["title"], target=change["logparams"]['target_title'], target_url=link, comment=parsed_comment) elif action == "protect/move_prot": - link = "".format(wiki=settings["wiki"], - article=change["logparams"]["oldtitle_title"].replace( - " ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["logparams"]["oldtitle_title"])) content = _( "[{author}]({author_url}) moved protection settings from {redirect}*{article}* to [{target}]({target_url}){comment}").format(author=author, author_url=author_url, redirect="⤷ " if "redirect" in change else "", article=change["logparams"]["oldtitle_title"], target=change["title"], target_url=link, comment=parsed_comment) elif action == "block/block": - link = "".format(wiki=settings["wiki"], - user=change["title"].replace(" ", "_").replace(')', - '\)')) + link = link_formatter("https://{wiki}.gamepedia.com/{user}".format(wiki=settings["wiki"], + user=change["title"])) user = change["title"].split(':')[1] if change["logparams"]["duration"] == "infinite": block_time = _("infinity and beyond") @@ -236,50 +235,46 @@ def compact_formatter(action, change, parsed_comment, categories): content = _( "[{author}]({author_url}) blocked [{user}]({user_url}) for {time}{comment}").format(author=author, author_url=author_url, user=user, time=block_time, user_url=link, comment=parsed_comment) elif action == "block/reblock": - link = "".format(wiki=settings["wiki"], - user=change["title"].replace(" ", "_").replace(')', - '\)')) + link = link_formatter("https://{wiki}.gamepedia.com/{user}".format(wiki=settings["wiki"], + user=change["title"])) user = change["title"].split(':')[1] content = _("[{author}]({author_url}) changed block settings for [{blocked_user}]({user_url}){comment}").format(author=author, author_url=author_url, blocked_user=user, user_url=link, comment=parsed_comment) elif action == "block/unblock": - link = "".format(wiki=settings["wiki"], - user=change["title"].replace(" ", "_").replace(')', - '\)')) + link = link_formatter("https://{wiki}.gamepedia.com/{user}".format(wiki=settings["wiki"], + user=change["title"])) user = change["title"].split(':')[1] content = _("[{author}]({author_url}) unblocked [{blocked_user}]({user_url}){comment}").format(author=author, author_url=author_url, blocked_user=user, user_url=link, comment=parsed_comment) elif action == "curseprofile/comment-created": - link = "".format(wiki=settings["wiki"], - commentid=change["logparams"]["4:comment_id"]) + link = link_formatter("https://{wiki}.gamepedia.com/Special:CommentPermalink/{commentid}".format(wiki=settings["wiki"], + commentid=change["logparams"]["4:comment_id"])) content = _("[{author}]({author_url}) left a [comment]({comment}) on {target} profile").format(author=author, author_url=author_url, comment=link, target=change["title"].split(':')[1]+"'s" if change["title"].split(':')[1] != change["user"] else _("their own profile")) elif action == "curseprofile/comment-replied": - link = "".format(wiki=settings["wiki"], + link = link_formatter("https://{wiki}.gamepedia.com/Special:CommentPermalink/{commentid}".format(wiki=settings["wiki"], commentid=change["logparams"][ - "4:comment_id"]) + "4:comment_id"])) content = _("[{author}]({author_url}) replied to a [comment]({comment}) on {target} profile").format(author=author, author_url=author_url, comment=link, target=change["title"].split(':')[1] + "'s" if change["title"].split(':')[1] !=change["user"] else _("their own profile")) elif action == "curseprofile/comment-edited": - link = "".format(wiki=settings["wiki"], + link = link_formatter("https://{wiki}.gamepedia.com/Special:CommentPermalink/{commentid}".format(wiki=settings["wiki"], commentid=change["logparams"][ - "4:comment_id"]) + "4:comment_id"])) content = _("[{author}]({author_url}) edited a [comment]({comment}) on {target} profile").format(author=author, author_url=author_url, comment=link, target=change["title"].split(':')[1] + "'s" if change["title"].split(':')[1] !=change["user"] else _("their own profile")) elif action == "curseprofile/comment-deleted": - link = "".format(wiki=settings["wiki"], - commentid=change["logparams"]["4:comment_id"]) + link = link_formatter("https://{wiki}.gamepedia.com/Special:CommentPermalink/{commentid}".format(wiki=settings["wiki"], + commentid=change["logparams"]["4:comment_id"])) # link = "https://{wiki}.gamepedia.com/UserProfile:{target}".format(wiki=settings["wiki"], target=params["target"].replace(" ", "_").replace(')', '\)')) content = _("[{author}]({author_url}) deleted a comment on {target} profile").format(author=author, author_url=author_url, target=change["title"].split(':')[1] + "'s" if change["title"].split(':')[1] !=change["user"] else _("their own profile")) elif action == "curseprofile/profile-edited": - link = "".format(wiki=settings["wiki"], - target=change["title"].split(':')[1].replace(" ", - "_").replace( - ')', '\)')) + link = link_formatter("https://{wiki}.gamepedia.com/UserProfile:{target}".format(wiki=settings["wiki"], + target=change["title"].split(':')[1])) if change["logparams"]['4:section'] == "profile-location": field = _("Location") elif change["logparams"]['4:section'] == "profile-aboutme": @@ -311,7 +306,7 @@ def compact_formatter(action, change, parsed_comment, categories): field=field, desc=BeautifulSoup(change["parsedcomment"], "lxml").get_text()) elif action in ("rights/rights", "rights/autopromote"): - link = "".format(wiki=settings["wiki"], user=change["title"].split(":")[1].replace(" ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/User:{user}".format(wiki=settings["wiki"], user=change["title"].split(":")[1])) old_groups = [] new_groups = [] for name in change["logparams"]["oldgroups"]: @@ -331,15 +326,15 @@ def compact_formatter(action, change, parsed_comment, categories): old_groups=", ".join(old_groups), new_groups=', '.join(new_groups), comment=parsed_comment) elif action == "protect/protect": - link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = _("[{author}]({author_url}) protected [{article}]({article_url}) with the following settings: {settings}{comment}").format(author=author, author_url=author_url, article=change["title"], article_url=link, settings=change["logparams"]["description"]+_(" [cascading]") if "cascade" in change["logparams"] else "", comment=parsed_comment) elif action == "protect/modify": - link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = _( "[{author}]({author_url}) modified protection settings of [{article}]({article_url}) to: {settings}{comment}").format( author=author, author_url=author_url, @@ -347,25 +342,25 @@ def compact_formatter(action, change, parsed_comment, categories): settings=change["logparams"]["description"] + _(" [cascading]") if "cascade" in change["logparams"] else "", comment=parsed_comment) elif action == "protect/unprotect": - link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = _("[{author}]({author_url}) removed protection from [{article}]({article_url}){comment}").format(author=author, author_url=author_url, article=change["title"], article_url=link, comment=parsed_comment) elif action == "delete/revision": amount = len(change["logparams"]["ids"]) - link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + link = link_formatter("".format(wiki=settings["wiki"], + article=change["title"])) content = ngettext("[{author}]({author_url}) changed visibility of revision on page [{article}]({article_url}){comment}", "[{author}]({author_url}) changed visibility of {amount} revisions on page [{article}]({article_url}){comment}", amount).format(author=author, author_url=author_url, article=change["title"], article_url=link, amount=amount, comment=parsed_comment) elif action == "import/upload": - link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = ngettext("[{author}]({author_url}) imported [{article}]({article_url}) with {count} revision{comment}", "[{author}]({author_url}) imported [{article}]({article_url}) with {count} revisions{comment}", change["logparams"]["count"]).format( author=author, author_url=author_url, article=change["title"], article_url=link, count=change["logparams"]["count"], comment=parsed_comment) elif action == "delete/restore": - link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = _("[{author}]({author_url}) restored [{article}]({article_url}){comment}").format(author=author, author_url=author_url, article=change["title"], article_url=link, comment=parsed_comment) elif action == "delete/event": content = _("[{author}]({author_url}) changed visibility of log events{comment}").format(author=author, author_url=author_url, comment=parsed_comment) @@ -378,43 +373,43 @@ def compact_formatter(action, change, parsed_comment, categories): link = "".format(wiki=settings["wiki"], number=change["logparams"]['newId']) content = _("[{author}]({author_url}) created abuse filter [number {number}]({filter_url})").format(author=author, author_url=author_url, number=change["logparams"]['newId'], filter_url=link) elif action == "merge/merge": - link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) - link_dest = "".format(wiki=settings["wiki"], - article=change["logparams"]["dest_title"].replace(" ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) + link_dest = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["logparams"]["dest_title"])) content = _("[{author}]({author_url}) merged revision histories of [{article}]({article_url}) into [{dest}]({dest_url}){comment}").format(author=author, author_url=author_url, article=change["title"], article_url=link, dest_url=link_dest, dest=change["logparams"]["dest_title"], comment=parsed_comment) elif action == "interwiki/iw_add": - link = "".format(wiki=settings["wiki"]) + link = link_formatter("https://{wiki}.gamepedia.com/Special:Interwiki".format(wiki=settings["wiki"])) content = _("[{author}]({author_url}) added an entry to the [interwiki table]({table_url}) pointing to {website} with {prefix} prefix").format(author=author, author_url=author_url, desc=parsed_comment, prefix=change["logparams"]['0'], website=change["logparams"]['1'], table_url=link) elif action == "interwiki/iw_edit": - link = "".format(wiki=settings["wiki"]) + link = link_formatter("https://{wiki}.gamepedia.com/Special:Interwiki".format(wiki=settings["wiki"])) content = _("[{author}]({author_url}) edited an entry in [interwiki table]({table_url}) pointing to {website} with {prefix} prefix").format(author=author, author_url=author_url, desc=parsed_comment, prefix=change["logparams"]['0'], website=change["logparams"]['1'], table_url=link) elif action == "interwiki/iw_delete": - link = "".format(wiki=settings["wiki"]) + link = link_formatter("https://{wiki}.gamepedia.com/Special:Interwiki".format(wiki=settings["wiki"])) content = _("[{author}]({author_url}) deleted an entry in [interwiki table]({table_url})").format(author=author, author_url=author_url, table_url=link) elif action == "contentmodel/change": - link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = _("[{author}]({author_url}) changed the content model of the page [{article}]({article_url}) from {old} to {new}{comment}").format(author=author, author_url=author_url, article=change["title"], article_url=link, old=change["logparams"]["oldmodel"], new=change["logparams"]["newmodel"], comment=parsed_comment) elif action == "sprite/sprite": - link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = _("[{author}]({author_url}) edited the sprite for [{article}]({article_url})").format(author=author, author_url=author_url, article=change["title"], article_url=link) elif action == "sprite/sheet": - link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=change["title"])) content = _("[{author}]({author_url}) created the sprite sheet for [{article}]({article_url})").format(author=author, author_url=author_url, article=change["title"], article_url=link) elif action == "sprite/slice": - link = "".format(wiki=settings["wiki"], - article=change["title"].replace(" ", "_")) + link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], + article=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 == "managetags/create": link = "".format(wiki=settings["wiki"])