-
-
Save TheMuellenator/f0e2e06ac8b6a45dbdee08a38e2da22d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
@jmorris236 It means if that cell is empty, then execute the code below,
Thank you! I was way too lost in this project. I had a hard time finding my way into tequila API but your code showed me it actually pretty easy.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am having a hard time understanding this line in Step 5. What is being indexed? -> if sheet_data[0]["iataCode"] == "":