/
index.js
365 lines (348 loc) · 11.7 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
var Promise = require('bluebird');
var rp = require('request-promise');
var _ = require('underscore');
var parseString = Promise.promisify(require('xml2js').parseString);
var cheerio = require('cheerio');
var random_useragent = require('random-useragent');
var Tram = function(options) {
this.options = _.extendOwn({
userAgent: random_useragent.getRandom()
}, options);
// For now this is not handled
this.options.lang = 'en';
};
Tram.WEST_DIRECTION = 'west';
Tram.EAST_DIRECTION = 'east';
Tram.BOTH_DIRECTIONS = 'east & west';
Tram.TERMINUS = 'terminus';
/**
* Downloads Tram Stops from Server
*/
Tram.prototype.getTramStops = function() {
// if (typeof withLatLong !== 'boolean') {
// withLatLong = true;
// }
var options;
//if (withLatLong) {
options = {
method: 'GET',
baseUrl: 'http://hktramways.com/js',
uri: '/googleMap.js',
headers: {
'User-Agent': this.options.userAgent
},
};
return rp(options)
.then(function(jsCode) {
// Here we just define some objects to be able to get the .js file data
var evalTramStops = eval.call({},"'use strict'; var window; var google = {maps: {InfoWindow: function() {},event: {addDomListener: function() {}}}};" + jsCode + '; var stops = stopsArrayEB; stops');
var tramStops = [];
_.each(evalTramStops, function(tramStop) {
var stopData = {
stop_code: tramStop[0],
en_name: tramStop[1],
tc_name: tramStop[2],
sc_name: tramStop[3],
latitude: tramStop[4],
longitude: tramStop[5],
};
var stopDirection = stopData.stop_code.substr(stopData.stop_code.length-1,stopData.stop_code.length);
if (stopDirection === 'W') {
stopData.direction = Tram.WEST_DIRECTION;
} else if (stopDirection === 'E') {
stopData.direction = Tram.EAST_DIRECTION;
} else if (stopDirection === 'T') {
stopData.direction = Tram.TERMINUS;
// Need special handling for kennedy town
} else if (stopDirection === 'B') {
stopData.direction = Tram.EAST_DIRECTION;
if (stopData.stop_code === 'HVT_B') {
tramStops.push({
stop_code: 'HVT_K',
en_name: stopData.en_name,
tc_name: stopData.tc_name,
sc_name: stopData.sc_name,
latitude: stopData.latitude,
longitude: stopData.longitude,
direction: Tram.WEST_DIRECTION
});
}
} else if (!isNaN(parseInt(stopDirection))) {
stopData.direction = Tram.BOTH_DIRECTIONS;
}
tramStops.push(stopData);
});
return tramStops;
});
// } else {
// options = {
// method: 'GET',
// baseUrl: 'http://hktramways.com/js',
// uri: '/nextTramData.js'
// };
//
// return rp(options)
// .then(function(jsCode) {
// var evalTramStops = eval.call({},"'use strict';" + jsCode + '; var stops = {east: nextTramStopsE, west: nextTramStopsW}; stops');
// var tramStops = [];
// if (!evalTramStops.hasOwnProperty("west") || !evalTramStops.hasOwnProperty("east")) {
// throw Error("Invalid Server Data Error");
// }
// _.each(evalTramStops.east, function(tramStop) {
// tramStops.push({
// stop_code: tramStop[0],
// en_name: tramStop[1],
// tc_name: tramStop[2],
// sc_name: tramStop[3],
// direction: Tram.EAST_DIRECTION
// });
// });
// _.each(evalTramStops.west, function(tramStop) {
// tramStops.push({
// stop_code: tramStop[0],
// en_name: tramStop[1],
// tc_name: tramStop[2],
// sc_name: tramStop[3],
// direction: Tram.WEST_DIRECTION
// });
// });
// return tramStops;
// });
// }
};
Tram.prototype.getNextTramETA = function(stopCode) {
var options = {
method: 'GET',
baseUrl: 'http://hktramways.com',
uri: '/nextTram/geteat.php?stop_code=' + stopCode,
headers: {
'User-Agent': this.options.userAgent
},
};
return rp(options)
.then(function(xml) {
return parseString(xml);
}).then(function(result) {
return _.map(result.root.metadata, function(item){
var newItem = item.$;
newItem.eta = newItem.eat;
newItem.is_arrived = newItem.is_arrived === 1 ? true : false;
newItem.is_last_tram = newItem.is_last_tram === 1 ? true : false;
delete newItem.eat;
return newItem;
});
});
};
Tram.prototype.getEmergencyMessageForTramStop = function(stopCode) {
var options = {
method: 'GET',
baseUrl: 'http://hktramways.com',
uri: '/nextTram/getmessage.php?stop_code=' + stopCode,
headers: {
'User-Agent': this.options.userAgent
},
};
return rp(options)
.then(function(xml) {
return parseString(xml);
}).then(function(result) {
if (result.root === '') {
return [];
} else if (result.root.hasOwnProperty("metadata")) {
return _.map(result.root.metadata, function(item){
var newItem = item.$;
// { stop_code: 'WST',
// special_msg_id: '574',
// msg_tc: '緊急事故,服務暫停',
// msg_en: 'Due to emergency, service suspended',
// start_dt: 'Jun 20 2016 8:49PM',
// end_dt: '',
// update_dt: 'Jun 20 2016 8:49PM' }
newItem.start_date = newItem.start_dt.length === 0 ? null : newItem.start_dt;
newItem.end_date = newItem.end_dt.length === 0 ? null : newItem.end_dt;
newItem.update_date = newItem.update_dt.length === 0 ? null : newItem.update_dt;
delete newItem.start_dt;
delete newItem.end_dt;
delete newItem.update_dt;
return newItem;
});
}
});
};
Tram.prototype.getServiceUpdates = function() {
var options = {
method: 'GET',
baseUrl: 'http://hktramways.com',
uri: '/' + this.options.lang + '/service-updates/',
headers: {
'User-Agent': this.options.userAgent
},
};
return rp(options)
.then(function(html) {
return cheerio.load(html);
}).then(function($) {
var items = [];
//console.log($('div#serviceUpdate table tr').children());
$('div#serviceUpdate table').children('tr').each(function() {
var item = {};
item.id = $(this).attr('data-id');
$(this).children('td').each(function(i, elem) {
var td = $(this);
if (td.hasClass('date')) {
item.date = td.text().trim();
} else if (td.hasClass('subject')) {
item.subject = td.text().trim();
} else if (td.attr('data-id')) {
item.id = td.attr('data-id').trim();
}
});
items.push(item);
});
return items;
}).then(function(items) {
return Promise.map(items, function(item) {
var options = {
method: 'GET',
baseUrl: 'http://hktramways.com',
uri: '/' + this.options.lang + '/service-updates-detail/' + item.id + '/1'
};
return rp(options)
.then(function(html) {
return cheerio.load(html, {
normalizeWhitespace: true,
xmlMode: true
});
}).then(function($) {
item.details_html = $('div#serviceUpdate dl dd div.desc').html().trim();
// Promise.map awaits for returned promises as well.
return item;
});
});
});
};
Tram.prototype.getFares = function() {
var options = {
method: 'GET',
baseUrl: 'http://hktramways.com',
uri: '/' + this.options.lang + '/schedules-fares/',
headers: {
'User-Agent': this.options.userAgent
},
};
return rp(options)
.then(function(html) {
return cheerio.load(html, {
normalizeWhitespace: true,
xmlMode: true
});
}).then(function($) {
var fares = [];
$('div.fares table').children('tr').each(function() {
var fare = {};
$(this).children('td').each(function(i) {
switch (i) {
case 0:
fare.type = $(this).text().trim();
if (fare.type.substring(fare.type.length -1, fare.type.length) === '*')
fare.type = fare.type.slice(0, fare.type.length - 1);
break;
case 1:
fare.price = parseFloat($(this).text().trim().slice(1));
break;
}
});
fares.push(fare);
});
return fares;
});
};
Tram.prototype.getSchedules = function() {
var options = {
method: 'GET',
baseUrl: 'http://hktramways.com',
uri: '/' + this.options.lang + '/schedules-fares/',
headers: {
'User-Agent': this.options.userAgent
},
};
return rp(options)
.then(function(html) {
return cheerio.load(html, {
normalizeWhitespace: true,
xmlMode: true
});
}).then(function($) {
var schedules = [];
$('div.tab2 table tbody').children('tr').each(function() {
//console.log($(this).text());
var schedule = {};
$(this).children('td').each(function(i) {
//console.log($(this).text());
switch (i) {
case 0:
schedule = {
start: $(this).children('span').eq(0).text().trim(),
end: $(this).children('span').eq(1).text().trim(),
direction: Tram.WEST_DIRECTION,
timetable: {
mon_fri: {},
sat: {},
sun_ph: {}
}
};
break;
case 2:
schedule.timetable.mon_fri.first = $(this).find('p').eq(0).text().trim();
schedule.timetable.mon_fri.last = $(this).find('p').eq(1).text().trim();
break;
case 3:
schedule.timetable.sat.first = $(this).find('p').eq(0).text().trim();
schedule.timetable.sat.last = $(this).find('p').eq(1).text().trim();
break;
case 4:
schedule.timetable.sun_ph.first = $(this).find('p').eq(0).text().trim();
schedule.timetable.sun_ph.last = $(this).find('p').eq(1).text().trim();
break;
}
});
schedules.push(schedule);
});
$('div.tab3 table tbody').children('tr').each(function() {
//console.log($(this).text());
var schedule = {};
$(this).children('td').each(function(i) {
//console.log($(this).text());
switch (i) {
case 0:
schedule = {
start: $(this).children('span').eq(0).text().trim(),
end: $(this).children('span').eq(1).text().trim(),
direction: Tram.EAST_DIRECTION,
timetable: {
mon_fri: {},
sat: {},
sun_ph: {}
}
};
break;
case 2:
schedule.timetable.mon_fri.first = $(this).find('p').eq(0).text().trim();
schedule.timetable.mon_fri.last = $(this).find('p').eq(1).text().trim();
break;
case 3:
schedule.timetable.sat.first = $(this).find('p').eq(0).text().trim();
schedule.timetable.sat.last = $(this).find('p').eq(1).text().trim();
break;
case 4:
schedule.timetable.sun_ph.first = $(this).find('p').eq(0).text().trim();
schedule.timetable.sun_ph.last = $(this).find('p').eq(1).text().trim();
break;
}
});
schedules.push(schedule);
});
return schedules;
});
};
module.exports = Tram;