diff --git a/python/FlightRadar24/__init__.py b/python/FlightRadar24/__init__.py index 2187947..2e996d2 100644 --- a/python/FlightRadar24/__init__.py +++ b/python/FlightRadar24/__init__.py @@ -13,7 +13,7 @@ """ __author__ = "Jean Loui Bernard Silva de Jesus" -__version__ = "1.3.17" +__version__ = "1.3.19" from .api import FlightRadar24API, FlightTrackerConfig from .entities import Airport, Entity, Flight diff --git a/python/FlightRadar24/api.py b/python/FlightRadar24/api.py index a94af1b..2ed4a63 100644 --- a/python/FlightRadar24/api.py +++ b/python/FlightRadar24/api.py @@ -88,6 +88,9 @@ def get_airport(self, code: str, *, details: bool = False) -> Airport: :param code: ICAO or IATA of the airport :param details: If True, it returns an Airport instance with detailed information. """ + if 4 < len(code) or len(code) < 3: + raise ValueError(f"The code '{code}' is invalid. It must be the IATA or ICAO of the airport.") + if details: airport = Airport() @@ -100,9 +103,9 @@ def get_airport(self, code: str, *, details: bool = False) -> Airport: content = response.get_content() if not content or not isinstance(content, dict) or not content.get("details"): - raise AirportNotFoundError(f"Could not find an airport by the code '{code}'."); + raise AirportNotFoundError(f"Could not find an airport by the code '{code}'.") - return Airport(info=content["details"]) + return Airport(info = content["details"]) def get_airport_details(self, code: str, flight_limit: int = 100, page: int = 1) -> Dict: """ @@ -112,6 +115,9 @@ def get_airport_details(self, code: str, flight_limit: int = 100, page: int = 1) :param flight_limit: Limit of flights related to the airport :param page: Page of result to display """ + if 4 < len(code) or len(code) < 3: + raise ValueError(f"The code '{code}' is invalid. It must be the IATA or ICAO of the airport.") + request_params = {"format": "json"} if self.__login_data is not None: @@ -126,10 +132,24 @@ def get_airport_details(self, code: str, flight_limit: int = 100, page: int = 1) response = APIRequest(Core.api_airport_data_url, request_params, Core.json_headers, exclude_status_codes=[400,]) content: Dict = response.get_content() - if response.get_status_code() == 400 and isinstance(content, dict) and content.get("errors"): - raise ValueError(content["errors"]["errors"]["parameters"]["limit"]["notBetween"]) + if response.get_status_code() == 400 and content.get("errors"): + errors = content["errors"]["errors"]["parameters"] + + if errors.get("limit"): + raise ValueError(errors["limit"]["notBetween"]) + + raise AirportNotFoundError(f"Could not find an airport by the code '{code}'.", errors) + + result = content["result"]["response"] + + # Check whether it received data of an airport. + data = result.get("airport", dict()).get("pluginData", dict()) - return content["result"]["response"] + if not "details" in data and len(data.get("runways", [])) == 0 and len(data) <= 3: + raise AirportNotFoundError(f"Could not find an airport by the code '{code}'.") + + # Return the airport details. + return result def get_airports(self) -> List[Airport]: """