Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1129,29 +1129,31 @@ def fetch_google_hotels(query="Birmingham Hotel", check_in="2024-08-14", check_o
|
|
1129 |
|
1130 |
|
1131 |
def fetch_google_flights(departure_id="JFK", arrival_id="BHM", outbound_date="2024-08-14", return_date="2024-08-20"):
|
|
|
|
|
1132 |
params = {
|
1133 |
-
|
1134 |
-
|
1135 |
-
|
1136 |
-
|
1137 |
-
|
1138 |
-
|
1139 |
-
|
1140 |
-
|
1141 |
}
|
1142 |
|
1143 |
search = GoogleSearch(params)
|
1144 |
results = search.get_dict()
|
1145 |
|
1146 |
-
|
1147 |
best_flights = results.get('best_flights', [])
|
|
|
1148 |
|
1149 |
-
|
1150 |
-
|
1151 |
-
|
1152 |
-
continue
|
1153 |
|
1154 |
-
for segment in
|
1155 |
departure_airport = segment.get('departure_airport', {}).get('name', 'Unknown Departure Airport')
|
1156 |
departure_time = segment.get('departure_airport', {}).get('time', 'Unknown Time')
|
1157 |
arrival_airport = segment.get('arrival_airport', {}).get('name', 'Unknown Arrival Airport')
|
@@ -1159,15 +1161,21 @@ def fetch_google_flights(departure_id="JFK", arrival_id="BHM", outbound_date="20
|
|
1159 |
duration = segment.get('duration', 'Unknown Duration')
|
1160 |
airplane = segment.get('airplane', 'Unknown Airplane')
|
1161 |
|
1162 |
-
|
1163 |
-
flight_info += f" -
|
1164 |
-
flight_info += f" -
|
1165 |
-
flight_info += f" -
|
1166 |
-
flight_info += f" -
|
1167 |
|
1168 |
-
flight_info
|
|
|
|
|
|
|
|
|
|
|
|
|
1169 |
|
1170 |
-
return
|
1171 |
|
1172 |
|
1173 |
|
|
|
1129 |
|
1130 |
|
1131 |
def fetch_google_flights(departure_id="JFK", arrival_id="BHM", outbound_date="2024-08-14", return_date="2024-08-20"):
|
1132 |
+
from serpapi import GoogleSearch
|
1133 |
+
|
1134 |
params = {
|
1135 |
+
"engine": "google_flights",
|
1136 |
+
"departure_id": "PEK",
|
1137 |
+
"arrival_id": "AUS",
|
1138 |
+
"outbound_date": "2024-08-14",
|
1139 |
+
"return_date": "2024-08-20",
|
1140 |
+
"currency": "USD",
|
1141 |
+
"hl": "en",
|
1142 |
+
"api_key": os.getenv("SERP_API") # Replace with your actual API key
|
1143 |
}
|
1144 |
|
1145 |
search = GoogleSearch(params)
|
1146 |
results = search.get_dict()
|
1147 |
|
1148 |
+
# Extract flight details from the results
|
1149 |
best_flights = results.get('best_flights', [])
|
1150 |
+
formatted_flights = []
|
1151 |
|
1152 |
+
# Process each flight in the best_flights list
|
1153 |
+
for i, flight in enumerate(best_flights, start=1):
|
1154 |
+
flight_info = f"Flight {i}:\n"
|
|
|
1155 |
|
1156 |
+
for segment in flight.get('flights', []):
|
1157 |
departure_airport = segment.get('departure_airport', {}).get('name', 'Unknown Departure Airport')
|
1158 |
departure_time = segment.get('departure_airport', {}).get('time', 'Unknown Time')
|
1159 |
arrival_airport = segment.get('arrival_airport', {}).get('name', 'Unknown Arrival Airport')
|
|
|
1161 |
duration = segment.get('duration', 'Unknown Duration')
|
1162 |
airplane = segment.get('airplane', 'Unknown Airplane')
|
1163 |
|
1164 |
+
# Format the flight segment details
|
1165 |
+
flight_info += f" - Departure: {departure_airport} at {departure_time}\n"
|
1166 |
+
flight_info += f" - Arrival: {arrival_airport} at {arrival_time}\n"
|
1167 |
+
flight_info += f" - Duration: {duration} minutes\n"
|
1168 |
+
flight_info += f" - Airplane: {airplane}\n"
|
1169 |
|
1170 |
+
formatted_flights.append(flight_info)
|
1171 |
+
|
1172 |
+
# Combine the formatted flight information into a single output
|
1173 |
+
final_output = "Here are some available flights for today:\n\n" + "\n".join(formatted_flights)
|
1174 |
+
|
1175 |
+
|
1176 |
+
|
1177 |
|
1178 |
+
return final_output
|
1179 |
|
1180 |
|
1181 |
|