def search_flights(self, destination): now = datetime.now() today = now.strftime("%d/%m/%Y") six_months_from_today = now + relativedelta(months=6) end_date = six_months_from_today.strftime("%d/%m/%Y") url = f"{self.endpoint}search" headers = {"apikey": self.key} params = { "fly_from": "SFO", "fly_to": destination, "date_from": today, "date_to": end_date, "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "GBP" } response = requests.get(url="https://tequila-api.kiwi.com/v2/search?", headers=headers, params=params) try: data = response.json()["data"][0] except IndexError: ########################## params["max_stopovers"] = 1 response = requests.get( url="https://tequila-api.kiwi.com/v2/search?", headers=headers, params=params, ) data = response.json()["data"][0] pprint(data) flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][1]["cityTo"], destination_airport=data["route"][1]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][2]["local_departure"].split("T")[0], stop_overs=1, via_city=data["route"][0]["cityTo"]) return flight_data ########################### else: flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0]) return flight_data
def check_flights(self, origin_city_code, destination_city_code, from_time, to_time): headers = {"apikey": TEQUILA_API_KEY} query = { "fly_from": origin_city_code, "fly_to": destination_city_code, "date_from": from_time.strftime("%d/%m/%Y"), "date_to": to_time.strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "GBP" } response = requests.get( url=f"{TEQUILA_ENDPOINT}/v2/search", headers=headers, params=query, ) try: data = response.json()["data"][0] except IndexError: query["max_stopovers"] = 1 response = requests.get( url=f"{TEQUILA_ENDPOINT}/v2/search", headers=headers, params=query, ) try: data = response.json()["data"][0] except IndexError: return None else: flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][1]["cityTo"], destination_airport=data["route"][1]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split( "T")[0], stop_overs=1, via_city=data["route"][0]["cityTo"]) return flight_data else: flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0]) return flight_data
def search_flights(self, destination_city_code): tmr_date = dt.datetime.today() + dt.timedelta(days=1) max_date = dt.datetime.today() + dt.timedelta(days=180) headers = {'apikey': self.tequila_api} parameters = { 'fly_from': ORIGIN_CITY_IATA, 'fly_to': destination_city_code, 'date_from': tmr_date.strftime(date_format), 'date_to': max_date.strftime(date_format), 'max_stopovers': 0, 'curr': 'GBP', 'nights_in_dst_from': 7, 'nights_in_dst_to': 28, 'flight_type': 'round', "one-for-city": 1 } response = requests.get(url=f'{TEQUILA_ENDPOINT}/v2/search', headers=headers, params=parameters) response.raise_for_status() try: data = response.json()['data'][0] except IndexError: parameters['max_stopovers'] = 1 response = requests.get(url=f'{TEQUILA_ENDPOINT}/v2/search', headers=headers, params=parameters) response.raise_for_status() print(response.json()) if len(response.json()['data']) > 0: data = response.json()['data'][0] flight_data = FlightData(price=data['price'], city_from=data['cityFrom'], fly_from=data['flyFrom'], city_to=data['cityTo'], fly_to=data['flyTo'], leave_date=data["route"][0] ["local_departure"].split("T")[0], return_date=data["route"][1] ["local_departure"].split("T")[0], stop_overs=1, via_city=data["route"][0]["cityTo"]) return flight_data else: return None else: flight_data = FlightData( price=data['price'], city_from=data['cityFrom'], fly_from=data['flyFrom'], city_to=data['cityTo'], fly_to=data['flyTo'], leave_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0], ) return flight_data
def search_flight(self, destination_code): tomorrow = datetime.now() + timedelta(1) six_months = datetime.now() + timedelta(180) formated_tomorrow = tomorrow.strftime("%d/%m/%Y") formated_six_months = six_months.strftime("%d/%m/%Y") headers = {"apikey": KIWI_FLIGHT_API_KEY} query = { "fly_from": "LON", "fly_to": destination_code, "date_from": formated_tomorrow, "date_to": formated_six_months, "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "GBP" } response = requests.get(url=f"{KIWI_BASE_URL}/v2/search", params=query, headers=headers) response.raise_for_status() try: data = response.json()["data"][0] #print(f"{destination_code}: £{data['price']}") except IndexError: query["max_stopovers"] = 1 response = requests.get(url=f"{KIWI_BASE_URL}/v2/search", params=query, headers=headers) response.raise_for_status() try: data = response.json()["data"][0] except IndexError: flight_data = None else: flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][1]["cityTo"], destination_airport=data["route"][1]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][2]["local_departure"].split( "T")[0], stop_overs=1, via_city=data["route"][0]["cityTo"]) else: flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0]) return flight_data
def flight_search(self, city_code): parameters = { "fly_from": "RDU", "fly_to": city_code, "date_from": fly_from_date, "date_to": fly_to_date, "nights_in_dst_from": length_stay_min, "nights_in_dst_to": length_stay_max, "curr": "USD", "adults": 4, "one_for_city": 1, "max_stopovers": 0, } print(f"Now searching for flights to {city_code}") f_search = requests.get(url=f"{self.api_url}v2/search", params=parameters, headers=self.api_headers) f_search.raise_for_status() print(f_search.json()) try: f_dict = f_search.json()['data'][0] except IndexError: parameters['max_stopovers'] = 2 f_search = requests.get(url=f"{self.api_url}v2/search", params=parameters, headers=self.api_headers) f_search.raise_for_status() print(f" Rerunning with up to 2 max stopovers\n {f_search.json()}") try: f_dict = f_search.json()['data'][0] except IndexError: print("No flights are available") return None f_fare = f_dict['price'] f_dep_city = "Raleigh" f_dep_code = "RDU" f_arr_city = f_dict['cityTo'] f_arr_code = city_code f_dep_date = f_dict['route'][0]['local_departure'].split('T')[0] f_ret_date = f_dict['route'][1]['local_departure'].split('T')[0] f_stop_over = 1 f_via_city = f_dict['route'][0]['cityTo'] flight = FlightData(f_fare, f_arr_city, f_arr_code, f_dep_date, f_ret_date, f_stop_over, f_via_city) return flight else: f_fare = f_dict['price'] f_dep_city = "Raleigh" f_dep_code = "RDU" f_arr_city = f_dict['cityTo'] f_arr_code = city_code f_dep_date = f_dict['route'][0]['local_departure'].split('T')[0] f_ret_date = f_dict['route'][1]['local_departure'].split('T')[0] flight = FlightData(f_fare, f_arr_city, f_arr_code, f_dep_date, f_ret_date) print(f"Fare:{f_fare}") print(f"Departure City:{f_dep_city}") print(f"Departure Code:{f_dep_code}") print(f"Arrival City:{f_arr_city}") print(f"Arrival Code:{f_arr_code}") print(f"Departure Date:{f_dep_date}") print(f"Return Date:{f_ret_date}") return flight
def check_flights(self, origin_city_code, destination_city_code, from_time, to_time): """Method to query API for flight information.""" print(f"Check flights triggered for {destination_city_code}") query = { "fly_from": origin_city_code, "fly_to": destination_city_code, "date_from": from_time.strftime("%d/%m/%Y"), "date_to": to_time.strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "GBP" } response = requests.get( url=f"{TEQUILA_ENDPOINT}/v2/search", headers=HEADERS, params=query, ) try: data = response.json()["data"][0] print(f"{destination_city_code} {data['price']}") except IndexError: query["max_stopovers"] = 1 response = requests.get( url=f"{TEQUILA_ENDPOINT}/v2/search", headers=HEADERS, params=query, ) data = response.json()["data"][0] pprint(data) flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][1]["cityTo"], destination_airport=data["route"][1]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][2]["local_departure"].split("T")[0], stop_overs=1, via_city=data["route"][0]["cityTo"]) return flight_data else: flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0]) return flight_data
def check_flights(self, fly_from, fly_to, date_from, date_to): check_data = { 'fly_from': fly_from, 'fly_to': fly_to, 'date_from': date_from, 'date_to': date_to, 'nights_in_dst_from': 7, 'nights_in_dst_to': 28, 'flight_type': 'round', 'one_for_city': 1, 'curr': 'USD', 'max_stopovers': 0 } tequila_flights_response = requests.get(self.tequila_flights_endpoint, headers=self.tequila_header, params=check_data) # tequila_flights_response.raise_for_status() # Creates extra errors as there are many missing flights w/ covid try: data = tequila_flights_response.json()["data"][0] except IndexError: try: check_data[ "max_stopovers"] = 1 # There just aren't many flights now due to covid, change to 10 to get results tequila_flights_response = requests.get( url=self.tequila_flights_endpoint, headers=self.tequila_header, params=check_data) data = tequila_flights_response.json()["data"][0] print(data) flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][1]["cityTo"], destination_airport=data["route"][1]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][2]["local_departure"].split( "T")[0], stop_overs=1, via_city=data["route"][0]["cityTo"]) return flight_data except IndexError: return None else: flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0]) return flight_data
def search_for_flight(self, origin_city_code, destination_city_code, from_date, to_date): """The default flight is to get a direct flight, however if none is found the Tequila API will provide an empty request. The exception will request changing the stops to 2 in order to obtain a request with information. """ headers = {"apikey": TEQUILA_API_KEY} query = { "fly_from": origin_city_code, "fly_to": destination_city_code, "date_from": from_date.strftime("%d/%m/%Y"), "date_to": to_date.strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 0, "max_stopovers": 0, "cur": "USD", } response = requests.get(url=f"{TEQUILA_ENDPOINT}/v2/search", headers=headers, params=query) try: data = response.json()['data'][0] except IndexError: query["max_stopovers"] = 2 result = requests.get(url=f"{TEQUILA_ENDPOINT}/v2/search", headers=headers, params=query) data = result.json()['data'][0] flight_data = FlightData( price=data['price'], origin_city=data['route'][0]['cityFrom'], origin_airport=data['route'][0]['flyFrom'], destination_city=data['route'][1]['cityTo'], destination_airport=data['route'][1]['flyTo'], out_date=data['route'][0]['local_departure'].split('T')[0], return_date=data['route'][1]['local_departure'].split('T')[0], stop_overs=1, via_city=data['route'][0]['cityTo'], flight_link=data['deep_link']) return flight_data else: flight_data = FlightData( price=data['price'], origin_city=data['route'][0]['cityFrom'], origin_airport=data['route'][0]['flyFrom'], destination_city=data['route'][0]['cityTo'], destination_airport=data['route'][0]['flyTo'], out_date=data['route'][0]['local_departure'].split('T')[0], return_date=data['route'][1]['local_departure'].split('T')[0], flight_link=data['deep_link']) return flight_data
def find_flight(self, origin_city_code, destination_city_code, from_time, to_time): body = { "fly_from": origin_city_code, "fly_to": destination_city_code, "date_from": from_time.strftime("%d/%m/%Y"), "date_to": to_time.strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "USD" } header = {'apikey': API} response = requests.get(url=SEARCH_URL, headers=header, params=body) try: data = response.json()["data"][0] except IndexError: #print(f"No flights found for {destination_city_code}.") body['max_stopovers'] = 1 response = requests.get( url=SEARCH_URL, headers=header, params=body ) if len(response.json()["data"]) == 0: print("No flights found.") return None data = response.json()["data"][0] flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][1]["cityTo"], destination_airport=data["route"][1]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][2]["local_departure"].split("T")[0], stop_overs=1, via_city=data["route"][0]["cityTo"] ) return flight_data else: flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0] ) print(f"{flight_data.destination_city}: ${flight_data.price}") return flight_data
def search_flights(self, code): tomorrow = datetime.datetime.today() + datetime.timedelta(days=1) six_month = datetime.datetime.today() + datetime.timedelta(days=1) + datetime.timedelta(days=180) tomorrow_formatted = str(tomorrow.strftime("%d/%m/%Y")).split(" ")[0] six_month_formatted = str(six_month.strftime("%d/%m/%Y")).split(" ")[0] query = { "fly_from": 'LON', "fly_to": code, "date_from": tomorrow_formatted, "date_to": six_month_formatted, "nights_in_dst_from": 7, "nights_in_dst_to": 30, "flight_type": "round", "selected_cabins": "M", "max_stopovers": 0, "curr": "GBP", } response = requests.get("https://tequila-api.kiwi.com/v2/search", params=query, headers=self.headers) try: data = response.json()['data'][0] except IndexError: print(f'No flights available to {code}') query["max_stopovers"] = 1 response = requests.get("https://tequila-api.kiwi.com/v2/search", params=query, headers=self.headers) data = response.json()['data'][0] flight_data = FlightData( price=data['price'], dept_city_name=data["route"][0]['cityFrom'], dept_airport_iata_code=data["route"][0]['flyFrom'], arr_city_name=data["route"][2]['cityTo'], arr_airport_iata_code=data["route"][2]['flyTo'], outbound_date=data["route"][0]["local_departure"].split("T")[0], inbound_date=data["route"][2]["local_departure"].split("T")[0], via_city=data["route"][1]['cityFrom'], stop_overs=1 ) return flight_data else: flight_data = FlightData( price=data['price'], dept_city_name=data['cityFrom'], dept_airport_iata_code=data['flyFrom'], arr_city_name=data['cityTo'], arr_airport_iata_code=data['flyTo'], outbound_date=data["route"][0]["local_departure"].split("T")[0], inbound_date=data["route"][1]["local_departure"].split("T")[0] ) print(f'City: {flight_data.arr_city_name}: {flight_data.price}') return flight_data
def search_flights(self, origin_city_code, destination_city_code, from_time, to_time): headers = { 'apikey': API_KEY } parameters = { 'fly_from': origin_city_code, 'fly_to': destination_city_code, 'date_from': from_time.strftime('%d/%m/%Y'), 'date_to': to_time.strftime('%d/%m/%Y'), 'nights_in_dst_from': 7, 'nights_in_dst_to': 28, 'one_for_city': 1, 'max_stopovers': 0, 'curr': 'GBP' } response = requests.get(url=f'{FLIGHT_API_ENDPOINT}v2/search', params=parameters, headers=headers) try: data = response.json()['data'][0] except IndexError: parameters['max_stopovers'] = 1 response = requests.get( url=f"{FLIGHT_API_ENDPOINT}/v2/search", headers=headers, params=parameters ) data = response.json()['data'][0] flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][1]["cityTo"], destination_airport=data["route"][1]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][2]["local_departure"].split("T")[0], stop_overs=1, via_city=data["route"][0]["cityTo"] ) return flight_data else: flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0] ) return flight_data
def checkFlights(self, base_city_code, location_code, from_time, to_time): header = {"apikey": location_api} query = { "fly_from": base_city_code, "fly_to": location_code, "date_from": from_time.strftime("%d/%m/%Y"), "date_to": to_time.strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "INR" } response = requests.get(url=f"{location_endpoint}/v2/search", params=query, headers=header) try: data = response.json()["data"] except IndexError: query["max_stopovers"] = 1 data = data[0] flight_data = FlightData( price=data['price'], base_city=data["route"][0]["cityFrom"], base_airport=data["route"][0]["flyFrom"], location_city=data["route"][0]["cityTo"], location_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0], stop_overs=1, via_city=data["route"][0]["cityTo"]) return flight_data else: data = data[0] flight_data = FlightData( price=data['price'], base_city=data["route"][0]["cityFrom"], base_airport=data["route"][0]["flyFrom"], location_city=data["route"][0]["cityTo"], location_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0], stop_overs=0, via_city='') return flight_data
def check_flights(self, origin_city_code, destination_city_code, from_time, to_time): headers = { "apikey": self.TEQUILA_SEARCH_API_KEY } params = { "fly_from": origin_city_code, "fly_to": destination_city_code, "date_from": from_time.strftime("%d/%m/%Y"), "date_to": to_time.strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "USD" } response = requests.get(url="https://tequila-api.kiwi.com/v2/search", headers=headers, params=params) data = response.json()["data"][0] flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0] ) print(f"{flight_data.destination_city}: £{flight_data.price}") return flight_data
def get_flight_prices(self, origin_city_code, destination_city_code, from_date, to_date): headers = {"apikey": TEQUILA_API} query = { "fly_from": origin_city_code, "fly_to": destination_city_code, "date_from": from_date, "date_to": to_date, "flight_type": "round", "max_stopovers": 0, "nights_in_dst_from": 7, "nights_in_dst_to": 28, "curr": "GBP", } response = requests.get(url=f"{TEQUILA_ENDPOINT}/v2/search", params=query, headers=headers) data = response.json()["data"][0] print(data) flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0]) print(f"{flight_data.destination_city}: £{flight_data.price}") return flight_data
def check_flights(self, origin_city_code, destination_city_code, from_time, to_time): header = {"apikey": TEQUILA_API_KEY} query = { "fly_from": origin_city_code, "fly_to": destination_city_code, "date_from": from_time.strftime("%d/%m/%Y"), "date_to": to_time.strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "CAD" } end_point = f"TEQUILA_ENDPOINT/v2/search" response = requests.get(end_point=end_point, params=query, headers=header) data = response.json()["data"][0] flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0] ) print(f"{flight_data.destination_city}: £{flight_data.price}") return flight_data
def check_flights(self, from_city, to_city, from_date, to_date): params = { 'fly_from': from_city, 'fly_to': to_city, 'date_from': from_date.strftime("%d/%m/%Y"), 'date_to': to_date.strftime("%d/%m/%Y"), 'nights_in_dst_from': 7, 'nights_in_dst_to': 28, "max_stopovers": 0 } response = requests.get(url=f"{TEQUILA_ENDPOINT}/v2/search", headers=TEQUILA_HEADERS, params=params).json() try: data = response['data'][0] except IndexError: print(f'No flights for {from_city} -- {to_city}') return None else: flight_data = FlightData( price=data['price'], origin_city=data['route'][0]['cityFrom'], destination_city=data['route'][0]['cityTo'], origin_airport=data['route'][0]['flyFrom'], destination_airport=data['route'][0]['flyTo'], flight_date=data['route'][0]['local_departure'].split("T")[0], return_date=data['route'][1]['local_departure'].split("T")[0], ) return flight_data
def search_for_flights(self, departure_code, destination_code, from_date, return_date): header = {"apikey": kiwi_apikey} param = { "fly_from": departure_code, "fly_to": destination_code, "date_from": from_date, "date_to": return_date, "nights_in_dst_from": 7, "nights_in_dst_to": 21, "flight_type": "round", "max_stopovers": 0, "curr": "USD", } response = requests.get(url=f"{kiwi_get_url}/v2/search", headers=header, params=param) try: response_data = response.json()["data"][0] except IndexError: return None flight_data = FlightData( price=response_data["price"], origin_city=response_data["route"][0]["cityFrom"], origin_airport=response_data["route"][0]["flyFrom"], destination_city=response_data["route"][0]["cityTo"], destination_airport=response_data["route"][0]["flyTo"], day_you_leave=response_data["route"][0]["local_departure"].split( "T")[0], return_date=response_data["route"][1]["local_departure"].split( "T")[0], airline=response_data["airlines"]) return flight_data
def search_price(self, origin_city_code, destination_city_code, from_date, to_date): SEARCH_ENDPOINT = f'{ENDPOINT}/search' param = { 'fly_from': origin_city_code, 'fly_to': destination_city_code, 'date_from': from_date, 'date_to': to_date, 'nights_in_dst_from': 7, 'nights_in_dst_to': 28, 'flight_type': 'round', 'one_for_city': 1, 'max_stopovers': 0, 'curr': 'GBP' } response = requests.get(url=SEARCH_ENDPOINT, headers=headers, params=param) data = response.json()['data'][0] flight_data = FlightData( price=data['price'], origin_city=data['route'][0]['cityFrom'], origin_airport=data['route'][0]['flyFrom'], destination_city=data['route'][0]['cityTo'], destination_airport=data['route'][0]['flyTo'], out_date=data['route'][0]['local_departure'].split('T')[0], return_date=data['route'][1]['local_departure'].split('T')[0]) print(f'{flight_data.destination_city}: £{flight_data.price}') return flight_data
def get_flight_details(self, origin, destination): """ Gets particular details about cheapest flight found flight. """ date_today = datetime.datetime.now().date().strftime("%d/%m/%Y") after_six_months = (datetime.datetime.now().date() + relativedelta(months=+6)).strftime("%d/%m/%Y") header = { "apikey": TEQUILA_API_KEY } query = { "fly_from": f"city:{origin}", "fly_to": f"city:{destination}", "date_from": date_today, "date_to": after_six_months, "one_for_city": 1, "curr": "KES", } response = requests.get(url=f"{TEQUILA_ENDPOINT}/search", params=query, headers=header) response.raise_for_status # Pick first index in case of flights with similar prices try: data = response.json()['data'][0] except IndexError: print(f"There are currently no flights to {destination}") return None # Come back and check on what is happening to arrival and deprt. dates flight_details = FlightData( price = data['price'], source_city = data['cityCodeFrom'], source_airport = data['flyFrom'], dest_city = data['cityCodeTo'], dest_airport = data['flyTo'] ) return flight_details
def check_flights(self, origin_city_code=None, destination_city_code=None, from_time=None, to_time=None): check_query = { "fly_from": origin_city_code, "fly_to": destination_city_code, "date_from": from_time.strftime("%d/%m/%Y"), "date_to": to_time.strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "GBP" } response = requests.get(url=f"{TEQUILA_ENDPOINT}/v2/search", headers=self.headers, params=check_query) response.raise_for_status() try: result = response.json()["data"][0] except IndexError: print(f"There are no flights found for {check_query['fly_to']}.") return None flight_data = FlightData( price=result["price"], city=result["route"][0]["cityFrom"], airport=result["route"][0]["flyFrom"], destination_city=result["route"][0]["cityTo"], destination_airport=result["route"][0]["flyTo"], date=result["route"][0]["local_departure"].split("T")[0], return_date=result["route"][1]["local_departure"].split("T")[0] ) print(f"{flight_data.destination_city}: £{flight_data.price}") return flight_data
def search_flight(self, origin_city_code, destination_city_code, from_time, to_time): param = { "fly_from": origin_city_code, "fly_to": destination_city_code, "date_from": from_time.strftime("%d/%m/%Y"), "date_to": to_time.strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "INR" } response_from_api = requests.get(url=f"{END_POINT}/v2/search", headers=FLIGHT_SEARCH_API, params=param) # IF IN CASE THERE IS NO FLIGHT TO OUR DESTINATION try: data_from_api = response_from_api.json()["data"][0] except IndexError: print(f"No flights found for {destination_city_code}.") return None # THIS FUNCTION IS PASSING ALL DATA TO flight_data class to store data flight_data = FlightData( price=data_from_api["price"], origin_city=data_from_api["route"][0]["cityFrom"], origin_airport=data_from_api["route"][0]["flyFrom"], destination_city=data_from_api["route"][0]["cityTo"], destination_airport=data_from_api["route"][0]["flyTo"], out_date=data_from_api["route"][0]["local_departure"].split("T")[0], return_date=data_from_api["route"][1]["local_departure"].split("T")[0] ) print(f"{flight_data.destination_city}: ₹{flight_data.price}") return flight_data
def __init__(self, own_city): self.now = datetime.datetime.now() tmr = self.now + datetime.timedelta(days=1) six_month = self.now + datetime.timedelta(days=180) self.date_tmr = tmr.strftime("%d/%m/%Y") self.date_six_month = six_month.strftime("%d/%m/%Y") self.flight_data = FlightData() self.own_city = own_city
def get_prices(self, city): global data header = { "apikey": TEQUILA_API_KEY, } query = { "fly_from": departure_airport_code, "date_from": formatted_day, "date_to": formatted_period, "fly_to": city, "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "GBP", } response = requests.get(url=end_point, params=query, headers=header) try: data = response.json()["data"][0] except: print("There is no flight in specified days") flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0], noinfo=False) return flight_data else: flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0], noinfo=True) print(f"{flight_data.destination_city}: £{flight_data.price}") return flight_data
def get_flights( self, departure_iata, destination_iata, departure_date, return_date, currency="GBP", min_nights=7, max_nights=28, ): """find flight information using Tequila Kiwi API and return a FlightData object Args: departure_iata (str): IATA Code for origin city destination_iata (str): IATA Code for destination city departure_date (str): dd/mm/YYYY for earliest departure return_date (str): dd/mm/YYYY for latest return currency (str, optional): currency to return. Defaults to "GBP". min_nights (int, optional): minimum nights. Defaults to 7. max_nights (int, optional): maximum nights. Defaults to 28. Returns: FlightData: FlightData for lowst cost flight """ url = self.url_base + "/v2/search" params = { "fly_from": departure_iata, "fly_to": destination_iata, "date_from": departure_date, "date_to": return_date, "nights_in_dst_from": min_nights, "nights_in_dst_to": max_nights, "curr": currency, "flight_type": "round", "one_for_city": 1, } response = requests.get(url, params=params, headers=self.headers) data = response.json() if len(data["data"]) > 0: data = data["data"][0] else: return None flight_data = FlightData( departure_city=data["cityFrom"], departure_airport=data["flyFrom"], destination_city=data["cityTo"], destination_airport=data["flyTo"], departure_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][-1]["local_arrival"].split("T")[0], price=data["price"], ) return flight_data
def fetch_flight_data_from_google_sheet(self) -> list[FlightData]: """This method will fetch the flight data of an user from an Google Sheet.""" response = requests.get( url=f"{SHEETY_BASE_URL}{self.sheety_sheet_endpoint}") response.raise_for_status() data = response.json() flight_data = [ FlightData(record_id=destination["id"], city=destination["city"], iata_code=destination["iataCode"], lowest_price=destination["lowestPrice"]) for destination in data["prices"] ] return flight_data
def search_flights(self): date_now = dt.datetime.now() search_url = f"{TEQUILA_URL}/v2/search" search_headers = { "apikey": TEQUILA_API_KEY, "accept": "application/json" } search_params = { "fly_from": "LON", "fly_to": self.city_obj['iataCode'], "date_from": date_now.strftime("%d/%m/%Y"), "date_to": (date_now + dt.timedelta(days=(6 * 30))).strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 28, "curr": "GBP", "flight_type": "round", "one_for_city": 1 } search_res = requests.get(url=search_url, headers=search_headers, params=search_params) search_data = search_res.json() if len(search_data['data']) > 0: flight_data_result = FlightData(search_data) else: flight_data_result = None if flight_data_result: if flight_data_result.price < self.city_obj['lowestPrice']: text_message = NotificationManager( flight_data_result.flight_dict) text_message.send_text() else: print("No Deal")
def check_flights(fly_to, max_stopovers) -> [FlightData]: tomorrow = datetime.datetime.now() + datetime.timedelta(days=1) within_six_months = tomorrow + datetime.timedelta(days=180) json = { "apikey": TEQUILA_API_KEY, "fly_from": "LON", "fly_to": fly_to, "date_from": tomorrow.strftime("%d/%m/%Y"), "date_to": within_six_months.strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 28, "flight_type": "round", "one_for_city": 1, "curr": "GBP", "max_stopovers": max_stopovers } tequila = requests.get( url=f"{TEQUILA_ENDPOINT}/v2/search?{urlencode(json)}") tequila.raise_for_status() flights = [] for flight_data in tequila.json()["data"]: flight = FlightData( arrival_airport_city=flight_data["cityTo"], arrival_airport_code=flight_data["flyTo"], arrival_city_code=flight_data["cityCodeTo"], departure_airport_city=flight_data["cityFrom"], departure_airport_code=flight_data["flyFrom"], inbound_date=flight_data["route"][ (max_stopovers + 1) * 0]["utc_departure"].split("T")[0], outbound_date=flight_data["route"][ (max_stopovers + 1) * 1]["utc_departure"].split("T")[0], price=flight_data["price"], stopovers=max_stopovers, via_city=flight_data["route"][0]["cityTo"]) flights.append(flight) return flights
def find_flights(self, src_city_iata, dest_city_iata, tomorrow_date, to_date, city_name): flight_search_endpoint = f"{self.kiwi_endpoint}/v2/search" flight_search_params = { "fly_from": src_city_iata, "fly_to": dest_city_iata, "date_from": tomorrow_date, "date_to": to_date, "nights_in_dst_from": 7, "nights_in_dst_to": 10, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "USD" } response = requests.get( url=flight_search_endpoint, headers=self.kiwi_header, params=flight_search_params, ) try: data = response.json()["data"][0] except IndexError: return (f"No flights found for {city_name}") flight_data = FlightData( price=data['price'], src_city=data['route'][0]['cityFrom'], src_airport=data['route'][0]['flyFrom'], dest_city=data['route'][0]['cityTo'], dest_airport=data['route'][0]['flyTo'], out_date=data['route'][0]['local_departure'].split("T")[0], return_date=data['route'][1]['local_departure'].split("T")[0], ) #print(f"{flight_data.dest_city} for {flight_data.price}.") return flight_data
def get_flight(self, to_city, lowest_price): endpoint = f"{self.base_endpoint}/v2/search" date_from = dt.datetime.now().strftime("%d/%m/%Y") date_to = dt.datetime.now() + dt.timedelta(days=1) date_to = date_to.strftime("%d/%m/%Y") body = { "fly_from": self.from_city, "fly_to": to_city, "date_from": date_from, "date_to": date_to, "nights_in_dst_from": self.min_stay, "nights_in_dst_to": self.max_stay, "curr": self.currency, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, } response = requests.get(url=endpoint, params=body, headers=self.headers) response.raise_for_status() data = response.json() print(data) if len(data["data"]) > 0: data = data["data"][0] flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], out_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0]) print( f"{flight_data.destination_city}: {flight_data.price} {self.currency}" ) return flight_data else: return None
def check_flights(self, origin_city_code, destination_city_code, from_time, to_time): headers = {"apikey": tequila_api_key} query = { "fly_from": origin_city_code, "fly_to": destination_city_code, "date_from": from_time.strftime("%d/%m/%Y"), "date_to": to_time.strftime("%d/%m/%Y"), "nights_in_dst_from": 7, "nights_in_dst_to": 21, "flight_type": "round", "one_for_city": 1, "max_stopovers": 0, "curr": "USD" } response = requests.get( url=f"{tequila_main_endpoint}/v2/search", headers=headers, params=query, ) try: data = response.json()["data"][0] except IndexError: print(f"No flights found for {destination_city_code}.") return None flight_data = FlightData( price=data["price"], origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], depart_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0], ) print(f"{flight_data.destination_city}: ${flight_data.price}") return flight_data