From 7c858272a3bb2a2d05849f9e0d18c268962340e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonia=20P=C3=A9rez-Cerezo?= Date: Mon, 2 Sep 2024 23:30:56 +0200 Subject: [PATCH] Show intermediate stops, handle too early arrivals --- travelynx-waybar.py | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/travelynx-waybar.py b/travelynx-waybar.py index 1c2053d..1e5e221 100755 --- a/travelynx-waybar.py +++ b/travelynx-waybar.py @@ -43,11 +43,26 @@ response = json.loads(contents) st = "" +def humantime(t) : + return time.strftime("%H:%M", time.localtime(int(t))) + +def delay(s,i) : + return (int(s["real"+i]) - int(s["scheduled"+i])) // 60 + +def timestring(s,i): + if not "real"+i in s : + return humantime(s["scheduled"+i]) + dl = delay(s,i) + ht = humantime(s["real"+i]) + if dl > 0 : + return ht + " (+%d)" % dl + if dl < 0 : + return ht + " (%d)" % dl + return ht + def get_destination(response) : if "toStation" in response : s = response["toStation"] - s["humantime"] = time.strftime("%H:%M", time.localtime(int(s["realTime"]))) - s["delay"] = (int(s["realTime"]) - int(s["scheduledTime"])) // 60 if "ds100" in s and s["ds100"] and args.ds_100 : s["prefname"] = s["ds100"] else : @@ -61,13 +76,27 @@ if checked_in or args.last_checkin is not None : s = get_destination(response) elapsed = int(time.time()) - int(s["realTime"]) if checked_in or args.last_checkin == 0 or args.last_checkin * 60 > elapsed : - st = f'{s["prefname"]} {s["humantime"]}' - if s["delay"] > 0 : - st = st + " (+%d)" % s["delay"] + tstr = timestring(s,"Time") + st = f'{s["prefname"]} {tstr}' if "train" in response : train = response["train"] - tooltip = f'{train["type"]} {train["line"]} ({train["no"]}) to {s["name"]}' + if train["line"] is not None : + tooltip = f'{train["type"]} {train["line"]} ({train["no"]}) to {s["name"]}' + else : + tooltip = f'{train["type"]} {train["no"]} to {s["name"]}' + if "intermediateStops" in response : + stops = response["intermediateStops"] + now = time.time() + for stop in stops: + if stop["realDeparture"] is not None and int(stop["realDeparture"]) > now : + dep = timestring(stop, "Departure") + arr = timestring(stop, "Arrival") + tooltip = tooltip + f'\n{arr}–{dep} {stop["name"]}' + elif int(stop["scheduledDeparture"]) > now : + dep = humantime(stop["scheduledDeparture"]) + arr = humantime(stop["scheduledArrival"]) + tooltip = tooltip + f'\n{arr}–{dep} {stop["name"]}' print(json.dumps({"text": st, "tooltip": tooltip, "class": "active", "percentage": ""}))