From 8f4229b1e29bf7660696ad002216de2b2141499d Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Sun, 10 Oct 2021 18:23:28 +0200 Subject: [PATCH] i3pystatus: add ice portal info --- .config/i3pystatus/config.py | 7 +++- .config/i3pystatus/ice-status.py | 59 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100755 .config/i3pystatus/ice-status.py diff --git a/.config/i3pystatus/config.py b/.config/i3pystatus/config.py index 1836cb7..1622c7f 100644 --- a/.config/i3pystatus/config.py +++ b/.config/i3pystatus/config.py @@ -96,6 +96,11 @@ status.register("shell", on_leftclick="xinput enable 'SynPS/2 Synaptics TouchPad'", on_rightclick="xinput disable 'SynPS/2 Synaptics TouchPad'") +status.register("shell", + format="{output}", + command="/home/kunsi/.config/i3pystatus/ice-status.py", + interval=2) + status.register("network", interface="wlp4s0", format_up="{interface}:[ {essid} ({freq:01.3f}GHz – {quality}%)][ {v6cidr}][ {v4cidr}]", @@ -103,7 +108,7 @@ status.register("network", detect_active=True, freq_divisor=1000000000, divisor=1024, - on_leftclick="xiate -name xiate-floating -e sudo wifi-menu", + on_leftclick="xiate -class xiate-floating -e sudo wifi-menu", hints={"markup":"pango"}) #status.register("mpd", diff --git a/.config/i3pystatus/ice-status.py b/.config/i3pystatus/ice-status.py new file mode 100755 index 0000000..9dafd06 --- /dev/null +++ b/.config/i3pystatus/ice-status.py @@ -0,0 +1,59 @@ +#!/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 +def format_time(seconds): + components = [] + if seconds >= 60: + minutes = int(seconds / 60) + seconds -= minutes * 60 + components.append('{}m'.format(minutes)) + if seconds > 0 or not components: + 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) + + 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: + next_stop = '{} {}{}'.format( + stop['station']['name'], + datetime.fromtimestamp(stop['timetable']['actualArrivalTime']/1000).strftime('%H:%M'), + stop['timetable']['departureDelay'] if stop['timetable']['departureDelay'] else '', + ) + break + else: + next_stop = 'Endstation, bitte Aussteigen' + + print('{}km/h > {} (Net: {} > [{}] {})'.format( + 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)