From 0babef0693510d93d876afcad4c5385328fbf1cb Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 17 Sep 2020 04:22:31 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=83=20Add=20=E2=8A=95=20substitution,?= =?UTF-8?q?=20scheduled=20for=2017=20Oct?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalnet/serf/discord/escape.py | 20 +++++++++++++++----- royalnet/serf/telegram/escape.py | 22 ++++++++++++++++------ royalnet/utils/__init__.py | 2 ++ royalnet/utils/escalating_odds.py | 19 +++++++++++++++++++ 4 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 royalnet/utils/escalating_odds.py diff --git a/royalnet/serf/discord/escape.py b/royalnet/serf/discord/escape.py index e8af742a..70848e50 100644 --- a/royalnet/serf/discord/escape.py +++ b/royalnet/serf/discord/escape.py @@ -1,4 +1,14 @@ import re +from ...utils import escalating_odds +import datetime + + +# https://stackoverflow.com/questions/18621568/regex-replace-text-outside-html-tags +spooky_pattern = re.compile(r"o(?![^\[]*]|[^[]]*\[/)") +spooky_replacement = r'⊕' + +url_pattern = re.compile(r"\[url=(.*?)](.*?)\[/url]") +url_replacement = r'\2 (\1)' def escape(string: str) -> str: @@ -6,10 +16,10 @@ def escape(string: str) -> str: Warning: Currently escapes everything, even items in code blocks.""" - url_pattern = re.compile(r"\[url=(.*?)](.*?)\[/url]") - url_replacement = r'\2 (\1)' + if escalating_odds(datetime.datetime(2020, 10, 17, 4, 0)): + string = re.sub(spooky_pattern, spooky_replacement, string) - simple_parse = string \ + string = string \ .replace("*", "\\*") \ .replace("_", "\\_") \ .replace("`", "\\`") \ @@ -24,6 +34,6 @@ def escape(string: str) -> str: .replace("[p]", "```") \ .replace("[/p]", "```") - advanced_parse = re.sub(url_pattern, url_replacement, simple_parse) + string = re.sub(url_pattern, url_replacement, string) - return advanced_parse + return string diff --git a/royalnet/serf/telegram/escape.py b/royalnet/serf/telegram/escape.py index ecdc6066..a0147555 100644 --- a/royalnet/serf/telegram/escape.py +++ b/royalnet/serf/telegram/escape.py @@ -1,5 +1,15 @@ import re from typing import * +from ...utils import escalating_odds +import datetime + + +# https://stackoverflow.com/questions/18621568/regex-replace-text-outside-html-tags +spooky_pattern = re.compile(r"o(?![^\[]*]|[^[]]*\[/)") +spooky_replacement = r'⊕' + +url_pattern = re.compile(r"\[url=(.*?)](.*?)\[/url]") +url_replacement = r'\2' def escape(string: Optional[str]) -> Optional[str]: @@ -8,12 +18,12 @@ def escape(string: Optional[str]) -> Optional[str]: Warning: Currently escapes everything, even items in code blocks.""" - url_pattern = re.compile(r"\[url=(.*?)](.*?)\[/url]") - url_replacement = r'\2' + string = string.replace("<", "<").replace(">", ">") - escaped_string = string.replace("<", "<").replace(">", ">") + if escalating_odds(datetime.datetime(2020, 10, 17, 4, 0)): + string = re.sub(spooky_pattern, spooky_replacement, string) - simple_parse = escaped_string \ + string = string \ .replace("[b]", "") \ .replace("[/b]", "") \ .replace("[i]", "") \ @@ -25,6 +35,6 @@ def escape(string: Optional[str]) -> Optional[str]: .replace("[p]", "
") \
         .replace("[/p]", "
") - advanced_parse = re.sub(url_pattern, url_replacement, simple_parse) + string = re.sub(url_pattern, url_replacement, string) - return advanced_parse + return string diff --git a/royalnet/utils/__init__.py b/royalnet/utils/__init__.py index c0aec256..12345899 100644 --- a/royalnet/utils/__init__.py +++ b/royalnet/utils/__init__.py @@ -9,6 +9,7 @@ from .sleep_until import sleep_until from .strip_tabs import strip_tabs from .taskslist import TaskList from .urluuid import to_urluuid, from_urluuid +from .escalating_odds import escalating_odds __all__ = [ "asyncify", @@ -30,4 +31,5 @@ __all__ = [ "strip_tabs", "TaskList", "RoyalnetProcess", + "escalating_odds", ] diff --git a/royalnet/utils/escalating_odds.py b/royalnet/utils/escalating_odds.py new file mode 100644 index 00000000..66c758dd --- /dev/null +++ b/royalnet/utils/escalating_odds.py @@ -0,0 +1,19 @@ +import random +import datetime +import logging + +log = logging.getLogger(__name__) + + +def escalating_odds(target_day: datetime.datetime) -> bool: + time_left: datetime.timedelta = target_day - datetime.datetime.now() + log.debug(f"Time left is {time_left}") + + if time_left.total_seconds() <= 0: + log.debug("Target has passed, autorolling a success") + return True + + odds = 1 / (time_left.days + 1) + result = random.uniform(0, 1) <= odds + log.debug(f"Odds are {odds}, rolled a {result}") + return result