Add parameter to choose the date and time of query
This commit is contained in:
parent
e574ac6251
commit
3420497586
1 changed files with 27 additions and 9 deletions
36
sncf.ss
36
sncf.ss
|
@ -18,12 +18,15 @@
|
|||
|
||||
(def (main . args)
|
||||
(def gopt (getopt (optional-argument 'station help: "Name of the station (default Vernon-Giverny).")
|
||||
(optional-argument 'datetime help: "Date and time (ISO 8601 format).")
|
||||
(flag 'help "-h" "--help" help: "Display this help.")
|
||||
(option 'mattermost-url "--mattermost-url" help: "Mattermost incoming webhook URL.")
|
||||
(option 'mattermost-channel "--channel" help: "Mattermost channel.")))
|
||||
(try
|
||||
(let* ((options (getopt-parse gopt args))
|
||||
(station (hash-ref options 'station))
|
||||
(datetime-str (hash-ref options 'datetime))
|
||||
(datetime (if datetime-str (string->date datetime-str "~Y-~m-~dT~H:~M:~S") #f))
|
||||
(help (hash-ref options 'help #f))
|
||||
(mattermost-url (hash-ref options 'mattermost-url))
|
||||
(mattermost-channel (hash-ref options 'mattermost-channel)))
|
||||
|
@ -39,15 +42,27 @@
|
|||
(if station
|
||||
(get-station-id sncf-key station)
|
||||
(values "Vernon - Giverny (Vernon)" "stop_area:SNCF:87415604")))
|
||||
(let-values (((departures disruptions) (get-departures sncf-key station-id)))
|
||||
(displayln (parse-markup (format "[bold]Prochains départs de [green]~a[/green] :" station-name)))
|
||||
(let-values (((departures disruptions) (get-departures sncf-key station-id datetime)))
|
||||
(display (parse-markup
|
||||
(format "[bold]Prochains départs de [green]~a[/green] " station-name)))
|
||||
(displayln (if datetime
|
||||
(parse-markup (format "le ~a à ~a :"
|
||||
(date->string datetime "~a ~d ~b ~Y")
|
||||
(date->string datetime "~H:~M")))
|
||||
":"))
|
||||
(display-departures-table departures)
|
||||
(display-disruptions disruptions)
|
||||
(when mattermost-url
|
||||
(let ((tab-str-md (with-output-to-string (lambda ()
|
||||
(displayln (format "Prochains départs de **~a** :" station-name))
|
||||
(display-departures-table departures style: 'markdown)
|
||||
(display-disruptions disruptions style: 'markdown)))))
|
||||
(let ((tab-str-md (with-output-to-string
|
||||
(lambda ()
|
||||
(display (format "Prochains départs de **~a** " station-name))
|
||||
(displayln (if datetime
|
||||
(parse-markup (format "le ~a à ~a :"
|
||||
(date->string datetime "~a ~d ~b ~Y")
|
||||
(date->string datetime "~H:~M")))
|
||||
":"))
|
||||
(display-departures-table departures style: 'markdown)
|
||||
(display-disruptions disruptions style: 'markdown)))))
|
||||
(post-to-mattermost mattermost-url tab-str-md channel: mattermost-channel)))))
|
||||
(catch (getopt-error? exn)
|
||||
(getopt-display-help exn "sncf" (current-error-port))
|
||||
|
@ -70,9 +85,12 @@
|
|||
(request-status req) (request-status-text req)))
|
||||
(exit 1))))
|
||||
|
||||
(def (get-departures sncf-key station-id)
|
||||
(def (get-departures sncf-key station-id (datetime #f))
|
||||
(def url (format "~a/stop_areas/~a/departures" +sncf-url+ station-id))
|
||||
(def req (http-get url headers: `(("Authorization" . ,sncf-key))))
|
||||
(def params (if datetime
|
||||
`(("from_datetime" . ,(date->string datetime "~Y~m~dT~H~M~S")))
|
||||
#f))
|
||||
(def req (http-get url headers: `(("Authorization" . ,sncf-key)) params: params))
|
||||
(if (eq? 200 (request-status req))
|
||||
(let (req-json (request-json req))
|
||||
(values (parse-departures (hash-ref req-json 'departures))
|
||||
|
@ -110,7 +128,7 @@
|
|||
(def headers (if (eq? style 'markdown)
|
||||
'("Réseau" "Direction" "Heure")
|
||||
'("[bold]Réseau" "[bold]Direction" "[bold]Heure")))
|
||||
(def tab (table headers [(map max '(5 9) widths) ... (if (eq? style 'markdown) 32 13)] style))
|
||||
(def tab (table headers [(map max '(6 9) widths) ... (if (eq? style 'markdown) 32 13)] style))
|
||||
(display (table-header tab))
|
||||
(for ((dep departures))
|
||||
(with ((departure network direction base-dep-dt dep-dt) dep)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue