Now sleeps until updates are probable

This commit is contained in:
Christopher Teutsch 2019-05-15 21:31:11 +02:00
parent 5775f07319
commit 9001108904
Signed by: iwonder
GPG Key ID: 0EE33D788D50130D
2 changed files with 53 additions and 20 deletions

View File

@ -3,6 +3,8 @@ import requests
import json import json
from typing import List from typing import List
from pprint import pprint, pformat from pprint import pprint, pformat
import datetime
import pause
class MOT: class MOT:
@ -19,6 +21,13 @@ class MOT:
ALL_LINES = [] ALL_LINES = []
TRIP_CANCELLED = -9999 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: def t(s: str) -> str:
""" """
@ -88,14 +97,6 @@ def get_data(request_data: dict, headers: dict = None, cookies: dict = None) ->
return reply.json() 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: def is_cancelled(trip: dict) -> bool:
if trip['delay'] is not None: if trip['delay'] is not None:
return int(trip['delay']) == TRIP_CANCELLED return int(trip['delay']) == TRIP_CANCELLED
@ -114,14 +115,6 @@ def is_early(trip: dict) -> bool:
return False return False
reply_data = get_data(
make_request_data(
20021002,
8,
#lines=lines_filter
)
)
# Pretty-print the reply data. # Pretty-print the reply data.
"""print("Data:") """print("Data:")
pprint(reply_data)""" pprint(reply_data)"""
@ -137,7 +130,7 @@ def fixup_data(d: dict) -> dict:
def fmt_trip(trip: dict) -> str: def fmt_trip(trip: dict) -> str:
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, {}:{} ({}) -> {}:{} ({})".format(trip['orgFullTime'], trip['orgHour'], trip['orgMinute'], trip['fullTime'], trip['hour'], trip['minute']) status_part = "is cancelled."
elif is_late(trip): elif is_late(trip):
status_part = "is {} minutes late.".format(trip['delay']) status_part = "is {} minutes late.".format(trip['delay'])
elif is_early(trip): elif is_early(trip):
@ -147,6 +140,44 @@ def fmt_trip(trip: dict) -> str:
return trip_part + status_part return trip_part + status_part
reply_data = fixup_data(reply_data) def get_next_refresh(data: dict):
for t in reply_data['departureData']: times = []
print(fmt_trip(t)) 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()

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
requests
pause