-
Notifications
You must be signed in to change notification settings - Fork 0
/
tap.js
344 lines (309 loc) · 11.7 KB
/
tap.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env node
// vim:ft=javascript
'use strict';
import cheerio from 'cheerio';
import { Command, Argument, Option } from 'commander';
import log4js from 'log4js';
import fetch from 'node-fetch';
import fetchSync from 'sync-fetch';
import _ from 'lodash';
const logger = log4js.getLogger('tap');
const TAP_MAX_RETRIES = 5;
const TAP_RETRY_MS = 5000;
const TAP_MAX_USER_DISTANCE = 1000000;
const TAP_MAX_PAGE_SIZE = 96;
const TAP_SEARCH_URL = 'https://www.trade-a-plane.com/search?';
const TAP_TYPES = [
'Single Engine Piston',
'Multi Engine Piston',
'Turboprop',
'Jets',
'Gliders | Sailplanes',
'Rotary Wing',
'Piston Helicopters',
'Turbine Helicopters'
];
const NOW = Date.now();
let SHUTDOWN = false;
function parseRange(range) {
let min;
let max;
const rangeSplit = range.split('-');
if (rangeSplit.length === 2) {
min = rangeSplit[0];
max = rangeSplit[1];
} else {
logger.error(`invalid range format for ${range}`);
}
return [min, max];
}
function buildURL(options, cmd, action) {
let url = TAP_SEARCH_URL;
switch (action) {
case 'search':
url += '&s-advanced=yes';
url += '&s-type=aircraft';
url += '&sale_status=For+Sale';
url += `&s-page_size=${TAP_MAX_PAGE_SIZE}`;
switch (options.fractional) {
case 'Any':
break;
case 'None':
url += '&fractional_ownership=1%2F1';
break;
default:
url += `&fractional_ownership=${encodeURIComponent(options.fractional)}`;
break;
}
url += '&user_distance=' + options.distance.toString();
url += `&category_level1=${options.type.replaceAll(' ', '+')}`;
if (options.modelGroup) url += `&model_group=${options.modelGroup.toUpperCase().replaceAll(' ', '+')}`;
if (options.model) url += `&model=${options.model.toUpperCase()}`;
if (options.make) url += `&make=${options.make.toUpperCase()}`;
if (options.year) {
const [min, max] = parseRange(options.year);
if (min && max) {
url += `&year-min=${min}`;
url += `&year-max=${max}`;
}
}
if (options.totalTime) {
const [min, max] = parseRange(options.totalTime);
if (min && max) {
url += `&total_time-min=${min}`;
url += `&total_time-max=${max}`;
}
}
if (options.price) {
const [min, max] = parseRange(options.price);
if (min && max) {
url += `&price-min=${min}`;
url += `&price-max=${max}`;
}
}
if (options.sort) {
url += `&s-sort_key=${options.sort}`;
url += `&s-sort_order=${options.sortOrder}`;
}
break;
case 'category':
url += '&s-type=aircraft;'
url += `&category_level1=${options.type.replaceAll(' ', '+')}`;
url += `&s-lvl=${options.level}`;
break;
case 'listing':
url += `&listing_id=${options.id}`;
break;
default:
throw new Error(`can not build URL, invalid action ${action}`);
break;
}
return url;
}
function sleep(ms) {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
}
function tapFetchSync(url) {
// TaP has severe rate-throttling so everything is synchronous with
// three second back-off.
let resp;
for (let i = 0; i < TAP_MAX_RETRIES; ++i) {
try {
logger.debug(`fetching ${url} retries: ${i} ...`);
resp = fetchSync(url);
if (resp.status === 200)
return resp.text();
} catch (err) {
if (i === TAP_MAX_RETRIES)
logger.error(err);
}
sleep(TAP_RETRY_MS);
}
logger.error(`could not fetch '${url}': ${resp?.status} - ${resp?.statusText}`);
return "";
}
async function shutdown(err) {
logger.debug("shutdown hook called ...");
SHUTDOWN = true;
if (err)
logger.error(err);
process.exit(-1);
}
async function categoryCmd(options, cmd) {
const url = buildURL(options, cmd, 'category');
const text = tapFetchSync(url);
const $ = cheerio.load(text);
const levels = [];
const selector = $('.column > ul > li > a');
$(selector).each((index, element) => {
const level = $(element).attr('title').trim();
if (level !== 'Show All Makes')
console.log(level);
});
}
async function searchCmd(options, cmd) {
let totalPages = 1;
const fetchTexts = [];
const url = buildURL(options, cmd, 'search');
const text = tapFetchSync(url);
fetchTexts.push(text);
// Parse total number of results found first before fetching the rest.
const $ = cheerio.load(text);
const selector = $('#search_results_area > .search_options > h2');
let resultsFound = $(selector).text();
if (resultsFound) {
resultsFound = resultsFound.replace(/\r?\n|\r/g, "");
resultsFound = resultsFound.replace(/\s/g, "");
const resultsRE = /^Showing.*of(.*)results*/;
const match = resultsFound.match(resultsRE);
if (match && match.length > 1) {
let numResults = parseInt(match[1].replace(/,/g, ''), 10)
const number = (options.number) ? parseInt(options.number) : Infinity;
numResults = Math.min(numResults, number);
// Now determine how many URLs to fetch and fetch tte rest
totalPages = (numResults <= TAP_MAX_PAGE_SIZE) ? 1 : Math.round(numResults / TAP_MAX_PAGE_SIZE) + ((numResults % TAP_MAX_PAGE_SIZE) ? 1 : 0);
const fetchURLs = [];
for (let page = 1; page <= totalPages; ++page) {
fetchURLs.push(url + `&s-page=${page}`);
}
logger.debug(`fetching ${numResults} results over ${totalPages} page(s) ...`);
// We run this loop synchronously since TaP enforces some rate-limiting (429)
for (const url of fetchURLs.slice(1, fetchURLs.length)) {
if (SHUTDOWN)
return;
fetchTexts.push(tapFetchSync(url));
}
logger.debug(`scraped ${totalPages} pages ...`);
let resultsProcessed = 0;
for (const body of fetchTexts) {
if (SHUTDOWN)
return;
const $ = cheerio.load(body);
// Parse the entire result listing
const selector = '.result_listing';
$(selector).each((index, element) => {
const ad = {};
ad['id'] = $(element).attr('data-listing_id');
const dataModelGroup = $(element).attr('data-model_group');
ad['model_group'] = dataModelGroup.trim();
const dataSellerID = $(element).attr('data-seller_id');
ad['seller_id'] = dataSellerID;
const titleLink = $(element).find('.lst-title > h3 > a');
ad['title'] = $(titleLink).text().trim();
const titleHref = $(titleLink).attr('href');
const titleHrefNoSearch = titleHref.replace('/search?', '');
const titleHrefSplit = titleHrefNoSearch.split('&');
_.forEach(titleHrefSplit, (nvp) => {
const nvpSplit = nvp.split('=');
if (nvpSplit.length === 2) {
const [key, value] = nvpSplit;
if (key === 'make' || key === 'model') ad[key] = value.replaceAll('+', ' ');
if (key === 's-type') ad['type'] = value.replaceAll('+', ' ');
if (key === 'category_level1') ad['category'] = value.replaceAll('+', ' ');
}
});
const titleSplit = ad['title'].split(' ');
ad['year'] = isNaN(titleSplit[0]) ? 'Not Listed' : titleSplit[0];
const priceArea = $(element).find('.txt-price');
ad['price'] = $(priceArea).text().trim();
const regArea = $(element).find('.txt-reg-num');
ad['registration'] = $(regArea).text().trim().replace(/Reg#\s*/g, '');
const ttArea = $(element).find('.txt-total-time');
ad['total_time'] = $(ttArea).text().trim().replace(/TT:\s*/g, '');
const addressArea = $(element).find('.address');
ad['address'] = $(addressArea).text().trim();
ad['last_updated'] = $(element).find('.last-update').text().trim().replace(/Last Update:\s*/, '');
ad['fetch_date'] = NOW;
if (options.deep) {
options.id = ad['id'];
const url = buildURL(options, cmd, 'listing');
if (SHUTDOWN)
return;
const text = tapFetchSync(url);
const $ = cheerio.load(text);
let selector = $('#bottom_section > #general_specs > p');
ad['specs'] = $(selector).text().trim();
selector = $('#detailed_desc > pre');
ad['description'] = $(selector).text().trim();
selector = $('#avionics_equipment > pre');
ad['avionics'] = $(selector).text().trim();
selector = $('#airframe > pre');
ad['airframe'] = $(selector).text().trim();
selector = $('#engines_mods > pre');
ad['engine'] = $(selector).text().trim();
selector = $('#interior_exterior > pre');
ad['int_ext'] = $(selector).text().trim();
selector = $('#remarks > pre');
ad['remarks'] = $(selector).text().trim();
}
process.stdout.write(JSON.stringify(ad));
process.stdout.write('\n');
resultsProcessed += 1;
if (resultsProcessed == numResults)
return false;
});
}
}
}
}
const tap = new Command();
tap
.name('tap')
.description('Trade-a-Plane')
.version('0.1.0')
.addOption(new Option('-d, --debug', 'debug output'))
.hook('preSubcommand', async (thisCommand, subCommand) => {
try {
logger.level = (thisCommand.opts().debug) ? 'debug' : 'info';
logger.debug(`${thisCommand._name} preSubcommand ... `);
logger.debug('command options: ' + JSON.stringify(thisCommand.opts()));
} catch (err) {
logger.error(err);
process.exit(-1);
}
});
const search = tap.command('search').alias('s').description('Search Trade-a-Plane');
search
.addOption(new Option('-t, --type <type>', 'category type').choices(TAP_TYPES).default(TAP_TYPES[0]))
.addOption(new Option('-f, --fractional <percentage>', 'fractional ownership percentage').choices(['None', 'Any', '1/2', '1/3', '1/4', '1/6', '1/7', '1/8', '1/16' ]).default('None'))
.addOption(new Option('-d, --distance <distance>', 'user distance').default(TAP_MAX_USER_DISTANCE))
.addOption(new Option('-m, --make <make>', 'make'))
.addOption(new Option('-o, --model <model>', 'model'))
.addOption(new Option('-g, --model-group <group>', 'model group'))
.addOption(new Option('-y, --year <year>', 'year range'))
.addOption(new Option('-p, --price <price>', 'price range'))
.addOption(new Option('-l, --total-time <time>', 'total time range'))
.addOption(new Option('--sort <sort>', 'sort key').choices(['days_since_update', 'price', 'make', 'model', 'year', 'overhaul1_time', 'total_time']))
.addOption(new Option('--sort-order <order>', 'sort order').choices(['asc', 'desc']).default('asc'))
.addOption(new Option('-n, --number <number>', 'number of results'))
.addOption(new Option('--deep', 'deep query mode'))
.action(async (options, cmd) => {
try {
logger.debug('search subcommand ... ');
logger.debug('command options: ' + JSON.stringify(options));
await searchCmd(options, cmd);
} catch (err) {
shutdown(err);
}
});
const category = tap.command('category').alias('c').description('List Categories');
category
.addOption(new Option('-t, --type <type>', 'category level').choices(TAP_TYPES).default(TAP_TYPES[0]))
.addOption(new Option('-l, --level <level>').choices(['1', '2']).default('1'))
.action(async (options, cmd) => {
try {
logger.debug('category subcommand ... ');
logger.debug('command options: ' + JSON.stringify(options));
await categoryCmd(options, cmd);
} catch (err) {
shutdown(err);
}
});
function main() {
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
tap.parse();
}
if (import.meta.url.startsWith(`file://${process.argv[1]}`)) {
main();
}