forked from powerlanguage/bartDown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdartDown.js
105 lines (86 loc) · 3.43 KB
/
dartDown.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
// Handle parameter changes
const urlParams = new URLSearchParams(window.location.search);
// number of trains to display in either direction
const MAX_TRAINS_PER_DIRECTION = parseInt(urlParams.get('limit')) || 3;
// station of origin abbreviation - https://api.bart.gov/docs/overview/abbrev.aspx
const STATION_CODE = urlParams.get('station') || 'gcdk';
// departure times below the cutoff will not be displayed
const MINUTE_CUTOFF = parseInt(urlParams.get('minute_cutoff')) || 3;
// How often to poll for updates
const UPDATE_MS = parseInt(urlParams.get('refresh')) * 1000 || 180000;
// Scale content
const fontWidth = 100 / (MAX_TRAINS_PER_DIRECTION * 1.5);
document.body.style.fontSize = `${fontWidth}vw`;
async function dartDown() {
console.log('dartDown');
var origin = window.location.protocol + '//' + window.location.host;
try {
const response = await fetch(
'https://corsproxy.io/?' +
`https://api.irishrail.ie/realtime/realtime.asmx/getStationDataByCodeXML?StationCode=${STATION_CODE}`
);
const xmlText = await response.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlText, 'text/xml');
// Extracting data from XML
const estimatesForStation = Array.from(xmlDoc.querySelectorAll('objStationData')).map((estimate) => {
return Array.from(estimate.children).reduce((obj, child) => {
obj[child.nodeName] = child.textContent.trim();
return obj;
}, {});
});
// Filter and process estimates
const estimates = estimatesForStation
.filter((estimate) => parseInt(estimate.Duein) >= MINUTE_CUTOFF) // Keep trains that are due in less than the cutoff
.filter((estimate) => estimate.Traintype === 'DART') // Keep only DART trains
.map((estimate) => {
estimate.Duein = estimate.Duein === 'Leaving' ? '00' : estimate.Duein;
estimate.Duein = estimate.Duein.length < 2 ? '0' + estimate.Duein : estimate.Duein;
return estimate;
})
.sort((a, b) => parseInt(a.Duein) - parseInt(b.Duein));
// Hide the error state
document.getElementById('disconnected').style.display = 'none';
// Remove existing estimates from DOM
Array.from(document.getElementsByClassName('estimate')).forEach((line) => {
line.remove();
});
let directionCount = {
northbound: 0,
southbound: 0,
};
// Add the new estimates to the DOM
estimates.forEach((estimate) => {
const direction = estimate.Direction.toLowerCase();
if (directionCount[direction] < MAX_TRAINS_PER_DIRECTION) {
document
.getElementById(direction)
.insertAdjacentHTML(
'beforeEnd',
`<div class="estimate ${estimate.Destination.toLowerCase()}">${estimate.Duein}</div>`
);
directionCount[direction]++;
}
});
} catch (error) {
displayErrorState(error);
}
}
// Display an icon on error
function displayErrorState(error) {
console.log(error);
// If API is showing no trains, say that
// Otherwise, show disconnected symbol on error
if (trains_available) {
document.getElementById('disconnected').style.display = 'flex';
} else {
document.getElementById('no-trains').style.display = 'flex';
document.getElementById('no-trains').style.fontSize = `${fontWidth}vw`;
}
}
// Kick it off!
dartDown().catch(displayErrorState);
// Set up recurring call
setInterval(() => {
dartDown().catch(displayErrorState);
}, UPDATE_MS);