From c26c8820f296de41ce7093416b584cfdb6b657fa Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 9 Nov 2022 18:14:07 +0100 Subject: [PATCH] Switch to internal HAFAS client for connection details --- examples/travelynx.conf | 6 -- lib/Travelynx.pm | 3 - lib/Travelynx/Controller/Traveling.pm | 85 ++++++++++----------------- lib/Travelynx/Helper/HAFAS.pm | 16 +++++ 4 files changed, 47 insertions(+), 63 deletions(-) diff --git a/examples/travelynx.conf b/examples/travelynx.conf index 47bc92c..1209345 100644 --- a/examples/travelynx.conf +++ b/examples/travelynx.conf @@ -15,12 +15,6 @@ # via multiple URLs, use any one of them. base_url => Mojo::URL->new('https://FIXME.local'), - # travelynx relies on several backend projects. You may override the - # defaults to use other (e.g. self-hosted) instances. - backend => { - hafas_rest_api => 'https://v5.db.transport.rest', - }, - # Cache directories for schedule and realtime data. Mandatory. The parent # directory ('/var/cache/travelynx' in this case) must already exist. cache => { diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 7c8b48e..07b2a89 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -93,8 +93,6 @@ sub startup { $self->secrets( $self->config->{secrets} ); } - $self->config->{backend}{hafas_rest_api} //= 'https://v5.db.transport.rest'; - chomp $self->config->{version}; $self->plugin( @@ -300,7 +298,6 @@ sub startup { my ($self) = @_; state $hafas = Travelynx::Helper::HAFAS->new( log => $self->app->log, - hafas_rest_api => $self->app->config->{backend}{hafas_rest_api}, main_cache => $self->app->cache_iris_main, realtime_cache => $self->app->cache_iris_rt, root_url => $self->base_url_for('/')->to_abs, diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index c70d44a..8d16811 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -241,15 +241,14 @@ sub get_connecting_trains_p { } my $hafas_promise = Mojo::Promise->new; - my $rest_api = $self->config->{backend}{hafas_rest_api}; - $self->hafas->get_json_p( -"${rest_api}/stops/${eva}/departures?results=120&duration=${lookahead}&stopovers=true&when=10 minutes ago", - realtime => 1, - encoding => 'utf-8' + $self->hafas->get_departures_p( + eva => $eva, + lookbehind => 10, + lookahead => $lookahead )->then( sub { - my ($json) = @_; - $hafas_promise->resolve($json); + my ($status) = @_; + $hafas_promise->resolve( [ $status->results ] ); return; } )->catch( @@ -268,11 +267,6 @@ sub get_connecting_trains_p { my @hafas_trains = @{ $hafas->[0] }; my @transit_fyi; - my $strp = DateTime::Format::Strptime->new( - pattern => '%Y-%m-%dT%H:%M:%S%z', - time_zone => 'Europe/Berlin', - ); - # We've already got a list of connecting trains; this function # only adds further information to them. We ignore errors, as # partial data is better than no data. @@ -282,25 +276,19 @@ sub get_connecting_trains_p { next; } for my $hafas_train (@hafas_trains) { - if ( $hafas_train->{line}{fahrtNr} + if ( $hafas_train->number + and $hafas_train->number == $iris_train->[0]->train_no ) { - for my $stop ( - @{ $hafas_train->{nextStopovers} // [] } ) - { - if ( $stop->{stop}{name} - and $stop->{stop}{name} eq $iris_train->[1] - and $stop->{arrival} ) + for my $stop ( $hafas_train->route ) { + if ( $stop->{name} + and $stop->{name} eq $iris_train->[1] + and $stop->{arr} ) { - $iris_train->[2] = $strp->parse_datetime( - $stop->{arrival} ); - if ( $iris_train->[2] - and $iris_train->[0]->departure_delay - and $stop->{arrival} eq - $stop->{plannedArrival} ) + $iris_train->[2] = $stop->{arr}; + if ( $iris_train->[0]->departure_delay + and not $stop->{arr_delay} ) { -# If the departure is delayed, but the arrival supposedly on time, we assume that this is an API issue and manually compute the expected arrival time. -# This avoids cases where a connection is shown as arriving at its destination before having departed at a previous stop. $iris_train->[2] ->add( minutes => $iris_train->[0] ->departure_delay ); @@ -315,29 +303,20 @@ sub get_connecting_trains_p { if ( $use_history & 0x04 and @{$lt_stops} ) { my %via_count = map { $_ => 0 } @{$lt_stops}; for my $hafas_train (@hafas_trains) { - for - my $stop ( @{ $hafas_train->{nextStopovers} // [] } ) - { + for my $stop ( $hafas_train->route ) { for my $dest ( @{$lt_stops} ) { - if ( $stop->{stop}{name} - and $stop->{stop}{name} eq $dest + if ( $stop->{name} + and $stop->{name} eq $dest and $via_count{$dest} < 2 - and $hafas_train->{when} ) + and $hafas_train->datetime ) { - my $departure = $strp->parse_datetime( - $hafas_train->{when} ); - my $arrival - = $strp->parse_datetime( - $stop->{arrival} ); - my $delay = undef; - if ( defined $hafas_train->{delay} ) { - $delay = $hafas_train->{delay} / 60; - if ( $delay - and $stop->{arrival} eq - $stop->{plannedArrival} ) - { - $arrival->add( minutes => $delay ); - } + my $departure = $hafas_train->datetime; + my $arrival = $stop->{arr}; + my $delay = $hafas_train->delay; + if ( $delay + and $stop->{arr} == $stop->{sched_arr} ) + { + $arrival->add( minutes => $delay ); } if ( $departure->epoch >= $exclude_before ) { @@ -346,9 +325,7 @@ sub get_connecting_trains_p { @transit_fyi, [ { - line => - $hafas_train->{line} - {name}, + line => $hafas_train->line, departure => $departure, departure_delay => $delay }, @@ -1164,18 +1141,18 @@ sub station { } )->catch( sub { - my ($status) = @_; - if ( $status->{errstr} ) { + my ($err) = @_; + if ( ref($err) eq 'HASH' ) { $self->render( 'landingpage', version => $self->app->config->{version} // 'UNKNOWN', with_autocomplete => 1, with_geolocation => 1, - error => $status->{errstr} + error => $err->{errstr}, ); } else { - $self->render( 'exception', exception => $status ); + $self->render( 'exception', exception => $err ); } } )->wait; diff --git a/lib/Travelynx/Helper/HAFAS.pm b/lib/Travelynx/Helper/HAFAS.pm index 7b42384..a8fbbfb 100644 --- a/lib/Travelynx/Helper/HAFAS.pm +++ b/lib/Travelynx/Helper/HAFAS.pm @@ -83,6 +83,22 @@ sub get_json_p { return $promise; } +sub get_departures_p { + my ( $self, %opt ) = @_; + + my $when = DateTime->now( time_zone => 'Europe/Berlin' ) + ->subtract( minutes => $opt{lookbehind} ); + return Travel::Status::DE::HAFAS->new_p( + station => $opt{eva}, + datetime => $when, + duration => $opt{lookahead}, + results => 120, + cache => $self->{realtime_cache}, + promise => 'Mojo::Promise', + user_agent => $self->{user_agent}->request_timeout(5), + ); +} + sub get_route_timestamps_p { my ( $self, %opt ) = @_;