refactor: switch API access

now using R bindings from eneller/openSkies instead of raw REST access
This commit is contained in:
eneller
2026-01-19 12:24:35 +01:00
parent 27813426ff
commit c2700b16a2

View File

@@ -24,117 +24,28 @@ library(trajr)
``` ```
# Download flights # Download flights
```{r opensky_lib, include=FALSE} ```{r opensky}
time_now <- Sys.time() time_now <- Sys.time()
time_one_hour_ago <- time_now - 3600 creds <- getCredentials(
client_id = Sys.getenv('OPENSKY_CLIENT_ID'),
client_secret = Sys.getenv('OPENSKY_CLIENT_SECRET'))
# get departures from frankfurt airport # get departures from Frankfurt airport
flights <- getAirportDepartures(airport = "EDDF", startTime = time_one_hour_ago, endTime = time_now ) departures <- getAirportDepartures(airport = "EDDF", startTime = time_now - hours(1), endTime = time_now, credentials = creds )
print(paste("Found flights:", length(flights)))
head(flights)
```
getFlights <- function(icao, time, creds){
```{r opensky_rest, include=FALSE} flights <-getAircraftFlights(icao, startTime = time - days(1), endTime = time, credentials = creds )
# gets opensky data using the REST API at https://openskynetwork.github.io/opensky-api/rest.html return(flights)
my_client_id <- Sys.getenv("OPENSKY_CLIENT_ID")
my_client_secret <- Sys.getenv("OPENSKY_CLIENT_SECRET")
token_url <- "https://auth.opensky-network.org/auth/realms/opensky-network/protocol/openid-connect/token"
token_resp <- POST(
token_url,
body = list(
grant_type = "client_credentials",
client_id = my_client_id,
client_secret = my_client_secret
),
encode = "form"
)
if (status_code(token_resp) == 200) {
my_token <- content(token_resp)$access_token
message("Token successfully generated")
} else {
stop(paste("Error while collecting token:", content(token_resp, as = "text")))
} }
icao <- departures[[1]][["ICAO24"]]
flights <- getFlights(icao,Sys.time(), creds)
# TODO map from all available flights to tracks
query <- list(icao24= icao, time=as.numeric(flights[[1]][["departure_time"]]))
time_now <- as.numeric(Sys.time()) response <-makeAuthenticatedRequest('tracks/all',query, creds)
time_one_hour_ago <- time_now - 3600 track_data <- fromJSON(content(response, as = "text", encoding = "UTF-8"))
if (!is.null(track_data$path) && length(track_data$path) > 0) {
api_url <- paste0("https://opensky-network.org/api/flights/departure?airport=EDDF&begin=",
round(time_one_hour_ago), "&end=", round(time_now))
data_resp <- GET(
api_url,
add_headers(Authorization = paste("Bearer", my_token))
)
if (status_code(data_resp) == 200) {
departures_df <- fromJSON(content(data_resp, as = "text", encoding = "UTF-8"))
print(head(departures_df))
} else {
print(paste("API Error:", status_code(data_resp)))
print(content(data_resp, as = "text"))
}
```
```{r last5}
icao24 <- "4bcdf9"
time_now <- as.numeric(Sys.time())
time_then <- time_now - (1 * 24 * 60 * 60)
api_url_aircraft <- paste0("https://opensky-network.org/api/flights/aircraft?icao24=",
icao24, "&begin=", round(time_then), "&end=", round(time_now))
aircraft_resp <- GET(
api_url_aircraft,
add_headers(Authorization = paste("Bearer", my_token))
)
if (status_code(aircraft_resp) == 200) {
all_flights <- fromJSON(content(aircraft_resp, as = "text", encoding = "UTF-8"))
if (length(all_flights) > 0) {
all_flights <- all_flights[order(all_flights$firstSeen, decreasing = TRUE), ]
last_5_flights <- head(all_flights, 5)
last_5_flights$firstSeen <- as.POSIXct(last_5_flights$firstSeen, origin="1970-01-01")
last_5_flights$lastSeen <- as.POSIXct(last_5_flights$lastSeen, origin="1970-01-01")
print(last_5_flights[, c("icao24", "firstSeen", "estDepartureAirport", "estArrivalAirport")])
} else {
print("No flights in this timespan for this airplane")
}
} else {
print(paste("Error:", status_code(aircraft_resp)))
}
```
# Plot the flights
```{r route}
target_icao <- all_flights$icao24[1]
target_time <- as.numeric(all_flights$firstSeen[1])
api_url_track <- paste0("https://opensky-network.org/api/tracks/all?icao24=",
target_icao, "&time=", target_time)
track_resp <- GET(
api_url_track,
add_headers(Authorization = paste("Bearer", my_token))
)
if (status_code(track_resp) == 200) {
track_data <- fromJSON(content(track_resp, as = "text", encoding = "UTF-8"))
if (!is.null(track_data$path) && length(track_data$path) > 0) {
route_df <- as.data.frame(track_data$path) route_df <- as.data.frame(track_data$path)
colnames(route_df) <- c("time", "lat", "lon", "alt", "heading", "on_ground") colnames(route_df) <- c("time", "lat", "lon", "alt", "heading", "on_ground")
@@ -142,19 +53,14 @@ if (status_code(track_resp) == 200) {
message("Loading of route successfull! Points: ", nrow(route_df)) message("Loading of route successfull! Points: ", nrow(route_df))
plot(route_df$lon, route_df$lat, type="o", pch=20, col="blue", plot(route_df$lon, route_df$lat, type="o", pch=20, col="blue",
main=paste("Geographic route from", target_icao), main=paste("Geographic route of", icao),
xlab="Longitude", ylab="Latitude") xlab="Longitude", ylab="Latitude")
plot(route_df$time, route_df$alt, type="l", col="red", lwd=2, plot(route_df$time, route_df$alt, type="l", col="red", lwd=2,
main=paste("Altitude profile of", target_icao), main=paste("Altitude profile of", icao),
xlab="Time (Unix)", ylab="Height (Meter)") xlab="Time (Unix)", ylab="Height (Meter)")
} else { } else {
print("No path points from api") print("No path points from api")
} }
} else {
print(paste("Error while getting the route. Status:", status_code(track_resp)))
print(content(track_resp, as = "text"))
}
``` ```