Now sleeps until updates are probable

This commit is contained in:
Christopher Teutsch 2019-05-15 21:42:37 +02:00
parent 9001108904
commit d4516fc87d
Signed by: iwonder
GPG Key ID: 0EE33D788D50130D

View File

@ -5,6 +5,7 @@ from typing import List
from pprint import pprint, pformat from pprint import pprint, pformat
import datetime import datetime
import pause import pause
import sys
class MOT: 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' url = 'https://abfahrtsmonitor.vrr.de/backend/api/stations/table'
reply = requests.post(url, data=request_data, headers=headers, cookies=cookies) reply = requests.post(url, data=request_data, headers=headers, cookies=cookies)
reply.raise_for_status() reply.raise_for_status()
print('Request time elapsed: ' + str(reply.elapsed)) print('Request time elapsed: ' + str(reply.elapsed), file=sys.stderr)
return reply.json() return reply.json()
@ -127,17 +128,14 @@ def fixup_data(d: dict) -> dict:
return d 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']) trip_part = "The {}:{} {} (???:{}: :{}) service to {} ".format(trip['hour'], trip['minute'], trip['lineNumber'], trip['lineCode'], trip['directionCode'], trip['direction'])
if is_cancelled(trip): if is_cancelled(trip):
status_part = "is cancelled." print(trip_part + "is cancelled.")
elif is_late(trip): 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): elif is_early(trip):
status_part = "is {} minutes early.".format(-trip['delay']) print(trip_part + "is {} minutes early.".format(-trip['delay']))
else:
status_part = "is on time."
return trip_part + status_part
def get_next_refresh(data: dict): def get_next_refresh(data: dict):
@ -163,7 +161,7 @@ def update():
) )
reply_data = fixup_data(reply_data) reply_data = fixup_data(reply_data)
for trip in reply_data['departureData']: for trip in reply_data['departureData']:
print(fmt_trip(trip)) print_trip(trip)
return reply_data return reply_data
@ -171,7 +169,7 @@ def wait():
data = update() data = update()
while True: while True:
next_refresh = get_next_refresh(data) 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) pause.until(next_refresh)
data = update() data = update()