ADSB backfill fix?

This commit is contained in:
2026-06-27 16:58:46 -04:00
parent d9fbe0568d
commit 6bc4d920f8
4 changed files with 327 additions and 24 deletions

View File

@@ -5,6 +5,7 @@ import * as fs from 'node:fs';
import Database from 'better-sqlite3';
import { fromCsv } from '@ztimson/utils';
import {getIcon} from './adsb-shapes.mjs';
import * as cheerio from 'cheerio';
const DIR = dirname(fileURLToPath(import.meta.url));
const DATA = resolve(DIR, '../data');
@@ -171,27 +172,6 @@ export function classifyAircraft(row) {
return 'unknown';
}
async function scrapeHexDatabase(icao) {
const url = `https://hexdatabase.com/h/${icao}`;
try {
const resp = await fetch(url);
if (!resp.ok) return null;
const html = await resp.text();
const rowMatch = html.match(new RegExp(`<tr[^>]*>.*?<span[^>]*>.*?${icao}.*?<\/span>.*?<\/tr>`, 's'));
if (!rowMatch) return null;
const cells = rowMatch.match(/<td[^>]*>.*?<\/td>/g) || [];
return {
country: cells[1]?.replace(/<[^>]*>/g, '').trim() || null,
registration: cells[2]?.replace(/<[^>]*>/g, '').trim() || null,
aircraft: cells[3]?.replace(/<[^>]*>/g, '').trim() || null,
operator: cells[4]?.replace(/<[^>]*>/g, '').trim() || null,
serialNumber: cells[6]?.replace(/<[^>]*>/g, '').trim() || null,
};
} catch (e) {
return null;
}
}
async function fetchHexDb(icao) {
const resp = await fetchWithTimeout(`https://hexdb.io/api/v1/aircraft/${icao}`);
if (!resp.ok) return null;
@@ -205,6 +185,25 @@ async function fetchHexDb(icao) {
};
}
async function scrapeHexDatabase(icao) {
const url = `https://hexdatabase.com/h/${icao}`;
const resp = await fetch(url);
if (!resp.ok) return null;
const $ = cheerio.load(await resp.text());
const row = $('tr').filter((_, el) => $(el).text().toLowerCase().includes(icao.toLowerCase())).first();
if (!row.length) return null;
const cells = row.find('td');
return {
country: $(cells[1]).text().trim() || null,
registration: $(cells[2]).text().trim() || null,
aircraft: $(cells[3]).text().trim() || null,
operator: $(cells[4]).text().trim() || null,
serialNumber: $(cells[6]).text().trim() || null,
};
}
function backfillFromSimilar(aircraft) {
if (!aircraft) return {};
const similar = db.prepare(`