Allow using natural language for start_date and end_date

This commit is contained in:
Guilherme Bufolo 2024-09-16 18:37:40 +02:00
parent 7b08bfc8e3
commit ff87db3467
3 changed files with 19 additions and 15 deletions

View File

@ -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"},

View File

@ -1,3 +1,4 @@
dateparser==1.2.0
python-telegram-bot==21.4
requests==2.32.3
urllib3==2.2.2

View File

@ -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"