-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBusEirann.gs
91 lines (66 loc) · 2.59 KB
/
BusEirann.gs
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
function getNextLimerickPassage() {
try {
var request_options = {
"method": "get"
};
// This is hardcoded for the Ballycasey Opp Old Lodge bus stop
var url = "http://www.buseireann.ie/inc/proto/stopPassageTdi.php?stop_point=6350786630982641495&_=1446809752646"
response = UrlFetchApp.fetch(url, request_options);
if (response.getResponseCode() !== 200) {
Logger.log(response);
return null;
}
var nextPassage = {
waitTime: 999999
};
var data = JSON.parse(response.getContentText());
if (data) {
var now = getTimestamp();
for( var key in data['stopPassageTdi'] ) {
if (key === "foo")
continue;
var passage = data['stopPassageTdi'][key];
var departure = passage.departure_data;
var destination = departure.multilingual_direction_text.defaultValue;
// Ignore all but Limerick buses
if (destination !== "Limerick")
continue;
passage.actual = departure.actual_passage_time_utc;
passage.scheduled = departure.scheduled_passage_time_utc;
if (passage.actual)
passage.waitTime = parseInt(passage.actual - now);
else
passage.waitTime = parseInt(passage.scheduled - now);
// Ignore passages that happened before of 5 minutes ago
if (passage.waitTime < -300)
continue;
passage.waitTimeString = timeString(passage.waitTime);
if (!passage.actual)
passage.waitTimeString += " (scheduled)";
Logger.log("Passage for Limerick due in " + passage.waitTimeString + " ( " + passage.waitTime + ")");
if (passage.waitTime < nextPassage.waitTime) {
nextPassage = passage;
}
}
}
if (!nextPassage.waitTimeString) {
Logger.log("Failed to get info from BusEireann on next passage. See data below");
Logger.log(JSON.stringify(data));
return null;
}
if (nextPassage.waitTime > 0) {
nextPassage.text = "The bus for Limerick is due in " + nextPassage.waitTimeString;
} else {
nextPassage.text = "The bus for Limerick has just departed";
if (nextPassage.actual) {
var delayString = timeString(nextPassage.actual - nextPassage.scheduled);
nextPassage.text += " with a #delay of " + delayString;
}
}
Logger.log("Result: " + nextPassage.text);
return nextPassage;
} catch (e) {
Logger.log(JSON.stringify(e));
throw e;
}
}