Add src/server/api_flight.ts
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
5a45dcb1b9
commit
ca9edace2f
86
src/server/api_flight.ts
Normal file
86
src/server/api_flight.ts
Normal file
@ -0,0 +1,86 @@
|
||||
import { JSDOM } from 'jsdom';
|
||||
|
||||
interface FlightData {
|
||||
id: string;
|
||||
date: string;
|
||||
from: string;
|
||||
to: string;
|
||||
std: string;
|
||||
atd: string | string[] | null;
|
||||
sta: string;
|
||||
ata: string | string[] | null;
|
||||
}
|
||||
|
||||
function clean_times(s: string): string | null {
|
||||
if (s == "—" || s == "Scheduled") return null
|
||||
if (s.indexOf("Estimated departure") == 0) return null
|
||||
if (s.indexOf("Landed") == 0) return s.replace("Landed ", "")
|
||||
return s
|
||||
}
|
||||
|
||||
export function flight_get_data(flightId: string): Promise<FlightData[]> {
|
||||
const url = new URL(`https://www.flightradar24.com/data/flights/${flightId}`);
|
||||
return fetch(url).then(res => res.text()).then(res => new JSDOM(res)).then(dom => {
|
||||
const rows = dom.window.document.querySelectorAll('table tbody tr');
|
||||
const flightData: FlightData[] = [];
|
||||
rows.forEach(row => {
|
||||
const columns = row.querySelectorAll('td');
|
||||
/*
|
||||
* 2/6 = Date/Duration
|
||||
* 3/4 = From/To
|
||||
* 7/8 = STD/ATD
|
||||
* 9/11 = STA/ATA
|
||||
*/
|
||||
const flight: FlightData = {
|
||||
id: flightId,
|
||||
date: columns[2].textContent.trim(),
|
||||
from: columns[3].textContent.trim(),
|
||||
to: columns[4].textContent.trim(),
|
||||
std: columns[7].textContent.trim(),
|
||||
atd: clean_times(columns[8].textContent.trim()),
|
||||
sta: columns[9].textContent.trim(),
|
||||
ata: clean_times(columns[11].textContent.trim())
|
||||
};
|
||||
flightData.push(flight);
|
||||
});
|
||||
|
||||
return groupByPair(flightData).map(v => groupByFrequency(v));
|
||||
})
|
||||
}
|
||||
|
||||
function groupByPair(flightData: FlightData[]): FlightData[][] {
|
||||
const flightMap: { [key: string]: FlightData[] } = {};
|
||||
flightData.forEach(flight => {
|
||||
const key = `${flight.from}-${flight.to}`;
|
||||
if (!flightMap[key])
|
||||
flightMap[key] = [];
|
||||
flightMap[key].push(flight);
|
||||
});
|
||||
return Object.values(flightMap);
|
||||
}
|
||||
|
||||
function groupByFrequency(flightData: FlightData[]): FlightData {
|
||||
let data = 'OOOOOOO'; // Initialize with no flights
|
||||
const atdArray: string[] = [];
|
||||
const ataArray: string[] = [];
|
||||
|
||||
flightData.forEach(flight => {
|
||||
const dayOfWeek = (new Date(flight.date).getDay() + 6) % 7;
|
||||
data = data.substring(0, dayOfWeek) + 'X' + data.substring(dayOfWeek + 1);
|
||||
if (flight.atd)
|
||||
atdArray.push(flight.atd as string);
|
||||
if (flight.ata)
|
||||
ataArray.push(flight.ata as string);
|
||||
});
|
||||
|
||||
return {
|
||||
id: flightData[0].id,
|
||||
date: data,
|
||||
from: flightData[0].from,
|
||||
to: flightData[0].to,
|
||||
std: flightData[0].std,
|
||||
sta: flightData[0].sta,
|
||||
atd: atdArray,
|
||||
ata: ataArray
|
||||
};
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user