From 6b63276dd7ef96341e6fe11e5c4b7bf49890f7f6 Mon Sep 17 00:00:00 2001 From: Remy Moll Date: Sat, 21 Oct 2023 15:23:05 +0200 Subject: [PATCH 1/2] playing with regexes --- bot/commands/memory.py | 4 +++- bot/cronjob/random_memory.py | 1 + bot/models.py | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bot/commands/memory.py b/bot/commands/memory.py index cd25b65..5bae67f 100644 --- a/bot/commands/memory.py +++ b/bot/commands/memory.py @@ -1,6 +1,7 @@ import os from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler, CallbackContext from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, InputMediaPhoto +from telegram.constants import ParseMode # ACTION_CHOICE, DATE_ENTRY, ADD_CONTENT = range(3) MEMORY_CHOICE = range(1) @@ -81,7 +82,8 @@ class MemoryHandler(BaseHandler): await query.edit_message_text( f"On {chosen_match.date_pretty}, " f"{chosen_match.author} wrote: \n" - f"{chosen_match.text}" + f"{chosen_match.spoiler_text}", + parse_mode=ParseMode.HTML ) return ConversationHandler.END diff --git a/bot/cronjob/random_memory.py b/bot/cronjob/random_memory.py index e5cdf0a..a9db632 100644 --- a/bot/cronjob/random_memory.py +++ b/bot/cronjob/random_memory.py @@ -1,4 +1,5 @@ from datetime import time, timedelta, timezone, datetime, date +from telegram.constants import ParseMode import os from peewee import fn import logging diff --git a/bot/models.py b/bot/models.py index 7f8b341..e485aa2 100644 --- a/bot/models.py +++ b/bot/models.py @@ -1,5 +1,6 @@ from peewee import * from pathlib import Path +import re import os import datetime @@ -60,6 +61,14 @@ class JournalEntry(BaseModel): except ValueError: #fck windows return self.date.strftime('%a, %d. %b %Y') + @property + def spoiler_text(self) -> str: + new_text = self.text.replace("\n", "
").replace("<", "<").replace(">", ">").replace("&", "&") + matches = re.findall(r"[A-Z](\s|\w)+\:\)(\w|\s)+\.", new_text) + for match in matches: + new_text = new_text.replace(match, f"{match}") + return new_text + def set_db(db_path): db.initialize(SqliteDatabase(db_path)) From c9254a3e88aa2fcdf684ed1d60f48fbaf143098f Mon Sep 17 00:00:00 2001 From: Remy Moll Date: Sat, 18 Nov 2023 12:32:20 +0100 Subject: [PATCH 2/2] memories are now redacted --- bot/commands/memory.py | 3 ++- bot/cronjob/random_memory.py | 6 ++++-- bot/models.py | 17 ++++++++++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/bot/commands/memory.py b/bot/commands/memory.py index 5bae67f..f9bcb02 100644 --- a/bot/commands/memory.py +++ b/bot/commands/memory.py @@ -76,7 +76,8 @@ class MemoryHandler(BaseHandler): caption= f"On {chosen_match.date_pretty}, " f"{chosen_match.author} wrote: \n" - f"{chosen_match.text}" + f"{chosen_match.spoiler_text}", + parse_mode=ParseMode.HTML ) else: await query.edit_message_text( diff --git a/bot/cronjob/random_memory.py b/bot/cronjob/random_memory.py index a9db632..b0a397d 100644 --- a/bot/cronjob/random_memory.py +++ b/bot/cronjob/random_memory.py @@ -49,7 +49,8 @@ class RandomMemoryJob(): caption = f"On {chosen_entry.date_pretty}, " f"{chosen_entry.author} wrote: \n" - f"{chosen_entry.text}" + f"{chosen_entry.spoiler_text}", + parse_mode=ParseMode.HTML ) else: await self.bot.send_message( @@ -57,6 +58,7 @@ class RandomMemoryJob(): text = f"On {chosen_entry.date_pretty}, " f"{chosen_entry.author} wrote: \n" - f"{chosen_entry.text}" + f"{chosen_entry.spoiler_text}", + parse_mode=ParseMode.HTML ) diff --git a/bot/models.py b/bot/models.py index e485aa2..6894243 100644 --- a/bot/models.py +++ b/bot/models.py @@ -63,10 +63,21 @@ class JournalEntry(BaseModel): @property def spoiler_text(self) -> str: - new_text = self.text.replace("\n", "
").replace("<", "<").replace(">", ">").replace("&", "&") - matches = re.findall(r"[A-Z](\s|\w)+\:\)(\w|\s)+\.", new_text) + """Returns the text with all the frisky details hidden away""" + new_text = self.text.replace("<", "<").replace(">", ">").replace("&", "&") + 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: - new_text = new_text.replace(match, f"{match}") + group_to_replace = match[0] + new_text = new_text.replace(group_to_replace, f"{group_to_replace}") return new_text