-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimetable_medium.js
384 lines (343 loc) · 11.1 KB
/
timetable_medium.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* PTV Timetable V2.0
* Run on Scriptable For Medium Widegt
* Created by Ricky Li on 2022/12/09
*
* For instructions visit:
* https://github.com/imchlorine/PTVTimetable.git
*
* This Script is used for feching PTV timetable for specific route
* You can always duplicate this Script to add multiple timetable for iOS Stack Widget
*
* For fetching Myki Balance Widget visit:
* https://github.com/imchlorine/MykiBalance.git
*
*
* Important! This Widget is not affiliated to PTV or Myki. For personal use only.
*/
// =============================
// [routeType] All the options from below
// Train: 0
// Tram: 1
// Bus: 2
// Vline: 3
// Night Bus: 4
// [routeName] Can be Train line, Tram route, Bus route, V/Line route
// Train: eg. "Alamein" or "Alamein Line", "Belgrave" or "Belgrave Line" etc.
// Bus: Route number is ok, eg. "200", "207", "900" etc. If not, try full name eg."200 East Coburg - South Melbourne Beach"
// Tram: Route number is ok, eg. "1", "3-3a", "96" etc. If not, try full name eg."1 East Coburg - South Melbourne Beach"
// V/Line: Must use route full name, eg. "Ballarat-Wendouree - Melbourne via Melton".
// To search the route full name, please visit https://www.ptv.vic.gov.au/routes
// [fromStop] Your Departure Stop Name
// [toStop] Your Arriving Stop Name
// Go PTV App or website to find stop name.
// Go PTV App or website to find stop name
// Make sure use the full name of the stop to get the accurate result.
// Here is an example for Tram Route 1 from "Melbourne University/Swanston St #1" to "Federation Square/Swanston St #13"
// Please change the content inside " " below to customize your PTV timetable.
const routeType = "1"
const routeName = "1"
const fromStop = "Melbourne University/Swanston St #1"
const toStop = "Federation Square/Swanston St #13"
// ================================
// ================================
// Do not edit the code below
const baseURL = "https://www.ptv.vic.gov.au/lithe";
let token = await getToken()
let stopDep = await searchStop(fromStop)
let stopDes = await searchStop(toStop)
let route = await getRoute()
let allDepartures = await getStopServices()
let disruptions = await getDisruptions(route.id)
let directionId = await getDirection()
let departures = getDepartures()
let widget = await createWidget(departures);
if (!config.runsInWidget) {
await widget.presentMedium()
}
Script.setWidget(widget)
Script.complete()
async function createWidget(departures) {
let typeColor = getRouteColor(routeType)
let typeIcon = getRouteSymb(routeType)
let hasDisrupt = disruptions.length > 0
let widget = new ListWidget()
let startColor = new Color(typeColor)
let thenColor = new Color("333434")
let midColor = new Color("333434")
let endColor = new Color("#ffffff")
let gradient = new LinearGradient()
gradient.colors = [startColor, thenColor, midColor, endColor]
gradient.locations = [0.0, 0.3, 0.51, 0.51]
widget.backgroundGradient = gradient
widget.addSpacer()
let titleWidget = widget.addStack()
titleWidget.centerAlignContent()
addSymbol({
symbol: typeIcon,
stack: titleWidget,
size: 14
})
titleWidget.addSpacer(10)
let df = new DateFormatter()
df.useShortTimeStyle()
let updated = df.string(new Date())
addTextWithStyle({
stack: titleWidget,
text: "Updated at " + updated,
size: 10
})
titleWidget.addSpacer()
if (hasDisrupt) {
addTextWithStyle({
stack: titleWidget,
text: "Disruptions",
size: 10,
color: "#F9D748",
url: "googlechrome://www.ptv.vic.gov.au" + disruptions[0].link
})
addSymbol({
symbol: "arrow.up.right.square",
stack: titleWidget,
size: 10,
color: "#F9D748"
})
}
widget.addSpacer(5)
addTextWithStyle({
stack: widget,
text: fromStop,
size: 18
})
widget.addSpacer(2)
addTextWithStyle({
stack: widget,
text: "to " + toStop,
size: 16
})
widget.addSpacer(15)
addTextWithStyle({
stack: widget,
text: "Route " + route.label,
color: typeColor,
size: 12
})
widget.addSpacer(5)
let routeWidget = widget.addStack();
routeWidget.addSpacer(10);
for (const dep of departures) {
let depWidget = widget.addStack();
var platText = dep["platform_number"]
if (platText != null) platText = "Platform " + platText;
addTextWithStyle({
stack: depWidget,
text: platText ?? "",
color: "#000000"
})
if (platText != null) depWidget.addSpacer(20)
var scheduledTime = dep["scheduled_departure_utc"]
var estimatedTime = dep["estimated_departure_utc"]
let dateText = new Date(scheduledTime)
let localTime = new Date(dateText)
let depText = "Scheduled " + ("0" + localTime.getHours()).slice(-2) + ":" + ("0" + localTime.getMinutes()).slice(-2)
addTextWithStyle({
stack: depWidget,
text: depText,
color: "#000000"
})
depWidget.addSpacer(15)
let isExpress = dep.run["express_stop_count"]
addTextWithStyle({
stack: depWidget,
text: isExpress > 0 ? "EXPRESS" : "",
color: "#88BC41",
})
depWidget.addSpacer()
var minText = "";
let dif = new Date(estimatedTime ?? scheduledTime).getTime() - new Date().getTime();
let time = Math.round(dif / 60000)
let min = time == 1 ? " min" : " mins"
if (time < 120) minText = time + min
if (time === 0) minText = "Now"
if (estimatedTime === null) minText = ""
if (new Date().toDateString() != localTime.toDateString()) {
let df = new DateFormatter()
df.useMediumDateStyle()
minText = df.string(localTime).slice(0, -4)
}
addTextWithStyle({
stack: depWidget,
text: minText,
color: "#88BC41"
})
if (estimatedTime != null && time < 120)
addSymbol({
symbol: "dot.radiowaves.up.forward",
stack: depWidget,
size: 12,
color: "#88BC41"
})
widget.addSpacer(5)
}
return widget
}
function getRouteColor(routeType) {
var color = ""
switch (routeType) {
case "0":
color = "#3070C7"
break;
case "1":
color = "#88BC41"
break;
case "2":
color = "#EF8933"
break;
case "3":
color = "#832690"
break;
case "4":
color = "#ffffff"
break;
default:
color = "#ffffff"
break;
}
return color;
}
function getRouteSymb(routeType) {
var icon = "tram"
switch (routeType) {
case "0":
case "3":
icon = "tram.fill"
break;
case "1":
icon = "tram"
break;
case "2":
case "4":
icon = "bus"
break;
default:
icon = "tram"
break;
}
return icon;
}
function addSymbol({
stack,
symbol = 'applelogo',
color = "#ffffff",
size = 20,
}) {
const _sym = SFSymbol.named(symbol)
const wImg = stack.addImage(_sym.image)
wImg.tintColor = new Color(color)
wImg.imageSize = new Size(size, size)
}
function addTextWithStyle({
stack,
text,
size = 14,
color = "#ffffff",
url = undefined,
lineLimit = 1
}) {
const textwidget = stack.addText(text)
textwidget.textColor = new Color(color)
textwidget.font = new Font("AppleSDGothicNeo-Bold", size)
textwidget.lineLimit = lineLimit
if (url != undefined) textwidget.url = url
return textwidget
}
function getDepartures() {
let departures = allDepartures.filter(departure => departure["direction_id"].toString() === directionId)
return departures
}
async function getDirection() {
let groupDirections = groupBy(allDepartures, (d) => d["direction_id"])
let keys = Object.keys(groupDirections)
let numOfDirect = keys.length
if (numOfDirect === 1) {
return keys[0]
} else {
var stopSeq = []
try {
stopSeq = await getStopSeq(routeType, groupDirections[keys[0]][0].route.id, keys[0])
} catch (e) {
console.error(e)
}
if (stopSeq.length > 0) {
let stopDepIndex = stopSeq.findIndex(stop => stop.id === stopDep.id)
let stopDesIndex = stopSeq.findIndex(stop => stop.id === stopDes.id)
if (stopDepIndex < stopDesIndex) return keys[0]
return keys[1]
}
}
}
function groupBy(xs, f) {
return xs.reduce((r, v, i, a, k = f(v)) => ((r[k] || (r[k] = [])).push(v), r), {});
}
async function getRoute() {
let uri = `/routes?route_type=${routeType}&`
let result = await apiRequest(uri)
let routes = result["routes"].filter((r) => r["short_label"] === routeName);
if (routes.length === 1) {
return routes[0]
} else {
let route = result["routes"].find((r) => r["label"] === routeName)
return route
}
}
async function searchStop(stopName) {
let queryName = encodeURIComponent(stopName)
let uri = `/search?term=${queryName}&mode=home&`
let result = await apiRequest(uri)
// Why PTV API return data type is not consistent??????????????
var stops = result["results"]["stop"]
if (Array.isArray(stops)) {
stops = result["results"]["stop"][0]
} else {
stops = result["results"]["stop"][routeType]
}
let stop = stops.find(stop => stop.label === stopName)
if (stop === undefined)
stop = stops[0]
return stop
}
async function getStopServices() {
let uri = `/stop-services?stop_id=${stopDep.id}&route_id=${route.id}&mode_id=${routeType}&max_results=2&look_backwards=false&`
let result = await apiRequest(uri)
return result["departures"].filter(dep => route.id === dep.route.id)
}
async function getStopSeq(routeType, routeId, directionId) {
let utcTime = new Date().getUTCDate()
let uri = `/route-stops?route_type=${routeType}&route_id=${routeId}&direction_id=${directionId}&`
let result = await apiRequest(uri)
return result["stops_pattern"]
}
function getDirectionIdFromDes(depId, desId, depSeq, desSeq) {
if (depSeq["seqs"][depId] < desSeq["seqs"][desId]) return depSeq["directions"]
return desSeq["directions"]
}
async function getDisruptions(routeId) {
let uri = `/disruptions?`
let result = await apiRequest(uri)
return result["disruptions"].filter(d => d["route_ids"].includes(routeId) && d["kind"] === "Planned Works")
}
async function getToken() {
let url = "https://www.ptv.vic.gov.au"
let req = new Request(url)
let result = await req.loadString()
let token = result.match(/"fetch-key" value="([^"]+)"/)[1]
return token
}
async function apiRequest(uri) {
let encodedUri = encodeURI(uri)
let url = baseURL + encodedUri + `__tok=${token}`
let req = new Request(url)
let result = await req.loadString()
let jsonResult = JSON.parse(result)
return jsonResult
}