i had this code working a little while ago, but now it’s returning 0 rows… I’ve tried everything i can think of, and i just cant make it work anymore… Can someone help me understand whats going on?
import requests
import pandas as pd
API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxx"
def get_flights(carrier, start_time, end_time, max_pages=1, cursor=None):
base_url = f"https://aeroapi.flightaware.com/aeroapi/operators/{carrier}/flights"
headers = {"x-apikey": API_KEY}
params = {"start": start_time, "end": end_time, "max_pages": max_pages}
if cursor:
params["cursor"] = cursor
all_flights = []
for _ in range(max_pages):
response = requests.get(base_url, headers=headers, params=params)
print(f"Status Code: {response.status_code}") # Print the response status code for debugging
if response.status_code == 200:
data = response.json()
flights = data.get("flights", [])
print(f"Number of Flights Returned: {len(flights)}") # Debug: number of flights in this page
if len(flights) == 0: # If no flights are returned, break the loop early
print("No flights returned, stopping the loop.")
break
all_flights.extend(flights)
# Check if 'links' exists and if 'next' is available
if data.get("links") and data["links"].get("next"):
params["cursor"] = data["links"]["next"].split("cursor=")[-1]
else:
break
else:
print(f"Error: {response.status_code} - {response.text}")
break
# Convert to pandas DataFrame
df = pd.DataFrame(all_flights)
return df
df1 = get_flights('FLE', '2024-09-27', '2024-09-28', 500)