From 9001108904874811a5f13eceb04c96566bab79d1 Mon Sep 17 00:00:00 2001 From: Christopher Teutsch Date: Wed, 15 May 2019 21:31:11 +0200 Subject: [PATCH] Now sleeps until updates are probable --- monitor.py | 71 ++++++++++++++++++++++++++++++++++-------------- requirements.txt | 2 ++ 2 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 requirements.txt diff --git a/monitor.py b/monitor.py index c336883..0b228c6 100644 --- a/monitor.py +++ b/monitor.py @@ -3,6 +3,8 @@ import requests import json from typing import List from pprint import pprint, pformat +import datetime +import pause class MOT: @@ -19,6 +21,13 @@ class MOT: ALL_LINES = [] TRIP_CANCELLED = -9999 +lines_filter = [ + 'rbg:70070: :H', # U70 -> Düsseldorf Hbf + 'rbg:70070: :R', # U70 -> Krefeld Rheinstr + 'rbg:70076: :H', # U76 -> Düsseldorf Hbf + 'rbg:70076: :R', # U76 -> Krefeld Rheinstr +] + def t(s: str) -> str: """ @@ -88,14 +97,6 @@ def get_data(request_data: dict, headers: dict = None, cookies: dict = None) -> return reply.json() -lines_filter = [ - 'rbg:70070: :H', # U70 -> Düsseldorf Hbf - 'rbg:70070: :R', # U70 -> Krefeld Rheinstr - 'rbg:70076: :H', # U76 -> Düsseldorf Hbf - 'rbg:70076: :R', # U76 -> Krefeld Rheinstr -] - - def is_cancelled(trip: dict) -> bool: if trip['delay'] is not None: return int(trip['delay']) == TRIP_CANCELLED @@ -114,14 +115,6 @@ def is_early(trip: dict) -> bool: return False -reply_data = get_data( - make_request_data( - 20021002, - 8, - #lines=lines_filter - ) -) - # Pretty-print the reply data. """print("Data:") pprint(reply_data)""" @@ -137,7 +130,7 @@ def fixup_data(d: dict) -> dict: def fmt_trip(trip: dict) -> str: 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, {}:{} ({}) -> {}:{} ({})".format(trip['orgFullTime'], trip['orgHour'], trip['orgMinute'], trip['fullTime'], trip['hour'], trip['minute']) + status_part = "is cancelled." elif is_late(trip): status_part = "is {} minutes late.".format(trip['delay']) elif is_early(trip): @@ -147,6 +140,44 @@ def fmt_trip(trip: dict) -> str: return trip_part + status_part -reply_data = fixup_data(reply_data) -for t in reply_data['departureData']: - print(fmt_trip(t)) +def get_next_refresh(data: dict): + times = [] + for trip in data['departureData']: + times.append(trip['orgFullTime']) + times.append(trip['fullTime']) + times = [int(time) for time in times if int(time) > datetime.datetime.now().timestamp()] + times.sort() + for time in times: + if (datetime.datetime.fromtimestamp(time) - datetime.datetime.now()) > datetime.timedelta(seconds=30): + return time + return (datetime.datetime.now() + datetime.timedelta(seconds=60)).timestamp() + + +def update(): + reply_data = get_data( + make_request_data( + 20021002, + 8, + lines=lines_filter + ) + ) + reply_data = fixup_data(reply_data) + for trip in reply_data['departureData']: + print(fmt_trip(trip)) + return reply_data + + +def wait(): + data = update() + while True: + next_refresh = get_next_refresh(data) + print("Sleeping until " + datetime.datetime.fromtimestamp(next_refresh).isoformat()) + pause.until(next_refresh) + data = update() + + +def main(): + wait() + + +main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8310781 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +requests +pause \ No newline at end of file