2021-10-10 16:23:28 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2022-03-26 17:18:50 +00:00
|
|
|
from datetime import datetime, timedelta
|
2021-10-10 16:23:28 +00:00
|
|
|
from requests import get
|
|
|
|
from subprocess import check_output
|
2022-07-02 13:56:49 +00:00
|
|
|
from sys import argv, exit
|
|
|
|
|
|
|
|
LINK_MODE = False
|
|
|
|
if len(argv) > 1 and argv[1] == '--link':
|
|
|
|
LINK_MODE = True
|
2022-07-02 16:30:50 +00:00
|
|
|
if len(argv) > 2:
|
|
|
|
LINK_TARGET = argv[2]
|
|
|
|
else:
|
|
|
|
LINK_TARGET = 'travelynx.franzi.business'
|
2021-10-10 16:23:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
# bundlewrap.utils.text.format_duration, but trimmed down
|
2021-10-11 19:04:56 +00:00
|
|
|
def format_time(seconds):
|
2021-12-09 09:44:58 +00:00
|
|
|
if seconds is None:
|
|
|
|
return "?"
|
2021-12-10 14:53:20 +00:00
|
|
|
seconds = int(seconds)
|
2021-10-10 16:23:28 +00:00
|
|
|
components = []
|
2021-10-10 17:57:23 +00:00
|
|
|
if seconds >= 3600:
|
|
|
|
hours = int(seconds / 3600)
|
|
|
|
seconds -= hours * 3600
|
|
|
|
components.append('{}h'.format(hours))
|
2021-10-10 16:23:28 +00:00
|
|
|
if seconds >= 60:
|
|
|
|
minutes = int(seconds / 60)
|
|
|
|
seconds -= minutes * 60
|
|
|
|
components.append('{}m'.format(minutes))
|
2021-10-11 19:04:56 +00:00
|
|
|
if not components:
|
|
|
|
components.append('now')
|
2021-10-10 16:23:28 +00:00
|
|
|
return " ".join(components)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
2022-02-13 07:47:44 +00:00
|
|
|
wifi_ssid = check_output("iw dev wlp3s0 link | awk '/SSID/ {print $2}'", shell=True).decode().strip().lower()
|
2021-10-10 16:23:28 +00:00
|
|
|
|
|
|
|
if wifi_ssid not in (
|
|
|
|
'wifi@db',
|
|
|
|
'wifionice'
|
|
|
|
):
|
|
|
|
exit(0)
|
|
|
|
|
2021-10-10 17:57:23 +00:00
|
|
|
now = datetime.now()
|
|
|
|
|
2021-12-05 14:31:53 +00:00
|
|
|
trip_info_req = get('https://iceportal.de/api1/rs/tripInfo/trip')
|
2021-10-10 16:23:28 +00:00
|
|
|
trip_info_req.raise_for_status()
|
|
|
|
trip_info = trip_info_req.json()['trip']
|
|
|
|
|
2021-12-05 14:31:53 +00:00
|
|
|
ice_status_req = get('https://iceportal.de/api1/rs/status')
|
2021-10-10 16:23:28 +00:00
|
|
|
ice_status_req.raise_for_status()
|
|
|
|
ice_status = ice_status_req.json()
|
|
|
|
|
2022-07-02 13:56:49 +00:00
|
|
|
if LINK_MODE:
|
2022-07-02 16:30:50 +00:00
|
|
|
check_output(['xdg-open', 'https://{}/s/{}?train={}%20{}'.format(
|
|
|
|
LINK_TARGET,
|
2022-07-02 13:56:49 +00:00
|
|
|
trip_info['stopInfo']['actualLast'],
|
|
|
|
trip_info['trainType'],
|
|
|
|
trip_info['vzn'],
|
|
|
|
)])
|
|
|
|
exit(0)
|
|
|
|
|
2021-10-10 16:23:28 +00:00
|
|
|
next_stop_id = trip_info['stopInfo']['actualNext']
|
|
|
|
for stop in trip_info['stops']:
|
|
|
|
if stop['station']['evaNr'] == next_stop_id:
|
2022-07-02 13:56:49 +00:00
|
|
|
|
2021-10-10 16:39:11 +00:00
|
|
|
if stop['timetable']['departureDelay']:
|
2021-10-10 17:57:23 +00:00
|
|
|
delay = ' | <span color="#FF0000">{}</span>'.format(stop['timetable']['departureDelay'])
|
2021-10-10 16:39:11 +00:00
|
|
|
else:
|
|
|
|
delay = ''
|
|
|
|
|
2022-03-26 17:18:50 +00:00
|
|
|
if stop['timetable'].get('actualArrivalTime', 0):
|
|
|
|
arrival = datetime.fromtimestamp(stop['timetable']['actualArrivalTime']/1000)
|
|
|
|
arrival_in = arrival - now
|
2022-07-03 12:08:12 +00:00
|
|
|
elif stop['timetable'].get('scheduledArrivalTime', 0):
|
|
|
|
arrival = datetime.fromtimestamp(stop['timetable']['scheduledArrivalTime']/1000)
|
|
|
|
arrival_in = arrival - now
|
2022-03-26 17:18:50 +00:00
|
|
|
else:
|
|
|
|
arrival = datetime.now()
|
|
|
|
arrival_in = timedelta()
|
2021-10-10 17:57:23 +00:00
|
|
|
|
|
|
|
next_stop = '{} <span color="#999999">[{}]</span> {} ({}{})'.format(
|
2021-10-10 16:23:28 +00:00
|
|
|
stop['station']['name'],
|
2021-10-10 16:39:11 +00:00
|
|
|
stop['track']['actual'],
|
2021-10-10 17:57:23 +00:00
|
|
|
arrival.strftime('%H:%M'),
|
2021-10-11 19:04:56 +00:00
|
|
|
format_time(arrival_in.total_seconds()),
|
2021-10-10 16:39:11 +00:00
|
|
|
delay
|
2021-10-10 16:23:28 +00:00
|
|
|
)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
next_stop = 'Endstation, bitte Aussteigen'
|
|
|
|
|
2022-03-19 09:31:01 +00:00
|
|
|
net_current = ice_status['connectivity']['currentState']
|
|
|
|
net_future = ice_status['connectivity']['nextState']
|
|
|
|
|
|
|
|
if net_current not in (None, 'NO_INFO') or net_future not in (None, 'NO_INFO'):
|
|
|
|
net = ' <span color="#999999">(Net: {} > [{}] {})</span>'.format(
|
|
|
|
net_current,
|
|
|
|
format_time(ice_status['connectivity']['remainingTimeSeconds']),
|
|
|
|
net_future,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
net = ''
|
|
|
|
|
|
|
|
print('<span color="#999999">{}km/h</span> > {}{}'.format(
|
2021-10-10 16:23:28 +00:00
|
|
|
ice_status['speed'],
|
|
|
|
next_stop,
|
2022-03-19 09:31:01 +00:00
|
|
|
net,
|
2021-10-10 16:23:28 +00:00
|
|
|
))
|
|
|
|
except Exception as e:
|
|
|
|
print(repr(e))
|
|
|
|
exit(0)
|