summaryrefslogtreecommitdiff
path: root/lib/Travelynx.pm
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-04-16 09:47:31 +0200
committerDaniel Friesel <derf@finalrewind.org>2020-04-16 09:47:31 +0200
commitac2a23c3faae849f3a13583bb4a3b7dc4e39fd81 (patch)
treea8306ce9b8865f69e2ab1cb24f66b8923e1b5a6c /lib/Travelynx.pm
parentd66cf00d9d424ae8dfb3e0a6edeb3ddab2e8308b (diff)
Fix occasionally incorrect TripIDs (and thus incorrect polylines)1.15.12
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.
Diffstat (limited to 'lib/Travelynx.pm')
-rwxr-xr-xlib/Travelynx.pm46
1 files 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;
}
@@ -2174,6 +2162,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 ) = @_;