From ff87db3467a86c04e0e77f796d30fb54bda6426d Mon Sep 17 00:00:00 2001 From: Guilherme Bufolo Date: Mon, 16 Sep 2024 18:37:40 +0200 Subject: [PATCH] Allow using natural language for start_date and end_date --- cfg/config_template.json | 1 + requirements.txt | 1 + src/reportMissingThekendienst.py | 32 +++++++++++++++++--------------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cfg/config_template.json b/cfg/config_template.json index b22237c..19431f1 100644 --- a/cfg/config_template.json +++ b/cfg/config_template.json @@ -10,6 +10,7 @@ "footer": "\\- Der freundliche Theckendiensterinnerungsbot", "no_open_slots": "Juhu\\! Diese Woche gibt es keine offenen [Slots](https://teamup.com/ksp4hsa93c1nt5kmym)\\!", "appointment_motivator": "Da mache ich Theke\\.", + "start_date": "tomorrow", "time_slots": { "Monday": {"start": "17:00", "end": "22:00"}, "Tuesday": {"start": "17:00", "end": "22:00"}, diff --git a/requirements.txt b/requirements.txt index e79fac5..4000030 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +dateparser==1.2.0 python-telegram-bot==21.4 requests==2.32.3 urllib3==2.2.2 diff --git a/src/reportMissingThekendienst.py b/src/reportMissingThekendienst.py index 07f971f..6a5cde6 100644 --- a/src/reportMissingThekendienst.py +++ b/src/reportMissingThekendienst.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Tuple import urllib.parse import requests import json @@ -11,7 +11,7 @@ import locale import urllib import telegram import asyncio - +import dateparser class Subcalendar(NamedTuple): name: str @@ -172,10 +172,8 @@ def fetch_subcalendar_id_from_name(config) -> List[Subcalendar]: def convert_to_date(zone, text, days_to_check=None) -> datetime: - date = datetime.strptime(text, "%Y-%m-%d") if text else None - - if date: - date = date.replace(tzinfo=zone) + if text: + date = dateparser.parse(text).replace(tzinfo=zone) else: date = datetime.now(zone) if days_to_check: @@ -246,16 +244,16 @@ def make_slots_message(config, sub_calendars, open_slots: List[TimeSlot])-> str: return message -def finalize_message(config, sub_calendars, open_slots: List[TimeSlot])-> str: +def finalize_message(config, sub_calendars, open_slots: List[TimeSlot])-> Tuple[str, bool]: message = make_slots_message(config, sub_calendars, open_slots) - if message: message = config["header"] + message else: message = config["no_open_slots"] + had_message_without_footer = True if message else False message += "\n" + config["footer"] - return message + return (message, had_message_without_footer) async def check_slots_and_notify(config: map, dry_run: bool = False) -> None: @@ -264,7 +262,7 @@ async def check_slots_and_notify(config: map, dry_run: bool = False) -> None: tzone = ZoneInfo(config["timezone"]) start_date = convert_to_date(tzone, config.get("start_date")).replace( hour=0, minute=0, second=0, microsecond=0 - ) + timedelta(days=1) + ) end_date = convert_to_date( tzone, config.get("end_date"), config.get("days_to_check", 7) ).replace(hour=23, minute=59, second=59, microsecond=999) @@ -287,15 +285,19 @@ async def check_slots_and_notify(config: map, dry_run: bool = False) -> None: open_slots = find_open_slots(config, events, start_date, end_date) - message = finalize_message(config, sub_calendars, open_slots) + (message, had_message_without_footer) = finalize_message(config, sub_calendars, open_slots) if dry_run: print("Messsage that would be sent on Telegram:") - print(message) + if not had_message_without_footer: + print("No message would be sent.") + else: + print(message) else: - await send_telegram_message( - config["telegram_bot_token"], config["telegram_channels"], message - ) + if had_message_without_footer: + await send_telegram_message( + config["telegram_bot_token"], config["telegram_channels"], message + ) if __name__ == "__main__": default_config_file = "reportMissingThekendienstConfig.json"