contrib: switch displayed times from scheduled times to predicted times

ie. times now include the delay

This code was written at +80 …
This commit is contained in:
clonejo 2021-12-26 20:49:56 +01:00 committed by Daniel Friesel
parent 879b3b3815
commit 0ff4ad2f8c

View file

@ -24,22 +24,22 @@ import sys
import xdg # not pyxdg! import xdg # not pyxdg!
def format_stop(stop_name, scheduled_arrival_timestamp, delay): def format_stop(stop_name, predicted_arrival_timestamp, delay):
color = "#ffffff" color = "#ffffff"
if delay > 0: if delay > 0:
if delay <= 2: if delay <= 2:
color = "#ffff00" color = "#ffff00"
else: else:
color = "#ff0000" color = "#ff0000"
delayStr = "{:+.0f}".format(delay) delayStr = "({:+.0f})".format(delay)
else: else:
delayStr = "" delayStr = ""
if isinstance(scheduled_arrival_timestamp, int): if isinstance(predicted_arrival_timestamp, int):
scheduled_arrival_time = datetime.fromtimestamp(scheduled_arrival_timestamp) predicted_arrival_time = datetime.fromtimestamp(predicted_arrival_timestamp)
else: else:
# We assume it's datetime already. # We assume it's datetime already.
scheduled_arrival_time = scheduled_arrival_timestamp predicted_arrival_time = predicted_arrival_timestamp
return f'{stop_name} at <span fgcolor="{color}">{scheduled_arrival_time:%H:%M}{delayStr}</span>' return f'{stop_name} at <span fgcolor="{color}">{predicted_arrival_time:%H:%M}{delayStr}</span>'
api_key_path = Path(xdg.xdg_config_home(), "travelynx.conf") api_key_path = Path(xdg.xdg_config_home(), "travelynx.conf")
@ -48,7 +48,7 @@ if api_key_path.exists():
api_key = f.read().strip() api_key = f.read().strip()
else: else:
print( print(
f"Could not find Travelyxn API key at {api_key_path}.", f"Could not find Travelynx API key at {api_key_path}.",
file=sys.stderr, file=sys.stderr,
) )
sys.exit(1) sys.exit(1)
@ -76,7 +76,8 @@ train = "{}{}".format(j["train"]["type"], j["train"]["no"])
out_fields.append(train) out_fields.append(train)
destination_name = j["toStation"]["name"] destination_name = j["toStation"]["name"]
scheduled_arrival_timestamp = j["toStation"]["scheduledTime"] scheduled_arrival_timestamp = j["toStation"]["scheduledTime"]
delay = (j["toStation"]["realTime"] - j["toStation"]["scheduledTime"]) / 60 predicted_arrival_timestamp = j["toStation"]["realTime"]
delay = (predicted_arrival_timestamp - scheduled_arrival_timestamp) / 60
try: try:
details_res = requests.get(f"https://marudor.de/api/hafas/v2/details/{train}") details_res = requests.get(f"https://marudor.de/api/hafas/v2/details/{train}")
@ -86,22 +87,22 @@ try:
if next_stop_name == destination_name: if next_stop_name == destination_name:
out_fields.append("next") out_fields.append("next")
else: else:
next_scheduled_arrival_time = dateutil.parser.isoparse( next_predicted_arrival_time = dateutil.parser.isoparse(
details["currentStop"]["arrival"]["scheduledTime"] details["currentStop"]["arrival"]["time"]
) )
next_scheduled_arrival_time = next_scheduled_arrival_time.astimezone( next_predicted_arrival_time = next_predicted_arrival_time.astimezone(
dateutil.tz.tzlocal() dateutil.tz.tzlocal()
) )
next_delay = details["currentStop"]["arrival"]["delay"] next_delay = details["currentStop"]["arrival"]["delay"]
out_fields.append( out_fields.append(
"next: " "next: "
+ format_stop(next_stop_name, next_scheduled_arrival_time, next_delay) + format_stop(next_stop_name, next_predicted_arrival_time, next_delay)
) )
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
pass pass
out_fields.append( out_fields.append(
"dest: " + format_stop(destination_name, scheduled_arrival_timestamp, delay) "dest: " + format_stop(destination_name, predicted_arrival_timestamp, delay)
) )
s = ", ".join(out_fields) s = ", ".join(out_fields)