diff --git a/misc.py b/misc.py index 0941e41..c1743e9 100644 --- a/misc.py +++ b/misc.py @@ -1,6 +1,7 @@ -import json, logging, sys +import json, logging, sys, re # Create a custom logger + misc_logger = logging.getLogger("rcgcdw.misc") data_template = {"rcid": 99999999999, @@ -44,3 +45,8 @@ def save_datafile(data): def weighted_average(value, weight, new_value): """Calculates weighted average of value number with weight weight and new_value with weight 1""" return round(((value * weight) + new_value) / (weight + 1), 2) + + +def link_formatter(link): + """Formats a link to not embed it""" + return "<"+re.sub(r"([ \)])", "\\\\\\1", link)+">" \ No newline at end of file diff --git a/rcgcdw.py b/rcgcdw.py index b261bad..7609ae0 100644 --- a/rcgcdw.py +++ b/rcgcdw.py @@ -20,12 +20,14 @@ # WARNING! SHITTY CODE AHEAD. ENTER ONLY IF YOU ARE SURE YOU CAN TAKE IT # You have been warned -import time, logging, logging.config, json, requests, datetime, re, gettext, math, random, os.path, schedule, sys, ipaddress, misc +import time, logging.config, json, requests, datetime, re, gettext, math, random, os.path, schedule, sys, ipaddress, misc from bs4 import BeautifulSoup from collections import defaultdict, Counter from urllib.parse import quote_plus from html.parser import HTMLParser +from misc import link_formatter + if __name__ != "__main__": # return if called as a module logging.critical("The file is being executed as a module. Please execute the script using the console.") sys.exit(1) @@ -171,9 +173,6 @@ def send_to_discord(data): time.sleep(2.0) pass -def link_formatter(link): - return "<"+re.sub(r"([ \)])", "\\\\\\1", link)+">" - def compact_formatter(action, change, parsed_comment, categories): if action != "suppressed": author_url = link_formatter("https://{wiki}.gamepedia.com/User:{user}".format(wiki=settings["wiki"], user=change["user"])) @@ -196,7 +195,7 @@ def compact_formatter(action, change, parsed_comment, categories): 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + article=change["title"])) content = _("[{author}]({author_url}) uploaded [{file}]({file_link}){comment}").format(author=author, author_url=author_url, file=change["title"], @@ -204,34 +203,34 @@ def compact_formatter(action, change, parsed_comment, categories): comment=parsed_comment) elif action == "upload/overwrite": file_link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["logparams"]['target_title'])) + 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["logparams"]["target_title"])) + 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}* over redirect 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 == "protect/move_prot": link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["logparams"]["oldtitle_title"])) + 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) @@ -240,10 +239,10 @@ def compact_formatter(action, change, parsed_comment, categories): try: ipaddress.ip_address(user) link = link_formatter("https://{wiki}.gamepedia.com/Special:Contributions/{user}".format(wiki=settings["wiki"], - user=user)) + user=user)) except ValueError: link = link_formatter("https://{wiki}.gamepedia.com/{user}".format(wiki=settings["wiki"], - user=change["title"])) + user=change["title"])) if change["logparams"]["duration"] == "infinite": block_time = _("infinity and beyond") else: @@ -263,21 +262,21 @@ def compact_formatter(action, change, parsed_comment, categories): "[{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 = link_formatter("https://{wiki}.gamepedia.com/{user}".format(wiki=settings["wiki"], - user=change["title"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/{user}".format(wiki=settings["wiki"], - user=change["title"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/Special:CommentPermalink/{commentid}".format(wiki=settings["wiki"], - commentid=change["logparams"]["4:comment_id"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/Special:CommentPermalink/{commentid}".format(wiki=settings["wiki"], - commentid=change["logparams"][ + commentid=change["logparams"][ "4:comment_id"])) content = _("[{author}]({author_url}) replied to a [comment]({comment}) on {target} profile").format(author=author, author_url=author_url, @@ -285,7 +284,7 @@ def compact_formatter(action, change, parsed_comment, categories): target=change["title"].split(':')[1] if change["title"].split(':')[1] !=change["user"] else _("their own")) elif action == "curseprofile/comment-edited": link = link_formatter("https://{wiki}.gamepedia.com/Special:CommentPermalink/{commentid}".format(wiki=settings["wiki"], - commentid=change["logparams"][ + commentid=change["logparams"][ "4:comment_id"])) content = _("[{author}]({author_url}) edited a [comment]({comment}) on {target} profile").format(author=author, author_url=author_url, @@ -298,7 +297,7 @@ def compact_formatter(action, change, parsed_comment, categories): elif action == "curseprofile/profile-edited": link = link_formatter("https://{wiki}.gamepedia.com/UserProfile:{target}".format(wiki=settings["wiki"], - target=change["title"].split(':')[1])) + target=change["title"].split(':')[1])) if change["logparams"]['4:section'] == "profile-location": field = _("Location") elif change["logparams"]['4:section'] == "profile-aboutme": @@ -353,14 +352,14 @@ def compact_formatter(action, change, parsed_comment, categories): comment=parsed_comment) elif action == "protect/protect": link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + article=change["title"])) content = _( "[{author}]({author_url}) modified protection settings of [{article}]({article_url}) to: {settings}{comment}").format( author=author, author_url=author_url, @@ -369,24 +368,24 @@ def compact_formatter(action, change, parsed_comment, categories): comment=parsed_comment) elif action == "protect/unprotect": link = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + 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 = link_formatter("".format(wiki=settings["wiki"], - article=change["title"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + 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) @@ -400,9 +399,9 @@ def compact_formatter(action, change, parsed_comment, categories): 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + article=change["title"])) link_dest = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["logparams"]["dest_title"])) + 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": @@ -422,20 +421,20 @@ def compact_formatter(action, change, parsed_comment, categories): 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + 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 = link_formatter("https://{wiki}.gamepedia.com/{article}".format(wiki=settings["wiki"], - article=change["title"])) + 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"])