-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctaDataFetcher.js
More file actions
149 lines (136 loc) · 4.42 KB
/
ctaDataFetcher.js
File metadata and controls
149 lines (136 loc) · 4.42 KB
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
/*
This JS file is the handles calling the CTA API and getting the necessary data from it.
(C) Rakesh Das https://rakeshdas.com
*/
var request = require('request');
// API URLs:
var pinkTrackerURL = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=838ed1580f4242f09a361dfb6b8ff32c&mapid=40830&max=5&rt=Pink&outputType=json";
var busTrackerBase = "http://ctabustracker.com/bustime/api/v2/getpredictions?key=iKCawwmjUxPi6NQqeYaB2azjw";
var Bus18URL = "&rt=18&stpid=6813,6765&top=5&format=json";
var Bus60URL = "&rt=60&stpid=6375,6337&top=5&format=json";
//data arrays:
var plLoopTimes = []; //array for predicted arrival of Pink line Loop bound trains
var pl54Times = []; //array for predicted arrival of Pink line 54th/cermak bound trains
var bus18East = []; //array for predicted arrival of 18th eastbound buses
var bus18West = []; //array for predicted arrival of 18th westbound buses
var bus60East = []; //array for predicted arrival of 60 eastbound buses
var bus60West = []; //array for predicted arrival of 60 westbound buses
function getPLData() {
request({
url: pinkTrackerURL,
json: true
}, function (err, res, body) {
if (err) throw err;
else if (!err, res.statusCode === 200) {
if (body.ctatt.eta != undefined || body.ctatt.eta != null) {
var ETAs = body.ctatt.eta
if (plLoopTimes.length != 0 || pl54Times.length != 0) {
pl54Times = [];
plLoopTimes = [];
}
if (ETAs.length != 0) {
ETAs.forEach(function (eta) {
if (eta.destNm === "Loop") {
plLoopTimes.push(eta);
} else {
pl54Times.push(eta);
}
});
}
} else {
pl54Times = [];
plLoopTimes = [];
}
}
});
}
function get18Data() {
var url = busTrackerBase + Bus18URL;
request({
url: url,
json: true
}, function (err, res, body) {
if (err) throw err;
else if (!err && res.statusCode === 200) {
var ETAs = body['bustime-response'].prd;
if (ETAs != null) {
bus18East = [];
bus18West = [];
console.log("18 bus size: " + ETAs.length);
ETAs.forEach(function (eta) {
if (eta.rtdir === "Eastbound") {
bus18East.push(eta);
} else if (eta.rtdir === "Westbound") {
bus18West.push(eta);
}
});
}
}
});
}
function get60Data() {
var url = busTrackerBase + Bus60URL;
request({
url: url,
json: true
}, function (err, res, body) {
if (err) throw err;
else if (!err && res.statusCode === 200) {
var ETAs = body['bustime-response'].prd;
if (ETAs != null) {
if (bus60East.length != 0 && bus60West.length != 0) {
bus60East = [];
bus60West = [];
}
ETAs.forEach(function (eta) {
if (eta.rtdir === "Eastbound") {
bus60East.push(eta);
} else if (eta.rtdir === "Westbound") {
bus60West.push(eta);
}
});
}
}
});
}
module.exports = {
get18Data: get18Data,
get60Data: get60Data,
getPLData: getPLData,
plLoopTimes: function () {
if (plLoopTimes.length > 5) {
plLoopTimes.slice(0, 5);
}
return plLoopTimes;
},
pl54Times: function () {
if (pl54Times.length > 5) {
pl54Times.slice(0, 5);
}
return pl54Times;
},
bus18East: function () {
if (bus18East.length > 5) {
bus18East.slice(0, 5);
}
return bus18East;
},
bus18West: function () {
if (bus18West.length > 5) {
bus18West.slice(0, 5);
}
return bus18West;
},
bus60East: function () {
if (bus60East.length > 5) {
bus60East.slice(0, 5);
}
return bus60East;
},
bus60West: function () {
if (bus60West.length > 5) {
bus60West.slice(0, 5);
}
return bus60West;
}
}