-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.js
55 lines (47 loc) · 1.41 KB
/
data.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
const puppeteer = require('puppeteer');
const fs = require('fs');
console.log('Generating airline data...');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://en.wikipedia.org/wiki/List_of_airline_codes');
try {
const airlines = await page.evaluate(() => {
const trs = document.querySelectorAll('table tbody tr');
const airlines = [];
const idCache = [];
trs.forEach(tr => {
const tds = tr.querySelectorAll('td');
if (!tds[0]) return;
const iata = tds[0].textContent.trim();
if (!iata || !tds[2]) return;
const airline = tds[2].textContent.trim();
const id = `${iata}_${airline.toLowerCase()}`;
if (idCache.indexOf(id) !== -1) {
return;
}
idCache.push(id);
airlines.push({ id, iata, airline });
});
return airlines;
});
if (airlines.length === 0) {
console.log('No airline data to generate.');
await browser.close();
return;
}
fs.writeFile(
'functions/airlines.json',
JSON.stringify(airlines),
'utf8',
async () => {
console.log(`Generated ${airlines.length} airline data (functions)!`);
await browser.close();
}
);
} catch (error) {
console.error('Error generating airline data', error);
await browser.close();
return;
}
})();