From 24c0b05cf4a91c6098c228c44001fe2f4073dd76 Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Sun, 10 Oct 2021 19:57:23 +0200 Subject: [PATCH] ice-status.py: more improvements --- .config/i3pystatus/ice-status.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/.config/i3pystatus/ice-status.py b/.config/i3pystatus/ice-status.py index 27cc22c..46d6d46 100755 --- a/.config/i3pystatus/ice-status.py +++ b/.config/i3pystatus/ice-status.py @@ -7,13 +7,17 @@ from sys import exit # bundlewrap.utils.text.format_duration, but trimmed down -def format_time(seconds): +def format_time(seconds, with_seconds=True): components = [] + if seconds >= 3600: + hours = int(seconds / 3600) + seconds -= hours * 3600 + components.append('{}h'.format(hours)) if seconds >= 60: minutes = int(seconds / 60) seconds -= minutes * 60 components.append('{}m'.format(minutes)) - if seconds > 0 or not components: + if (seconds > 0 and with_seconds) or not components: components.append('{}s'.format(seconds)) return " ".join(components) @@ -27,6 +31,8 @@ try: ): exit(0) + now = datetime.now() + 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'] @@ -39,21 +45,25 @@ try: for stop in trip_info['stops']: if stop['station']['evaNr'] == next_stop_id: if stop['timetable']['departureDelay']: - delay = ' ({})'.format(stop['timetable']['departureDelay']) + delay = ' | {}'.format(stop['timetable']['departureDelay']) else: delay = '' - next_stop = '{} [{}] {}{}'.format( + arrival = datetime.fromtimestamp(stop['timetable']['actualArrivalTime']/1000) + arrival_in = arrival - now + + next_stop = '{} [{}] {} ({}{})'.format( stop['station']['name'], stop['track']['actual'], - datetime.fromtimestamp(stop['timetable']['actualArrivalTime']/1000).strftime('%H:%M'), + arrival.strftime('%H:%M'), + format_time(arrival_in.total_seconds(), False), delay ) break else: next_stop = 'Endstation, bitte Aussteigen' - print('{}km/h > {} (Net: {} > [{}] {})'.format( + print('{}km/h > {} (Net: {} > [{}] {})'.format( ice_status['speed'], next_stop, ice_status['connectivity']['currentState'],