Pijush2023 commited on
Commit
1a4224a
·
verified ·
1 Parent(s): b319975

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -21
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
- "engine": "google_flights",
1134
- "departure_id": departure_id,
1135
- "arrival_id": arrival_id,
1136
- "outbound_date": outbound_date,
1137
- "return_date": return_date,
1138
- "currency": "USD",
1139
- "hl": "en",
1140
- "api_key": os.getenv("SERP_API")
1141
  }
1142
 
1143
  search = GoogleSearch(params)
1144
  results = search.get_dict()
1145
 
1146
- flight_info = ""
1147
  best_flights = results.get('best_flights', [])
 
1148
 
1149
- for flight in best_flights[:5]: # Limiting to top 5 flights
1150
- flight_segments = flight.get('flights', [])
1151
- if not flight_segments:
1152
- continue
1153
 
1154
- for segment in flight_segments:
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
- flight_info += f"**Flight Segment:**\n"
1163
- flight_info += f" - **Departure:** {departure_airport} at {departure_time}\n"
1164
- flight_info += f" - **Arrival:** {arrival_airport} at {arrival_time}\n"
1165
- flight_info += f" - **Duration:** {duration} minutes\n"
1166
- flight_info += f" - **Airplane:** {airplane}\n"
1167
 
1168
- flight_info += "-" * 50 + "\n"
 
 
 
 
 
 
1169
 
1170
- return flight_info
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