Skip to content

Instantly share code, notes, and snippets.

@notalentgeek
Last active May 10, 2023 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save notalentgeek/fbd06d25deb350dda71e034d3e96fe9c to your computer and use it in GitHub Desktop.
Save notalentgeek/fbd06d25deb350dda71e034d3e96fe9c to your computer and use it in GitHub Desktop.
Schipol Python Flight API Puller from https://developer.schiphol.nl/
"""
I faced a problem when pulling data from Schipol Airport API. The first
data pull is only resulted in 20 elements, meanwhile there are hundreds
of flights in Schipol everyday. From my support contact, it appears that
the API are in a pagination of 20 flights for each paginations.
This script is made to pull all flights schedule from all paginations.
"""
import requests, sys
# Function to call Schipol flight API. This API comes in with pagination.
def get_public_flight_api():
"""
API application id and API application key. The API used is the Schipol
airport API, https://developer.schiphol.nl/.
"""
APP_ID = "PROVIDE_YOUR_OWN!"
APP_KEY = "PROVIDE_YOUR_OWN!"
CREDENTIALS = { "app_id": APP_ID, "app_key": APP_KEY }
# Initial URL.
INITIAL_URL = "https://api.schiphol.nl/public-flights/flights"
# Initial headers.
INITIAL_HEADERS = { "resourceversion": "v3" }
# Main list that will hold all dictionary of today's flight.
flight_raws = []
flight_raws = get_next_api_pagination(INITIAL_URL, INITIAL_HEADERS,
"flights", flight_raws, params=CREDENTIALS)
return flight_raws
"""
Function to get the API from the next pagination. The `elements` is a list
to dump each data from each paginations. At any case for both failure and
success return back the `elements`.
"""
def get_next_api_pagination(url, headers, key, elements, params={}):
print(url)
try:
# Get the response.
response = requests.request("GET", url, headers=headers, params=params)
# Check for proper response code.
if response.status_code == 200:
# Dump the current returned dictionary into the `elements`.
current_elements = response.json()[key]
elements.extend(current_elements)
"""
All links returned with link relation. Link relation (`rel`) can
be "next", "previous", "first", "last".
"""
return_links = [link_pagination.split("; ") for link_pagination in\
response.headers["Link"].replace("<", "").replace(">", "")\
.split(", ")]
for return_link in return_links:
# The the link relation.
rel = return_link[1].replace("rel=", "").replace("\"", "")
"""
If the currently examined link is the next URl from the `url`
then proceed this function again.
"""
if rel == "next":
get_next_api_pagination(return_link[0], headers, key,
elements)
return elements
except requests.exceptions.ConnectionError as error:
print(error)
return elements
if __name__ == "__main__":
print(get_public_flight_api())
@wpeterw
Copy link

wpeterw commented Jan 25, 2023

def get_all_flights():
    all_flights = []
    r = client.get_flight_data(url=settings.SCHIPHOL_API_URL)
    for flight in r.json().get("flights"):
        all_flights.append(flight)

    while "next" in r.links:
        r = client.get_flight_data(url=r.links.get("next").get("url"))
        for flight in r.json().get("flights"):
            all_flights.append(flight)

    return all_pages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment