From bb6acc0c6bc7a7aa9bc47cf87c5f0edf973fea21 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 26 Jul 2022 10:41:44 +0200 Subject: Use async IRIS API for /s/ --- lib/Travelynx/Controller/Traveling.pm | 67 +++++++++++++----------- lib/Travelynx/Helper/IRIS.pm | 99 +++++++++++++++++++++++++++++++++-- 2 files changed, 131 insertions(+), 35 deletions(-) (limited to 'lib/Travelynx') diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index d6253ac..ce4ab8e 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -604,45 +604,50 @@ sub station { my $station = $self->stash('station'); my $train = $self->param('train'); - my $status = $self->iris->get_departures( + $self->render_later; + $self->iris->get_departures_p( station => $station, lookbehind => 120, lookahead => 30, with_related => 1 - ); + )->then( + sub { + my ($status) = @_; - if ( $status->{errstr} ) { - $self->render( - 'landingpage', - version => $self->app->config->{version} // 'UNKNOWN', - with_autocomplete => 1, - with_geolocation => 1, - error => $status->{errstr} - ); - } - else { - # You can't check into a train which terminates here - my @results = grep { $_->departure } @{ $status->{results} }; + # You can't check into a train which terminates here + my @results = grep { $_->departure } @{ $status->{results} }; - @results = map { $_->[0] } - sort { $b->[1] <=> $a->[1] } - map { [ $_, $_->departure->epoch // $_->sched_departure->epoch ] } - @results; + @results = map { $_->[0] } + sort { $b->[1] <=> $a->[1] } + map { [ $_, $_->departure->epoch // $_->sched_departure->epoch ] } + @results; - if ($train) { - @results - = grep { $_->type . ' ' . $_->train_no eq $train } @results; - } + if ($train) { + @results + = grep { $_->type . ' ' . $_->train_no eq $train } @results; + } - $self->render( - 'departures', - eva => $status->{station_eva}, - results => \@results, - station => $status->{station_name}, - related_stations => $status->{related_stations}, - title => "travelynx: $status->{station_name}", - ); - } + $self->render( + 'departures', + eva => $status->{station_eva}, + results => \@results, + station => $status->{station_name}, + related_stations => $status->{related_stations}, + title => "travelynx: $status->{station_name}", + ); + } + )->catch( + sub { + my ($status) = @_; + $self->render( + 'landingpage', + version => $self->app->config->{version} // 'UNKNOWN', + with_autocomplete => 1, + with_geolocation => 1, + error => $status->{errstr} + ); + } + )->wait; $self->users->mark_seen( uid => $self->current_user->{id} ); } diff --git a/lib/Travelynx/Helper/IRIS.pm b/lib/Travelynx/Helper/IRIS.pm index 3c4fba1..0c6a611 100644 --- a/lib/Travelynx/Helper/IRIS.pm +++ b/lib/Travelynx/Helper/IRIS.pm @@ -10,6 +10,8 @@ use 5.020; use utf8; +use Mojo::Promise; +use Mojo::UserAgent; use Travel::Status::DE::IRIS; sub new { @@ -21,8 +23,8 @@ sub new { sub get_departures { my ( $self, %opt ) = @_; my $station = $opt{station}; - my $lookbehind = $opt{lookbehind} // 180; - my $lookahead = $opt{lookahead} // 30; + my $lookbehind = $opt{lookbehind} // 180; + my $lookahead = $opt{lookahead} // 30; my $with_related = $opt{with_related} // 0; my @station_matches @@ -48,8 +50,8 @@ sub get_departures { with_related => $with_related, ); return { - results => [ $status->results ], - errstr => $status->errstr, + results => [ $status->results ], + errstr => $status->errstr, station_ds100 => ( $status->station ? $status->station->{ds100} : undef ), station_eva => @@ -74,6 +76,95 @@ sub get_departures { } } +sub get_departures_p { + my ( $self, %opt ) = @_; + my $station = $opt{station}; + my $lookbehind = $opt{lookbehind} // 180; + my $lookahead = $opt{lookahead} // 30; + my $with_related = $opt{with_related} // 0; + + my @station_matches + = Travel::Status::DE::IRIS::Stations::get_station($station); + + if ( @station_matches == 1 ) { + $station = $station_matches[0][0]; + my $promise = Mojo::Promise->new; + Travel::Status::DE::IRIS->new_p( + station => $station, + main_cache => $self->{main_cache}, + realtime_cache => $self->{realtime_cache}, + keep_transfers => 1, + lookbehind => 20, + datetime => DateTime->now( time_zone => 'Europe/Berlin' ) + ->subtract( minutes => $lookbehind ), + lookahead => $lookbehind + $lookahead, + lwp_options => { + timeout => 10, + agent => 'travelynx/' + . $self->{version} + . ' +https://travelynx.de', + }, + with_related => $with_related, + promise => 'Mojo::Promise', + user_agent => Mojo::UserAgent->new, + get_station => \&Travel::Status::DE::IRIS::Stations::get_station, + meta => Travel::Status::DE::IRIS::Stations::get_meta(), + )->then( + sub { + my ($status) = @_; + $promise->resolve( + { + results => [ $status->results ], + errstr => $status->errstr, + station_ds100 => ( + $status->station + ? $status->station->{ds100} + : undef + ), + station_eva => ( + $status->station ? $status->station->{uic} : undef + ), + station_name => ( + $status->station ? $status->station->{name} : undef + ), + related_stations => [ $status->related_stations ], + } + ); + return; + } + )->catch( + sub { + my ($err) = @_; + $promise->reject( + { + results => [], + errstr => "Error in promise: $err", + } + ); + return; + } + )->wait; + return $promise; + } + elsif ( @station_matches > 1 ) { + return Mojo::Promise->reject( + { + results => [], + errstr => 'Mehrdeutiger Stationsname. Mögliche Eingaben: ' + . join( q{, }, map { $_->[1] } @station_matches ), + } + ); + } + else { + return Mojo::Promise->reject( + { + results => [], + errstr => 'Unbekannte Station', + } + ); + } +} + sub route_diff { my ( $self, $train ) = @_; my @json_route; -- cgit v1.2.3