From d4516fc87d6bbff764d3691191751ddb3ea3536d Mon Sep 17 00:00:00 2001 From: Christopher Teutsch Date: Wed, 15 May 2019 21:42:37 +0200 Subject: [PATCH] Now sleeps until updates are probable --- monitor.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/monitor.py b/monitor.py index 0b228c6..0aa83c9 100644 --- a/monitor.py +++ b/monitor.py @@ -5,6 +5,7 @@ from typing import List from pprint import pprint, pformat import datetime import pause +import sys class MOT: @@ -93,7 +94,7 @@ def get_data(request_data: dict, headers: dict = None, cookies: dict = None) -> url = 'https://abfahrtsmonitor.vrr.de/backend/api/stations/table' reply = requests.post(url, data=request_data, headers=headers, cookies=cookies) reply.raise_for_status() - print('Request time elapsed: ' + str(reply.elapsed)) + print('Request time elapsed: ' + str(reply.elapsed), file=sys.stderr) return reply.json() @@ -127,17 +128,14 @@ def fixup_data(d: dict) -> dict: return d -def fmt_trip(trip: dict) -> str: +def print_trip(trip: dict) -> None: trip_part = "The {}:{} {} (???:{}: :{}) service to {} ".format(trip['hour'], trip['minute'], trip['lineNumber'], trip['lineCode'], trip['directionCode'], trip['direction']) if is_cancelled(trip): - status_part = "is cancelled." + print(trip_part + "is cancelled.") elif is_late(trip): - status_part = "is {} minutes late.".format(trip['delay']) + print(trip_part + "is {} minutes late.".format(trip['delay'])) elif is_early(trip): - status_part = "is {} minutes early.".format(-trip['delay']) - else: - status_part = "is on time." - return trip_part + status_part + print(trip_part + "is {} minutes early.".format(-trip['delay'])) def get_next_refresh(data: dict): @@ -163,7 +161,7 @@ def update(): ) reply_data = fixup_data(reply_data) for trip in reply_data['departureData']: - print(fmt_trip(trip)) + print_trip(trip) return reply_data @@ -171,7 +169,7 @@ def wait(): data = update() while True: next_refresh = get_next_refresh(data) - print("Sleeping until " + datetime.datetime.fromtimestamp(next_refresh).isoformat()) + print("Sleeping until " + datetime.datetime.fromtimestamp(next_refresh).isoformat(), file=sys.stderr) pause.until(next_refresh) data = update()