Add monitor mode switch and monitor frequency

This commit is contained in:
Marty Oehme 2025-08-12 12:08:38 +02:00
parent 01d992fd4c
commit 7390d1febd
Signed by: Marty
GPG key ID: 4E535BC19C61886E

22
main.py
View file

@ -3,6 +3,7 @@ import json
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from time import sleep
from typing import Any
import requests
@ -16,7 +17,8 @@ NOTIFICATION_CHANNEL = "nightjet-price-notifier"
START_STATION = "8096003" # BerlinHBF
END_STATION = "8796001" # Paris Est
TRAVEL_DATE = "2025-10-14"
MONITOR_FREQUENCY = 3600
def dprint(txt) -> None:
@ -286,6 +288,14 @@ def main(
notification_channel: str = typer.Option(
NOTIFICATION_CHANNEL, help="ntfy channel to inform user on."
),
monitor_mode: bool = typer.Option(
True,
help="Run queries repeatedly over time. If False only runs a single query (oneshot mode).",
),
monitor_frequency: int = typer.Option(
MONITOR_FREQUENCY,
help="How often to run price queries if in monitoring mode, in seconds.",
),
base_output_directory: Path = typer.Option(
Path(BASE_DIR), help="Directory in which to output all result files."
),
@ -309,6 +319,8 @@ def main(
except ValueError:
typer.echo(f"Invalid date format: {travel_date}. Use YYYY-MM-DD", err=True)
raise typer.Exit(1)
while True:
prices = query(
start_station=start_station, end_station=end_station, travel_date=date_obj
)
@ -338,6 +350,14 @@ def main(
notification_channel,
)
# oneshot exit
if not monitor_mode:
break
dprint(
f"Query complete. Monitoring mode active, sleeping for {monitor_frequency} seconds..."
)
sleep(monitor_frequency)
if __name__ == "__main__":
app()