def find_flights(self, origin, destination, date_from, date_to): """Takes flight details as STRs and returns a flight_data object.""" search_url = f"{self.api_url}/v2/search" params = { "fly_from": origin, "fly_to": destination, "date_from": date_from, "date_to": date_to, "nights_in_dst_from": NIGHTS_FROM, "nights_in_dst_to": NIGHTS_TO, "flight_type": "round", "curr": CURRENCY, "one_for_city": 1, "max_stopovers": MAX_STOPOVERS } response = requests.get(url=search_url, params=params, headers=self.headers) # also just raise an error, this will returns a "HTTPError" if credentials are not set properly response.raise_for_status() # only use the first result, if any data = response.json()["data"][0] flight = fd.FlightData( origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], leave_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0], price=data["price"]) # to have some feedback print( f"Found a flight to {flight.destination_city} for {flight.price} {CURRENCY}." ) return flight
def find_flights(self, origin, destination, date_from, date_to): """Takes flight details as STRs and returns a flight_data object.""" search_url = f"{self.api_url}/v2/search" params = { "fly_from": origin, "fly_to": destination, "date_from": date_from, "date_to": date_to, "nights_in_dst_from": NIGHTS_FROM, "nights_in_dst_to": NIGHTS_TO, "flight_type": "round", "curr": CURRENCY, "one_for_city": 1, "max_stopovers": 0 } response = requests.get(url=search_url, params=params, headers=self.headers) # also just raise an error, this will returns a "HTTPError" if credentials are not set properly response.raise_for_status() # if there is a result try: data = response.json()["data"][0] except IndexError: # in case no direct flight was found, try again with one stopover params["max_stopovers"] = 1 response = requests.get(url=search_url, params=params, headers=self.headers) # one more try try: data = response.json()["data"][0] except IndexError: print(f"No flight found even with a stopover.") return None else: flight = fd.FlightData( origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], leave_date=data["route"][0]["local_departure"].split( "T")[0], return_date=data["route"][1]["local_departure"].split( "T")[0], price=data["price"], stopovers=1, via_city=data["route"][0]["cityTo"]) # some feedback print( f"Found a flight to {flight.destination_city} for {flight.price} {CURRENCY} with a stopover." ) return flight else: flight = fd.FlightData( origin_city=data["route"][0]["cityFrom"], origin_airport=data["route"][0]["flyFrom"], destination_city=data["route"][0]["cityTo"], destination_airport=data["route"][0]["flyTo"], leave_date=data["route"][0]["local_departure"].split("T")[0], return_date=data["route"][1]["local_departure"].split("T")[0], price=data["price"]) # also to have some feedback print( f"Found a direct flight to {flight.destination_city} for {flight.price} {CURRENCY}." ) return flight
notification_manager = nm.NotificationManager(nm.EMAIL, nm.PASSWORD) for idx, iata_code in enumerate(iata_codes): flight_params.update(fly_to=iata_code, max_stopovers=0) flight_search = fs.FlightSearch(fs.API_SEARCH, fs.ENDPOINT, flight_params) data = flight_search.data['data'] stopped = False if len(data) == 0: flight_params.update(max_stopovers=1) flight_search = fs.FlightSearch(fs.API_SEARCH, fs.ENDPOINT, flight_params) data = flight_search.data['data'] if len(data) == 0: continue stopped = True flight_data = fd.FlightData(data) if flight_data.data['price'] <= data_manager.data['prices'][idx][ 'lowestPrice']: data = flight_data.formatted_data message = '\n🚨Low price alert🚨' message += f'\n\nOnly £{data["price"]:.2f} to fly' message += f'\nfrom {data["from"]} to {data["to"]},' message += f'\nfrom {data["departure_date"]} to {data["arrival_date"]}.' if stopped: message += f'\n\nFlight has 1 stop over, via {data["via_city"]}.' message += f"\nhttps://www.google.co.uk/flights?hl=en#flt=" message += f"{flight_data.data['flyTo']}.{flight_data.data['flyFrom']}." message += f"{data['departure_date']}*{flight_data.data['flyFrom']}." message += f"{flight_data.data['flyTo']}.{data['arrival_date']}"