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

Seems Tequila / Kiwi does not do charity flight info anymore.

Trying to build the solution with Amadeus for Developers, not feeling very lucky or successful though :-)

I see many have tried this route and posted solutions but when I copy them down to test them before I commit they all kick up errors that i cant understand, let alone solve. Anyway, good luck to those that try to wander here

https://developers.amadeus.com/

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