-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathskyscanner.js
173 lines (144 loc) · 6.88 KB
/
skyscanner.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
var promise = require('bluebird');
var request = require('request-promise');
var util = require('util');
var _ = require('lodash');
module.exports = {
setApiKey: function (apiKey) {
this.apiKey = apiKey;
},
getLocation: function (searchLocation) {
var url = util.format(
'http://partners.api.skyscanner.net/apiservices/autosuggest/v1.0/HK/HKD/en-US/?query=%s&apiKey=%s',
encodeURIComponent(searchLocation),
this.apiKey);
return request(url).then(function (body) {
var data = JSON.parse(body);
return data.Places.map(function (loc) {
return { id: loc.PlaceId, name: loc.PlaceName };
});
});
},
searchCache: function (fromLocation, toLocation, fromDate, toDate) {
var url = util.format(
'http://partners.api.skyscanner.net/apiservices/browsequotes/v1.0/HK/HKD/en-US/%s/%s/%s/%s?apiKey=%s',
encodeURIComponent(fromLocation),
encodeURIComponent(toLocation),
encodeURIComponent(fromDate),
encodeURIComponent(toDate),
this.apiKey);
return request(url).then(function (body) {
var data = JSON.parse(body);
var toReturn = data.Quotes.map(function (quote) {
var segments = [quote.OutboundLeg, quote.InboundLeg].map(function (segment, index) {
var departPlace = _.filter(data.Places, { PlaceId: segment.OriginId })[0];
var arrivePlace = _.filter(data.Places, { PlaceId: segment.DestinationId })[0];
var carriers = segment.CarrierIds.map(c => _.filter(data.Carriers, { CarrierId: c })[0].Name);
return {
group: index + 1,
departAirport: { code: departPlace.IataCode, name: departPlace.Name },
arriveAirport: { code: arrivePlace.IataCode, name: arrivePlace.Name },
departCity: { code: departPlace.CityId, name: departPlace.CityName },
arriveCity: { code: arrivePlace.CityId, name: arrivePlace.CityName },
departTime: segment.DepartureDate,
carriers: carriers
};
});
return {
segments: segments,
price: quote.MinPrice,
}
});
return toReturn;
});
},
search: function (fromLocation, toLocation, fromDate, toDate, adults, children, infants, fastMode) {
var apiKey = this.apiKey;
var delay = 1000;
var pull = this.pull;
var options = {
method: 'POST',
uri: 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0',
form: {
cabinclass: 'Economy',
country: 'HK',
currency: 'HKD',
locale: 'en-US',
locationSchema: 'iata',
originplace: fromLocation,
destinationplace: toLocation,
outbounddate: fromDate,
inbounddate: toDate,
adults: adults || 1,
children: children || 0,
infants: infants || 0,
apikey: apiKey,
GroupPricing: false
},
transform: function (body, response) {
return {
url: util.format('%s?apiKey=%s&pageIndex=0&pageSize=10', response.headers['location'], apiKey)
};
},
json: false
};
return request(options).then(function (session) {
return pull(session.url, pull, delay, fastMode).then(function (body) {
var data = JSON.parse(body);
var toReturn = data.Itineraries.map(function (itin) {
var outboundLeg = _.filter(data.Legs, { Id: itin.OutboundLegId })[0];
var inboundLeg = _.filter(data.Legs, { Id: itin.InboundLegId })[0];
var segments = outboundLeg.SegmentIds.concat(inboundLeg.SegmentIds).map(function (segmentId, index) {
var segment = _.filter(data.Segments, { Id: segmentId })[0];
var departAirport = _.filter(data.Places, { Id: segment.OriginStation })[0];
var arriveAirport = _.filter(data.Places, { Id: segment.DestinationStation })[0];
var departCity = !departAirport.ParentId ? departAirport : _.filter(data.Places, { Id: departAirport.ParentId })[0];
var arriveCity = !arriveAirport.ParentId ? arriveAirport : _.filter(data.Places, { Id: arriveAirport.ParentId })[0];
var carriers = _.union(_.filter(data.Carriers, { Id: segment.OperatingCarrier }), _.filter(data.Carriers, { Id: segment.Carrier }));
return {
group: index < outboundLeg.SegmentIds.length ? 1 : 2,
departAirport: { code: departAirport.Code, name: departAirport.Name },
arriveAirport: { code: arriveAirport.Code, name: arriveAirport.Name },
departCity: { code: departCity.Code, name: departCity.Name },
arriveCity: { code: arriveCity.Code, name: arriveCity.Name },
departTime: segment.DepartureDateTime,
arriveTime: segment.ArrivalDateTime,
carrier: carriers.map(c => c.Name)
};
});
return {
segments: segments,
price: itin.PricingOptions[0].Price,
url: itin.PricingOptions[0].DeeplinkUrl
};
});
return toReturn;
});
});
},
pull: function (url, self, delay, fastMode) {
var pullinner = function () {
var currentRequest = request(url);
return currentRequest.then(function (body) {
var data = JSON.parse(body);
if (fastMode && data.Itineraries.length) {
return currentRequest;
} else if (data.Status === "UpdatesPending") {
return self(url, self, delay);
} else if (data.Status === "UpdatesComplete") {
return currentRequest;
} else {
return null;
}
}, function (error) {
if (error.statusCode === 304) {
return self(url, self, delay);
} else if (error.statusCode === 429) {
return self(url, self, 60000);
} else {
return null;
}
});
};
return promise.delay(delay).then(pullinner);
},
};