Skip to content

Commit 614e321

Browse files
committed
Add kiepscy and kiepscyurl command support. Now you can search through Kiepscy episodes and get urls to ipla!
1 parent ea6a489 commit 614e321

File tree

7 files changed

+63
-3
lines changed

7 files changed

+63
-3
lines changed

data/misc/commands.txt

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ fun - (period[optional]) fun metric for all users
1212
funchart - (username[optional], period[optional]) fun metric chart
1313
handlowa - (--all) shopping sundays in Poland
1414
help - a list of commands
15+
kiepscy - (filter[with & and | operators]) Search Kiepscy episodes either by title or description
16+
kiepscyurl - (number) Get a Kiepscy episode url by the episode number
1517
lastmessages - (username, number[optional]) display last x messages from chat history
1618
likechart - (username[optional], period[optional]) display number of reactions received per day per user
1719
monologuechart - (username[optional], period[optional], --acc) calculate monologue index (acc stands for accumulative, meaning it's counted from the beginning no matter the filter period)

data/misc/kiepscy.parquet

90.5 KB
Binary file not shown.

definitions.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def read_str_file(path):
6868
SHOPPING_SUNDAYS_PATH = os.path.join(DATA_DIR, 'misc/niedziele.txt')
6969
EUROPEJSKAFIRMA_PATH = os.path.join(DATA_DIR, 'misc/europejskafirma.txt')
7070
BOCZEK_PATH = os.path.join(DATA_DIR, 'misc/boczek.txt')
71+
KIEPSCY_PATH = os.path.join(DATA_DIR, 'misc/kiepscy.parquet')
7172

7273
# Load text files with funny phrases
7374
tvp_headlines = read_str_file(TVP_HEADLINES_PATH)
@@ -80,7 +81,7 @@ def read_str_file(path):
8081
shopping_sundays = read_str_file(SHOPPING_SUNDAYS_PATH)
8182
europejskafirma_phrases = read_str_file(EUROPEJSKAFIRMA_PATH)
8283
boczek_phrases = read_str_file(BOCZEK_PATH)
83-
84+
kiepscy_df = pd.read_parquet(KIEPSCY_PATH)
8485

8586
class PeriodFilterMode(Enum):
8687
"""Mode used for filtering the chat data for:

src/core/misc_commands.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from datetime import datetime
33

4+
import pandas as pd
45
import telegram
56
from telegram import Update
67
from telegram.ext import ContextTypes
@@ -10,7 +11,7 @@
1011
from src.models.bot_state import BotState
1112
from src.models.command_args import CommandArgs
1213
from definitions import ozjasz_phrases, bartosiak_phrases, tvp_headlines, tvp_latest_headlines, commands, bible_df, ArgType, shopping_sundays, USERS_PATH, arguments_help, europejskafirma_phrases, \
13-
boczek_phrases
14+
boczek_phrases, kiepscy_df
1415
import src.core.utils as core_utils
1516
import src.stats.utils as stats_utils
1617

@@ -246,3 +247,47 @@ async def cmd_remind_me(self, update: Update, context: ContextTypes.DEFAULT_TYPE
246247
self.job_persistance.save_job(job_queue=context.job_queue, dt=dt, func=core_utils.send_response_message, args=[update.effective_chat.id, update.message.message_id, command_args.string])
247248
response = f"You're gonna get pinged at {core_utils.dt_to_pretty_str(dt)}."
248249
await context.bot.send_message(chat_id=update.effective_chat.id, text=response)
250+
251+
async def cmd_kiepscy(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
252+
command_args = CommandArgs(args=context.args, expected_args=[ArgType.TEXT_MULTISPACED], min_string_length=1, max_string_length=1000)
253+
command_args = core_utils.parse_args(self.users_df, command_args)
254+
if command_args.error != '':
255+
await context.bot.send_message(chat_id=update.effective_chat.id, text=command_args.error)
256+
return
257+
258+
search_phrase = command_args.string
259+
search_phrases = command_args.strings
260+
261+
if search_phrases: # use & operator to match multiple words
262+
regex = core_utils.regexify_multiword_filter(search_phrases)
263+
matching_by_title_df = kiepscy_df[kiepscy_df['title'].str.contains(regex, case=False)]
264+
matching_by_description_df = kiepscy_df[kiepscy_df['description'].str.contains(regex, case=False)]
265+
else:
266+
matching_by_title_df = kiepscy_df[kiepscy_df['title'].str.contains(search_phrase, case=False)]
267+
matching_by_description_df = kiepscy_df[kiepscy_df['description'].str.contains(search_phrase, case=False)]
268+
merged_df = pd.concat([matching_by_title_df, matching_by_description_df], ignore_index=True)
269+
270+
text = f"Kiepscy episodes that match [{search_phrase}]:\n"
271+
for i, (index, row) in enumerate(merged_df.iterrows()):
272+
description = f"{row['description'][:100]}.." if len(row['description']) > 100 else row['description']
273+
text += f"- *{row['nr']}: {row['title']}* - {description}\n"
274+
text = stats_utils.escape_special_characters(text)
275+
await context.bot.send_message(chat_id=update.effective_chat.id, text=text, parse_mode=telegram.constants.ParseMode.MARKDOWN_V2)
276+
277+
async def cmd_kiepscyurl(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
278+
command_args = CommandArgs(args=context.args, expected_args=[ArgType.POSITIVE_INT], number_limit=1000)
279+
command_args = core_utils.parse_args(self.users_df, command_args)
280+
if command_args.error != '':
281+
await context.bot.send_message(chat_id=update.effective_chat.id, text=command_args.error)
282+
return
283+
284+
episode_nr = str(command_args.number)
285+
matching_episode_df = kiepscy_df[kiepscy_df['nr'] == episode_nr]
286+
if matching_episode_df.empty:
287+
await context.bot.send_message(chat_id=update.effective_chat.id, text=f"Nie ma takiego epizodu :(")
288+
return
289+
row = matching_episode_df.iloc[0]
290+
text = f"*{episode_nr}: {row['title']}* - {row['url']}"
291+
292+
text = stats_utils.escape_special_characters(text)
293+
await context.bot.send_message(chat_id=update.effective_chat.id, text=text, parse_mode=telegram.constants.ParseMode.MARKDOWN_V2)

src/core/ozjasz_bot.py

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ def get_commands_map(self):
6666
'remind': self.chat_commands.cmd_remind,
6767
'commands': self.chat_commands.cmd_command_usage,
6868
'summary': self.chat_commands.cmd_summary,
69+
'kiepscy': self.core_commands.cmd_kiepscy,
70+
'kiepscyurl': self.core_commands.cmd_kiepscyurl,
6971
'topmessages': lambda update, context: self.chat_commands.cmd_messages_by_reactions(update, context, EmojiType.ALL),
7072
'sadmessages': lambda update, context: self.chat_commands.cmd_messages_by_reactions(update, context, EmojiType.NEGATIVE),
7173
'topmemes': lambda update, context: self.chat_commands.cmd_media_by_reactions(update, context, MessageType.IMAGE, EmojiType.ALL),

src/core/utils.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,10 @@ def parse_string(command_args: CommandArgs, text: str) -> [str, CommandArgs, str
517517
if len(text) > command_args.max_string_length:
518518
error = f'{command_args.label} {text} is too long, it should have {command_args.max_string_length} characters or less.'
519519

520-
command_args.string = text
520+
if '&' in text: # user for 'AND' filtering
521+
command_args.strings = text.split('&')
522+
else:
523+
command_args.string = text
521524
return text, command_args, error
522525

523526

@@ -622,3 +625,9 @@ async def send_response_message(context, chat_id, message_id, message):
622625

623626
def dt_to_pretty_str(dt):
624627
return dt.strftime("%d-%m-%Y %H:%M:%S")
628+
629+
def regexify_multiword_filter(words):
630+
base = r'^{}'
631+
expr = '(?=.*{})'
632+
return base.format(''.join(expr.format(w) for w in words))
633+

src/models/command_args.py

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class CommandArgs:
2929
dt_format: DatetimeFormat = None
3030
number: int = 5
3131
string: str = ''
32+
strings: list[str] = field(default_factory=lambda: [])
3233
min_string_length: int = 0
3334
max_string_length: int = 20
3435
label: str = ''

0 commit comments

Comments
 (0)