From ac2a23c3faae849f3a13583bb4a3b7dc4e39fd81 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 16 Apr 2020 09:47:31 +0200 Subject: Fix occasionally incorrect TripIDs (and thus incorrect polylines) Until now, tripIDs were cached based on station and departure timestamp. These are identical for any two trains departing at the same time at the same station, leading to one of those getting being assigned a wrong tripID. From now on, only the JSON reported by transport.rest is cached -- tripIDs are always recomputed based on it. --- lib/Travelynx.pm | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 4014761..d5dca3f 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -2135,27 +2135,15 @@ sub startup { = "https://2.db.transport.rest/stations/${eva}/arrivals?duration=5&when=$dep_ts"; } - if ( my $content = $cache->get($url) ) { - $promise->resolve($content); - return $promise; - } - - $self->ua->request_timeout(5)->get_p( - $url => { - 'User-Agent' => 'travelynx/' . $self->app->config->{version} - } - )->then( + $self->get_hafas_rest_p($url)->then( sub { - my ($tx) = @_; - my $body = decode( 'utf-8', $tx->res->body ); - my $json = JSON->new->decode($body); + my ($json) = @_; for my $result ( @{$json} ) { if ( $result->{line} and $result->{line}{fahrtNr} == $train->train_no ) { my $trip_id = $result->{tripId}; - $cache->set( $url, $trip_id ); $promise->resolve($trip_id); return; } @@ -2173,6 +2161,36 @@ sub startup { } ); + $self->helper( + 'get_hafas_rest_p' => sub { + my ( $self, $url ) = @_; + + my $cache = $self->app->cache_iris_main; + my $promise = Mojo::Promise->new; + + if ( my $content = $cache->thaw($url) ) { + $promise->resolve($content); + return $promise; + } + + $self->ua->request_timeout(5)->get_p($url)->then( + sub { + my ($tx) = @_; + my $json = JSON->new->decode( $tx->res->body ); + $cache->freeze( $url, $json ); + $promise->resolve($json); + } + )->catch( + sub { + my ($err) = @_; + $self->app->log->warn("get($url): $err"); + $promise->reject($err); + } + )->wait; + return $promise; + } + ); + $self->helper( 'get_hafas_json_p' => sub { my ( $self, $url ) = @_; -- cgit v1.2.3