From f7ef7f1b6469102d1739e359f7e036d290e8a1dc Mon Sep 17 00:00:00 2001 From: Corban-Lee Date: Tue, 25 Jun 2024 10:22:35 +0100 Subject: [PATCH] Separate mutators for article title/desc --- src/extensions/tasks.py | 15 +++++-- src/feed.py | 10 +++-- src/mutators.py | 88 ++++++++++++++--------------------------- 3 files changed, 47 insertions(+), 66 deletions(-) diff --git a/src/extensions/tasks.py b/src/extensions/tasks.py index fb43909..d613943 100644 --- a/src/extensions/tasks.py +++ b/src/extensions/tasks.py @@ -154,7 +154,7 @@ class TaskCog(commands.Cog): api: API, session: aiohttp.ClientSession, sub_id: int, - mutators: list[dict], + mutators: dict[str, list[dict]], filters: list[dict], articles: list[Article] ) -> list[Embed]: @@ -207,7 +207,7 @@ class TaskCog(commands.Cog): api: API, session: aiohttp.ClientSession, sub_id: int, - mutators: list[dict], + mutators: dict[str, list[dict]], filters: list[dict], article: Article ) -> Embed | None: @@ -221,8 +221,7 @@ class TaskCog(commands.Cog): blocked = any(self.filter_article(_filter, article) for _filter in filters) log.debug("filter result: %s", "blocked" if blocked else "ok") - for mutator in mutators: - article.mutate(mutator) + self.mutate_article(article, mutators) try: await api.create_tracked_content( @@ -245,6 +244,14 @@ class TaskCog(commands.Cog): if not blocked: return await article.to_embed(session) + def mutate_article(self, article: Article, mutators: list[dict]): + + for mutator in mutators["title"]: + article.mutate("title", mutator) + + for mutator in mutators["desc"]: + article.mutate("description", mutator) + def filter_article(self, _filter: dict, article: Article) -> bool: """ Returns True if article should be ignored due to filters. diff --git a/src/feed.py b/src/feed.py index 8b9f1c1..23f6eff 100644 --- a/src/feed.py +++ b/src/feed.py @@ -68,7 +68,7 @@ class Article: source=source ) - def mutate(self, mutator: dict): + def mutate(self, attr: str, mutator: dict): """ Apply a mutation to a certain text attribute of this Article instance. @@ -79,7 +79,7 @@ class Article: mutator_value = mutator["value"] if mutator_value in mutator_map: - attr, func = mutator_map[mutator_value] + func = mutator_map[mutator_value] setattr(self, attr, func(getattr(self, attr))) log.debug("mutated %s, to: %s", attr, getattr(self, attr)) else: @@ -283,7 +283,7 @@ class Subscription(DjangoDataModel): creation_datetime: datetime extra_notes: str filters: list[int] - mutators: list[dict] + mutators: dict[str, list[dict]] active: bool channels_count: int @@ -291,6 +291,10 @@ class Subscription(DjangoDataModel): def parser(item: dict) -> dict: item["guild_id"] = int(item["guild_id"]) item["creation_datetime"] = datetime.strptime(item["creation_datetime"], "%Y-%m-%dT%H:%M:%S.%f%z") + item["mutators"] = { + "title": item.pop("article_title_mutators"), + "desc": item.pop("article_desc_mutators") + } return item diff --git a/src/mutators.py b/src/mutators.py index 69c579e..c49705d 100644 --- a/src/mutators.py +++ b/src/mutators.py @@ -273,63 +273,33 @@ def emojify(text: str) -> str: # Maps instructions for an Article item to mutate attributes. mutator_map = { - "UWU_TITLE": ("title", uwu), - "UWU_DESC": ("description", uwu), - "GIB_TITLE": ("title", gibberish), - "GIB_DESC": ("description", gibberish), - "L3_TITLE": ("title", leet), - "L3_DESC": ("description", leet), - "REV_TITLE": ("title", reverse), - "REV_DESC": ("description", reverse), - "RND_TITLE": ("title", shuffle), - "RND_DESC": ("description", shuffle), - "PGL_TITLE": ("title", pig_latin), - "PGL_DESC": ("description", pig_latin), - "RNC_TITLE": ("title", random_case), - "RNC_DESC": ("description", random_case), - "UDT_TITLE": ("title", upside_down_text), - "UDT_DESC": ("description", upside_down_text), - "GS_TITLE": ("title", gothic_script), - "GS_DESC": ("description", gothic_script), - "EMJ_TITLE": ("title", emoji_substitution), - "EMJ_DESC": ("description", emoji_substitution), - "SML_TITLE": ("title", small_caps), - "SML_DESC": ("description", small_caps), - "ZGO_TITLE": ("title", zalgo), - "ZGO_DESC": ("description", zalgo), - "MC_TITLE": ("title", morse_code), - "MC_DESC": ("description", morse_code), - "BIN_TITLE": ("title", to_binary), - "BIN_DESC": ("description", to_binary), - "HEX_TITLE": ("title", to_hexadecimal), - "HEX_DESC": ("description", to_hexadecimal), - "RMV_TITLE": ("title", remove_vowels), - "RMV_DESC": ("description", remove_vowels), - "DBL_TITLE": ("title", double_characters), - "DBL_DESC": ("description", double_characters), - "RNE_TITLE": ("title", randomly_inserted_emoji), - "RNE_DESC": ("description", randomly_inserted_emoji), - "PIR_TITLE": ("title", pirate_speak), - "PIR_DESC": ("description", pirate_speak), - "VAL_TITLE": ("title", valley_girl), - "VAL_DESC": ("description", valley_girl), - "DEG_TITLE": ("title", degeneracy), - "DEG_DESC": ("description", degeneracy), - "CAT_TITLE": ("title", cat_speak), - "CAT_DESC": ("description", cat_speak), - "NRD_TITLE": (), - "NRD_DESC": (), - "BKW_TITLE": (), - "BKW_DESC": (), - "RNG_TITLE": (), - "RNG_DESC": (), - "RAS_TITLE": (), - "RAS_DESC": (), - "SHK_TITLE": (), - "SHK_DESC": (), - "RBT_TITLE": (), - "RBT_DESC": (), - "EMI_TITLE": (), - "EMI_DESC": (), - + "UWU": uwu, + "GIB": gibberish, + "L3": leet, + "REV": reverse, + "RND": shuffle, + "PGL": pig_latin, + "RNC": random_case, + "UDT": upside_down_text, + "GS": gothic_script, + "EMJ": emoji_substitution, + "SML": small_caps, + "ZGO": zalgo, + "MC": morse_code, + "BIN": to_binary, + "HEX": to_hexadecimal, + "RMV": remove_vowels, + "DBL": double_characters, + "RNE": randomly_inserted_emoji, + "PIR": pirate_speak, + "VAL": valley_girl, + "DEG": degeneracy, + "CAT": cat_speak, + "NRD": (), + "BKW": (), + "RNG": (), + "RAS": (), + "SHK": (), + "RBT": (), + "EMI": (), } \ No newline at end of file