-
Notifications
You must be signed in to change notification settings - Fork 0
/
luca_sky_precioenvivo.js
115 lines (100 loc) · 3.17 KB
/
luca_sky_precioenvivo.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
/*
* main() will be run when you invoke this action
* @param Cloud Functions actions accept a single parameter, which must be a JSON object.
* @return The output of this action, which must be a JSON object.
*/
function main(params) {
var request = require('request');
var ciudaddestino = "CTG";
var ciudadorigen = "BOG";
var fechaida = "2020-03-15";
var fechavuelta = "";
var pasajeros = "1";
var endPoints = {
'buildSearch': 'https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0/',
'getSearchData': 'https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/uk2/v1.0/'
};
var headers = {
'X-RapidAPI-Host': "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com",
'X-RapidAPI-Key': "ccd0998f3amsh10e1fed69fc31afp14386ejsnb4fdf587810c"
};
var formData = {
inboundDate: fechavuelta,
cabinClass: 'economy',
children: '0',
infants: '0',
includeCarriers: 'VH;LA',
excludeCarriers: 'AV;EX',
country: 'CO',
currency: 'COP',
locale: 'es-MX',
originPlace: ciudadorigen + "-sky",
destinationPlace: ciudaddestino + "-sky",
outboundDate: fechaida,
adults: pasajeros
};
var promise = new Promise(function (resolve, reject) {
request.post({
url: endPoints.buildSearch,
headers: headers,
form: formData
}, function (error, response, body) {
if (!error && response.statusCode === 201) {
//RateLimit 400 x Minuto = 429
var loc = response.headers.location;
var locSplit = loc.split('/');
var key = locSplit[locSplit.length - 1];
getPricingWithKey(key, 'price', 'asc', 0, 0, 5);
} else {
reject({
error: error,
response: response,
body: body
});
}
})
function getPricingWithKey(key, sortType, sortOrder, stops, pageIndex, pageSize) {
var url = endPoints.getSearchData;
url += key;
url += '?sortType=' + sortType;
url += '&sortOrder=' + sortOrder;
url += '&stops=' + stops;
url += '&pageIndex=' + pageIndex;
url += '&pageSize=' + pageSize;
request.get({
url: url,
headers: headers,
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
//RateLimit 400 x Minuto = 410
var j = JSON.parse(body);
var dataClaro = stripData(j);
resolve(dataClaro);
} else {
reject({
error: error,
response: response,
body: body
});
}
})
}
});
function stripData(dataClaro){
var filteredData = [];
var result = dataClaro.Itineraries;
for(var i=0; i<result.length; i++){
var tempObj = {
InboundLegId: result[i].InboundLegId,
OutboundLegId: result[i].OutboundLegId,
price: result[i].PricingOptions[0].Price
};
filteredData.push(tempObj)
}
return filteredData
};
return promise.then(function(filteredData){
return filteredData
//console.log(filteredData)
});
}