forked from danielsmith-eu/ita-matrix-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseResults.js
57 lines (56 loc) · 2.44 KB
/
parseResults.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
'use strict';
var winston = require('winston');
// parse results from ITA Matrix sparse results format
// try/catch at every level because there isn't always content
// and we don't need to error out completely in that case.
module.exports = function(bodyStr) {
var body = JSON.parse(bodyStr);
var results = [];
var allResults = body.result[7][1];
allResults.forEach(function (month) {
try {
// this is each month (i.e. each physical grid), usually 1 or 2
var rows = month[1];
rows.forEach(function (row) {
try {
// each row in the grid
var days = row[1];
days.forEach(function (day) {
try {
var result = day[3];
if (result) {
var flights = result[1];
flights.forEach(function (flight) {
try {
// cheapest flight per day, one
// per duration, e.g. 12, 13, 14 days length
results.push({
price: flight[2],
priceInt: parseInt(flight[2].substr(3)),
durationDays: flight[4],
outDate: flight[1][3][0][1],
inDate: flight[1][3][1][1],
});
} catch (e) {
winston.debug("Results parsing 1: " + e + ", " + e.stack);
// do nothing
}
});
}
} catch (e) {
winston.debug("Results parsing 2: " + e + ", " + e.stack);
// no nothing
}
});
} catch (e) {
winston.debug("Results parsing 3: " + e + ", " + e.stack);
// do nothing
}
});
} catch (e) {
winston.debug("Results parsing 4: " + e + ", " + e.stack);
// do nothing
}
});
return {results: results};
};