0

my code tried to find best price for flight in a tkinter app. my def code is :

def search_flights():
    origin = origin_entry.get()
    destination = destination_entry.get()
    departure_date = departure_date_entry.get()
    num_days = int(num_days_entry.get())
    departure_flexibility = int(departure_flexibility_entry.get())
    return_flexibility = int(return_flexibility_entry.get())

    # Create a list of possible departure dates to search
    departure_date_list = []
    for i in range(-departure_flexibility, departure_flexibility+1):
        departure_date_list.append((datetime.strptime(departure_date, '%Y-%m-%d') + timedelta(days=i)).strftime('%Y-%m-%d'))

    # Create a list of possible return dates to search
    return_date_list = []
    for i in range(-return_flexibility, return_flexibility+1):
        return_date_list.append((datetime.strptime(departure_date, '%Y-%m-%d') + timedelta(days=num_days+i)).strftime('%Y-%m-%d'))

    # Create a dictionary to store the flight results
    flight_results = {}

    # Loop through each possible combination of departure date and return date and search for flights
    for departure_date in departure_date_list:
        for return_date in return_date_list:
            payload = {"query": {
                "market": "UK",
                "locale": "en-GB",
                "currency": "EUR",
                "queryLegs": [
                    {
                        "originPlaceId": {"iataCode": origin},
                        "destinationPlaceId": {"iataCode": destination},
                        "date": {
                            "year": int(departure_date[:4]),
                            "month": int(departure_date[5:7]),
                            "day": int(departure_date[8:])
                        }
                    },
                    {
                        "originPlaceId": {"iataCode": destination},
                        "destinationPlaceId": {"iataCode": origin},
                        "date": {
                            "year": int(return_date[:4]),
                            "month": int(return_date[5:7]),
                            "day": int(return_date[8:])
                        }
                    }
                ],
                "cabinClass": "CABIN_CLASS_ECONOMY",
                "adults": 1
            }}
            headers = {
                "content-type": "application/json",
                "X-RapidAPI-Key": API_KEY,
                "X-RapidAPI-Host": "skyscanner-api.p.rapidapi.com"
            }
            response = requests.post("https://skyscanner-api.p.rapidapi.com/v3e/flights/live/search/synced", json=payload, headers=headers)
            data = response.json()

            # Check if there are any quotes available
            try:
                if data['errors']:
                    # Handle the case where there are no quotes available
                    error_message = data['errors'][0]['message']
                    flight_results[f"{departure_date} - {return_date}"] = f'Error: {error_message}'
            except KeyError:
                # Set the error message to a default value
                error_message = 'Unknown error'
                flight_results[f"{departure_date} - {return_date}"] = f'Error: {error_message}'
            else:
                # Get the lowest price
                # Get the lowest price
                lowest_price = data['legs'][0]['segments'][0]['itineraryPrices'][0]['price']['amount']
                # Store the lowest price and departure and return dates in the flight results dictionary
                flight_results[f"{departure_date} - {return_date}"] = f"{lowest_price} EUR"

    # Print the flight results
    flight_results_text.delete(1.0, tk.END)
    flight_results_text.insert(tk.END, str(flight_results))

I tried to fill CDG and YUL for destination,

Payload: {'query': {'market': 'UK', 'locale': 'en-GB', 'currency': 'EUR', 'queryLegs': [{'originPlaceId': {'iataCode': 'YUL'}, 'destinationPlaceId': {'iataCode': 'CDG'}, 'date': {'year': 2023, 'month': 11, 'day': 30}}, {'originPlaceId': {'iataCode': 'CDG'}, 'destinationPlaceId': {'iataCode': 'YUL'}, 'date': {'year': 2023, 'month': 12, 'day': 10}}], 'cabinClass': 'CABIN_CLASS_ECONOMY', 'adults': 1}}

But I have this error (My APIKEY is ok ): b'{"status":"RESULT_STATUS_FAILED","message":"/query/queryLegs/0/originPlaceId must have required property 'iata'. /query/queryLegs/0/originPlaceId must have required property 'entityId'. /query/queryLegs/0/originPlaceId must match exactly one schema in oneOf."}'

0 Answers0