Display disruption messages

This commit is contained in:
Dimitri Lozeve 2022-09-09 22:22:01 +02:00
parent b7701ddc1f
commit d6d18ba43f

28
sncf.ss
View file

@ -21,13 +21,15 @@
(let* ((options (getopt-parse gopt args)) (let* ((options (getopt-parse gopt args))
(sncf-key (hash-ref options 'sncf-key)) (sncf-key (hash-ref options 'sncf-key))
(mattermost-url (hash-ref options 'mattermost-url)) (mattermost-url (hash-ref options 'mattermost-url))
(mattermost-channel (hash-ref options 'mattermost-channel)) (mattermost-channel (hash-ref options 'mattermost-channel)))
(departures (get-next-departures sncf-key))) (let-values (((departures disruptions) (get-departures sncf-key)))
(display-departures-table departures) (display-departures-table departures)
(def tab-str-md (display-disruptions disruptions)
(with-output-to-string (lambda () (display-departures-table-md departures)))) (when mattermost-url
(if mattermost-url (let ((tab-str-md (with-output-to-string (lambda ()
(post-to-mattermost mattermost-url tab-str-md channel: mattermost-channel))) (display-departures-table-md departures)
(display-disruptions disruptions)))))
(post-to-mattermost mattermost-url tab-str-md channel: mattermost-channel)))))
(catch (getopt-error? exn) (catch (getopt-error? exn)
(getopt-display-help exn "sncf" (current-error-port)) (getopt-display-help exn "sncf" (current-error-port))
(exit 1)) (exit 1))
@ -35,10 +37,12 @@
(display (error-message exn) (current-error-port)) (display (error-message exn) (current-error-port))
(exit 1)))) (exit 1))))
(def (get-next-departures sncf-key) (def (get-departures sncf-key)
(def req (http-get +sncf-url+ headers: `(("Authorization" . ,sncf-key)))) (def req (http-get +sncf-url+ headers: `(("Authorization" . ,sncf-key))))
(if (eq? 200 (request-status req)) (if (eq? 200 (request-status req))
(hash-ref (request-json req) 'departures) (let (req-json (request-json req))
(values (hash-ref req-json 'departures)
(hash-ref req-json 'disruptions)))
(begin (display (format "Failed to query the SNCF API: ~a ~a\n" (begin (display (format "Failed to query the SNCF API: ~a ~a\n"
(request-status req) (request-status-text req))) (request-status req) (request-status-text req)))
(exit 1)))) (exit 1))))
@ -84,6 +88,12 @@
(hash-ref info 'direction) (hash-ref info 'direction)
hour-str))))) hour-str)))))
(def (display-disruptions disruptions)
(displayln "Perturbations :")
(for ((dis disruptions))
(display "* ")
(displayln (hash-ref (car (hash-ref dis 'messages)) 'text))))
(def (post-to-mattermost url text channel: (channel #f)) (def (post-to-mattermost url text channel: (channel #f))
(def data (list->hash-table `((text . ,text)))) (def data (list->hash-table `((text . ,text))))
(when channel (when channel