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()
@K-Musty
Copy link

K-Musty commented Nov 1, 2024

Hi there. I have a problem. I сan't register on tequila.kiwi.com. Has anyone encountered this problem? tequila kiwi com
same here

Try using aviationstack instead

@K-Musty
Copy link

K-Musty commented Nov 1, 2024

If you try using Tequila and it doesn't give you access, try aviationstack and finally input the IATA codes directly to sheet and continue the task. i spent a whole day searching for APIs with no success in sight. Patience is Key

@ShmRhm
Copy link

ShmRhm commented Jan 24, 2025

image
I used "avaiationstack" instead of "Tequila". Here is the code for flight_search class.

@SadSack963
Copy link

@ShmRhm
I know Angela looked at Aviationstack when Tequila became unviable. I think the limited free requests (100 per month) put her off. Did you find that this was an issue for you?

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