This repository has been archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
209 lines (158 loc) · 5.65 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var Hapi = require('hapi');
var Joi = require('joi');
var Request = require('request');
var Crypto = require('crypto');
var Async = require('async');
var API_KEY = process.env.API_KEY;
var BASE_URL = 'http://ws.mybustracker.co.uk/?module=json&key=';
var config = {
hostname: 'localhost',
port: process.env.PORT || 9001
};
var routes = {};
var api = {};
api.getBusTimes = function(options, callback) {
var path = 'function=getBusTimes';
if(options.stopId) {
path += '&stopId=' + options.stopId.toString();
}
if(options.stopId1) {
path += '&stopId1=' + options.stopId1.toString();
}
if(options.refDest) {
path += '&refDest=' + options.refDest.toString();
}
console.log(getBaseURL() + path);
Request(getBaseURL() + path, function (error, response, body) {
callback(error, JSON.parse(body));
});
};
api.getJourneyTimes = function(stopId, journeyId, callback) {
var path = 'function=getJourneyTimes';
if(stopId) {
path += '&stopId=' + stopId.toString();
}
if(journeyId) {
path += '&journeyId=' + journeyId.toString();
}
console.log(getBaseURL() + path);
Request(getBaseURL() + path, function (error, response, body) {
callback(error, JSON.parse(body));
});
};
var getDestinationRefDest = function(stopId, destinationStopId, callback) {
api.getBusTimes({
stopId: destinationStopId
}, function(err, data) {
if(err) {
return callback(err);
}
callback(null, stopId, data.busTimes[0].timeDatas[0].refDest);
});
};
var getJourneyIds = function(stopId, refDest, callback) {
api.getBusTimes({
stopId1: stopId,
refDest: refDest
}, function(err, data) {
if(err) {
return callback(err);
}
var journeyIds = [];
data.busTimes.forEach(function(busTime) {
busTime.timeDatas.forEach(function(timeData) {
if(!timeData) {
return;
}
journeyIds.push(timeData.journeyId);
});
});
callback(null, stopId, journeyIds);
});
};
var getAllJourneyTimes = function(stopId, journeyIds, callback) {
Async.map(journeyIds, api.getJourneyTimes.bind(this, stopId), callback);
};
// Journeys are the journey objects returned from the API. We need to go through each journey looking for our stop and
// return the number of minutes it takes to get to our stop as well as bus stuff for that journey
var getTravelTimeForJourneys = function(stopId, journeys, callback) {
// Travel info is an array containing travel time and bus stuff
var travelInfoArray = journeys.map(getTravelTimeForJourney.bind(this, stopId));
callback(null, travelInfoArray);
};
var getTravelTimeForJourney = function(stopId, journey) {
var travelInfo = {
serviceNumber: journey.journeyTimes[0].mnemoService,
serviceName: journey.journeyTimes[0].nameService,
departureTime: journey.journeyTimes[0].journeyTimeDatas[0].time,
departureMinutes: journey.journeyTimes[0].journeyTimeDatas[0].minutes,
arrivalTime: "",
arrivalMinutes: ""
};
journey.journeyTimes[0].journeyTimeDatas.some(function(journeyTimeData) {
if(journeyTimeData.stopId !== stopId.toString()) {
return false;
}
travelInfo.arrivalTime = journeyTimeData.time;
// Get the time and date now
var now = new Date();
// Pull out the hours and minutes from the hh:mm time returned by the API
var hours = parseInt(journeyTimeData.time.split(':')[0], 10);
var minutes = parseInt(journeyTimeData.time.split(':')[1], 10);
// Get another Date object for right now
var arrivalTime = new Date();
// Set its hours and minutes
if(hours < now.getHours()) {
arrivalTime.setDate(arrivalTime.getDate()+1);
};
arrivalTime.setHours(hours);
arrivalTime.setMinutes(minutes);
var diffMs = arrivalTime - now;
var diffMins = Math.round(diffMs / (1000*60));
travelInfo.arrivalMinutes = diffMins;
return true;
});
return travelInfo;
};
routes.getJourneys = function(request, reply) {
var from = request.query.from;
var to = request.query.to;
var limit = request.query.limit || 1;
Async.waterfall([getDestinationRefDest.bind(this, from, to), getJourneyIds, getAllJourneyTimes, getTravelTimeForJourneys.bind(this, to)], function(err, results) {
var sortedResults = results.sort(function(a, b) {
return a.arrivalMinutes - b.arrivalMinutes;
});
reply(results);
});
};
var getBaseURL = function() {
var date = new Date();
var apiKey = 'Q9CASFDTCUS6D13FHGA5HSZFI';
var dateString = date.getFullYear().toString() + '0' + (date.getMonth() + 1).toString() + date.getDate().toString() + date.getHours().toString();
apiKey += dateString;
var hash = Crypto.createHash('md5').update(apiKey).digest('hex');
var url = BASE_URL + hash + '&';
return url;
};
var S = Joi.string;
var N = Joi.number;
var A = Joi.array;
// Create a server with a host and port
var server = Hapi.createServer(config.hostname, config.port, {cors: true});
// Add the route
server.route({
method: 'GET',
path: '/journeys',
config: {
handler: routes.getJourneys
}
});
server.start(function() {
var uri = "";
if(process.env.NODE_ENV === 'production') {
uri = 'https://' + process.env.HOSTNAME + ':' + process.env.PORT;
} else {
uri = 'http://' + config.hostname + ':' + config.port;
}
console.log('Server started at ' + uri);
});