Spaces:
Sleeping
Sleeping
Download src/fetchapi.py from ShreyaMendi/Airlines: direct link, hf CLI and curl.
- Browser
- Download file 4.73 kB
-
https://huggingface.co/spaces/ShreyaMendi/Airlines/resolve/723cf28a28932f8e8f7d37b4fc234149256c13f6/src/fetchapi.py
- Command line
-
hf download hf://spaces/ShreyaMendi/Airlines@723cf28a28932f8e8f7d37b4fc234149256c13f6/src/fetchapi.py
-
curl -L -o fetchapi.py https://huggingface.co/spaces/ShreyaMendi/Airlines/resolve/723cf28a28932f8e8f7d37b4fc234149256c13f6/src/fetchapi.py
4.73 kB
| import requests | |
| import pandas as pd | |
| from datetime import datetime | |
| from dotenv import load_dotenv | |
| import os | |
| import time | |
| from dotenv import load_dotenv | |
| OPENSKY_URL = "https://opensky-network.org/api/states/all" | |
| OPENSKY_URL_DEPARTURES = "https://opensky-network.org/api/flights/departure" | |
| def fetch_opensky_snapshot() -> pd.DataFrame: | |
| """ | |
| Fetches a snapshot of current flights from the OpenSky API. | |
| Returns a pandas DataFrame of flight state vectors. | |
| """ | |
| r = requests.get(OPENSKY_URL, timeout=20) | |
| if r.status_code != 200: | |
| raise RuntimeError(f"Failed to fetch OpenSky data: {r.status_code} {r.reason} -> {r.text[:200]}") | |
| data = r.json() | |
| states = data.get("states", []) | |
| timestamp = data.get("time", datetime.utcnow().timestamp()) | |
| cols = [ | |
| "icao24", "callsign", "origin_country", "time_position", "last_contact", | |
| "longitude", "latitude", "baro_altitude", "on_ground", "velocity", | |
| "true_track", "vertical_rate", "sensors", "geo_altitude", "squawk", | |
| "spi", "position_source" | |
| ] | |
| df = pd.DataFrame(states, columns=cols) | |
| df["last_contact"] = pd.to_datetime(df["last_contact"], unit="s") | |
| df.attrs["timestamp"] = datetime.utcfromtimestamp(timestamp) | |
| return df | |
| def fetch_aviation_API_airlines_endpoint(): | |
| """ | |
| Fetches airline data from the AviationStack API airlines endpoint. | |
| Parameters: | |
| - None | |
| Returns: | |
| - dict: The JSON response from the AviationStack API containing the airline data. | |
| """ | |
| #api_key = os.environ.get("AVIATION_KEY") # Retrieve the API key (when running on HuggingFace) | |
| # Comment the line above and uncomment the two lines below if you are running the app locally (not on HuggingFace) and have a .env file with the AviationStack API key | |
| load_dotenv() | |
| api_key = os.getenv("AVIATION_KEY") # Retrieve the API key | |
| url = f"https://api.aviationstack.com/v1/airlines?access_key={api_key}" | |
| response = requests.get(url) | |
| return response.json() | |
| def fetch_rdu_departures(hours=6) -> pd.DataFrame: | |
| """ | |
| Fetch recent departures from RDU (KRDU) within the last n hours (default is 6). | |
| Returns a pandas DataFrame. | |
| """ | |
| end = int(time.time()) | |
| begin = end - hours * 3600 | |
| params = { | |
| "airport": "KRDU", | |
| "begin": begin, | |
| "end": end | |
| } | |
| response = requests.get(OPENSKY_URL_DEPARTURES, params=params, timeout=20) | |
| if response.status_code != 200: | |
| raise RuntimeError(f"Failed to fetch data, {response.headers}") | |
| data = response.json() | |
| columns = [ | |
| "icao24", "firstSeen", "estDepartureAirport", "lastSeen", "estArrivalAirport", "callsign", | |
| "estDepartureAirportHorizDistance", "estDepartureAirportVertDistance", "estArrivalAirportHorizDistance", | |
| "estArrivalAirportVertDistance", "departureAirportCandidatesCount", "arrivalAirportCandidatesCount" | |
| ] | |
| data_df = pd.DataFrame(data, columns=columns) | |
| flights = [] | |
| for _, flight in data_df.iterrows(): | |
| flights.append({ | |
| "icao24": flight["icao24"], | |
| "callsign": flight["callsign"], | |
| "departure": flight["estDepartureAirport"], | |
| "arrival": flight["estArrivalAirport"] | |
| }) | |
| return pd.DataFrame(flights) | |
| def fetch_aviation_API_airlines_endpoint(): | |
| """ | |
| Fetches airline data from the AviationStack API airlines endpoint. | |
| Parameters: | |
| - None | |
| Returns: | |
| - dict: The JSON response from the AviationStack API containing the airline data. | |
| """ | |
| #api_key = os.environ.get("AVIATION_KEY") # Retrieve the API key (when running on HuggingFace) | |
| # Comment the line above and uncomment the two lines below if you are running the app locally (not on HuggingFace) and have a .env file with the AviationStack API key | |
| load_dotenv() | |
| api_key = os.getenv("AVIATION_KEY") # Retrieve the API key | |
| url = f"https://api.aviationstack.com/v1/airlines?access_key={api_key}" | |
| response = requests.get(url) | |
| return response.json() | |
| if __name__ == "__main__": | |
| print("Fetching live flight data from OpenSky…") | |
| try: | |
| df = fetch_opensky_snapshot() | |
| print(f"Fetched {len(df)} flights at {df.attrs['timestamp']}") | |
| print(df.head()) | |
| df_2 = fetch_rdu_departures(hours=6) | |
| print(f"Fetched {len(df)} flights at {df.attrs['timestamp']}") | |
| print(df.head()) | |
| except Exception as e: | |
| print("Error:", e) | |
| print("Fetching airline data from AviationStack…") | |
| try: | |
| airline_data = fetch_aviation_API_airlines_endpoint() | |
| print(f"Fetched {len(airline_data.get('data', []))} airlines") | |
| print(airline_data) | |
| except Exception as e: | |
| print("Error:", e) |