Skip to content

Instantly share code, notes, and snippets.

@boehs
Created October 30, 2023 15:56
Show Gist options
  • Save boehs/58ac62c47a55a27307fc9f45c63261c6 to your computer and use it in GitHub Desktop.
Save boehs/58ac62c47a55a27307fc9f45c63261c6 to your computer and use it in GitHub Desktop.
FlightRadar24 scrapper for tracking specific flights (used in 2023 Lewiston, Maine Shootings Wikipedia article)
//[...document.querySelectorAll('a[data-flight-duration]')].map(e => e.getAttribute('data-flight-hex'))
interface FlightPlaybackPoints {
latitude: number
longitude: number
timestamp: number
}
type FlightPlaybackResponse = {
flight: {
track: FlightPlaybackPoints[]
}
}
let ids: string[] = []
const rewriter = new HTMLRewriter().on('a[data-flight-duration]', {
element(element) {
ids.push(element.getAttribute('data-flight-hex')!)
}
});
async function getFlightHistory(id: string) {
const url = "https://www.flightradar24.com/data/aircraft/" + id
const siteHTML = await fetch(url)
rewriter.transform(siteHTML)
const idsT = [...ids]
ids = []
return idsT
}
async function getFlightPlayback(flightId: string) {
const url = "https://api.flightradar24.com/common/v1/flight-playback.json?flightId=" + flightId
return (await (await fetch(url)).json())
.result.response.data as FlightPlaybackResponse
}
const copters = await Bun.file("flights.json").json()
let str = "timestamp,latitude,longitude\n"
for await (const copter of copters) {
console.log("Now Logging: " + copter)
const flights = await getFlightHistory(copter)
for await (const flight of flights) {
const flightData = await getFlightPlayback(flight)
Bun.write(`data/${copter}.${flight}.json`, JSON.stringify(flightData))
str += flightData.flight.track.map(point => `${point.timestamp},${point.latitude},${point.longitude}\n`).join("")
}
}
await Bun.write(`output.csv`, str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment