Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Created October 21, 2020 10:29
Show Gist options
  • Save angelabauer/99301f807578ad8b6f54e8fb8ec7162b to your computer and use it in GitHub Desktop.
Save angelabauer/99301f807578ad8b6f54e8fb8ec7162b to your computer and use it in GitHub Desktop.
from pprint import pprint
import requests
SHEETY_PRICES_ENDPOINT = YOUR SHEETY PRICES ENDPOINT URL
class DataManager:
def __init__(self):
self.destination_data = {}
def get_destination_data(self):
# 2. Use the Sheety API to GET all the data in that sheet and print it out.
response = requests.get(url=SHEETY_PRICES_ENDPOINT)
data = response.json()
self.destination_data = data["prices"]
# 3. Try importing pretty print and printing the data out again using pprint().
# pprint(data)
return self.destination_data
# 6. In the DataManager Class make a PUT request and use the row id  from sheet_data
# to update the Google Sheet with the IATA codes. (Do this using code).
def update_destination_codes(self):
for city in self.destination_data:
new_data = {
"price": {
"iataCode": city["iataCode"]
}
}
response = requests.put(
url=f"{SHEETY_PRICES_ENDPOINT}/{city['id']}",
json=new_data
)
print(response.text)
import requests
TEQUILA_ENDPOINT = "https://tequila-api.kiwi.com"
TEQUILA_API_KEY = YOUR FLIGHT SEARCH API KEY
class FlightSearch:
def get_destination_code(self, city_name):
location_endpoint = f"{TEQUILA_ENDPOINT}/locations/query"
headers = {"apikey": TEQUILA_API_KEY}
query = {"term": city_name, "location_types": "city"}
response = requests.get(url=location_endpoint, headers=headers, params=query)
results = response.json()["locations"]
code = results[0]["code"]
return code
# 4. Pass the data back to the main.py file, so that you can print the data from main.py
from data_manager import DataManager
data_manager = DataManager()
sheet_data = data_manager.get_destination_data()
# print(sheet_data)
# 5. In main.py check if sheet_data contains any values for the "iataCode" key.
# If not, then the IATA Codes column is empty in the Google Sheet.
# In this case, pass each city name in sheet_data one-by-one
# to the FlightSearch class to get the corresponding IATA code
# for that city using the Flight Search API.
# You should use the code you get back to update the sheet_data dictionary.
if sheet_data[0]["iataCode"] == "":
from flight_search import FlightSearch
flight_search = FlightSearch()
for row in sheet_data:
row["iataCode"] = flight_search.get_destination_code(row["city"])
print(sheet_data)
data_manager.destination_data = sheet_data
data_manager.update_destination_codes()
@tunchi-stuntin
Copy link

tunchi-stuntin commented Jun 14, 2025

You can't actually use the flight future schedules API unless you subscribe to the basic plan or a higher plan. I guess that condition and some other limitations, like failure to cover airports with very small traffic—like around 10 or fewer flights per day, and the enforced rate limitation on the endpoint, i.e., each user is allowed to make one API call per minute, and exceeding this limit will result in temporary access restrictions, discouraged Angela from using Aviationstack. So you can either look for an alternative API to use or move on to the next section.
image

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