From c085a3ee10c8a4f9b09bf703c3a3dde6aea9902d Mon Sep 17 00:00:00 2001 From: Marc Koch Date: Tue, 17 Sep 2024 15:56:35 +0200 Subject: [PATCH] implement a maximum time delta for appointment notification --- README.md | 10 +++++- src/cgnappointments/__main__.py | 44 ++++++++++++++++++------ src/cgnappointments/config_template.yaml | 4 +++ 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8afc99e..364d2c1 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,10 @@ services: locations: - Ehrenfeld - Kalk + +# Max time between today and a new appointment to notify about the new +# appointment. Set to -1 to notify about all new appointments. +max_timedelta: 14 # Path to the CSV file to store the scraped appointments # csv_path: ~/Termine.csv @@ -118,7 +122,7 @@ service name if it contains spaces. ```bash usage: cgn-appointments [-h] [-s SERVICES [SERVICES ...]] - [-l LOCATIONS [LOCATIONS ...]] + [-l LOCATIONS [LOCATIONS ...]] [-t MAX_TIMEDELTA] [--config-file CONFIG_FILE] [--csv-file CSV_FILE] [--log-file LOG_FILE] [--log-level {CRITICAL,ERROR,WARNING,INFO,DEBUG,NOTSET}] @@ -132,6 +136,9 @@ options: Services to check -l LOCATIONS [LOCATIONS ...], --locations LOCATIONS [LOCATIONS ...] Locations to check + -t MAX_TIMEDELTA, --max-timedelta MAX_TIMEDELTA + Maximum timedelta in days to notify about new + appointments --config-file CONFIG_FILE Path to the configuration file --csv-file CSV_FILE Path to the csv file, which stores the last fetched @@ -147,6 +154,7 @@ options: cgn-apppointments \ --services "Personalausweis - Antrag" "Reisepass - Antrag (seit 01.01.2024 auch für Kinder unter 12 Jahren)" \ --locations Ehrenfeld Kalk \ + --max-timedelta 7 \ --config-file /path/to/config.yaml \ --csv-file /path/to/csvfile.csv \ --log-file /path/to/logfile.log \ diff --git a/src/cgnappointments/__main__.py b/src/cgnappointments/__main__.py index 479e48a..d5d1443 100644 --- a/src/cgnappointments/__main__.py +++ b/src/cgnappointments/__main__.py @@ -50,6 +50,14 @@ def parse_arguments() -> dict: help="Locations to check", required=False, ) + argparser.add_argument( + "-t", + "--max-timedelta", + action="store", + type=int, + help="Maximum timedelta in days to notify about new appointments", + required=False, + ) argparser.add_argument( "--config-file", action="store", @@ -92,6 +100,7 @@ def update_config_with_args(config: dict, args: dict) -> dict: update_config = { "services": args.get("services"), "locations": args.get("locations"), + "max_timedelta": args.get("max_timedelta"), "csv_path": args.get("csv_file"), } for key, value in update_config.items(): @@ -257,6 +266,7 @@ def main(): url = config.get("url") services = config.get("services") check_locations = config.get("locations") + max_timedelta = config.get("max_timedelta") csv_name = config.get("csv_name", "cgn-appointments.csv") csv_path = define_csv_path(config.get("csv_path"), csv_name) date_regex = config.get("date_regex") @@ -382,17 +392,31 @@ def main(): if new_date and new_date != previous_date: logger.info(f"New appointment found for {name}: {new_date}", extra={"location": name, "previous_date": previous_date, - "new_date": new_date}) + "new_date": new_date, "max_timedelta": max_timedelta}) lines.append((name, new_date.strftime(date_format))) - ntfy( - ntfy_server, - ntfy_topic, - ntfy_title, - ntfy_message % (name, new_date), - session_url, - ntfy_tags, - ntfy_priority, - ) + + # Send notification if new date is within timedelta or + # timedelta is not set + time_delta = (new_date - datetime.now()).days if max_timedelta > 0 else False + if time_delta == False or time_delta <= max_timedelta: + logger.info(f"Sending notification for new appointment.", + extra={"location": name, "new_date": new_date, + "time_delta": time_delta, + "max_timedelta": max_timedelta}) + ntfy( + ntfy_server, + ntfy_topic, + ntfy_title, + ntfy_message % (name, new_date), + session_url, + ntfy_tags, + ntfy_priority, + ) + else: + logger.info(f"New appointment is not within timedelta.", + extra={"location": name, "new_date": new_date, + "time_delta": time_delta, + "max_timedelta": max_timedelta}) elif previous_date is not None: lines.append((name, previous_date.strftime(date_format))) diff --git a/src/cgnappointments/config_template.yaml b/src/cgnappointments/config_template.yaml index bad8e50..bfbfecf 100644 --- a/src/cgnappointments/config_template.yaml +++ b/src/cgnappointments/config_template.yaml @@ -11,6 +11,10 @@ locations: - Ehrenfeld - Kalk +# Max time between today and a new appointment to notify about the new +# appointment. Set to -1 to notify about all new appointments. +max_timedelta: 14 + # Path to the CSV file to store the scraped appointments # csv_path: ~/Termine.csv