-
Notifications
You must be signed in to change notification settings - Fork 4
/
routes.js
63 lines (52 loc) · 1.7 KB
/
routes.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
var routes = function(app) {
app.get("/max-bookable-date", function(req, res) {
var request = require("request");
console.log("Received GET: " + JSON.stringify(req.query));
if (!req.query.dest) {
return res.send({
"status": "error",
"message": "missing 'dest' parameter"
});
} else if (!req.query.origin) {
return res.send({
"status": "error",
"message": "missing origin' parameter"
});
}
var d = new Date();
var todayStr = d.getFullYear() + "-" + ("0" + (d.getMonth() + 1)).slice(-2) + "-" + ("0" + d.getDate()).slice(-2);
var options = {
method: 'GET',
url: 'https://www.easyjet.com/ejcms/nocache/api/lowestfares/get/',
qs: {
destinationIata: req.query.dest,
displayCurrencyId: '0',
languageCode: 'en-US',
originIata: req.query.origin,
startDate: todayStr
},
headers: {
'cache-control': 'no-cache',
accept: 'application/json, text/plain, */*',
'accept-encoding': 'gzip, deflate, sdch, br'
}
};
request(options, function(error, response, body) {
if (error) throw new Error(error);
var json = JSON.parse(body);
var farthestDate = new Date();
for (var i = 0; i < json.months.length; i++) {
var month = json.months[i];
for (var j = 0; j < month.days.length; j++) {
var dayObj = month.days[j];
var date = new Date(json.months[i].year, json.months[i].month - 1, dayObj.day);
if (dayObj.flightStatus > 0 && date > farthestDate) {
farthestDate = date;
}
}
}
res.send(farthestDate);
});
});
};
module.exports = routes;