-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
47 lines (44 loc) · 1.32 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
var _ = require('lodash');
var xray = require('x-ray');
var Promise = require('bluebird');
var flights = {
kef: function(query, cb) {
var langs = ['en','is'];
if (langs.indexOf(query.lang) < 0) {
query.lang = langs[0];
}
var url = 'http://www.kefairport.is/';
url += query.lang === 'is' ? '/Flugaaetlun' : '/English/Timetables';
if (query.type === 'departures') {
url += query.lang === 'is' ? '/Brottfarir/' : '/Departures/';
} else {
url += query.lang === 'is' ? '/Komur/' : '/Arrivals/';
}
xray(url)
.select([{
$root: 'table tr',
date: 'td:nth-child(1)',
flightNumber: 'td:nth-child(2)',
airline: 'td:nth-child(3)',
to: 'td:nth-child(4)',
from: 'td:nth-child(4)',
scheduledTime: 'td:nth-child(5)',
estimatedTime: 'td:nth-child(6)',
status: 'td:nth-child(7)'
}])
.run(function(err, array) {
var cleaned = _.map(array, function(flights) {
if (query.type === 'departures') {
delete flights.from;
} else {
delete flights.to;
}
return flights;
}).filter(function(item){
return Object.keys(item).length > 0;
});
return cb(err, cleaned);
});
}
};
module.exports = Promise.promisifyAll(flights);