-
Notifications
You must be signed in to change notification settings - Fork 7
/
FutarRequest.js
175 lines (152 loc) · 5.73 KB
/
FutarRequest.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
/*global require, module, console, __dirname, parseInt, Date */
/*jslint unparam: true, sloppy: true, vars: true */
var request = require("request"),
strftime = require("strftime");
module.exports = function () {
/**
* A Futar API-jan keresztul lekerdezi egy megallo adatait.
*
* @param {String} id
* @param {Function} callback
*/
var getFutarDataForStop = function (id, callback) {
var requestOptions = {
url: "https://futar.bkk.hu/api/query/v1/ws/otp/api/where/arrivals-and-departures-for-stop.json?stopId=BKK_" + id + "&onlyDepartures=1&minutesBefore=0&minutesAfter=20",
json: true
};
// Adatok lekerese a Futartol.
request(requestOptions, function (error, response, data) {
// nincs API valasz
if (!response) {
console.error("[api] - nem sikerult elerni a BKK Futart");
callback("Nem sikerult elerni a BKK Futart", null);
return;
}
// hibas API valasz
if (200 != response.statusCode) {
console.error("[api] - hibas valasz erkezett a BKK Futartol:\n");
console.error(new Error(response.body));
callback(response.body, null);
return;
}
// nincs menetrend
if (!data.data || 1 > data.data.entry.stopTimes.length) {
console.log("[api] - nincs menetrendinfo: " + id);
callback(false, null);
return;
}
callback(false, data.data);
});
};
/**
* Lekeri egy megallohely jaratait
* es a sablonnak szukseges adatokat adja vissza.
*
* Az adatokat a jarat viszonylata
* szerint rendezve adja vissza.
*
* Egy jarat csak egyszer jelenik meg.
*
* @param {String} id
* @param {Function} callback
*/
var getRoutesInStop = function (id, callback) {
getFutarDataForStop(id, function (error, data) {
var i,
errorMsg,
trips,
routes,
routeIds = {},
tripsInStop = [];
if (error || null == data || !data.hasOwnProperty("references")) {
errorMsg = error || new Error("Nincsenek jaratinformaciok.")
console.log("[api] - nincsenek jaratinformaciok");
callback(errorMsg, null);
return;
}
trips = data.references.trips;
routes = data.references.routes;
// jaratok osszegyujtese
for (i in trips) {
if (routes.hasOwnProperty(trips[i]["routeId"])) {
if (!routeIds.hasOwnProperty(trips[i]["routeId"])) {
routeIds[trips[i]["routeId"]] = 1;
tripsInStop.push({
routeId : trips[i]["routeId"],
shortName : routes[trips[i]["routeId"]]["shortName"],
tripHeadsign : trips[i]["tripHeadsign"]
});
}
}
}
// jaratnev szerinti rendezes
tripsInStop = tripsInStop.sort(function(a, b) {
var x = parseInt(a["shortName"]),
y = parseInt(b["shortName"]);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
callback(false, tripsInStop);
});
};
/**
* Lekeri a kovetkezo busz indulasat es visszaadja
* a sablonnak szukseges adatokat.
*
* @param {String} stopId
* @param {String} routeId
* @param {Function} callback
*
* @returns {null}
*/
var getNextArrival = function (stopId, routeId, callback) {
getFutarDataForStop(stopId, function (error, data) {
var stoptime,
timestamp = null,
time,
tripId,
tripIds = {},
route,
i;
if (error || data == null) {
callback(error, null);
return;
}
// kovetkezo jaratok tripId-ja
for (i in data.references.trips) {
if (routeId == data.references.trips[i]["routeId"]) {
tripIds[data.references.trips[i]["id"]] = 1;
}
}
// kovetkezo jaratok indulasa
if (Object.keys(tripIds).length) {
for (i in data.entry.stopTimes) {
if (tripIds.hasOwnProperty(data.entry.stopTimes[i]["tripId"])) {
stoptime = data.entry.stopTimes[i];
time = (stoptime.predictedArrivalTime || stoptime.arrivalTime) * 1000;
if (null === timestamp || time < timestamp) {
timestamp = time;
tripId = data.entry.stopTimes[i]["tripId"];
}
}
}
}
// a legkorabbi indulast visszaadjuk
if (timestamp) {
time = strftime("%H:%M", new Date(timestamp));
route = data.references.routes[data.references.trips[tripId]["routeId"]];
callback(false, {
route: route.shortName,
time: time,
timestamp: timestamp,
type: route.type.toString().toLowerCase()
});
return;
}
callback(new Error("Nincs menetrend info az adott megallo-viszonylat parosra."), null);
});
};
return {
getRoutesInStop: getRoutesInStop,
getNextArrival: getNextArrival
};
}