Spaces:
Sleeping
Sleeping
Update tools/flight_status.py
Browse files- tools/flight_status.py +10 -17
tools/flight_status.py
CHANGED
@@ -5,10 +5,7 @@ import requests
|
|
5 |
class FlightStatus(Tool):
|
6 |
name = "flight_status"
|
7 |
description = "Fetches the status of a given flight based on the flight number using AviationStack API."
|
8 |
-
inputs = {
|
9 |
-
'flight': {'type': 'string', 'description': 'Flight Code to fetch the information.'},
|
10 |
-
'api_key': {'type': 'string', 'description': 'The API key for accessing the AviationStack data'}
|
11 |
-
}
|
12 |
output_type = {'flight_info': {'type': 'dict', 'description': 'A dictionary containing the flight status information or an error message'}}
|
13 |
|
14 |
def forward(self) -> dict:
|
@@ -17,8 +14,7 @@ class FlightStatus(Tool):
|
|
17 |
Returns a dictionary containing flight status or error details.
|
18 |
"""
|
19 |
flight_number = self.inputs.get('flight')
|
20 |
-
|
21 |
-
api_key = ""
|
22 |
|
23 |
# Ensure flight number and API key are provided
|
24 |
if not flight_number or not api_key:
|
@@ -34,20 +30,17 @@ class FlightStatus(Tool):
|
|
34 |
|
35 |
try:
|
36 |
# Send a GET request to the AviationStack API
|
37 |
-
|
38 |
-
response = 200
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
if response == 200:
|
50 |
-
flight_data = {'flight_date': '2025-02-16', 'flight_status': 'scheduled', 'departure': {'airport': 'John F Kennedy International', 'timezone': 'America/New_York', 'iata': 'JFK', 'icao': 'KJFK', 'terminal': '8', 'gate': '4', 'delay': None, 'scheduled': '2025-02-16T19:00:00+00:00', 'estimated': '2025-02-16T19:00:00+00:00', 'actual': None, 'estimated_runway': None, 'actual_runway': None}, 'arrival': {'airport': 'Heathrow', 'timezone': 'Europe/London', 'iata': 'LHR', 'icao': 'EGLL', 'terminal': '3', 'gate': None, 'baggage': None, 'delay': None, 'scheduled': '2025-02-17T06:50:00+00:00', 'estimated': '2025-02-17T06:50:00+00:00', 'actual': None, 'estimated_runway': None, 'actual_runway': None}, 'airline': {'name': 'American Airlines', 'iata': 'AA', 'icao': 'AAL'}, 'flight': {'number': '100', 'iata': 'AA100', 'icao': 'AAL100', 'codeshared': None}, 'aircraft': None, 'live': None}
|
51 |
|
52 |
# Extract relevant flight information
|
53 |
flight_info = {
|
|
|
5 |
class FlightStatus(Tool):
|
6 |
name = "flight_status"
|
7 |
description = "Fetches the status of a given flight based on the flight number using AviationStack API."
|
8 |
+
inputs = {'flight': {'type': 'string', 'description': 'Flight Code to fetch the information.'}}
|
|
|
|
|
|
|
9 |
output_type = {'flight_info': {'type': 'dict', 'description': 'A dictionary containing the flight status information or an error message'}}
|
10 |
|
11 |
def forward(self) -> dict:
|
|
|
14 |
Returns a dictionary containing flight status or error details.
|
15 |
"""
|
16 |
flight_number = self.inputs.get('flight')
|
17 |
+
api_key = "6ffd5cb6e366ed0cf24a1caa38b17c31"
|
|
|
18 |
|
19 |
# Ensure flight number and API key are provided
|
20 |
if not flight_number or not api_key:
|
|
|
30 |
|
31 |
try:
|
32 |
# Send a GET request to the AviationStack API
|
33 |
+
response = requests.get(base_url, params=params)
|
|
|
34 |
|
35 |
+
Check if the request was successful
|
36 |
+
if response.status_code == 200:
|
37 |
+
data = response.json()
|
38 |
|
39 |
+
# If no flight data is found
|
40 |
+
if not data.get("data"):
|
41 |
+
return {'error': f"No information available for flight {flight_number}."}
|
42 |
|
43 |
+
flight_data = data['data'][0] # Get the first flight result
|
|
|
|
|
44 |
|
45 |
# Extract relevant flight information
|
46 |
flight_info = {
|