FlightRadarAPI 1.2.8__tar.gz → 1.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,4 +13,4 @@ https://www.flightradar24.com/terms-and-conditions
13
13
  """
14
14
 
15
15
  __author__ = "Jean Loui Bernard Silva de Jesus"
16
- __version__ = "1.2.8"
16
+ __version__ = "1.3.1"
@@ -0,0 +1,226 @@
1
+ # -*- coding: utf-8 -*-
2
+ from typing import Any, Dict, List, Optional, Tuple
3
+
4
+ from .core import Core
5
+ from .errors import LoginError
6
+ from .flight import Flight
7
+ from .request import APIRequest
8
+
9
+
10
+ class FlightRadar24API(object):
11
+ """
12
+ Main class of the FlightRadarAPI
13
+ """
14
+
15
+ def __init__(self, user: Optional[str] = None, password: Optional[str] = None):
16
+ self.__real_time_flight_tracker_config = {
17
+ "faa": "1",
18
+ "satellite": "1",
19
+ "mlat": "1",
20
+ "flarm": "1",
21
+ "adsb": "1",
22
+ "gnd": "1",
23
+ "air": "1",
24
+ "vehicles": "1",
25
+ "estimated": "1",
26
+ "maxage": "14400",
27
+ "gliders": "1",
28
+ "stats": "1",
29
+ "limit": "5000"
30
+ }
31
+ self.__login_data: Optional[Dict] = None
32
+
33
+ if user is not None and password is not None:
34
+ self.login(user, password)
35
+
36
+ def get_airlines(self) -> List[Dict]:
37
+ """
38
+ Return a list with all airlines.
39
+ """
40
+ response = APIRequest(Core.airlines_data_url, headers = Core.json_headers)
41
+ return response.get_content()["rows"]
42
+
43
+ def get_airline_logo(self, iata: str, icao: str) -> Optional[Tuple[bytes, str]]:
44
+ """
45
+ Download the logo of an airline from FlightRadar24 and return it as bytes.
46
+ """
47
+ first_logo_url = Core.airline_logo_url.format(iata, icao)
48
+
49
+ # Try to get the image by the first URL option.
50
+ response = APIRequest(first_logo_url, headers = Core.image_headers)
51
+ status_code = response.get_status_code()
52
+
53
+ if not str(status_code).startswith("4"):
54
+ return response.get_content(), first_logo_url.split(".")[-1]
55
+
56
+ # Get the image by the second airline logo URL.
57
+ second_logo_url = Core.alternative_airline_logo_url.format(icao)
58
+
59
+ response = APIRequest(second_logo_url, headers = Core.image_headers)
60
+ status_code = response.get_status_code()
61
+
62
+ if not str(status_code).startswith("4"):
63
+ return response.get_content(), second_logo_url.split(".")[-1]
64
+
65
+ def get_airport(self, code: str) -> Dict:
66
+ """
67
+ Return detailed information about an airport.
68
+
69
+ :param code: ICAO or IATA of the airport.
70
+ """
71
+ response = APIRequest(Core.airport_data_url.format(code), headers = Core.json_headers)
72
+ return response.get_content()["details"]
73
+
74
+ def get_airports(self) -> List[Dict]:
75
+ """
76
+ Return a list with all airports.
77
+ """
78
+ response = APIRequest(Core.airports_data_url, headers = Core.json_headers)
79
+ return response.get_content()["rows"]
80
+
81
+ def get_bounds(self, zone: Dict[str, float]) -> str:
82
+ """
83
+ Convert coordinate dictionary to a string "y1, y2, x1, x2".
84
+
85
+ :param zone: Dictionary containing the following keys: tl_y, tl_x, br_y, br_x
86
+ """
87
+ return "{},{},{},{}".format(zone["tl_y"], zone["br_y"], zone["tl_x"], zone["br_x"])
88
+
89
+ def get_country_flag(self, country: str) -> Optional[Tuple[bytes, str]]:
90
+ """
91
+ Download the flag of a country from FlightRadar24 and return it as bytes.
92
+
93
+ :param country: Country name
94
+ """
95
+ flag_url = Core.country_flag_url.format(country.lower().replace(" ", "-"))
96
+ headers = Core.image_headers.copy()
97
+
98
+ if "origin" in headers:
99
+ headers.pop("origin") # Does not work for this request.
100
+
101
+ response = APIRequest(flag_url, headers = headers)
102
+ status_code = response.get_status_code()
103
+
104
+ if not str(status_code).startswith("4"):
105
+ return response.get_content(), flag_url.split(".")[-1]
106
+
107
+ def get_flight_details(self, flight_id: str) -> Dict[Any, Any]:
108
+ """
109
+ Return the flight details from Data Live Flightradar24.
110
+ """
111
+ response = APIRequest(Core.flight_data_url.format(flight_id), headers = Core.json_headers)
112
+ return response.get_content()
113
+
114
+ def get_flights(self, airline: str = None, bounds: str = None, registration: str = None, aircraft_type: str = None) -> List[Flight]:
115
+ """
116
+ Return a list of flights. See more options at set_real_time_flight_tracker_config() method.
117
+
118
+ :param airline: the airline ICAO. Ex: "DAL"
119
+ :param bounds: coordinates (y1, y2 ,x1, x2). Ex: "75.78,-75.78,-427.56,427.56"
120
+ :param registration: aircraft registration
121
+ :param aircraft_type: aircraft model code. Ex: "B737"
122
+ """
123
+ request_params = self.__real_time_flight_tracker_config.copy()
124
+
125
+ # Insert the parameters "airline", "bounds", "reg",and "type" in the dictionary for the request.
126
+ if airline: request_params["airline"] = airline
127
+ if bounds: request_params["bounds"] = bounds.replace(",", "%2C")
128
+ if registration: request_params["reg"] = registration
129
+ if aircraft_type: request_params["type"] = aircraft_type
130
+
131
+ # Get all flights from Data Live Flightradar24.
132
+ response = APIRequest(Core.real_time_flight_tracker_data_url, request_params, Core.json_headers)
133
+ response = response.get_content()
134
+
135
+ flights = []
136
+
137
+ for flight_id, flight_info in response.items():
138
+
139
+ # Get flights only.
140
+ if flight_id[0].isnumeric():
141
+ flights.append(Flight(flight_id, flight_info))
142
+
143
+ return flights
144
+
145
+ def get_login_data(self) -> Dict[Any, Any]:
146
+ """
147
+ Return the user data.
148
+ """
149
+ if not self.is_logged_in():
150
+ raise LoginError("You must log in to your account.")
151
+
152
+ return self.__login_data["userData"].copy()
153
+
154
+ def get_real_time_flight_tracker_config(self) -> Dict[str, str]:
155
+ """
156
+ Return the current config of the real time flight tracker, used by get_flights() method.
157
+ """
158
+ return self.__real_time_flight_tracker_config.copy()
159
+
160
+ def get_zones(self) -> Dict[str, Dict]:
161
+ """
162
+ Returns all major zones on the globe.
163
+ """
164
+ response = APIRequest(Core.zones_data_url, headers = Core.json_headers)
165
+ zones = response.get_content()
166
+
167
+ if "version" in zones:
168
+ zones.pop("version")
169
+
170
+ return zones
171
+
172
+ def is_logged_in(self) -> bool:
173
+ """
174
+ Check if the user is logged into the FlightRadar24 account.
175
+ """
176
+ return self.__login_data is not None
177
+
178
+ def login(self, user: str, password: str) -> None:
179
+ """
180
+ Log in to a FlightRadar24 account.
181
+
182
+ :param user: Your email.
183
+ :param password: Your password.
184
+ """
185
+ data = {
186
+ "email": user,
187
+ "password": password,
188
+ "remember": "true",
189
+ "type": "web"
190
+ }
191
+
192
+ response = APIRequest(Core.user_login_url, headers = Core.json_headers, data = data)
193
+ status_code = response.get_status_code()
194
+ content = response.get_content()
195
+
196
+ if not str(status_code).startswith("2") or not content.get("success"):
197
+ if isinstance(content, dict): raise LoginError(content["message"])
198
+ else: raise LoginError("Your email or password is incorrect")
199
+
200
+ self.__real_time_flight_tracker_config["enc"] = response.get_cookie("_frPl")
201
+
202
+ self.__login_data = {
203
+ "userData": content["userData"],
204
+ "cookies": response.get_cookies()
205
+ }
206
+
207
+ def logout(self) -> None:
208
+ """
209
+ Log out of the Flightradar24 account.
210
+ """
211
+ if not self.__login_data: return
212
+
213
+ APIRequest(Core.user_login_url, headers = Core.json_headers, cookies = self.__login_data["cookies"])
214
+
215
+ self.__real_time_flight_tracker_config.pop("enc")
216
+ self.__login_data = None
217
+
218
+ def set_real_time_flight_tracker_config(self, **config: str) -> None:
219
+ """
220
+ Set config for the real time flight tracker, used by get_flights() method.
221
+ """
222
+ for key, value in config.items():
223
+
224
+ # Check if the parameter exists and if the value is numeric.
225
+ if key in self.__real_time_flight_tracker_config and value.isnumeric():
226
+ self.__real_time_flight_tracker_config[key] = value
@@ -13,6 +13,7 @@ class Core(ABC):
13
13
 
14
14
  # User login URL.
15
15
  user_login_url = flightradar_base_url + "/user/login"
16
+ user_logout_url = flightradar_base_url + "/user/logout"
16
17
 
17
18
  # Flights data URLs.
18
19
  real_time_flight_tracker_data_url = data_cloud_base_url + "/zones/fcgi/feed.js"
@@ -0,0 +1,2 @@
1
+ class LoginError(Exception):
2
+ pass
@@ -15,18 +15,19 @@ class APIRequest(object):
15
15
  "gzip": gzip.decompress
16
16
  }
17
17
 
18
- def __init__(self, url, params = {}, headers = {}, data = None):
18
+ def __init__(self, url, params = {}, headers = {}, data = None, cookies = None):
19
19
 
20
20
  self.url = url
21
21
  self.params = params
22
22
  self.headers = headers
23
23
  self.data = data
24
+ self.cookies = cookies
24
25
 
25
26
  if data is None:
26
27
  if params: url += "?" + "&".join(["{}={}".format(k, v) for k, v in params.items()])
27
- self.__response = requests.get(url, headers = headers)
28
+ self.__response = requests.get(url, headers = headers, cookies = cookies)
28
29
  else:
29
- self.__response = requests.post(url, data = data, headers = headers)
30
+ self.__response = requests.post(url, headers = headers, cookies = cookies, data = data)
30
31
 
31
32
  def get_content(self) -> Union[Dict, bytes]:
32
33
  content = self.__response.content
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: FlightRadarAPI
3
- Version: 1.2.8
3
+ Version: 1.3.1
4
4
  Summary: API for FlightRadar24
5
5
  Home-page: https://github.com/JeanExtreme002/FlightRadarAPI
6
6
  Author: Jean Loui Bernard Silva de Jesus
@@ -4,6 +4,7 @@ setup.py
4
4
  FlightRadar24/__init__.py
5
5
  FlightRadar24/api.py
6
6
  FlightRadar24/core.py
7
+ FlightRadar24/errors.py
7
8
  FlightRadar24/flight.py
8
9
  FlightRadar24/request.py
9
10
  FlightRadarAPI.egg-info/PKG-INFO
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: FlightRadarAPI
3
- Version: 1.2.8
3
+ Version: 1.3.1
4
4
  Summary: API for FlightRadar24
5
5
  Home-page: https://github.com/JeanExtreme002/FlightRadarAPI
6
6
  Author: Jean Loui Bernard Silva de Jesus
@@ -1,147 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- from .core import Core
4
- from .flight import Flight
5
- from .request import APIRequest
6
-
7
- from deprecated import deprecated
8
- from typing import Dict, List
9
-
10
-
11
- class FlightRadar24API(object):
12
- """
13
- Flight Radar 24 API
14
- """
15
-
16
- __real_time_flight_tracker_config = {
17
- "faa": "1",
18
- "satellite": "1",
19
- "mlat": "1",
20
- "flarm": "1",
21
- "adsb": "1",
22
- "gnd": "1",
23
- "air": "1",
24
- "vehicles": "1",
25
- "estimated": "1",
26
- "maxage": "14400",
27
- "gliders": "1",
28
- "stats": "1",
29
- "limit": "5000"
30
- }
31
-
32
- def login(self, user: str, password: str) -> Dict:
33
- # Log in with Flightradar24 Premium user credentials
34
- data = {
35
- "email": user,
36
- "password": password,
37
- "remember": "true",
38
- "type": "web"
39
- }
40
-
41
- request = APIRequest(Core.user_login_url, headers = Core.json_headers, data = data)
42
- self.__real_time_flight_tracker_config["enc"] = request.get_cookie("_frPl")
43
-
44
- return request.get_content()
45
-
46
- def get_airlines(self) -> List[Dict]:
47
- # Get the data from Flightradar24.
48
- request = APIRequest(Core.airlines_data_url, headers = Core.json_headers)
49
- return request.get_content()["rows"]
50
-
51
- def get_airline_logo(self, iata: str, icao: str) -> str:
52
- # Get the first airline logo URL.
53
- first_logo_url = Core.airline_logo_url.format(iata, icao)
54
-
55
- # Check if there was a problem with the request. If not, the URL is returned.
56
- first_status_code = APIRequest(first_logo_url, headers = Core.image_headers).get_status_code()
57
- if not str(first_status_code).startswith("4"): return first_logo_url
58
-
59
- # Get the second airline logo URL.
60
- second_logo_url = Core.alternative_airline_logo_url.format(icao)
61
-
62
- # Check if there was a problem with the request. If not, the URL is returned.
63
- second_status_code = APIRequest(second_logo_url, headers = Core.image_headers).get_status_code()
64
- if not str(second_status_code).startswith("4"): return second_logo_url
65
-
66
- def get_airport(self, code: str) -> Dict:
67
- # Get the airport data from Flightradar24.
68
- request = APIRequest(Core.airport_data_url.format(code), headers = Core.json_headers)
69
- return request.get_content()["details"]
70
-
71
- def get_airports(self) -> List[Dict]:
72
- # Get the airports data from Flightradar24.
73
- request = APIRequest(Core.airports_data_url, headers = Core.json_headers)
74
- return request.get_content()["rows"]
75
-
76
- def get_bounds(self, zone: Dict[str, float]) -> str:
77
- # Convert coordinate dictionary (tl_y, tl_x, br_y, br_x) to string "y1, y2, x1, x2".
78
- return "{},{},{},{}".format(zone["tl_y"], zone["br_y"] , zone["tl_x"], zone["br_x"])
79
-
80
- def get_country_flag(self, country: str) -> str:
81
- # Get the country flag image URL.
82
- flag_url = Core.country_flag_url.format(country.lower().replace(" ", "-"))
83
-
84
- headers = Core.image_headers.copy()
85
-
86
- if "origin" in headers:
87
- headers.pop("origin") # Doesn't work for this request
88
-
89
- # Check if there is a problem with the request. If not, the URL is returned.
90
- status_code = APIRequest(flag_url, headers = headers).get_status_code()
91
- if not str(status_code).startswith("4"): return flag_url
92
-
93
- def get_flight_details(self, flight_id: str) -> Dict:
94
- # Get the flight details from Data Live Flightradar24.
95
- request = APIRequest(Core.flight_data_url.format(flight_id), headers = Core.json_headers)
96
- return request.get_content()
97
-
98
- def get_flights(self, airline: str = None, bounds: str = None, registration: str = None, aircraft_type: str = None) -> List[Flight]:
99
- """
100
- :param airline: the airline ICAO. Ex: "DAL"
101
- :param bounds: coordinates (y1, y2 ,x1, x2). Ex: "75.78,-75.78,-427.56,427.56"
102
- :param registration: aircraft registration
103
- :param aircraft_type: aircraft model code. Ex: "B737"
104
- """
105
-
106
- request_params = self.__real_time_flight_tracker_config.copy()
107
-
108
- # Insert the parameters "airline", "bounds", "reg",and "type" in the dictionary for the request.
109
- if airline: request_params["airline"] = airline
110
- if bounds: request_params["bounds"] = bounds.replace(",", "%2C")
111
- if registration: request_params["reg"] = registration
112
- if aircraft_type: request_params["type"] = aircraft_type
113
-
114
- # Get all flights from Data Live Flightradar24.
115
- request = APIRequest(Core.real_time_flight_tracker_data_url, request_params, Core.json_headers)
116
- response = request.get_content()
117
-
118
- flights = []
119
-
120
- for flight_id, flight_info in response.items():
121
-
122
- # Get flights only.
123
- if flight_id[0].isnumeric():
124
- flights.append(Flight(flight_id, flight_info))
125
-
126
- return flights
127
-
128
- def get_real_time_flight_tracker_config(self) -> Dict[str, str]:
129
- return self.__real_time_flight_tracker_config.copy()
130
-
131
- def get_zones(self) -> Dict:
132
-
133
- # Get the zones data from Flightradar24.
134
- request = APIRequest(Core.zones_data_url, headers = Core.json_headers)
135
- zones = request.get_content()
136
-
137
- # Remove version information.
138
- zones.pop("version")
139
- return zones
140
-
141
- def set_real_time_flight_tracker_config(self, **config: str) -> None:
142
-
143
- for key, value in config.items():
144
-
145
- # Check if the parameter exists and if the value is numeric.
146
- if key in self.__real_time_flight_tracker_config and value.isnumeric():
147
- self.__real_time_flight_tracker_config[key] = value
File without changes
File without changes
File without changes
File without changes