2021-08-17 13:39:24 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
# This script queries the Travelynx API if you are checked into a train. If
|
2022-08-19 15:21:41 +00:00
|
|
|
# yes, bahn.expert is additionally queried for the next stop, and a JSON object
|
2021-08-17 13:39:24 +00:00
|
|
|
# like this is written to stdout:
|
|
|
|
# {"full_text": "RE26824, next: D\u00fcren at <span fgcolor=\"#ff0000\">15:38+5</span>, dest: Aachen Hbf at <span fgcolor=\"#ff0000\">16:07+5</span>", "markup": "pango"},
|
|
|
|
# The script then exits.
|
|
|
|
#
|
|
|
|
# Configuration:
|
|
|
|
# - Place your API key from https://travelynx.de/account at
|
|
|
|
# ~/.config/travelynx.conf .
|
|
|
|
# - Then integrate into whatever generates your i3bar input.
|
|
|
|
# - Make sure you use i3bar with a pango font, so that the color tags are
|
|
|
|
# picked up.
|
|
|
|
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
import dateutil
|
|
|
|
import dateutil.parser
|
|
|
|
import json
|
|
|
|
from pathlib import Path
|
|
|
|
import requests
|
|
|
|
import sys
|
|
|
|
import xdg # not pyxdg!
|
|
|
|
|
|
|
|
|
2021-12-26 19:49:56 +00:00
|
|
|
def format_stop(stop_name, predicted_arrival_timestamp, delay):
|
2021-08-17 13:39:24 +00:00
|
|
|
color = "#ffffff"
|
|
|
|
if delay > 0:
|
|
|
|
if delay <= 2:
|
|
|
|
color = "#ffff00"
|
|
|
|
else:
|
|
|
|
color = "#ff0000"
|
2021-12-26 19:49:56 +00:00
|
|
|
delayStr = "({:+.0f})".format(delay)
|
2021-08-17 13:39:24 +00:00
|
|
|
else:
|
|
|
|
delayStr = ""
|
2021-12-26 19:49:56 +00:00
|
|
|
if isinstance(predicted_arrival_timestamp, int):
|
|
|
|
predicted_arrival_time = datetime.fromtimestamp(predicted_arrival_timestamp)
|
2021-08-17 13:39:24 +00:00
|
|
|
else:
|
|
|
|
# We assume it's datetime already.
|
2021-12-26 19:49:56 +00:00
|
|
|
predicted_arrival_time = predicted_arrival_timestamp
|
|
|
|
return f'{stop_name} at <span fgcolor="{color}">{predicted_arrival_time:%H:%M}{delayStr}</span>'
|
2021-08-17 13:39:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
api_key_path = Path(xdg.xdg_config_home(), "travelynx.conf")
|
|
|
|
if api_key_path.exists():
|
|
|
|
with api_key_path.open("r") as f:
|
|
|
|
api_key = f.read().strip()
|
|
|
|
else:
|
|
|
|
print(
|
2021-12-26 19:49:56 +00:00
|
|
|
f"Could not find Travelynx API key at {api_key_path}.",
|
2021-08-17 13:39:24 +00:00
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
api_base = f"https://travelynx.de/api/v1/status/{api_key}"
|
|
|
|
try:
|
|
|
|
res = requests.get(api_base)
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
print(
|
|
|
|
json.dumps({"full_text": "Could not connect to travelynx", "color": "#ff0000"})
|
|
|
|
+ ","
|
|
|
|
)
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
j = res.json()
|
|
|
|
# print(json.dumps(j, sort_keys=True, indent=4), file=sys.stderr)
|
|
|
|
|
|
|
|
if not j["checkedIn"]:
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
out_fields = []
|
|
|
|
|
|
|
|
train = "{}{}".format(j["train"]["type"], j["train"]["no"])
|
|
|
|
out_fields.append(train)
|
|
|
|
destination_name = j["toStation"]["name"]
|
|
|
|
scheduled_arrival_timestamp = j["toStation"]["scheduledTime"]
|
2021-12-26 19:49:56 +00:00
|
|
|
predicted_arrival_timestamp = j["toStation"]["realTime"]
|
|
|
|
delay = (predicted_arrival_timestamp - scheduled_arrival_timestamp) / 60
|
2021-08-17 13:39:24 +00:00
|
|
|
|
|
|
|
try:
|
2022-08-19 15:21:41 +00:00
|
|
|
details_res = requests.get(f"https://bahn.expert/api/hafas/v2/details/{train}")
|
2021-08-17 13:39:24 +00:00
|
|
|
details = details_res.json()
|
|
|
|
# print(json.dumps(details, sort_keys=True, indent=4), file=sys.stderr)
|
|
|
|
next_stop_name = details["currentStop"]["station"]["title"]
|
|
|
|
if next_stop_name == destination_name:
|
|
|
|
out_fields.append("next")
|
|
|
|
else:
|
2021-12-26 19:49:56 +00:00
|
|
|
next_predicted_arrival_time = dateutil.parser.isoparse(
|
|
|
|
details["currentStop"]["arrival"]["time"]
|
2021-08-17 13:39:24 +00:00
|
|
|
)
|
2021-12-26 19:49:56 +00:00
|
|
|
next_predicted_arrival_time = next_predicted_arrival_time.astimezone(
|
2021-08-17 13:39:24 +00:00
|
|
|
dateutil.tz.tzlocal()
|
|
|
|
)
|
|
|
|
next_delay = details["currentStop"]["arrival"]["delay"]
|
|
|
|
out_fields.append(
|
|
|
|
"next: "
|
2021-12-26 19:49:56 +00:00
|
|
|
+ format_stop(next_stop_name, next_predicted_arrival_time, next_delay)
|
2021-08-17 13:39:24 +00:00
|
|
|
)
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
out_fields.append(
|
2021-12-26 19:49:56 +00:00
|
|
|
"dest: " + format_stop(destination_name, predicted_arrival_timestamp, delay)
|
2021-08-17 13:39:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
s = ", ".join(out_fields)
|
|
|
|
|
|
|
|
out_obj = {"full_text": s, "markup": "pango"}
|
|
|
|
print(json.dumps(out_obj) + ",")
|