AzimuthAero.pm

filter: Statementstmt 92.5% 25 / 27 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.4% 34 / 36 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« AzimuthAero/Utils.pm ↑ Summary
1 package WWW::AzimuthAero;
2 $WWW::AzimuthAero::VERSION = '0.3';
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 get {
49 my ( $self, %params ) = @_;
50  
51 confess "from is not defined" unless defined $params{from};
52 confess "to is not defined" unless defined $params{to};
53 confess "date is not defined" unless defined $params{date};
54 confess "date is not defined" unless defined $params{date};
55 confess "adults > 9" if ( $params{adults} > 9 );
56  
57 $params{adults} = 1 unless defined $params{adults};
58  
59 my $url =
60 'https://booking.azimuth.aero/!/'
61 . $params{from} . '/'
62 . $params{to} . '/'
63 . $params{date} . '/'
64 . $params{adults} . '-0-0/';
65 my $req = $self->{ua}->get($url);
66 my $target_dom = $req->res->dom->at('div.sf-day__content');
67  
68 if ( ref($target_dom) eq 'Mojo::DOM' ) {
69  
70 my @res;
71  
72 for my $flight_dom ( $target_dom->find('div.sf-flight-block')->each ) {
73  
74 my $flight = WWW::AzimuthAero::Flight->new(
75 from_city => $params{from},
76 to_city => $params{to},
77 flight_date => $params{date}
78 );
79  
80 my $stops_css =
81 $flight_dom->at('div.ts-flight__duration div.ts-flight__stops');
82  
83 if ($stops_css) {
84 if ( $stops_css->text =~ /пересадк/ ) {
85 $flight->has_stops(1);
86  
87 warn "Dur: "
88 . fix_html_string $flight_dom->at(
89 'div.ts-flight__duration div.ts-flight__dur')->text;
90  
91 $flight->trip_duration(
92 fix_html_string $flight_dom->at(
93 'div.ts-flight__duration div.ts-flight__dur')->text
94 );
95 }
96 }
97  
98 $flight->arrival_time(
99 fix_html_string $flight_dom->at(
100 'div.ts-flight__arrival div.ts-flight__time')->text );
101  
102 $flight->departure_time(
103 fix_html_string $flight_dom->at(
104 'div.ts-flight__deparure div.ts-flight__time')->text
105 );
106  
107 $flight->flight_num(
108 fix_html_string $flight_dom->at('div.ts-flight__num')->text );
109  
110 my $fares = {};
111 for my $class ( $self->possible_fares() ) {
112 my $tdom = $flight_dom->at(
113 'div.td_fare.' . $class . ' span.sf-price__value.rub' );
114 if ($tdom) {
115 my $f = $tdom->text;
116 $f =~ s/\D+//g; # remove all non-digits
117 $fares->{$class} = $f;
118 }
119 }
120  
121 $fares->{lowest} = min values %$fares;
122 $flight->fares($fares);
123 push @res, $flight;
124  
125 }
126  
127 return \@res;
128  
129 }
130  
131 # return die
132 else {
133 return { error => 'No flights found' };
134 }
135 }
136  
137  
138 sub route_map {
139 return shift->{route_map};
140 }
141  
142  
143 # ENDPOINT LIKE: https://azimuth.aero/ru/flights?from=ROV&to=ASF
144  
145 sub get_schedule_dates {
146 my ( $self, %params ) = @_;
147  
148 confess "from is not defined" unless defined $params{from};
149 confess "to is not defined" unless defined $params{to};
150  
151 my $url =
152 'https://azimuth.aero/ru/flights?from='
153 . $params{from} . '&to='
154 . $params{to};
155 my $res = $self->{ua}->get($url)->res->json;
156  
157 if ( ref( $res->{available_to} ) eq 'ARRAY' ) {
158  
159 my @dates;
160 for my $interval ( @{ $res->{available_to} } ) {
161  
162 # warn "Interval : ".Dumper $interval;
163 # $interval->{min}
164 # $interval->{max}
165 # $interval->{days}
166 push @dates,
167 get_dates_from_dows(%$interval)
168 ; # to-do : check min_date and max_date
169 }
170  
171 # sort dates again for keeping sequence of interval
172 return sort_dates(@dates);
173 }
174 else {
175 carp "No schedule available between "
176 . $params{from} . " and "
177 . $params{to}
178 . ", url : $url, most likely is transit route"
179 if $params{v};
180  
181 return get_dates_from_range( min => $params{min}, max => $params{max} )
182 ; # to-do : check max_date
183 }
184 }
185  
186  
187 sub possible_fares {
188 return qw/legkiy vygodnyy optimalnyy svobodnyy komfort/;
189 }
190  
191  
192 sub find_no_schedule {
193 my ( $self, %params ) = @_;
194  
195 my $iata_map = $self->route_map->route_map_iata;
196  
197 my %res = ();
198 my $i;
199 while ( my ( $from, $cities ) = each(%$iata_map) ) {
200 for my $to (@$cities) {
201 my $url =
202 'https://azimuth.aero/ru/flights?from=' . $from . '&to=' . $to;
203 my $res = $self->{ua}->get($url)->res->json;
204 say $i;
205 $res{$from} = $to if ( ref( $res->{available_to} ) ne 'ARRAY' );
206 $i++;
207 }
208 }
209  
210 return %res;
211 }
212  
213  
214 sub get_fares_schedule {
215 my ( $self, %params ) = @_;
216  
217 my @available_dates = $self->get_schedule_dates(%params);
218  
219 # filter schedule dates from now to max
220 @available_dates = filter_dates(
221 \@available_dates,
222 max => $params{max},
223 min => $params{min}
224 );
225  
226 # warn "Dates : ".Dumper @available_dates;
227  
228 my @fares;
229  
230 # say scalar @available_dates . ' days will be checked' if $params{progress_bar};
231  
232 say $params{from} . '->'
233 . $params{to} . ' : '
234 . $params{min} . '->'
235 . $params{max}
236 if $params{progress_bar};
237 for my $i ( 0 .. $#available_dates ) {
238  
239 my $date_str = $available_dates[$i];
240  
241 say '' . ( $i + 1 ) . '/' . scalar @available_dates
242 if $params{progress_bar};
243  
244 my $flights = $self->get(
245 from => $params{from},
246 to => $params{to},
247 date => $date_str
248 );
249  
250 if ( ref($flights) eq 'ARRAY' ) { # not error
251 if ( $params{print_immediately} ) {
252 say $_->as_string( order => [qw/flight_date/] ) for (@$flights);
253 }
254 push @fares, @$flights;
255 }
256 }
257  
258 if ( $params{print_table} ) {
259 say $_->as_string( order => [qw/flight_date/], separator => "\t" )
260 for (@fares);
261 }
262  
263 # say scalar @fares . ' days of direct flights with available tickets found' if $params{progress_bar};
264  
265 return sort { $a->flight_date cmp $b->flight_date } @fares;
266 }
267  
268  
269 sub get_lowest_fares {
270 my ( $self, %params ) = @_;
271  
272 my @fares = $self->get_fares_schedule(%params);
273  
274 if ( $params{check_neighbors} ) {
275  
276 my @from_neighbors =
277 $self->route_map->get_neighbor_airports_iata( $params{from} );
278 my @to_neighbors =
279 $self->route_map->get_neighbor_airports_iata( $params{to} );
280  
281 # say 'Will check neighbor airports also : '.join("\t",@from_neighbors,@to_neighbors) if ($params{progress_bar});
282 # say '== neighbours : ' . join( " ", @from_neighbors, @to_neighbors );
283  
284 for my $from2 (@from_neighbors) {
285  
286 # warn "from 2 : ".$from2;
287 push @fares,
288 $self->get_fares_schedule(
289 from => $from2,
290 to => $params{to},
291 min => $params{min},
292 max => $params{max},
293 progress_bar => $params{progress_bar},
294 );
295 }
296  
297 for my $to2 (@to_neighbors) {
298 push @fares,
299 $self->get_fares_schedule(
300 from => $params{from},
301 to => $to2,
302 min => $params{min},
303 max => $params{max},
304 progress_bar => $params{progress_bar},
305 );
306 }
307  
308 }
309  
310 if ( $params{find_transits} ) {
311  
312 my $routes = $self->route_map->transfer_routes(
313 from => $params{from},
314 to => $params{to}
315 );
316  
317 # [ [ 'ROV', 'MOW', 'LED' ], [ 'ROV', 'KRR', 'LED' ] ]
318 # to
319 # [ { from => 'ROV', via => 'MOW', to => 'LED' }, ... ]
320 my @flights = iata_pairwise($routes);
321  
322 # process transit flights
323 # for my $x (@flights) {
324 # push @fares, $self->get_fares_schedule_transit(
325 # from => $x->{from},
326 # via => $x->{via},
327 # via => $x->{to},
328 # min => $params{min},
329 # max => $params{max},
330 # progress_bar => $params{progress_bar},
331 # max_delay_days => $params{max_delay_days}
332 # );
333 # }
334  
335 }
336  
337 return sort { $a->fares->{lowest} <=> $b->fares->{lowest} } @fares;
338  
339 }
340  
341 1;
342  
343 __END__
344  
345 =pod
346  
347 =encoding UTF-8
348  
349 =head1 NAME
350  
351 WWW::AzimuthAero - Parser for https://azimuth.aero/
352  
353 =head1 VERSION
354  
355 version 0.3
356  
357 =head1 SYNOPSIS
358  
359 use WWW::AzimuthAero;
360 my $az = WWW::AzimuthAero->new();
361
362 $az->get_schedule_dates( from => 'ROV', to => 'KLF' );
363
364 $az->get( from => 'ROV', to => 'LED', date => '14.06.2019' );
365
366 $az->get_lowest_fares( from => 'ROV', to => 'LED', max => '14.08.2019' );
367
368 $az->print_flights(
369 $az->get_lowest_fares(
370 from => 'ROV',
371 to => 'LED',
372 max => '14.08.2019',
373 progress_bar => 1
374 )
375 )
376  
377 Outside:
378  
379 perl -Ilib -MData::Dumper -MWWW::AzimuthAero -e 'my $x = WWW::AzimuthAero->new->route_map->transfer_routes; warn Dumper $x;'
380  
381 =head1 DESCRIPTION
382  
383 This module provides a parser for L<https://azimuth.aero/>
384  
385 Module can be useful for creating price monitoring services and flexible travel planners
386  
387 Module uses L<Mojo::UserAgent> as user agent and L<Mojo::DOM> + L<JavaScript::V8> as DOM parser
388  
389 =head1 INSTALLATION NOTES
390  
391 Since this module depends on L<JavaScript::V8> you need to install libv8-3.14.5 libv8-3.14-dev on your system
392  
393 =head1 FOR DEVELOPERS
394  
395 How to generate DOM samples for unit tests after git clone:
396  
397 $ perl -Ilib -e "use WWW::AzimuthAero::Mock; WWW::AzimuthAero::Mock->generate()"
398  
399 See L<WWW::AzimuthAero::Mock> and L<Mojo::UserAgent::Mockable> for more details
400  
401 =head2 API endpoints
402  
403 urls that modules uses:
404  
405 L<https://booking.azimuth.aero/> (for fetching route map and initialize session)
406  
407 L<https://azimuth.aero/ru/flights?from=ROV&to=LED> (for fetching schedule)
408  
409 L<https://booking.azimuth.aero/!/ROV/LED/19.06.2019/1-0-0/> (for fetching prices)
410  
411 =head1 TO-DO
412  
413 + implement find_transits at L<WWW::AzimuthAero/get_lowest_fares>
414  
415 + implement check_tickets at L<WWW::AzimuthAero/get>
416  
417 =head1 MAIN METHODS
418  
419 =head2 new
420  
421 use WWW::AzimuthAero;
422 my $az = Azimuth->new();
423 # or my $az = Azimuth->new(ua_str => 'yandex-travel');
424  
425 =head2 get
426  
427 Checks for flight between two cities on selected date.
428  
429 Cities are specified as IATA codes.
430  
431 $az->get( from => 'ROV', to => 'LED', date => '04.06.2019' );
432 $az->get( from => 'ROV', to => 'LED', date => '04.06.2019', check_tickets => 1 );
433  
434 You can also set adults params, from 1 to 9, and check_tickets (auto check with adults from 9 to 1)
435  
436 Those params may be convenient for monitoring tickets availability.
437  
438 WARN: check_tickets is not implemented yet
439  
440 Return ARRAYref with L<WWW::AzimuthAero::Flight> objects or hash with error like
441  
442 { 'error' => 'No flights found' }
443  
444 There could be two flights between same cities in same day
445 so for unification this method always returns ARRAYref even if array contains one item
446  
447 =head2 route_map
448  
449 Return L<WWW::AzimuthAero::RouteMap> object
450  
451 perl -Ilib -MWWW::AzimuthAero -MData::Dumper::AutoEncode -e 'my $x = WWW::AzimuthAero->new->route_map; warn eDumper $x;'
452  
453 =head2 get_schedule_dates
454  
455 Get schedule by requested direction
456  
457 $az->get_schedule_dates( from => 'ROV', to => 'KLF' );
458
459 $az->get_schedule_dates( from => 'ROV', to => 'PKV', max => '20.06.2019' ); # will start search from today
460
461 $az->get_schedule_dates( from => 'ROV', to => 'PKV', min => '16.06.2019', max => '20.06.2019' );
462  
463 Return list of available dates in C<'%d.%m.%Y'> format
464  
465 Always return dates from today
466  
467 Method is useful for minimize amount of API requests
468  
469 If no available_to property set (like at L<https://azimuth.aero/ru/flights?from=ROV&to=PKV> )
470 will check for 2 months forward and return all dates in range
471  
472 =head1 HELPER METHODS
473  
474 Useful for making CLI utilites
475  
476 =head2 possible_fares
477  
478 Return names of div.td_fare CSS classes which contain prices
479  
480 Current:
481  
482 qw/legkiy vygodnyy optimalnyy svobodnyy komfort/;
483  
484 =head2 find_no_schedule
485  
486 Return hash with routes with no available schedule, presumably all transit routes.
487  
488 =head2 get_fares_schedule
489  
490 Get fares schedule between selected cities. Cities are specified as IATA codes.
491  
492 my @flights = $az->get_fares_schedule(
493 from => 'ROV',
494 to => 'LED',
495 min => '25.06.2019',
496 max => '30.06.2019',
497 progress_bar => 1,
498 print_immediately => 1,
499 print_table => 1
500 );
501  
502 Returned list of L<WWW::AzimuthAero::Flight> objects sorted by date, ascending
503  
504 Params:
505  
506 from
507  
508 to
509  
510 min
511  
512 max
513  
514 progress_bar - will print on STDOUT progress bar like
515  
516 ROV->LED : 25.06.2019->30.06.2019
517 1/3
518 2/3
519 3/3
520  
521 print_immediately - will print on STDOUT flight data immediately with progress bar like
522  
523 ROV->LED : 25.06.2019->30.06.2019
524 1/3
525 27.06.2019 10:00 07:20 A4 203 ROV LED
526 2/3
527 28.06.2019 10:00 07:20 A4 203 ROV LED
528 3/3
529 29.06.2019 09:50 07:10 A4 203 ROV LED
530  
531 print_table - will print on STDOUT result table like
532  
533 =head2 get_lowest_fares
534  
535 Wrapper for L<WWW::AzimuthAero/get_fares_schedule>
536  
537 Main difference that it sort flights by price and can also checks neighbor airports
538  
539 Get lowest fares between selected cities. Cities are specified as IATA codes.
540  
541 $az->get_lowest_fares(
542 from => 'ROV',
543 to => 'LED',
544 min => '7.06.2019',
545 max => '15.06.2019',
546 progress_bar => 1,
547 check_neighbors => 1, # will check PKV instead LED and KLG instead of MOW
548 find_transits => 1, # will find transit cities that are not mentioned by azimuth
549 max_delay_days => 1,
550 # max_edges => 2 # TO-DO, hardcoded for now
551 );
552  
553 =head1 AUTHOR
554  
555 Pavel Serikov <pavelsr@cpan.org>
556  
557 =head1 COPYRIGHT AND LICENSE
558  
559 This software is copyright (c) 2019 by Pavel Serikov.
560  
561 This is free software; you can redistribute it and/or modify it under
562 the same terms as the Perl 5 programming language system itself.
563  
564 =cut
« AzimuthAero/Utils.pm ↑ Summary