-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
321 lines (285 loc) · 10.2 KB
/
index.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const BAD_REQUEST_TEXT = 'Bad request.'
const BAD_REQUEST_CODE = 400
const CORS_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
}
// Maps line colour codes to line names.
const LINE_NAME_LOOKUP = {
'#935E3A': 'brown',
'#007A4D': 'green',
'#CD202C': 'red',
'#DA487E': 'pink',
'#3FCFD5': 'turquoise',
'#E37222': 'orange',
'#6AADE4': 'skyblue',
'#C1AFE5': 'lilac',
'#FED100': 'yellow',
'#522398': 'purple',
'#002663': 'navy',
'#B5B6B3': 'grey',
'#00A1DE': 'blue',
'#92D400': 'lime',
}
// Used when filtering by route number.
const NUMBER_CHARS_LOOKUP = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
// Used when getting the current UK time... see
// https://stackoverflow.com/questions/25050034/get-iso-8601-using-intl-datetimeformat
const INTL_DATE_TIME_FORMAT_OPTIONS = {
timeZone: 'Europe/London',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
timeZoneName: 'short',
}
// Use a locale that has adopted ISO 8601 as there is no locale for
// that directly so using Sweden here...
const INTL_DATE_TIME_FORMAT_LOCALE = 'sv-SE'
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url)
const stopId = url.searchParams.get('stopId')
if (request.method !== 'GET' || !stopId) {
return new Response(BAD_REQUEST_TEXT, {
status: BAD_REQUEST_CODE,
headers: CORS_HEADERS,
})
}
const stopUrl = `https://nctx.co.uk/stops/${stopId}`
const stopPage = await fetch(stopUrl)
const departures = []
let currentDeparture = {}
let stopName = ''
const htmlRewriter = await new HTMLRewriter()
.on('h1.place-info-banner__name', {
text(text) {
// Take the first match.
if (text.text.length > 0 && stopName.length === 0) {
stopName = text.text.trim()
}
},
})
.on('div.single-visit__highlight', {
element(elem) {
// Pull this out of the style attribute whose value looks like: background-color:#92D400;
const styleAttr = elem.getAttribute('style')
const routeColour = styleAttr.substring(
'background-color:'.length,
styleAttr.length - 1,
)
currentDeparture.lineColour = routeColour
currentDeparture.line = LINE_NAME_LOOKUP[routeColour]
},
})
.on('p.single-visit__name', {
text(text) {
if (text.text.length > 0) {
currentDeparture.routeNumber = text.text.trim()
}
},
})
.on('p.single-visit__description', {
text(text) {
if (text.text.length > 0) {
currentDeparture.destination = text.text.trim()
}
},
})
.on('div.single-visit__time--expected', {
// Bus has live tracking, value will be "Due" or a number of minutes e.g. "2 mins".
text(text) {
if (text.text.length > 0) {
const trimmedText = text.text.trim()
currentDeparture.expected = trimmedText
// When due, the bus is expected in 0 minutes.
if (trimmedText.toLowerCase() === 'due') {
currentDeparture.expectedMins = 0
} else {
// Parse out the number of minutes.
currentDeparture.expectedMins = parseInt(
trimmedText.split(' ')[0],
10,
)
}
currentDeparture.isRealTime = true
departures.push(currentDeparture)
currentDeparture = {}
}
},
})
.on('div.single-visit__time--aimed', {
// Bus does not have live tracking, value will be "Due" or a clock time e.g. "22:30"
// Sometimes though it's a number of minutes e.g. "59 mins".
text(text) {
if (text.text.length > 0) {
const trimmedText = text.text.trim()
currentDeparture.expected = trimmedText
// When due, the bus is expected in 0 minutes.
if (trimmedText.toLowerCase() === 'due') {
currentDeparture.expectedMins = 0
} else {
// Calculate number of minutes in the future that the value of trimmedText
// represents (value is a clock time e.g. 22:30) and store in expectedMins.
// careful too as 00:10 could be today or tomorrow...
if (trimmedText.indexOf(':') !== -1) {
// This time is in the "hh:mm" 24hr format.
const ukNow = new Date(
new Intl.DateTimeFormat(
INTL_DATE_TIME_FORMAT_LOCALE,
INTL_DATE_TIME_FORMAT_OPTIONS,
).format(new Date()),
)
const departureDate = new Date(
new Intl.DateTimeFormat(
INTL_DATE_TIME_FORMAT_LOCALE,
INTL_DATE_TIME_FORMAT_OPTIONS,
).format(new Date()),
)
// Zero these out for better comparisons at the minute level.
ukNow.setSeconds(0)
ukNow.setMilliseconds(0)
departureDate.setSeconds(0)
departureDate.setMilliseconds(0)
const [departureHours, departureMins] = trimmedText.split(':')
const departureHoursInt = parseInt(departureHours, 10)
const departureMinsInt = parseInt(departureMins, 10)
departureDate.setHours(departureHoursInt)
departureDate.setMinutes(departureMinsInt)
if (ukNow.getHours() > departureHoursInt) {
// The departure is tomorrow e.g. it's now 23:00 and the departure is 00:20.
departureDate.setDate(departureDate.getDate() + 1)
}
const millis = departureDate - ukNow
const minsToDeparture = millis / 1000 / 60
currentDeparture.expectedMins = minsToDeparture
} else {
// This time is in the "59 mins" format.
currentDeparture.expectedMins = parseInt(
trimmedText.split(' ')[0],
10,
)
}
}
currentDeparture.isRealTime = false
departures.push(currentDeparture)
currentDeparture = {}
}
},
})
.transform(stopPage)
.text()
const results = {
stopId,
stopName,
departures,
}
// Filter by line name if needed.
const lineToFilter = url.searchParams.get('line')
if (lineToFilter) {
results.departures = results.departures.filter(
departure => departure.line === lineToFilter,
)
}
// Filter by line colour (hex code) if needed.
const lineColourToFilter = url.searchParams.get('lineColour')
if (lineColourToFilter) {
results.departures = results.departures.filter(
departure => departure.lineColour === `#${lineColourToFilter}`,
)
}
// Filter by route if needed... route 69 includes 69A, 69X etc but not 169 or 690.
const routeToFilter = url.searchParams.get('routeNumber')
if (routeToFilter) {
results.departures = results.departures.filter(departure => {
if (!departure.routeNumber) {
// Seems to be we get the odd bit of bad data, so throw it.
return false
}
const lastChar = departure.routeNumber.substring(
departure.routeNumber.length - 1,
)
// Route number either needs to match exactly, or start with the provded route number and
// not end in a number... so if we're looking for route 58 this should return route 58,
// 58A, 58X but not 590. This also allows us to be more specific and look for 58X.
// You could probably use a regular expression here but I find they introduce more issues
// than they solve, so I avoid them :)
return (
departure.routeNumber === routeToFilter ||
(departure.routeNumber.startsWith(routeToFilter) &&
!NUMBER_CHARS_LOOKUP.includes(lastChar))
)
})
}
// Filter out results that aren't realtime estimates if realTimeOnly is set to any value.
if (url.searchParams.get('realTimeOnly')) {
results.departures = results.departures.filter(
departure => departure.isRealTime,
)
}
// Filter out results that arrive more than a given number of minutes into the future.
const maxWaitTime = parseInt(url.searchParams.get('maxWaitTime'), 10)
if (maxWaitTime) {
results.departures = results.departures.filter(
departure =>
departure.hasOwnProperty('expectedMins') &&
departure.expectedMins <= maxWaitTime,
)
}
// Limit the number of results returned if required.
const maxResults = parseInt(url.searchParams.get('maxResults'), 10)
if (maxResults && results.departures.length > maxResults) {
results.departures.length = maxResults
}
// Only return specified fields if required.
if (url.searchParams.get('fields')) {
const fieldsToReturn = url.searchParams.get('fields').split(',')
if (fieldsToReturn.length > 0) {
results.departures = results.departures.map(departure => {
const newDeparture = {}
for (const fieldName of fieldsToReturn) {
newDeparture[fieldName] = departure[fieldName]
}
return newDeparture
})
}
}
// Format the response appropriately (default = JSON)
const responseFormat = url.searchParams.get('format')
if (!responseFormat || responseFormat === 'json') {
return new Response(JSON.stringify(results, null, 2), {
headers: {
'content-type': 'application/json;charset=UTF-8',
...CORS_HEADERS,
},
})
} else if (responseFormat === 'string') {
let stringResults = `${results.stopId}|${results.stopName}`
let stringDepartures = ''
for (const departure of results.departures) {
for (const val of Object.values(departure)) {
stringDepartures = `${stringDepartures}${
stringDepartures.length > 0 ? '^' : ''
}${val}`
}
stringDepartures = `${stringDepartures}|`
}
stringResults = `${stringResults}|${
stringDepartures.length > 0
? stringDepartures.substring(0, stringDepartures.length - 1)
: ''
}`
return new Response(stringResults, { headers: CORS_HEADERS })
} else {
// Unknown format...
return new Response(BAD_REQUEST_TEXT, {
status: BAD_REQUEST_CODE,
headers: CORS_HEADERS,
})
}
}