memories are now redacted

This commit is contained in:
Remy Moll 2023-11-18 12:32:20 +01:00
parent 6b63276dd7
commit c9254a3e88
3 changed files with 20 additions and 6 deletions

View File

@ -76,7 +76,8 @@ class MemoryHandler(BaseHandler):
caption= caption=
f"On {chosen_match.date_pretty}, " f"On {chosen_match.date_pretty}, "
f"{chosen_match.author} wrote: \n" f"{chosen_match.author} wrote: \n"
f"{chosen_match.text}" f"{chosen_match.spoiler_text}",
parse_mode=ParseMode.HTML
) )
else: else:
await query.edit_message_text( await query.edit_message_text(

View File

@ -49,7 +49,8 @@ class RandomMemoryJob():
caption = caption =
f"On {chosen_entry.date_pretty}, " f"On {chosen_entry.date_pretty}, "
f"{chosen_entry.author} wrote: \n" f"{chosen_entry.author} wrote: \n"
f"{chosen_entry.text}" f"{chosen_entry.spoiler_text}",
parse_mode=ParseMode.HTML
) )
else: else:
await self.bot.send_message( await self.bot.send_message(
@ -57,6 +58,7 @@ class RandomMemoryJob():
text = text =
f"On {chosen_entry.date_pretty}, " f"On {chosen_entry.date_pretty}, "
f"{chosen_entry.author} wrote: \n" f"{chosen_entry.author} wrote: \n"
f"{chosen_entry.text}" f"{chosen_entry.spoiler_text}",
parse_mode=ParseMode.HTML
) )

View File

@ -63,10 +63,21 @@ class JournalEntry(BaseModel):
@property @property
def spoiler_text(self) -> str: def spoiler_text(self) -> str:
new_text = self.text.replace("\n", "<br>").replace("<", "&lt;").replace(">", "&gt;").replace("&", "&amp;") """Returns the text with all the frisky details hidden away"""
matches = re.findall(r"[A-Z](\s|\w)+\:\)(\w|\s)+\.", new_text) new_text = self.text.replace("<", "&lt;").replace(">", "&gt;").replace("&", "&amp;")
pattern = re.compile(
"("
"(((?<=(\.|\!|\?)\s)[A-Z])|(^[A-Z]))" # beginning of a sentence
"([^\.\!\?])+" # any character being part of a sentence
"\:\)" # the smiley
"([^\.\!\?])*" # continuation of sentence
"(\.|\!|\?|\,|$)" # end of the sentence
")"
)
matches = pattern.findall(new_text)
for match in matches: for match in matches:
new_text = new_text.replace(match, f"<tg-spoiler>{match}</tg-spoiler>") group_to_replace = match[0]
new_text = new_text.replace(group_to_replace, f"<tg-spoiler>{group_to_replace}</tg-spoiler>")
return new_text return new_text