Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
adding the current and very advanced edition
- Loading branch information
Showing
5 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import requests | ||
import pprint | ||
|
||
|
||
def get_plane_data(icao_code): | ||
|
||
base_url = "https://api.adsb.lol/v2/" | ||
api_url = "hex/" | ||
|
||
url = base_url + api_url | ||
req = requests.get(url+icao_code) | ||
pprint.pprint(req.json()) | ||
return req.json() | ||
|
||
get_plane_data("4BAA84") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import requests | ||
from pprint import pprint | ||
|
||
def get_plane_data(icao_code): | ||
base_url = "https://api.airplanes.live/v2/" | ||
api_url = "icao/" | ||
url = f"{base_url}{api_url}{icao_code}" | ||
|
||
# Make the request | ||
req = requests.get(url) | ||
|
||
jsondata = req.json() # Return JSON data from response | ||
flight = jsondata["ac"][0] | ||
|
||
pprint(flight) | ||
|
||
return flight | ||
|
||
get_plane_data("4BAA84") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from fastapi import FastAPI, HTTPException | ||
from fastapi.middleware.cors import CORSMiddleware | ||
import requests | ||
from fastapi.staticfiles import StaticFiles | ||
|
||
app = FastAPI() | ||
|
||
# Define allowed origins and configure CORS middleware | ||
origins = [ | ||
"http://127.0.0.1:8000", | ||
"http://localhost:8000" | ||
] | ||
|
||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=origins, | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
|
||
# Function to fetch plane data from external API | ||
def get_plane_data(icao_code): | ||
base_url = "https://api.airplanes.live/v2/" | ||
api_url = "icao/" | ||
url = f"{base_url}{api_url}{icao_code}" | ||
|
||
# Make the request | ||
req = requests.get(url) | ||
|
||
# Check response status code | ||
if req.status_code != 200: | ||
raise HTTPException(status_code=req.status_code, detail="Error fetching data") | ||
|
||
jsondata = req.json() # Return JSON data from response | ||
|
||
if len(jsondata["ac"]) > 0: | ||
flight = jsondata["ac"][0] | ||
return flight | ||
else: | ||
return "No flight found" | ||
|
||
|
||
# Endpoint to fetch plane data | ||
@app.get("/plane/{icao_code}") | ||
async def read_plane_data(icao_code: str): | ||
return get_plane_data(icao_code) | ||
|
||
app.mount('/static', StaticFiles(directory='static',html=True)) | ||
|
||
# uvicorn data_api:app --reload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import datetime | ||
import requests | ||
import pprint | ||
|
||
current_time = datetime.datetime.now(datetime.UTC) | ||
print(current_time) | ||
recent_time = current_time - datetime.timedelta(seconds=60) | ||
print(recent_time) | ||
|
||
# to unix timestamp | ||
unix_time = recent_time.timestamp() | ||
print(unix_time) | ||
|
||
base_url = "https://opensky-network.org/api" | ||
api_url = "/states/all" | ||
url = base_url + api_url | ||
print(url) | ||
|
||
""" | ||
example_query = requests.get(f"https://opensky-network.org/api/states/all?lamin=45.8389&lomin=5.9962&lamax=47.8229&lomax=10.5226") | ||
pprint.pprint(example_query.json()) | ||
""" | ||
|
||
|
||
icao_code = "4BAA84" | ||
params = { | ||
"time": int(unix_time), | ||
"icao24": icao_code, | ||
} | ||
req = requests.get(url, params=params) | ||
pprint.pprint(req.json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Aircraft Position on Mapbox</title> | ||
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"> | ||
<link href="https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.css" rel="stylesheet"> | ||
<style> | ||
body { margin: 0; padding: 0; } | ||
#map { position: absolute; top: 0; bottom: 0; width: 100%; } | ||
</style> | ||
</head> | ||
<body> | ||
<div id="map"></div> | ||
<script src="https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.js"></script> | ||
<script> | ||
// Your Mapbox access token | ||
mapboxgl.accessToken = 'mapbox token here!!'; | ||
|
||
// Initialize the map | ||
const map = new mapboxgl.Map({ | ||
container: 'map', | ||
style: 'mapbox://styles/mapbox/streets-v11', | ||
center: [5.0, 50.0], // Initial map center [lon, lat]. CHANGE! | ||
zoom: 5 | ||
}); | ||
|
||
// Function to fetch aircraft data from the API | ||
async function fetchAircraftData(icao24) { | ||
const response = await fetch(`http://127.0.0.1:8000/plane/${icao24}`); | ||
return await response.json(); | ||
} | ||
|
||
// Function to add a marker for an aircraft | ||
async function addAircraftMarker(icao24, map) { | ||
try { | ||
const aircraft = await fetchAircraftData(icao24); | ||
console.log(`[${icao24}]:\n ${JSON.stringify(aircraft)}`); | ||
|
||
if (aircraft === "No flight found") { | ||
return; // ugly - switch it around? | ||
} else { | ||
// Create a custom marker element | ||
const el = document.createElement('div'); | ||
el.className = 'marker'; | ||
|
||
// Create a span element for the callsign text | ||
const callsign = document.createElement('span'); | ||
callsign.innerText = aircraft.flight; | ||
callsign.style.fontFamily = 'Arial, sans-serif'; | ||
callsign.style.fontSize = '12px'; | ||
callsign.style.color = '#0078A8'; | ||
callsign.style.fontWeight = 'bold'; | ||
callsign.style.backgroundColor = 'white'; | ||
callsign.style.padding = '2px'; | ||
callsign.style.borderRadius = '3px'; | ||
callsign.style.marginBottom = '5px'; | ||
|
||
// Append the callsign text to the marker element | ||
el.appendChild(callsign); | ||
|
||
// Create the marker and add it to the map | ||
new mapboxgl.Marker(el) | ||
.setLngLat([aircraft.lon, aircraft.lat]) | ||
.setPopup(new mapboxgl.Popup({ offset: 25 }) // add popups | ||
.setHTML(` | ||
<div style="font-family: Arial, sans-serif; font-size: 14px;"> | ||
<h3 style="margin: 0; color: #0078A8;">Flight: ${aircraft.flight} (${icao24})</h3> | ||
<p style="margin: 0; font-weight: bold;">Aircraft: ${aircraft.desc} (${aircraft.t})</p> | ||
<p style="margin: 5px 0 0;">Operator: ${aircraft.ownOp}</p> | ||
<p style="margin: 5px 0 0;">Altitude (Barometric): ${aircraft.alt_baro} ft</p> | ||
<p style="margin: 5px 0 0;">Ground Speed: ${aircraft.gs} knots</p> | ||
<p style="margin: 5px 0 0;">Heading: ${aircraft.true_heading}°</p> | ||
<p style="margin: 5px 0 0;">Latitude: ${aircraft.lat}</p> | ||
<p style="margin: 5px 0 0;">Longitude: ${aircraft.lon}</p> | ||
</div> | ||
`)) | ||
.addTo(map); | ||
} | ||
|
||
} catch (error) { | ||
console.error('Error fetching the aircraft data:', error); | ||
} | ||
} | ||
|
||
// Function to refresh the markers | ||
function refreshMarkers() { | ||
// Clear existing markers | ||
const markers = document.querySelectorAll('.marker'); | ||
markers.forEach(marker => marker.remove()); | ||
|
||
// Fetch and add markers for multiple aircraft | ||
addAircraftMarker('70209E', map); // Biman | ||
addAircraftMarker('394C10', map); // Air France | ||
} | ||
|
||
// Initial call to add markers | ||
refreshMarkers(); | ||
|
||
// Set interval to refresh markers every 30 seconds | ||
setInterval(refreshMarkers, 30000); | ||
</script> | ||
</body> | ||
</html> |