-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (91 loc) · 4.66 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
import {Template} from './modules/template.js';
import * as Train from './modules/train.js';
const moment = window.moment;
function modifyMomentLocale() {
moment.updateLocale('en', {
relativeTime: {
future : '%s',
past : '<span class="smaller">lähti</span>',
s : '<span class="smaller">nyt</span>',
m : '1<span class="smaller"> min</span>',
mm : '%d<span class="smaller"> min</span>',
h : '1<span class="smaller"> h</span>',
hh : '%d<span class="smaller"> h</span>',
d : '1 pv',
dd : '%d pv',
M : '1 kk',
MM : '%d kk',
y : '1 v',
yy : '%d v'
}
});
}
const stationShortcuts = {
"helsinki": "HKI",
"pasila": "PSL",
"tampere": "TPE",
"tikkurila": "TKL",
}
function getStationCodeFromQueryParameter(param) {
return stationShortcuts[param] ? stationShortcuts[param] : param;
}
async function main() {
modifyMomentLocale();
const t = new Template();
const urlParams = new URLSearchParams(window.location.search);
const fromStationCode = getStationCodeFromQueryParameter(urlParams.get('from'));
const toStationCode = getStationCodeFromQueryParameter(urlParams.get('to'));
if (!fromStationCode || !toStationCode) {
window.location.replace("/?from=TKL&to=HKI");
}
t.setTitleStationNames(fromStationCode, toStationCode);
const response = await Train.getTrainsForStation(fromStationCode, toStationCode);
if (response.code) {
t.setMessage(`${response.code}<br><br>${response.errorMessage}`);
throw response.errorMessage;
}
// Only show commuter trains that are traveling from fromStationCode to toStationCode
const trains = response.filter(train =>
["Commuter", "Long-distance"].includes(train.trainCategory) && (
train.timeTableRows = train.timeTableRows.filter(
station =>
(station.stationShortCode == fromStationCode && station.type == "DEPARTURE" && Train.setBestEstimate(station) && !station.cancelled) ||
(station.stationShortCode == toStationCode && station.type == "ARRIVAL" && Train.setBestEstimate(station) && !station.cancelled)
)
).length > 1);
// console.debug(JSON.stringify(trains, null, 1));
// Sort trains by destination arrival time
trains.sort(function(a, b) {
return moment(a.timeTableRows[0].bestEstimatedTime) - moment(b.timeTableRows[0].bestEstimatedTime);
});
t.addTableHeader();
const MAX_NUMBER_OF_TRAINS = 14;
var trainsListed = 0;
trains.forEach(train => {
// After filtering in previous stage, each train should have exactly 2 stops, the from and to stations the user is interested in.
// If it has more than 2, it's probably on the Ring Rail Line.
if (train.timeTableRows.length != 2) {
if (["I", "P"].includes(train.commuterLineID)) {
console.info(`Train ${Train.getTrainName(train)} (${train.trainNumber}) has ${train.timeTableRows.length} interesting stops probably due to being on circular track: https://www.junat.net/fi/juna/${train.trainNumber}/${train.departureDate}/\n${JSON.stringify(train, null, 1)}`);
if (train.timeTableRows[0].stationShortCode == fromStationCode && train.timeTableRows[1].stationShortCode == toStationCode) {
console.info("We should probably add this train as viable!");
}
}
else {
console.warn(`Train ${Train.getTrainName(train)} (${train.trainNumber}) has ${train.timeTableRows.length} interesting stops: https://www.junat.net/fi/juna/${train.trainNumber}/${train.departureDate}/\n${JSON.stringify(train, null, 1)}`);
}
}
// From and to stations should be in travel order, according to API specification.
else if (train.timeTableRows[0].stationShortCode == toStationCode && train.timeTableRows[1].stationShortCode == fromStationCode) {
console.debug(`Train ${Train.getTrainName(train)} (${train.trainNumber}) is coming the opposite direction: https://www.junat.net/fi/juna/${train.trainNumber}/${train.departureDate}`);
}
else if (train.timeTableRows[0].stationShortCode != fromStationCode || train.timeTableRows[1].stationShortCode != toStationCode) {
console.warn(`Train ${Train.getTrainName(train)} (${train.trainNumber}) has interesting stops in unexpected order:\n${JSON.stringify(train, null, 1)}`);
}
else if (trainsListed <= MAX_NUMBER_OF_TRAINS) {
t.addTableRow(train);
trainsListed++;
}
});
}
main();