From 5d5b893948493044a83d1218b8cebe512cded59a Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Fri, 25 Apr 2025 16:33:12 +0200 Subject: [PATCH] Sort departures by countdown --- flake.nix | 2 +- main.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 18fd68f..4edc93c 100644 --- a/flake.nix +++ b/flake.nix @@ -16,7 +16,7 @@ vrnp-static = pkgs.buildGoModule { pname = "vrnp"; - version = "0.0.6"; + version = "0.0.7"; vendorHash = null; # For building the package, we use only the files not ignored by Git as inputs. diff --git a/main.go b/main.go index 5e08305..052d2f1 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,6 @@ package main +import "cmp" import "encoding/json" import "errors" import "fmt" @@ -97,6 +98,7 @@ func (c KVVEFAClient) BuildRequest(stopId string) (*http.Request, error) { form.Set("useRealtime", "1") form.Set("limit", "10") form.Set("mode", "direct") + form.Set("useRealtime", "1") form.Set("outputFormat", "json") body := strings.NewReader(form.Encode()) @@ -179,6 +181,7 @@ type Departure struct { Symbol string `json:"symbol"` Direction string `json:"direction"` Leaving string `json:"leaving"` + Countdown int } func ParseDepartures(response DMResponse, allowedPlatform *string) (Departures, error) { @@ -223,9 +226,14 @@ func ParseDepartures(response DMResponse, allowedPlatform *string) (Departures, d.ServingLine.Symbol, direction, leaving, + countdown, }) } + slices.SortFunc(ds, func(a Departure, b Departure) int { + return cmp.Compare(a.Countdown, b.Countdown) + }) + return Departures{ds}, nil } @@ -265,6 +273,7 @@ func main() { } c := efaClients[(efaClient.Add(1) - 1) % uint64(len(efaClients))] + log.Print(c.GetName()) ds, err := FetchDepartures(c, stopId) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError)