| 1 | package WWW::AzimuthAero; | ||
| 2 | $WWW::AzimuthAero::VERSION = '0.2'; | ||
| 3 | |||
| 4 | # ABSTRACT: Parser for https://azimuth.aero/ | ||
| 5 | |||
| 6 | |||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use feature 'say'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use List::Util qw/min/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | 1 | ▶ | use Mojo::DOM; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | ▶ | use Mojo::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | 1 | ▶ | use WWW::AzimuthAero::Utils qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | use WWW::AzimuthAero::RouteMap; | ||
| 19 | use WWW::AzimuthAero::Flight; | ||
| 20 | |||
| 21 | use Data::Dumper; | ||
| 22 | use Data::Dumper::AutoEncode; | ||
| 23 | |||
| 24 | |||
| 25 | sub new { | ||
| 26 | my ( $self, %params ) = @_; | ||
| 27 | $params{ua_obj} = Mojo::UserAgent->new() unless defined $params{ua_obj}; | ||
| 28 | $params{ua_str} = | ||
| 29 | 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36' | ||
| 30 | unless defined $params{ua_str}; | ||
| 31 | $params{ua_obj}->transactor->name( $params{ua_str} ); | ||
| 32 | |||
| 33 | # get cookies, otheerwise will be 403 error | ||
| 34 | my $req = $params{ua_obj}->get('https://booking.azimuth.aero/'); | ||
| 35 | |||
| 36 | # get route map | ||
| 37 | my $route_map = $req->res->dom->find('script') | ||
| 38 | ->grep( sub { $_->text =~ /\/\/ Route map/ } )->first->text; | ||
| 39 | $route_map = extract_js_glob_var( $route_map, 'data.routes' ); | ||
| 40 | |||
| 41 | bless { | ||
| 42 | ua => $params{ua_obj}, | ||
| 43 | route_map => WWW::AzimuthAero::RouteMap->new($route_map) | ||
| 44 | }, $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub route_map { | ||
| 49 | return shift->{route_map}; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | # ENDPOINT LIKE: https://azimuth.aero/ru/flights?from=ROV&to=ASF | ||
| 54 | |||
| 55 | sub get_schedule_dates { | ||
| 56 | my ( $self, %params ) = @_; | ||
| 57 | |||
| 58 | confess "from is not defined" unless defined $params{from}; | ||
| 59 | confess "to is not defined" unless defined $params{to}; | ||
| 60 | |||
| 61 | my $url = | ||
| 62 | 'https://azimuth.aero/ru/flights?from=' | ||
| 63 | . $params{from} . '&to=' | ||
| 64 | . $params{to}; | ||
| 65 | my $res = $self->{ua}->get($url)->res->json; | ||
| 66 | |||
| 67 | if ( ref( $res->{available_to} ) eq 'ARRAY' ) { | ||
| 68 | |||
| 69 | my @dates; | ||
| 70 | for my $interval ( @{ $res->{available_to} } ) { | ||
| 71 | |||
| 72 | # warn "Interval : ".Dumper $interval; | ||
| 73 | # $interval->{min} | ||
| 74 | # $interval->{max} | ||
| 75 | # $interval->{days} | ||
| 76 | push @dates, | ||
| 77 | get_dates_from_dows(%$interval); # to-do : check max_date | ||
| 78 | } | ||
| 79 | |||
| 80 | # to-do: push from now only | ||
| 81 | |||
| 82 | return sort_dates(@dates); | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | carp "No schedule available between " | ||
| 86 | . $params{from} . " and " | ||
| 87 | . $params{to} | ||
| 88 | . ", url : $url, most likely is transit route" | ||
| 89 | if $params{v}; | ||
| 90 | |||
| 91 | return get_dates_from_range( min => $params{min}, max => $params{max} ) | ||
| 92 | ; # to-do : check max_date | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub find_no_schedule { | ||
| 98 | my ( $self, %params ) = @_; | ||
| 99 | |||
| 100 | my $iata_map = $self->route_map->route_map_iata; | ||
| 101 | |||
| 102 | my %res = (); | ||
| 103 | my $i; | ||
| 104 | while ( my ( $from, $cities ) = each(%$iata_map) ) { | ||
| 105 | for my $to (@$cities) { | ||
| 106 | my $url = | ||
| 107 | 'https://azimuth.aero/ru/flights?from=' . $from . '&to=' . $to; | ||
| 108 | my $res = $self->{ua}->get($url)->res->json; | ||
| 109 | say $i; | ||
| 110 | $res{$from} = $to if ( ref( $res->{available_to} ) ne 'ARRAY' ); | ||
| 111 | $i++; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | return %res; | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | # assume that aray is already sorted by lowest price | ||
| 120 | |||
| 121 | sub print_flights { | ||
| 122 | my ( $self, @flights ) = @_; | ||
| 123 | my $str; | ||
| 124 | |||
| 125 | for my $f (@flights) { | ||
| 126 | |||
| 127 | my $transfer_time = $f->{has_stops} ? $f->{flight_duration} : ''; | ||
| 128 | |||
| 129 | $str .= | ||
| 130 | $f->{fares}{lowest} . "\t" | ||
| 131 | . $f->{date} . "\t" | ||
| 132 | . $f->{from} . '->' | ||
| 133 | . $f->{to} | ||
| 134 | . $transfer_time | ||
| 135 | . "\n "; | ||
| 136 | } | ||
| 137 | |||
| 138 | return $str; | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | sub get { | ||
| 143 | my ( $self, %params ) = @_; | ||
| 144 | |||
| 145 | confess "from is not defined" unless defined $params{from}; | ||
| 146 | confess "to is not defined" unless defined $params{to}; | ||
| 147 | confess "date is not defined" unless defined $params{date}; | ||
| 148 | |||
| 149 | my $url = | ||
| 150 | 'https://booking.azimuth.aero/!/' | ||
| 151 | . $params{from} . '/' | ||
| 152 | . $params{to} . '/' | ||
| 153 | . $params{date} | ||
| 154 | . '/1-0-0/'; | ||
| 155 | my $req = $self->{ua}->get($url); | ||
| 156 | my $target_dom = $req->res->dom->at('div.sf-day__content'); | ||
| 157 | |||
| 158 | if ( ref($target_dom) eq 'Mojo::DOM' ) { | ||
| 159 | |||
| 160 | my @res; | ||
| 161 | |||
| 162 | for my $flight_dom ( $target_dom->find('div.sf-flight-block')->each ) { | ||
| 163 | |||
| 164 | my $flight = WWW::AzimuthAero::Flight->new( | ||
| 165 | from => $params{from}, | ||
| 166 | to => $params{to}, | ||
| 167 | date => $params{date} | ||
| 168 | ); | ||
| 169 | |||
| 170 | my $stops_css = | ||
| 171 | $flight_dom->at('div.ts-flight__duration div.ts-flight__stops'); | ||
| 172 | |||
| 173 | if ($stops_css) { | ||
| 174 | if ( $stops_css->text =~ /пересадк/ ) { | ||
| 175 | $flight->has_stops(1); | ||
| 176 | |||
| 177 | $flight->duration( | ||
| 178 | fix_html_string $flight_dom->at( | ||
| 179 | 'div.ts-flight__duration div.ts-flight__dur')->text | ||
| 180 | ); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | $flight->arrival( | ||
| 185 | fix_html_string $flight_dom->at( | ||
| 186 | 'div.ts-flight__arrival div.ts-flight__time')->text ); | ||
| 187 | |||
| 188 | $flight->departure( | ||
| 189 | fix_html_string $flight_dom->at( | ||
| 190 | 'div.ts-flight__deparure div.ts-flight__time')->text | ||
| 191 | ); | ||
| 192 | |||
| 193 | $flight->flight_num( | ||
| 194 | fix_html_string $flight_dom->at('div.ts-flight__num')->text ); | ||
| 195 | |||
| 196 | my @possible_fares = qw/legkiy vygodnyy optimalnyy svobodnyy/; | ||
| 197 | |||
| 198 | my $fares = {}; | ||
| 199 | for my $class (@possible_fares) { | ||
| 200 | my $tdom = $flight_dom->at( | ||
| 201 | 'div.td_fare.' . $class . ' span.sf-price__value.rub' ); | ||
| 202 | if ($tdom) { | ||
| 203 | my $f = $tdom->text; | ||
| 204 | $f =~ s/\D+//g; # remove all non-digits | ||
| 205 | $fares->{$class} = $f; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | $fares->{lowest} = min values %$fares; | ||
| 210 | $flight->fares($fares); | ||
| 211 | push @res, $flight; | ||
| 212 | |||
| 213 | } | ||
| 214 | |||
| 215 | return \@res; | ||
| 216 | |||
| 217 | } | ||
| 218 | else { | ||
| 219 | return { error => 'No flights found' }; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | sub get_fares_schedule { | ||
| 225 | my ( $self, %params ) = @_; | ||
| 226 | |||
| 227 | my @available_dates = $self->get_schedule_dates(%params); | ||
| 228 | |||
| 229 | # filter schedule dates from now to max | ||
| 230 | @available_dates = filter_dates( | ||
| 231 | \@available_dates, | ||
| 232 | max => $params{max}, | ||
| 233 | min => $params{min} | ||
| 234 | ); | ||
| 235 | |||
| 236 | # warn "Dates : ".Dumper @available_dates; | ||
| 237 | |||
| 238 | my @fares; | ||
| 239 | |||
| 240 | # say scalar @available_dates . ' days will be checked' if $params{progress_bar}; | ||
| 241 | |||
| 242 | for my $i ( 0 .. $#available_dates ) { | ||
| 243 | |||
| 244 | my $date_str = $available_dates[$i]; | ||
| 245 | say $params{from} . '->' | ||
| 246 | . $params{to} . ' : ' | ||
| 247 | . ( $i + 1 ) . '/' | ||
| 248 | . scalar @available_dates | ||
| 249 | if $params{progress_bar}; | ||
| 250 | |||
| 251 | my $flights = $self->get( | ||
| 252 | from => $params{from}, | ||
| 253 | to => $params{to}, | ||
| 254 | date => $date_str | ||
| 255 | ); | ||
| 256 | |||
| 257 | #warn Dumper $flights; | ||
| 258 | $self->print_flights(@$flights) if $params{print_immediately}; | ||
| 259 | |||
| 260 | push @fares, @$flights | ||
| 261 | if ( ref($flights) eq 'ARRAY' ); | ||
| 262 | } | ||
| 263 | |||
| 264 | # say scalar @fares . ' days of direct flights with available tickets found' if $params{progress_bar}; | ||
| 265 | |||
| 266 | return sort { $a->{date} cmp $b->{date} } @fares; | ||
| 267 | } | ||
| 268 | |||
| 269 | |||
| 270 | # TO-DO: confess if max_date < now | ||
| 271 | |||
| 272 | sub get_lowest_fares { | ||
| 273 | my ( $self, %params ) = @_; | ||
| 274 | |||
| 275 | my @fares = $self->get_fares_schedule(%params); | ||
| 276 | |||
| 277 | if ( $params{check_neighbors} ) { | ||
| 278 | |||
| 279 | my @from_neighbors = | ||
| 280 | $self->route_map->get_neighbor_airports_iata( $params{from} ); | ||
| 281 | my @to_neighbors = | ||
| 282 | $self->route_map->get_neighbor_airports_iata( $params{to} ); | ||
| 283 | |||
| 284 | # say 'Will check neighbor airports also : '.join("\t",@from_neighbors,@to_neighbors) if ($params{progress_bar}); | ||
| 285 | say '== neighbours : ' . join( " ", @from_neighbors, @to_neighbors ); | ||
| 286 | |||
| 287 | for my $from2 (@from_neighbors) { | ||
| 288 | |||
| 289 | # warn "from 2 : ".$from2; | ||
| 290 | push @fares, | ||
| 291 | $self->get_fares_schedule( | ||
| 292 | from => $from2, | ||
| 293 | to => $params{to}, | ||
| 294 | min => $params{min}, | ||
| 295 | max => $params{max}, | ||
| 296 | progress_bar => $params{progress_bar}, | ||
| 297 | ); | ||
| 298 | } | ||
| 299 | |||
| 300 | for my $to2 (@to_neighbors) { | ||
| 301 | push @fares, | ||
| 302 | $self->get_fares_schedule( | ||
| 303 | from => $params{from}, | ||
| 304 | to => $to2, | ||
| 305 | min => $params{min}, | ||
| 306 | max => $params{max}, | ||
| 307 | progress_bar => $params{progress_bar}, | ||
| 308 | ); | ||
| 309 | } | ||
| 310 | |||
| 311 | } | ||
| 312 | |||
| 313 | if ( $params{find_transits} ) { | ||
| 314 | |||
| 315 | my $routes = $self->route_map->transfer_routes( | ||
| 316 | from => $params{from}, | ||
| 317 | to => $params{to} | ||
| 318 | ); | ||
| 319 | |||
| 320 | # [ [ 'ROV', 'MOW', 'LED' ], [ 'ROV', 'KRR', 'LED' ] ] | ||
| 321 | # to | ||
| 322 | # [ { from => 'ROV', via => 'MOW', to => 'LED' }, ... ] | ||
| 323 | my @flights = iata_pairwise($routes); | ||
| 324 | |||
| 325 | # process transit flights | ||
| 326 | # for my $x (@flights) { | ||
| 327 | # push @fares, $self->get_fares_schedule_transit( | ||
| 328 | # from => $x->{from}, | ||
| 329 | # via => $x->{via}, | ||
| 330 | # via => $x->{to}, | ||
| 331 | # min => $params{min}, | ||
| 332 | # max => $params{max}, | ||
| 333 | # progress_bar => $params{progress_bar}, | ||
| 334 | # max_delay_days => $params{max_delay_days} | ||
| 335 | # ); | ||
| 336 | # } | ||
| 337 | |||
| 338 | } | ||
| 339 | |||
| 340 | return sort { $a->{fares}{lowest} <=> $b->{fares}{lowest} } @fares; | ||
| 341 | |||
| 342 | } | ||
| 343 | |||
| 344 | 1; | ||
| 345 | |||
| 346 | __END__ | ||
| 347 | |||
| 348 | =pod | ||
| 349 | |||
| 350 | =encoding UTF-8 | ||
| 351 | |||
| 352 | =head1 NAME | ||
| 353 | |||
| 354 | WWW::AzimuthAero - Parser for https://azimuth.aero/ | ||
| 355 | |||
| 356 | =head1 VERSION | ||
| 357 | |||
| 358 | version 0.2 | ||
| 359 | |||
| 360 | =head1 SYNOPSIS | ||
| 361 | |||
| 362 | use WWW::AzimuthAero; | ||
| 363 | my $az = WWW::AzimuthAero->new(); | ||
| 364 | |||
| 365 | $az->get_schedule_dates( from => 'ROV', to => 'KLF' ); | ||
| 366 | |||
| 367 | $az->get( from => 'ROV', to => 'LED', date => '14.06.2019' ); | ||
| 368 | |||
| 369 | $az->get_lowest_fares( from => 'ROV', to => 'LED', max => '14.08.2019' ); | ||
| 370 | |||
| 371 | $az->print_flights( | ||
| 372 | $az->get_lowest_fares( | ||
| 373 | from => 'ROV', | ||
| 374 | to => 'LED', | ||
| 375 | max => '14.08.2019', | ||
| 376 | progress_bar => 1 | ||
| 377 | ) | ||
| 378 | ) | ||
| 379 | |||
| 380 | Outside: | ||
| 381 | |||
| 382 | perl -Ilib -MData::Dumper -MWWW::AzimuthAero -e 'my $x = WWW::AzimuthAero->new->route_map->transfer_routes; warn Dumper $x;' | ||
| 383 | |||
| 384 | =head1 DESCRIPTION | ||
| 385 | |||
| 386 | This module provides a parser for https://azimuth.aero/ | ||
| 387 | |||
| 388 | Module can be useful for creating price monitoring services and flexible travel planners | ||
| 389 | |||
| 390 | Module uses L<Mojo::UserAgent> as user agent and L<Mojo::DOM> + L<JavaScript::V8> as DOM parser | ||
| 391 | |||
| 392 | =head1 FOR DEVELOPERS | ||
| 393 | |||
| 394 | How to generate DOM samples for unit tests after git clone: | ||
| 395 | |||
| 396 | $ perl -Ilib -e "use WWW::AzimuthAero::Mock; WWW::AzimuthAero::Mock->generate()" | ||
| 397 | |||
| 398 | See L<WWW::AzimuthAero::Mock> and L<Mojo::UserAgent::Mockable> for more details | ||
| 399 | |||
| 400 | API urls that modules uses: | ||
| 401 | |||
| 402 | https://booking.azimuth.aero/ (for fetching route map and initialize session) | ||
| 403 | |||
| 404 | https://azimuth.aero/ru/flights?from=ROV&to=LED (for fetching schedule) | ||
| 405 | |||
| 406 | https://booking.azimuth.aero/!/ROV/LED/19.06.2019/1-0-0/ (for fetching prices) | ||
| 407 | |||
| 408 | =head1 TO-DO | ||
| 409 | |||
| 410 | + implement find_transits | ||
| 411 | |||
| 412 | + Checking more than 1 transfer | ||
| 413 | |||
| 414 | + debug output of L<WWW::AzimuthAero/get_fares_schedule> and others | ||
| 415 | |||
| 416 | =head1 new | ||
| 417 | |||
| 418 | use WWW::AzimuthAero; | ||
| 419 | my $az = Azimuth->new(); | ||
| 420 | # or my $az = Azimuth->new(ua_str => 'yandex-travel'); | ||
| 421 | |||
| 422 | =head1 route_map | ||
| 423 | |||
| 424 | Return L<WWW::AzimuthAero::RouteMap> object | ||
| 425 | |||
| 426 | perl -Ilib -MWWW::AzimuthAero -MData::Dumper::AutoEncode -e 'my $x = WWW::AzimuthAero->new->route_map; warn eDumper $x;' | ||
| 427 | |||
| 428 | =head1 get_schedule_dates | ||
| 429 | |||
| 430 | Get schedule by requested direction | ||
| 431 | |||
| 432 | $az->get_schedule_dates( from => 'ROV', to => 'KLF' ); | ||
| 433 | |||
| 434 | $az->get_schedule_dates( from => 'ROV', to => 'PKV', max => '20.06.2019' ); # will start search from today | ||
| 435 | |||
| 436 | $az->get_schedule_dates( from => 'ROV', to => 'PKV', min => '16.06.2019', max => '20.06.2019' ); | ||
| 437 | |||
| 438 | Return list of available dates in '%d.%m.%Y' format | ||
| 439 | |||
| 440 | Method is useful for minimize amount of API requests | ||
| 441 | |||
| 442 | If no available_to property set (like at https://azimuth.aero/ru/flights?from=ROV&to=PKV ) | ||
| 443 | will check for 2 months forward and return all dates in range | ||
| 444 | |||
| 445 | =head1 find_no_schedule | ||
| 446 | |||
| 447 | Return hash with routes with no available schedule, presumably all transit routes. | ||
| 448 | |||
| 449 | =head1 print_flights | ||
| 450 | |||
| 451 | my @x = $az->get_lowest_fares( from => 'ROV', to => 'MOW', max => '16.06.2019', progress_bar => 1 ); | ||
| 452 | $az->print_flights(@x); | ||
| 453 | |||
| 454 | =head1 get | ||
| 455 | |||
| 456 | Checks for flight between two cities on selected date. | ||
| 457 | |||
| 458 | Cities are specified as IATA codes. | ||
| 459 | |||
| 460 | $az->get( from => 'ROV', to => 'LED', date => '04.06.2019' ); | ||
| 461 | |||
| 462 | Return ARRAYref with L<WWW::AzimuthAero::Flight> objects or hash with error like | ||
| 463 | |||
| 464 | { 'error' => 'No flights found' } | ||
| 465 | |||
| 466 | =head1 get_fares_schedule | ||
| 467 | |||
| 468 | Get fares schedule between selected cities. Cities are specified as IATA codes. | ||
| 469 | |||
| 470 | Returned data is sorted by date, ascending | ||
| 471 | |||
| 472 | $az->get_lowest_fares( | ||
| 473 | from => 'ROV', | ||
| 474 | to => 'LED', | ||
| 475 | min => '7.06.2019', | ||
| 476 | max => '15.06.2019', | ||
| 477 | progress_bar => 1, | ||
| 478 | ); | ||
| 479 | |||
| 480 | =head1 get_lowest_fares | ||
| 481 | |||
| 482 | Get lowest fares between selected cities. Cities are specified as IATA codes. | ||
| 483 | |||
| 484 | $az->get_lowest_fares( | ||
| 485 | from => 'ROV', | ||
| 486 | to => 'LED', | ||
| 487 | min => '7.06.2019', | ||
| 488 | max => '15.06.2019', | ||
| 489 | progress_bar => 1, | ||
| 490 | check_neighbors => 1, # will check PKV instead LED and KLG instead of MOW | ||
| 491 | find_transits => 1, # will find transit cities that are not mentioned by azimuth | ||
| 492 | max_delay_days => 1, | ||
| 493 | # max_edges => 2 # hardcoded cow | ||
| 494 | ); | ||
| 495 | |||
| 496 | =head1 AUTHOR | ||
| 497 | |||
| 498 | Pavel Serikov <pavelsr@cpan.org> | ||
| 499 | |||
| 500 | =head1 COPYRIGHT AND LICENSE | ||
| 501 | |||
| 502 | This software is copyright (c) 2019 by Pavel Serikov. | ||
| 503 | |||
| 504 | This is free software; you can redistribute it and/or modify it under | ||
| 505 | the same terms as the Perl 5 programming language system itself. | ||
| 506 | |||
| 507 | =cut | ||