Separate mutators for article title/desc
This commit is contained in:
parent
bdc32ac5fa
commit
f7ef7f1b64
@ -154,7 +154,7 @@ class TaskCog(commands.Cog):
|
|||||||
api: API,
|
api: API,
|
||||||
session: aiohttp.ClientSession,
|
session: aiohttp.ClientSession,
|
||||||
sub_id: int,
|
sub_id: int,
|
||||||
mutators: list[dict],
|
mutators: dict[str, list[dict]],
|
||||||
filters: list[dict],
|
filters: list[dict],
|
||||||
articles: list[Article]
|
articles: list[Article]
|
||||||
) -> list[Embed]:
|
) -> list[Embed]:
|
||||||
@ -207,7 +207,7 @@ class TaskCog(commands.Cog):
|
|||||||
api: API,
|
api: API,
|
||||||
session: aiohttp.ClientSession,
|
session: aiohttp.ClientSession,
|
||||||
sub_id: int,
|
sub_id: int,
|
||||||
mutators: list[dict],
|
mutators: dict[str, list[dict]],
|
||||||
filters: list[dict],
|
filters: list[dict],
|
||||||
article: Article
|
article: Article
|
||||||
) -> Embed | None:
|
) -> Embed | None:
|
||||||
@ -221,8 +221,7 @@ class TaskCog(commands.Cog):
|
|||||||
blocked = any(self.filter_article(_filter, article) for _filter in filters)
|
blocked = any(self.filter_article(_filter, article) for _filter in filters)
|
||||||
log.debug("filter result: %s", "blocked" if blocked else "ok")
|
log.debug("filter result: %s", "blocked" if blocked else "ok")
|
||||||
|
|
||||||
for mutator in mutators:
|
self.mutate_article(article, mutators)
|
||||||
article.mutate(mutator)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await api.create_tracked_content(
|
await api.create_tracked_content(
|
||||||
@ -245,6 +244,14 @@ class TaskCog(commands.Cog):
|
|||||||
if not blocked:
|
if not blocked:
|
||||||
return await article.to_embed(session)
|
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:
|
def filter_article(self, _filter: dict, article: Article) -> bool:
|
||||||
"""
|
"""
|
||||||
Returns True if article should be ignored due to filters.
|
Returns True if article should be ignored due to filters.
|
||||||
|
10
src/feed.py
10
src/feed.py
@ -68,7 +68,7 @@ class Article:
|
|||||||
source=source
|
source=source
|
||||||
)
|
)
|
||||||
|
|
||||||
def mutate(self, mutator: dict):
|
def mutate(self, attr: str, mutator: dict):
|
||||||
"""
|
"""
|
||||||
Apply a mutation to a certain text attribute of
|
Apply a mutation to a certain text attribute of
|
||||||
this Article instance.
|
this Article instance.
|
||||||
@ -79,7 +79,7 @@ class Article:
|
|||||||
mutator_value = mutator["value"]
|
mutator_value = mutator["value"]
|
||||||
|
|
||||||
if mutator_value in mutator_map:
|
if mutator_value in mutator_map:
|
||||||
attr, func = mutator_map[mutator_value]
|
func = mutator_map[mutator_value]
|
||||||
setattr(self, attr, func(getattr(self, attr)))
|
setattr(self, attr, func(getattr(self, attr)))
|
||||||
log.debug("mutated %s, to: %s", attr, getattr(self, attr))
|
log.debug("mutated %s, to: %s", attr, getattr(self, attr))
|
||||||
else:
|
else:
|
||||||
@ -283,7 +283,7 @@ class Subscription(DjangoDataModel):
|
|||||||
creation_datetime: datetime
|
creation_datetime: datetime
|
||||||
extra_notes: str
|
extra_notes: str
|
||||||
filters: list[int]
|
filters: list[int]
|
||||||
mutators: list[dict]
|
mutators: dict[str, list[dict]]
|
||||||
active: bool
|
active: bool
|
||||||
channels_count: int
|
channels_count: int
|
||||||
|
|
||||||
@ -291,6 +291,10 @@ class Subscription(DjangoDataModel):
|
|||||||
def parser(item: dict) -> dict:
|
def parser(item: dict) -> dict:
|
||||||
item["guild_id"] = int(item["guild_id"])
|
item["guild_id"] = int(item["guild_id"])
|
||||||
item["creation_datetime"] = datetime.strptime(item["creation_datetime"], "%Y-%m-%dT%H:%M:%S.%f%z")
|
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
|
return item
|
||||||
|
|
||||||
|
@ -273,63 +273,33 @@ def emojify(text: str) -> str:
|
|||||||
|
|
||||||
# Maps instructions for an Article item to mutate attributes.
|
# Maps instructions for an Article item to mutate attributes.
|
||||||
mutator_map = {
|
mutator_map = {
|
||||||
"UWU_TITLE": ("title", uwu),
|
"UWU": uwu,
|
||||||
"UWU_DESC": ("description", uwu),
|
"GIB": gibberish,
|
||||||
"GIB_TITLE": ("title", gibberish),
|
"L3": leet,
|
||||||
"GIB_DESC": ("description", gibberish),
|
"REV": reverse,
|
||||||
"L3_TITLE": ("title", leet),
|
"RND": shuffle,
|
||||||
"L3_DESC": ("description", leet),
|
"PGL": pig_latin,
|
||||||
"REV_TITLE": ("title", reverse),
|
"RNC": random_case,
|
||||||
"REV_DESC": ("description", reverse),
|
"UDT": upside_down_text,
|
||||||
"RND_TITLE": ("title", shuffle),
|
"GS": gothic_script,
|
||||||
"RND_DESC": ("description", shuffle),
|
"EMJ": emoji_substitution,
|
||||||
"PGL_TITLE": ("title", pig_latin),
|
"SML": small_caps,
|
||||||
"PGL_DESC": ("description", pig_latin),
|
"ZGO": zalgo,
|
||||||
"RNC_TITLE": ("title", random_case),
|
"MC": morse_code,
|
||||||
"RNC_DESC": ("description", random_case),
|
"BIN": to_binary,
|
||||||
"UDT_TITLE": ("title", upside_down_text),
|
"HEX": to_hexadecimal,
|
||||||
"UDT_DESC": ("description", upside_down_text),
|
"RMV": remove_vowels,
|
||||||
"GS_TITLE": ("title", gothic_script),
|
"DBL": double_characters,
|
||||||
"GS_DESC": ("description", gothic_script),
|
"RNE": randomly_inserted_emoji,
|
||||||
"EMJ_TITLE": ("title", emoji_substitution),
|
"PIR": pirate_speak,
|
||||||
"EMJ_DESC": ("description", emoji_substitution),
|
"VAL": valley_girl,
|
||||||
"SML_TITLE": ("title", small_caps),
|
"DEG": degeneracy,
|
||||||
"SML_DESC": ("description", small_caps),
|
"CAT": cat_speak,
|
||||||
"ZGO_TITLE": ("title", zalgo),
|
"NRD": (),
|
||||||
"ZGO_DESC": ("description", zalgo),
|
"BKW": (),
|
||||||
"MC_TITLE": ("title", morse_code),
|
"RNG": (),
|
||||||
"MC_DESC": ("description", morse_code),
|
"RAS": (),
|
||||||
"BIN_TITLE": ("title", to_binary),
|
"SHK": (),
|
||||||
"BIN_DESC": ("description", to_binary),
|
"RBT": (),
|
||||||
"HEX_TITLE": ("title", to_hexadecimal),
|
"EMI": (),
|
||||||
"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": (),
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user