-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
129 lines (118 loc) · 4.57 KB
/
core.clj
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(ns org.passen.intransit.core
(:require
[clojure.data.json :as json]
[clojure.set :as set]
[clojure.string :as str]
[java-http-clj.core :as http]
[lambdaisland.uri :as uri])
(:import
(java.time LocalDateTime ZoneId ZonedDateTime)))
(def ^:const ^:private output-type "JSON")
(defn- parse-cta-timestamp
[ts]
(ZonedDateTime/of
(LocalDateTime/parse ts)
(ZoneId/of "America/Chicago")))
(defn- parse-common
[{:keys [body]}]
(-> body
(json/read-str :key-fn keyword)
:ctatt
(dissoc :TimeStamp)
(set/rename-keys {:errCd :error-code
:errNm :error-message
:tmst :timestamp})
(update :error-code parse-long)
(update :timestamp parse-cta-timestamp)))
(defn- parse-flag
[flag]
(not (zero? (parse-long flag))))
(defn- handle-arrival
[arrival]
(-> arrival
(select-keys [:staNm :arrT :rt :destNm :rn :isDly :isFlt :isApp :isSch])
(set/rename-keys {:staNm :station
:arrT :arrival-time
:rt :route
:destNm :destination
:rn :run-number
:isDly :delayed?
:isFlt :fault?
:isApp :approaching?
:isSch :scheduled?})
(update :arrival-time parse-cta-timestamp)
(update :run-number parse-long)
(update :route keyword)
(update :delayed? parse-flag)
(update :fault? parse-flag)
(update :approaching? parse-flag)
(update :scheduled? parse-flag)))
(defn arrivals
"Returns an object containing a list of arrival predictions
for all platforms at a given train station"
[api-key & {:keys [station-id stop-id route max-results]}]
(let [base-url (uri/uri "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx")
query-params (uri/map->query-string
{:key api-key
:mapid station-id
:stpid stop-id
:rt (cond-> route (some? route) name)
:max max-results
:outputType output-type})
url (assoc base-url :query query-params)]
(-> (http/get (str url))
parse-common
(set/rename-keys {:eta :arrivals})
(update :arrivals (partial map handle-arrival)))))
(defn- handle-follow
[follow]
(-> follow
(select-keys [:staNm :arrT :destNm])
(set/rename-keys {:staNm :station
:arrT :arrival-time
:destNm :destination})
(update :arrival-time parse-cta-timestamp)))
(defn follow
"Returns an object containing a list of arrival predictions for a given train
at all subsequent stations for which that train is estimated to arrive"
[api-key & {:keys [run-number]}]
(let [base-url (uri/uri "http://lapi.transitchicago.com/api/1.0/ttfollow.aspx")
query-params (uri/map->query-string
{:key api-key
:runnumber run-number
:outputType output-type})
url (assoc base-url :query query-params)]
(-> (http/get (str url))
parse-common
(dissoc :position)
(set/rename-keys {:eta :follows})
(update :follows (partial map handle-follow)))))
(defn- handle-position
[position]
(-> position
(select-keys [:nextStaNm :arrT :destNm :rn])
(set/rename-keys {:nextStaNm :next-station
:arrT :arrival-time
:destNm :destination
:rn :run-number})
(update :arrival-time parse-cta-timestamp)
(update :run-number parse-long)))
(defn- handle-route
[{:keys [train] :as route}]
(let [color (-> "@name" keyword route str/capitalize keyword)
positions (cond-> train (map? train) list)]
[color (map handle-position positions)]))
(defn positions
"Returns an object containing a list of in-service trains
and basic info and their locations for one or more specified routes"
[api-key routes]
(let [base-url (uri/uri "http://lapi.transitchicago.com/api/1.0/ttpositions.aspx")
query-params (uri/map->query-string
{:key api-key
:rt (map name routes)
:outputType output-type})
url (assoc base-url :query query-params)]
(-> (http/get (str url))
parse-common
(set/rename-keys {:route :routes})
(update :routes (partial into {} (map handle-route))))))