kunsis-dotfiles/.config/i3pystatus/ice-status.py

76 lines
2.4 KiB
Python
Raw Normal View History

2021-10-10 16:23:28 +00:00
#!/usr/bin/env python3
from datetime import datetime
from requests import get
from subprocess import check_output
from sys import exit
# bundlewrap.utils.text.format_duration, but trimmed down
2021-10-10 17:57:23 +00:00
def format_time(seconds, with_seconds=True):
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-10 17:57:23 +00:00
if (seconds > 0 and with_seconds) or not components:
2021-10-10 16:23:28 +00:00
components.append('{}s'.format(seconds))
return " ".join(components)
try:
wifi_ssid = check_output("iw dev wlp4s0 link | awk '/SSID/ {print $2}'", shell=True).decode().strip().lower()
if wifi_ssid not in (
'wifi@db',
'wifionice'
):
exit(0)
2021-10-10 17:57:23 +00:00
now = datetime.now()
2021-10-10 16:23:28 +00:00
trip_info_req = get('https://portal.imice.de/api1/rs/tripInfo/trip')
trip_info_req.raise_for_status()
trip_info = trip_info_req.json()['trip']
ice_status_req = get('https://portal.imice.de/api1/rs/status')
ice_status_req.raise_for_status()
ice_status = ice_status_req.json()
next_stop_id = trip_info['stopInfo']['actualNext']
for stop in trip_info['stops']:
if stop['station']['evaNr'] == next_stop_id:
if stop['timetable']['departureDelay']:
2021-10-10 17:57:23 +00:00
delay = ' | <span color="#FF0000">{}</span>'.format(stop['timetable']['departureDelay'])
else:
delay = ''
2021-10-10 17:57:23 +00:00
arrival = datetime.fromtimestamp(stop['timetable']['actualArrivalTime']/1000)
arrival_in = arrival - now
next_stop = '{} <span color="#999999">[{}]</span> {} ({}{})'.format(
2021-10-10 16:23:28 +00:00
stop['station']['name'],
stop['track']['actual'],
2021-10-10 17:57:23 +00:00
arrival.strftime('%H:%M'),
format_time(arrival_in.total_seconds(), False),
delay
2021-10-10 16:23:28 +00:00
)
break
else:
next_stop = 'Endstation, bitte Aussteigen'
2021-10-10 17:57:23 +00:00
print('<span color="#999999">{}km/h</span> > {} <span color="#999999">(Net: {} > [{}] {})</span>'.format(
2021-10-10 16:23:28 +00:00
ice_status['speed'],
next_stop,
ice_status['connectivity']['currentState'],
format_time(ice_status['connectivity']['remainingTimeSeconds']),
ice_status['connectivity']['nextState'],
))
except Exception as e:
print(repr(e))
exit(0)