| 1 | # see also https://github.com/openskynetwork/opensky-api | ||
| 2 | |||
| 3 | package OpenSky::API; | ||
| 4 | |||
| 5 | # ABSTRACT: Perl interface to the OpenSky Network API | ||
| 6 | |||
| 7 | our $VERSION = '0.002'; | ||
| 8 | use Moose; | ||
| 9 | use OpenSky::API::Types qw( | ||
| 10 | ArrayRef | ||
| 11 | Bool | ||
| 12 | Dict | ||
| 13 | HashRef | ||
| 14 | InstanceOf | ||
| 15 | Int | ||
| 16 | Latitude | ||
| 17 | Longitude | ||
| 18 | NonEmptyStr | ||
| 19 | Num | ||
| 20 | Optional | ||
| 21 | ); | ||
| 22 | use OpenSky::API::States; | ||
| 23 | use OpenSky::API::Flights; | ||
| 24 | use PerlX::Maybe; | ||
| 25 | use Config::INI::Reader; | ||
| 26 | use Carp qw( croak ); | ||
| 27 | |||
| 28 | use Mojo::UserAgent; | ||
| 29 | use Mojo::URL; | ||
| 30 | use Mojo::JSON qw( decode_json ); | ||
| 31 | use Type::Params -sigs; | ||
| 32 | use experimental qw( signatures ); | ||
| 33 | |||
| 34 | has config => ( | ||
| 35 | is => 'ro', | ||
| 36 | isa => NonEmptyStr, | ||
| 37 | default => sub ($self) { $ENV{HOME} . '/.openskyrc' }, | ||
| 38 | ); | ||
| 39 | |||
| 40 | has [qw/debug raw testing/] => ( | ||
| 41 | is => 'ro', | ||
| 42 | isa => Bool, | ||
| 43 | default => 0, | ||
| 44 | ); | ||
| 45 | |||
| 46 | has _config_data => ( | ||
| 47 | is => 'ro', | ||
| 48 | isa => HashRef, | ||
| 49 | init_arg => undef, | ||
| 50 | lazy => 1, | ||
| 51 | default => sub ($self) { | ||
| 52 | Config::INI::Reader->read_file( $self->config ); | ||
| 53 | }, | ||
| 54 | ); | ||
| 55 | |||
| 56 | has _ua => ( | ||
| 57 | is => 'ro', | ||
| 58 | isa => InstanceOf ['Mojo::UserAgent'], | ||
| 59 | init_arg => undef, | ||
| 60 | default => sub { Mojo::UserAgent->new }, | ||
| 61 | ); | ||
| 62 | |||
| 63 | has _username => ( | ||
| 64 | is => 'ro', | ||
| 65 | isa => NonEmptyStr, | ||
| 66 | lazy => 1, | ||
| 67 | init_arg => 'username', | ||
| 68 | default => sub ($self) { $ENV{OPENSKY_USERNAME} // $self->_config_data->{opensky}{username} }, | ||
| 69 | ); | ||
| 70 | |||
| 71 | has _password => ( | ||
| 72 | is => 'ro', | ||
| 73 | isa => NonEmptyStr, | ||
| 74 | lazy => 1, | ||
| 75 | init_arg => 'password', | ||
| 76 | default => sub ($self) { $ENV{OPENSKY_PASSWORD} // $self->_config_data->{opensky}{password} }, | ||
| 77 | ); | ||
| 78 | |||
| 79 | has _base_url => ( | ||
| 80 | is => 'ro', | ||
| 81 | init_arg => 'base_url', | ||
| 82 | isa => NonEmptyStr, | ||
| 83 | default => sub ($self) {'https://opensky-network.org/api'}, | ||
| 84 | ); | ||
| 85 | |||
| 86 | has limit_remaining => ( | ||
| 87 | is => 'ro', | ||
| 88 | isa => Int, | ||
| 89 | lazy => 1, | ||
| 90 | writer => '_set_limit_remaining', | ||
| 91 | default => sub ($self) { | ||
| 92 | return 4000 if $self->testing; | ||
| 93 | |||
| 94 | # per their documentation, | ||
| 95 | # https://openskynetwork.github.io/opensky-api/rest.html#api-credit-usage, | ||
| 96 | # this request should only cost one credit. However, it appears to | ||
| 97 | # cost three. | ||
| 98 | my %params = ( | ||
| 99 | lamin => 49.7, | ||
| 100 | lomin => 3.2, | ||
| 101 | lamax => 50.5, | ||
| 102 | lomax => 4.6, | ||
| 103 | ); | ||
| 104 | my $route = '/states/all'; | ||
| 105 | return $self->_get_response( route => $route, params => \%params, credits => 1 ); | ||
| 106 | }, | ||
| 107 | ); | ||
| 108 | |||
| 109 | signature_for get_states => ( | ||
| 110 | method => 1, | ||
| 111 | named => [ | ||
| 112 | time => Optional [Num], { default => 0 }, | ||
| 113 | icao24 => Optional [ NonEmptyStr | ArrayRef [NonEmptyStr] ], | ||
| 114 | bbox => Optional [ | ||
| 115 | Dict [ | ||
| 116 | lamin => Latitude, | ||
| 117 | lomin => Longitude, | ||
| 118 | lamax => Latitude, | ||
| 119 | lomax => Longitude, | ||
| 120 | ], | ||
| 121 | { default => {} }, | ||
| 122 | ], | ||
| 123 | extended => Optional [Bool], | ||
| 124 | ], | ||
| 125 | named_to_list => 1, | ||
| 126 | ); | ||
| 127 | |||
| 128 | sub get_states ( $self, $seconds, $icao24, $bbox, $extended ) { | ||
| 129 | my %params = ( | ||
| 130 | maybe time => $seconds, | ||
| 131 | maybe icao24 => $icao24, | ||
| 132 | maybe extended => $extended, | ||
| 133 | ); | ||
| 134 | if ( keys $bbox->%* ) { | ||
| 135 | $params{$_} = $bbox->{$_} for qw( lamin lomin lamax lomax ); | ||
| 136 | } | ||
| 137 | |||
| 138 | my $route = '/states/all'; | ||
| 139 | my $response = $self->_get_response( route => $route, params => \%params, no_auth_required => 1 ) // { | ||
| 140 | time => time - ( $seconds // 0 ), | ||
| 141 | states => [], | ||
| 142 | }; | ||
| 143 | if ( $self->raw ) { | ||
| 144 | return $response; | ||
| 145 | } | ||
| 146 | return OpenSky::API::States->new($response); | ||
| 147 | } | ||
| 148 | |||
| 149 | signature_for get_my_states => ( | ||
| 150 | method => 1, | ||
| 151 | named => [ | ||
| 152 | time => Optional [Num], { default => 0 }, | ||
| 153 | icao24 => Optional [ NonEmptyStr | ArrayRef [NonEmptyStr] ], | ||
| 154 | serials => Optional [ NonEmptyStr | ArrayRef [NonEmptyStr] ], | ||
| 155 | ], | ||
| 156 | named_to_list => 1, | ||
| 157 | ); | ||
| 158 | |||
| 159 | sub get_my_states ( $self, $seconds, $icao24, $serials ) { | ||
| 160 | my %params = ( | ||
| 161 | extended => 1, | ||
| 162 | maybe time => $seconds, | ||
| 163 | maybe icao24 => $icao24, | ||
| 164 | maybe serials => $serials, | ||
| 165 | ); | ||
| 166 | |||
| 167 | my $route = '/states/own'; | ||
| 168 | my $response = $self->_get_response( route => $route, params => \%params ); | ||
| 169 | if ( $self->raw ) { | ||
| 170 | return $response; | ||
| 171 | } | ||
| 172 | return OpenSky::API::States->new($response); | ||
| 173 | } | ||
| 174 | |||
| 175 | sub get_flights_from_interval ( $self, $begin, $end ) { | ||
| 176 | if ( $begin >= $end ) { | ||
| 177 | croak 'The end time must be greater than or equal to the start time.'; | ||
| 178 | } | ||
| 179 | if ( ( $end - $begin ) > 7200 ) { | ||
| 180 | croak 'The time interval must be smaller than two hours.'; | ||
| 181 | } | ||
| 182 | |||
| 183 | my %params = ( begin => $begin, end => $end ); | ||
| 184 | my $route = '/flights/all'; | ||
| 185 | my $response = $self->_get_response( route => $route, params => \%params ) // []; | ||
| 186 | |||
| 187 | if ( $self->raw ) { | ||
| 188 | return $response; | ||
| 189 | } | ||
| 190 | return OpenSky::API::Flights->new($response); | ||
| 191 | } | ||
| 192 | |||
| 193 | sub get_flights_by_aircraft ( $self, $icao24, $begin, $end ) { | ||
| 194 | if ( $begin >= $end ) { | ||
| 195 | croak 'The end time must be greater than or equal to the start time.'; | ||
| 196 | } | ||
| 197 | if ( ( $end - $begin ) > 2592 * 1e3 ) { | ||
| 198 | croak 'The time interval must be smaller than 30 days.'; | ||
| 199 | } | ||
| 200 | |||
| 201 | my %params = ( icao24 => $icao24, begin => $begin, end => $end ); | ||
| 202 | my $route = '/flights/aircraft'; | ||
| 203 | my $response = $self->_get_response( route => $route, params => \%params ) // []; | ||
| 204 | |||
| 205 | if ( $self->raw ) { | ||
| 206 | return $response; | ||
| 207 | } | ||
| 208 | return OpenSky::API::Flights->new($response); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub get_arrivals_by_airport ( $self, $airport, $begin, $end ) { | ||
| 212 | if ( $begin >= $end ) { | ||
| 213 | croak 'The end time must be greater than or equal to the start time.'; | ||
| 214 | } | ||
| 215 | if ( ( $end - $begin ) > 604800 ) { | ||
| 216 | croak 'The time interval must be smaller than 7 days.'; | ||
| 217 | } | ||
| 218 | |||
| 219 | my %params = ( airport => $airport, begin => $begin, end => $end ); | ||
| 220 | my $route = '/flights/arrival'; | ||
| 221 | my $response = $self->_get_response( route => $route, params => \%params ) // []; | ||
| 222 | |||
| 223 | if ( $self->raw ) { | ||
| 224 | return $response; | ||
| 225 | } | ||
| 226 | return OpenSky::API::Flights->new($response); | ||
| 227 | } | ||
| 228 | |||
| 229 | sub get_departures_by_airport ( $self, $airport, $begin, $end ) { | ||
| 230 | if ( $begin >= $end ) { | ||
| 231 | croak 'The end time must be greater than or equal to the start time.'; | ||
| 232 | } | ||
| 233 | if ( ( $end - $begin ) > 604800 ) { | ||
| 234 | croak 'The time interval must be smaller than 7 days.'; | ||
| 235 | } | ||
| 236 | |||
| 237 | my %params = ( airport => $airport, begin => $begin, end => $end ); | ||
| 238 | my $route = '/flights/departure'; | ||
| 239 | my $response = $self->_get_response( route => $route, params => \%params ) // []; | ||
| 240 | |||
| 241 | if ( $self->raw ) { | ||
| 242 | return $response; | ||
| 243 | } | ||
| 244 | return OpenSky::API::Flights->new($response); | ||
| 245 | } | ||
| 246 | |||
| 247 | signature_for _get_response => ( | ||
| 248 | method => 1, | ||
| 249 | named => [ | ||
| 250 | route => NonEmptyStr, | ||
| 251 | params => Optional [HashRef], | ||
| 252 | credits => Optional [Bool], | ||
| 253 | no_auth_required => Optional [Bool], | ||
| 254 | ], | ||
| 255 | named_to_list => 1, | ||
| 256 | ); | ||
| 257 | |||
| 258 | sub _get_response ( $self, $route, $params, $credits, $no_auth_required ) { | ||
| 259 | my $url = $self->_url( $route, $params, $no_auth_required ); | ||
| 260 | |||
| 261 | my $response = $self->_GET($url); | ||
| 262 | my $remaining = $response->headers->header('X-Rate-Limit-Remaining'); | ||
| 263 | |||
| 264 | $self->_debug( $response->headers->to_string . "\n" ); | ||
| 265 | |||
| 266 | # not all requests cost credits, so we only want to set the limit if | ||
| 267 | # $remaining is defined | ||
| 268 | $self->_set_limit_remaining($remaining) if !$credits && defined $remaining; | ||
| 269 | if ( !$response->is_success ) { | ||
| 270 | if ( $response->code == 404 ) { | ||
| 271 | |||
| 272 | # this is annoying. If the didn't match any criteria, return a 200 | ||
| 273 | # and and empty element. Instead, we get a 404. | ||
| 274 | return; | ||
| 275 | } | ||
| 276 | croak $response->to_string; | ||
| 277 | } | ||
| 278 | return $remaining if $credits; | ||
| 279 | if ( $self->debug ) { | ||
| 280 | $self->_debug( $response->body ); | ||
| 281 | } | ||
| 282 | return decode_json( $response->body ); | ||
| 283 | } | ||
| 284 | |||
| 285 | # an easy target to override for testing | ||
| 286 | sub _GET ( $self, $url ) { | ||
| 287 | $self->_debug("GET $url\n"); | ||
| 288 | return $self->_ua->get($url)->res; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub _debug ( $self, $msg ) { | ||
| 292 | return if !$self->debug; | ||
| 293 | say STDERR $msg; | ||
| 294 | } | ||
| 295 | |||
| 296 | sub _url ( $self, $url, $params = {}, $no_auth_required = 0 ) { | ||
| 297 | my $username = $self->_username; | ||
| 298 | my $password = $self->_password; | ||
| 299 | if ( ( !$username || !$password ) && $no_auth_required ) { | ||
| 300 | return Mojo::URL->new( $self->_base_url . $url )->query($params); | ||
| 301 | } | ||
| 302 | $url = Mojo::URL->new( $self->_base_url . $url )->userinfo( $self->_username . ':' . $self->_password ); | ||
| 303 | $url->query($params); | ||
| 304 | return $url; | ||
| 305 | } | ||
| 306 | |||
| 307 | __PACKAGE__->meta->make_immutable; | ||
| 308 | |||
| 309 | __END__ | ||
| 310 | |||
| 311 | =pod | ||
| 312 | |||
| 313 | =encoding UTF-8 | ||
| 314 | |||
| 315 | =head1 NAME | ||
| 316 | |||
| 317 | OpenSky::API - Perl interface to the OpenSky Network API | ||
| 318 | |||
| 319 | =head1 VERSION | ||
| 320 | |||
| 321 | version 0.002 | ||
| 322 | |||
| 323 | =head1 SYNOPSIS | ||
| 324 | |||
| 325 | use OpenSky::API; | ||
| 326 | |||
| 327 | my $api = OpenSky::API->new( | ||
| 328 | username => 'username', | ||
| 329 | password => 'password', | ||
| 330 | ); | ||
| 331 | |||
| 332 | my $states = $api->get_states; | ||
| 333 | while ( my $vector = $states->next ) { | ||
| 334 | say $vector->callsign; | ||
| 335 | } | ||
| 336 | |||
| 337 | =head1 DESCRIPTION | ||
| 338 | |||
| 339 | This is a Perl interface to the OpenSky Network API. It provides a simple, object-oriented | ||
| 340 | interface, but also allows you to fetch raw results for performance. | ||
| 341 | |||
| 342 | This is largely based on L<the official Python | ||
| 343 | implementation|https://github.com/openskynetwork/opensky-api/blob/master/python/opensky_api.py>, | ||
| 344 | but with some changes to make it more user-friendly for Perl developers. | ||
| 345 | |||
| 346 | =head1 CONSTRUCTOR | ||
| 347 | |||
| 348 | Basic usage: | ||
| 349 | |||
| 350 | my $open_sky = OpenSky::API->new; | ||
| 351 | |||
| 352 | This will create an instance of the API object with no authentication. This only allows you access | ||
| 353 | to the C<get_states> method. | ||
| 354 | |||
| 355 | If you want to use the other methods, you will need to provide a username and password: | ||
| 356 | |||
| 357 | my $open_sky = OpenSky::API->new( | ||
| 358 | username => 'username', | ||
| 359 | password => 'password', | ||
| 360 | ); | ||
| 361 | |||
| 362 | Alterantively, you can set the C<OPENSKY_USERNAME> and C<OPENSKY_PASSWORD> | ||
| 363 | environment variables, or create a C<.openskyrc> file in your home directory | ||
| 364 | with the following contents: | ||
| 365 | |||
| 366 | [opensky] | ||
| 367 | username = myusername | ||
| 368 | password = s3cr3t | ||
| 369 | |||
| 370 | By default, all methods return objects. If you want to get the raw results, you can set the C<raw> | ||
| 371 | attribute in the constructor: | ||
| 372 | |||
| 373 | my $open_sky = OpenSky::API->new( | ||
| 374 | raw => 1, | ||
| 375 | ); | ||
| 376 | |||
| 377 | If you are debugging why something failed, pass the C<debug> attribute: | ||
| 378 | |||
| 379 | my $open_sky = OpenSky::API->new( | ||
| 380 | debug => 1, | ||
| 381 | ); | ||
| 382 | |||
| 383 | =head1 METHODS | ||
| 384 | |||
| 385 | For more insight to all methods, see L<the OpenSky API | ||
| 386 | documentation|https://openskynetwork.github.io/opensky-api/>. | ||
| 387 | |||
| 388 | =head2 get_states | ||
| 389 | |||
| 390 | my $states = $api->get_states; | ||
| 391 | |||
| 392 | Returns an instance of L<OpenSky::API::States>. if C<< raw => 1 >> was passed | ||
| 393 | to the constructor, this will be the raw data structure instead. | ||
| 394 | |||
| 395 | This API call can be used to retrieve any state vector of the | ||
| 396 | OpenSky. Please note that rate limits apply for this call. For API calls | ||
| 397 | without rate limitation, see C<get_my_states>. | ||
| 398 | |||
| 399 | By default, the above fetches all current state vectors. | ||
| 400 | |||
| 401 | You can (optionally) request state vectors for particular airplanes or times using the following request parameters: | ||
| 402 | |||
| 403 | my $states = $api->get_states( | ||
| 404 | icao24 => 'abc9f3', | ||
| 405 | time => 1517258400, | ||
| 406 | ); | ||
| 407 | |||
| 408 | Both parameters are optional. | ||
| 409 | |||
| 410 | =over 4 | ||
| 411 | |||
| 412 | =item * C<icao24> | ||
| 413 | |||
| 414 | One or more ICAO24 transponder addresses represented by a hex string (e.g. abc9f3). To filter multiple ICAO24 append the property once for each address. If omitted, the state vectors of all aircraft are returned. | ||
| 415 | |||
| 416 | =item * C<time> | ||
| 417 | |||
| 418 | A Unix timestamp (seconds since epoch). Only state vectors after this timestamp are returned. | ||
| 419 | |||
| 420 | =back | ||
| 421 | |||
| 422 | In addition to that, it is possible to query a certain area defined by a | ||
| 423 | bounding box of WGS84 coordinates. For this purpose, add the following | ||
| 424 | parameters: | ||
| 425 | |||
| 426 | my $states = $api->get_states( | ||
| 427 | bbox => { | ||
| 428 | lomin => -0.5, # lower bound for the longitude in decimal degrees | ||
| 429 | lamin => 51.25, # lower bound for the latitude in decimal degrees | ||
| 430 | lomax => 0, # upper bound for the longitude in decimal degrees | ||
| 431 | lamax => 51.75, # upper bound for the latitude in decimal degrees | ||
| 432 | }, | ||
| 433 | ); | ||
| 434 | |||
| 435 | You can also request the category of aircraft by adding the following request parameter: | ||
| 436 | |||
| 437 | my $states = $api->get_states( | ||
| 438 | extended => 1, | ||
| 439 | ); | ||
| 440 | |||
| 441 | Any and all of the above parameters can be combined. | ||
| 442 | |||
| 443 | my $states = $api->get_states( | ||
| 444 | icao24 => 'abc9f3', | ||
| 445 | time => 1517258400, | ||
| 446 | bbox => { | ||
| 447 | lomin => -0.5, # lower bound for the longitude in decimal degrees | ||
| 448 | lamin => 51.25, # lower bound for the latitude in decimal degrees | ||
| 449 | lomax => 0, # upper bound for the longitude in decimal degrees | ||
| 450 | lamax => 51.75, # upper bound for the latitude in decimal degrees | ||
| 451 | }, | ||
| 452 | extended => 1, | ||
| 453 | ); | ||
| 454 | |||
| 455 | =head2 get_my_states | ||
| 456 | |||
| 457 | my $states = $api->get_my_states; | ||
| 458 | |||
| 459 | Returns an instance of L<OpenSky::API::States>. if C<< raw => 1 >> was passed, | ||
| 460 | this will be the raw data structure instead. | ||
| 461 | |||
| 462 | This API call can be used to retrieve state vectors for your own | ||
| 463 | sensors without rate limitations. Note that authentication is required for | ||
| 464 | this operation, otherwise you will get a 403 - Forbidden. | ||
| 465 | |||
| 466 | By default, the above fetches all current state vectors for your states. However, you can also pass | ||
| 467 | arguments to fine-tune this: | ||
| 468 | |||
| 469 | my $states = $api->get_my_states( | ||
| 470 | time => 1517258400, | ||
| 471 | icao24 => 'abc9f3', | ||
| 472 | serials => [ 1234, 5678 ], | ||
| 473 | ); | ||
| 474 | |||
| 475 | =over 4 | ||
| 476 | |||
| 477 | =item * C<time> | ||
| 478 | |||
| 479 | The time in seconds since epoch (Unix timestamp to retrieve states for. Current time will be used if omitted. | ||
| 480 | |||
| 481 | =item * <icao24> | ||
| 482 | |||
| 483 | One or more ICAO24 transponder addresses represented by a hex string (e.g. | ||
| 484 | abc9f3). To filter multiple ICAO24 append the property once for each address. | ||
| 485 | If omitted, the state vectors of all aircraft are returned. | ||
| 486 | |||
| 487 | =item * C<serials> | ||
| 488 | |||
| 489 | Retrieve only states of a subset of your receivers. You can pass this argument | ||
| 490 | several time to filter state of more than one of your receivers. In this case, | ||
| 491 | the API returns all states of aircraft that are visible to at least one of the | ||
| 492 | given receivers. | ||
| 493 | |||
| 494 | =back | ||
| 495 | |||
| 496 | =head2 C<get_arrivals_by_airport> | ||
| 497 | |||
| 498 | my $arrivals = $api->get_arrivals_by_airport('KJFK', $start, $end); | ||
| 499 | |||
| 500 | Returns an instance of L<OpenSky::API::Flights>. if C<< raw => 1 >> was | ||
| 501 | passed, you will get the raw data structure instead. | ||
| 502 | |||
| 503 | Positional arguments: | ||
| 504 | |||
| 505 | =over 4 | ||
| 506 | |||
| 507 | =item * C<airport> | ||
| 508 | |||
| 509 | The ICAO code of the airport you want to get arrivals for. | ||
| 510 | |||
| 511 | =item * C<start> | ||
| 512 | |||
| 513 | The start time in seconds since epoch (Unix timestamp). | ||
| 514 | |||
| 515 | =item * C<end> | ||
| 516 | |||
| 517 | The end time in seconds since epoch (Unix timestamp). | ||
| 518 | |||
| 519 | =back | ||
| 520 | |||
| 521 | The interval between start and end time must be smaller than seven days. | ||
| 522 | |||
| 523 | =head2 C<get_departures_by_airport> | ||
| 524 | |||
| 525 | Identical to C<get_arrivals_by_airport>, but returns departures instead of arrivals. | ||
| 526 | |||
| 527 | =head2 C<get_flights_by_aircraft> | ||
| 528 | |||
| 529 | my $flights = $api->get_flights_by_aircraft('abc9f3', $start, $end); | ||
| 530 | |||
| 531 | Returns an instance of L<OpenSky::API::Flights>. if C<< raw => 1 >> was passed | ||
| 532 | to the constructor, you will get the raw data structure instead. | ||
| 533 | |||
| 534 | The first argument is the ICAO24 transponder address of the aircraft you want. | ||
| 535 | |||
| 536 | =head2 C<get_flights_from_interval> | ||
| 537 | |||
| 538 | my $flights = $api->get_flights_from_interval($start, $end); | ||
| 539 | |||
| 540 | Returns an instance of L<OpenSky::API::Flights>. if C<< raw => 1 >> was passed | ||
| 541 | to the constructor, you will get the raw data structure instead. | ||
| 542 | |||
| 543 | =head1 EXAMPLES | ||
| 544 | |||
| 545 | Perl Wikipedia, L<OpenSky Network|https://en.wikipedia.org/wiki/OpenSky_Network> is ... | ||
| 546 | |||
| 547 | The OpenSky Network is a non-profit association based in Switzerland that | ||
| 548 | provides open access of flight tracking control data. It was set up as | ||
| 549 | a research project by several universities and government entities with | ||
| 550 | the goal to improve the security, reliability and efficiency of the | ||
| 551 | airspace. Its main function is to collect, process and store air traffic | ||
| 552 | control data and provide open access to this data to the public. Similar | ||
| 553 | to many existing flight trackers such as Flightradar24 and FlightAware, | ||
| 554 | the OpenSky Network consists of a multitude of sensors (currently around | ||
| 555 | 1000, mostly concentrated in Europe and the US), which are connected to | ||
| 556 | the Internet by volunteers, industrial supporters, academic, and | ||
| 557 | governmental organizations. All collected raw data is archived in a | ||
| 558 | large historical database, containing over 23 trillion air traffic control | ||
| 559 | messages (November 2020). The database is primarily used by researchers | ||
| 560 | from different areas to analyze and improve air traffic control | ||
| 561 | technologies and processes | ||
| 562 | |||
| 563 | =head2 Elon Musk's Jet | ||
| 564 | |||
| 565 | However, this data can be used to track the movements of certain aircraft. For | ||
| 566 | example, Elon Musk's primary private jet (he has three, but this is the one he | ||
| 567 | mainly uses), has the ICAO24 transponder address C<a835af>. Running the | ||
| 568 | following code ... | ||
| 569 | |||
| 570 | use OpenSky::API; | ||
| 571 | |||
| 572 | my $musks_jet = 'a835af'; | ||
| 573 | my $openapi = OpenSky::API->new; | ||
| 574 | |||
| 575 | my $days = 7; | ||
| 576 | my $now = time; | ||
| 577 | my $then = $now - 86400 * 7; # up to 7 days ago | ||
| 578 | |||
| 579 | my $flight_data = $openapi->get_flights_by_aircraft( $musks_jet, $then, $now ); | ||
| 580 | say "Jet $musks_jet has " . $flight_data->count . " flights"; | ||
| 581 | |||
| 582 | As of this writing, that prints out: | ||
| 583 | |||
| 584 | Jet a835af has 6 flights | ||
| 585 | |||
| 586 | =head1 ETHICS | ||
| 587 | |||
| 588 | There are some ethical considerations to be made when using this module. I was | ||
| 589 | ambivalent about writing it, but I decided to do so because I think it's | ||
| 590 | important to be aware of the privacy implications. However, it's also | ||
| 591 | important to be aware of the L<climate | ||
| 592 | implications|https://www.euronews.com/green/2023/03/30/wasteful-luxury-private-jet-pollution-more-than-doubles-in-europe>. | ||
| 593 | |||
| 594 | Others are using the OpenSky API to model the amount of carbon being released | ||
| 595 | by the aviation industry, while others have used this public data to predict | ||
| 596 | corporate mergers and acquisitions. There are a wealth of reasons why this | ||
| 597 | data is useful, but not all of those reasons are good. Be good. | ||
| 598 | |||
| 599 | =head1 TODO | ||
| 600 | |||
| 601 | =over 4 | ||
| 602 | |||
| 603 | =item * Implement rate limits | ||
| 604 | |||
| 605 | =item * Add a C<is_rate_limited> method to results | ||
| 606 | |||
| 607 | =item * Add Waypoints and Flight Routes | ||
| 608 | |||
| 609 | =back | ||
| 610 | |||
| 611 | =head1 AUTHOR | ||
| 612 | |||
| 613 | Curtis "Ovid" Poe <curtis.poe@gmail.com> | ||
| 614 | |||
| 615 | =head1 COPYRIGHT AND LICENSE | ||
| 616 | |||
| 617 | This software is Copyright (c) 2023 by Curtis "Ovid" Poe. | ||
| 618 | |||
| 619 | This is free software, licensed under: | ||
| 620 | |||
| 621 | The Artistic License 2.0 (GPL Compatible) | ||
| 622 | |||
| 623 | =cut |