Add monitor mode switch and monitor frequency
This commit is contained in:
parent
01d992fd4c
commit
7390d1febd
1 changed files with 47 additions and 27 deletions
22
main.py
22
main.py
|
|
@ -3,6 +3,7 @@ import json
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from time import sleep
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
@ -16,7 +17,8 @@ NOTIFICATION_CHANNEL = "nightjet-price-notifier"
|
||||||
|
|
||||||
START_STATION = "8096003" # BerlinHBF
|
START_STATION = "8096003" # BerlinHBF
|
||||||
END_STATION = "8796001" # Paris Est
|
END_STATION = "8796001" # Paris Est
|
||||||
TRAVEL_DATE = "2025-10-14"
|
|
||||||
|
MONITOR_FREQUENCY = 3600
|
||||||
|
|
||||||
|
|
||||||
def dprint(txt) -> None:
|
def dprint(txt) -> None:
|
||||||
|
|
@ -286,6 +288,14 @@ def main(
|
||||||
notification_channel: str = typer.Option(
|
notification_channel: str = typer.Option(
|
||||||
NOTIFICATION_CHANNEL, help="ntfy channel to inform user on."
|
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(
|
base_output_directory: Path = typer.Option(
|
||||||
Path(BASE_DIR), help="Directory in which to output all result files."
|
Path(BASE_DIR), help="Directory in which to output all result files."
|
||||||
),
|
),
|
||||||
|
|
@ -309,6 +319,8 @@ def main(
|
||||||
except ValueError:
|
except ValueError:
|
||||||
typer.echo(f"Invalid date format: {travel_date}. Use YYYY-MM-DD", err=True)
|
typer.echo(f"Invalid date format: {travel_date}. Use YYYY-MM-DD", err=True)
|
||||||
raise typer.Exit(1)
|
raise typer.Exit(1)
|
||||||
|
|
||||||
|
while True:
|
||||||
prices = query(
|
prices = query(
|
||||||
start_station=start_station, end_station=end_station, travel_date=date_obj
|
start_station=start_station, end_station=end_station, travel_date=date_obj
|
||||||
)
|
)
|
||||||
|
|
@ -338,6 +350,14 @@ def main(
|
||||||
notification_channel,
|
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__":
|
if __name__ == "__main__":
|
||||||
app()
|
app()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue