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": ""}))