Spaces:
Sleeping
Sleeping
Removing class structure
Browse files- tools/cheapest_flight.py +79 -109
tools/cheapest_flight.py
CHANGED
|
@@ -1,109 +1,79 @@
|
|
| 1 |
-
import http.client
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import json
|
| 5 |
-
from smolagents.tools import Tool
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
'
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
cheapest_flight
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
'to': cheapest_flight['segments'][0]['arrivalAirport'].get('cityName', 'Unknown'),
|
| 81 |
-
'time': cheapest_flight['segments'][0].get('arrivalTime', 'Unknown')
|
| 82 |
-
},
|
| 83 |
-
'airline': cheapest_flight['segments'][0]['legs'][0]['carriersData'][0].get('name', 'Unknown')
|
| 84 |
-
}
|
| 85 |
-
return result
|
| 86 |
-
except (KeyError, IndexError):
|
| 87 |
-
return None
|
| 88 |
-
return None
|
| 89 |
-
|
| 90 |
-
except Exception:
|
| 91 |
-
return None
|
| 92 |
-
|
| 93 |
-
if __name__ == "__main__":
|
| 94 |
-
|
| 95 |
-
tool = GetCheapestFlightTool()
|
| 96 |
-
|
| 97 |
-
departureId = "BOM"
|
| 98 |
-
arrivalId = "DEL"
|
| 99 |
-
departureDate = '2025-03-16'
|
| 100 |
-
|
| 101 |
-
cheapest = tool.get_flight(departureId, arrivalId, departureDate)
|
| 102 |
-
print(f"""
|
| 103 |
-
Cheapest Flight Details:
|
| 104 |
-
Price: {cheapest['price']}
|
| 105 |
-
From: {cheapest['departure']['from']} at {cheapest['departure']['time']}
|
| 106 |
-
To: {cheapest['arrival']['to']} at {cheapest['arrival']['time']}
|
| 107 |
-
Airline: {cheapest['airline']}
|
| 108 |
-
"""
|
| 109 |
-
)
|
|
|
|
| 1 |
+
import http.client
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import json
|
| 5 |
+
from smolagents.tools import Tool
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_flight(departureId, arrivalId, departureDate):
|
| 10 |
+
|
| 11 |
+
conn = http.client.HTTPSConnection("booking-com15.p.rapidapi.com")
|
| 12 |
+
|
| 13 |
+
headers = {
|
| 14 |
+
'x-rapidapi-key': "RAPIDAPI_KEY", #Replace with your own RapidAPI key
|
| 15 |
+
'x-rapidapi-host': "booking-com15.p.rapidapi.com"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
conn.request("GET", f"/api/v1/flights/searchFlights?fromId={departureId}.AIRPORT&toId={arrivalId}.AIRPORT&departDate={departureDate}¤cy_code=BRL", headers=headers)
|
| 19 |
+
|
| 20 |
+
res = conn.getresponse()
|
| 21 |
+
data = res.read()
|
| 22 |
+
json_data = json.loads(data.decode("utf-8"))
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
# Get all flight offers with validation
|
| 26 |
+
if not json_data or 'data' not in json_data or 'flightOffers' not in json_data['data']:
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
flight_offers = json_data['data']['flightOffers']
|
| 30 |
+
|
| 31 |
+
if not flight_offers:
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
# Initialize variables to track cheapest flight
|
| 35 |
+
cheapest_price = float('inf')
|
| 36 |
+
cheapest_flight = None
|
| 37 |
+
|
| 38 |
+
# Loop through each flight offer
|
| 39 |
+
for offer in flight_offers:
|
| 40 |
+
try:
|
| 41 |
+
# Validate price breakdown structure
|
| 42 |
+
if ('priceBreakdown' not in offer or
|
| 43 |
+
'total' not in offer['priceBreakdown'] or
|
| 44 |
+
'units' not in offer['priceBreakdown']['total']):
|
| 45 |
+
print('a estrutura está correta')
|
| 46 |
+
continue
|
| 47 |
+
# Get total price in units
|
| 48 |
+
price = float(offer['priceBreakdown']['total']['units'])
|
| 49 |
+
# Add nanos (decimal part) if present
|
| 50 |
+
if 'nanos' in offer['priceBreakdown']['total']:
|
| 51 |
+
price += float(offer['priceBreakdown']['total']['nanos']) / 1000000000
|
| 52 |
+
# Update cheapest if current price is lower
|
| 53 |
+
if price < cheapest_price:
|
| 54 |
+
cheapest_price = price
|
| 55 |
+
cheapest_flight = offer
|
| 56 |
+
except (ValueError, TypeError):
|
| 57 |
+
continue
|
| 58 |
+
|
| 59 |
+
# Extract relevant info from cheapest flight
|
| 60 |
+
if cheapest_flight:
|
| 61 |
+
try:
|
| 62 |
+
result = {
|
| 63 |
+
'price': f"{cheapest_flight['priceBreakdown']['total'].get('currencyCode', 'USD')} {cheapest_price:.2f}",
|
| 64 |
+
'departure': {
|
| 65 |
+
'from': cheapest_flight['segments'][0]['departureAirport'].get('cityName', 'Unknown'),
|
| 66 |
+
'time': cheapest_flight['segments'][0].get('departureTime', 'Unknown')
|
| 67 |
+
},
|
| 68 |
+
'arrival': {
|
| 69 |
+
'to': cheapest_flight['segments'][0]['arrivalAirport'].get('cityName', 'Unknown'),
|
| 70 |
+
'time': cheapest_flight['segments'][0].get('arrivalTime', 'Unknown')
|
| 71 |
+
},
|
| 72 |
+
'airline': cheapest_flight['segments'][0]['legs'][0]['carriersData'][0].get('name', 'Unknown')
|
| 73 |
+
}
|
| 74 |
+
return result
|
| 75 |
+
except (KeyError, IndexError):
|
| 76 |
+
return None
|
| 77 |
+
return None
|
| 78 |
+
except Exception:
|
| 79 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|