-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskyscanner.ts
181 lines (156 loc) · 6.1 KB
/
skyscanner.ts
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
174
175
176
177
178
179
180
181
import { FormData } from 'then-request';
import request from 'then-request';
import util from 'util';
import { Config } from './config';
export class SkyScanner {
private static _instance: SkyScanner;
public static get Instance(): SkyScanner {
if (!this._instance) {
this._instance = new SkyScanner();
this._instance.setApiKey(Config.getSkyscannerApiKey());
}
return this._instance;
}
private apiKey: string = '';
setApiKey(key: string): void {
this.apiKey = key;
}
getLocations(searchLocation: string): Promise<{ id: string, name: string }[]> {
var url = util.format(
'http://partners.api.skyscanner.net/apiservices/autosuggest/v1.0/TR/TRY/tr-TR/?query=%s&apiKey=%s',
encodeURIComponent(searchLocation),
this.apiKey
);
var url = util.format(
'http://partners.api.skyscanner.net/apiservices/autosuggest/v1.0/TR/TRY/tr-TR/?query=%s&apiKey=%s',
encodeURIComponent(searchLocation),
this.apiKey);
return request('GET', url).then(response => {
var data = JSON.parse(response.getBody().toString());
return data.Places.map((loc: any) => {
return {
id: loc.PlaceId,
name: loc.PlaceName
};
});
})
.catch(err => []);
}
searchCache(fromLocation: string, toLocation: string, fromDate: string, toDate: string) {
var url = util.format(
'http://partners.api.skyscanner.net/apiservices/browsequotes/v1.0/TR/TRY/tr-TR/%s/%s/%s/%s?apiKey=%s',
encodeURIComponent(fromLocation),
encodeURIComponent(toLocation),
encodeURIComponent(fromDate),
encodeURIComponent(toDate),
this.apiKey);
return request('GET', url).then(response => {
return JSON.parse(response.getBody().toString());
});
}
searchLive(fromLocation: string, toLocation: string, fromDate: string, toDate: string, adults: number, children: number, infants: number, fastMode: boolean) {
var apiKey = this.apiKey;
var delay = 1000;
var pull = this.pull;
var form = new FormData({});
form.append('cabinclass', 'Economy');
form.append('country', 'TR');
form.append('currency', 'TRY');
form.append('locale', 'tr-TR');
form.append('locationSchema', 'iata');
form.append('originplace', fromLocation);
form.append('destinationplace', toLocation);
form.append('outbounddate', fromDate);
form.append('inbounddate', toDate);
form.append('adults', adults || 1);
form.append('children', children || 0);
form.append('infants', infants || 0);
form.append('', toDate);
form.append('apikey', apiKey);
form.append('GroupPricing', 'false');
var options = {
form: form
};
return request('POST', 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0', options).then(session => {
return pull(session.url, pull, delay, fastMode).then(response => {
var data = JSON.parse(response.getBody());
var toReturn = data.Itineraries.map((itin: any) => {
var outboundLeg = data.Legs.filter((leg: any) => leg.Id === itin.OutboundLegId)[0];
var inboundLeg = data.Legs.filter((leg: any) => leg.Id === itin.InboundLegId)[0];
var segments = outboundLeg.SegmentIds.concat(inboundLeg.SegmentIds).map((segmentId: any, index: number) => {
var segment = data.Segments.filter((seg: any) => seg.Id === segmentId)[0];
var departAirport = data.Places.filter((place: any) => place.Id === segment.OriginStation)[0];
var arriveAirport = data.Places.filter((place: any) => place.Id === segment.DestinationStation)[0];
var departCity = !departAirport.ParentId
? departAirport
: data.Places.filter((place: any) => place.Id === departAirport.ParentId)[0];
var arriveCity = !arriveAirport.ParentId
? arriveAirport
: data.Places.filter((place: any) => place.Id === arriveAirport.ParentId)[0];
var carriers = [
...response.data.Carriers.filter((carry: any) => carry.Id === segment.OperatingCarrier),
...response.data.Carriers.filter((carry: any) => carry.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;
});
});
}
private pull(url: string, self: Function, delayAmount: number, fastMode: boolean) {
var pullinner = () => {
var currentRequest = request('GET', url);
return currentRequest.then(
response => {
var data = JSON.parse(response.getBody().toString());
if (fastMode && data.Itineraries.length) {
return currentRequest;
} else if (data.Status === 'UpdatesPending') {
return self(url, self, delayAmount);
} else if (data.Status === 'UpdatesComplete') {
return currentRequest;
} else {
return null;
}
},
error => {
if (error.statusCode === 304) {
return self(url, self, delayAmount);
} else if (error.statusCode === 429) {
return self(url, self, 60000);
} else {
return null;
}
}
);
};
return new Promise(resolve => setTimeout(resolve, delayAmount)).then(pullinner);
}
}