-
Notifications
You must be signed in to change notification settings - Fork 0
/
sncf.ss
executable file
·58 lines (54 loc) · 2.27 KB
/
sncf.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env gxi
(export main)
(import :std/getopt
:std/srfi/19
:std/sugar
:dlozeve/sncf/api
:dlozeve/sncf/display
:dlozeve/sncf/mattermost)
(def (main . args)
(define-values (station datetime mattermost-url mattermost-channel) (parse-arguments args))
(def sncf-key (getenv "SNCF_AUTH_KEY" #f))
(unless sncf-key
(display "No SNCF API authentication key found. Set the SNCF_AUTH_KEY environment variable.\n"
(current-error-port))
(exit 1))
(define-values (station-name station-id)
(if station
(get-station-id sncf-key station)
(values "Vernon - Giverny (Vernon)" "stop_area:SNCF:87415604")))
(unless station-name
(set! station-name "Vernon - Giverny"))
(unless station-id
(set! station-id "stop_area:SNCF:87415604"))
(define-values (departures disruptions) (get-departures sncf-key station-id datetime))
(display-all departures disruptions station-name datetime)
(when mattermost-url
(let ((tab-str-md
(with-output-to-string
(lambda () (display-all departures disruptions station-name datetime style: 'markdown)))))
(post-to-mattermost mattermost-url tab-str-md channel: mattermost-channel))))
(def (parse-arguments 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)))
(when help
(getopt-display-help gopt "sncf")
(exit 0))
(values station datetime mattermost-url mattermost-channel))
(catch (getopt-error? exn)
(getopt-display-help exn "sncf" (current-error-port))
(exit 1))
(catch (exn)
(display (error-message exn) (current-error-port))
(exit 1))))