diff --git a/sql/get-departures.sql b/sql/get-departures.sql index d55120d..26a0a50 100644 --- a/sql/get-departures.sql +++ b/sql/get-departures.sql @@ -4,10 +4,9 @@ WITH exceptions AS ( WHERE calendar_dates.date = date(:time) ) -SELECT timediff( - stop_times.departure, - time(:time) -) as wait, +SELECT + unixepoch(stop_times.departure) - + unixepoch(time(:time)) as wait, routes.*, trips.headsign, trips.short_name as trip_name, diff --git a/src/departures.fnl b/src/departures.fnl index bf991b8..639b917 100644 --- a/src/departures.fnl +++ b/src/departures.fnl @@ -1,39 +1,65 @@ (local {: client : query} (require :sqlite)) (local {: encode-wkb} (require :wkb)) -(local {: dofile} (require :fennel)) +(local {: dofile : compile} (require :fennel)) (local dev-config (dofile :config.fnl)) (fn now [] (os.date :%Y-%m-%dT%T (os.time))) +(fn parse-sqltime [sqltime] + (let [(hour min sec) + (string.match sqltime "(%d%d):(%d%d):(%d%d)")] + {:hour (tonumber hour) + :min (tonumber min) + :sec (tonumber sec)})) -(local types {:0 :tram - :1 :metro - :2 :train - :3 :bus - :4 :boat - :5 :cable-tram - :6 :aerial-tram - :7 :funicular - :11 :trolleybus - :12 :monorail}) +(local types {0 :tram + 1 :metro + 2 :train + 3 :bus + 4 :boat + 5 :cable-tram + 6 :aerial-tram + 7 :funicular + 11 :trolleybus + 12 :monorail}) -(fn parse-departure [{: color :text_color text-color}] +(fn parse-departure [{: color + : arrival + : wait + : short_name + : long_name + : headsign + : description + : type + :text_color text-color + &as raw}] (fn color->html [?color] - (and ?color (string.format :#%x color)))) + (and ?color (string.format :#%06x color))) + {: raw + :type (. types type) + :name short_name + : headsign + : wait + :arrival (parse-sqltime arrival) + :color (color->html color) + :text-color (color->html text-color)}) -(with-open [client (client)] +(fn get-departures [client location] (let [q (partial query client)] - (icollect + (icollect [{:short_name name : id &as stop} (q :find-stops - {:searchgeom (encode-wkb dev-config.dev-location)})] + {:searchgeom (encode-wkb location)})] {: stop :departures - (icollect [departure + (icollect [departure (q :get-departures {:limit 5 :time "2026-01-12T07:00:00" :stop id})] - departure)}))) + (parse-departure departure))}))) + +(with-open [client (client)] + (get-departures client dev-config.dev-location)) diff --git a/src/import.fnl b/src/import.fnl index b6cc14c..9504a2a 100644 --- a/src/import.fnl +++ b/src/import.fnl @@ -29,7 +29,8 @@ mask))) (fn normalize-time [value] - (let [(hour min sec) (string.match value "(%d+):(%d+):(%d+)")] + (let [(hour min sec) + (string.match value "(%d+):(%d+):(%d+)")] (string.format "'%02d:%02d:%02d'" (tonumber hour) (tonumber min)