From d2ecb154f2dd7f9ac0e4cae6fb5d7f2be2404e08 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 4 Feb 2021 18:12:57 +0100 Subject: fall back to apps-bahn if ist-wr does not know about a train --- lib/DBInfoscreen/Helper/Wagonorder.pm | 62 ++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/lib/DBInfoscreen/Helper/Wagonorder.pm b/lib/DBInfoscreen/Helper/Wagonorder.pm index 9e0612e..dbae538 100644 --- a/lib/DBInfoscreen/Helper/Wagonorder.pm +++ b/lib/DBInfoscreen/Helper/Wagonorder.pm @@ -30,7 +30,8 @@ sub is_available_p { $self->check_wagonorder_p( $train->train_no, $wr_link )->then( sub { - $promise->resolve; + my ($body) = @_; + $promise->resolve($body); return; }, sub { @@ -45,7 +46,8 @@ sub is_available_p { } )->then( sub { - $promise->resolve; + my ($body) = @_; + $promise->resolve($body); return; }, sub { @@ -57,6 +59,47 @@ sub is_available_p { return $promise; } +sub get_dbdb_p { + my ( $self, $url ) = @_; + + my $promise = Mojo::Promise->new; + + my $cache = $self->{main_cache}; + + if ( my $content = $cache->get($url) ) { + if ($content) { + return $promise->resolve($content); + } + else { + return $promise->reject; + } + } + + $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) + ->then( + sub { + my ($tx) = @_; + if ( $tx->result->is_success ) { + my $body = $tx->result->body; + $cache->set( $url, $body ); + $promise->resolve($body); + } + else { + $cache->set( $url, q{} ); + $promise->reject; + } + return; + } + )->catch( + sub { + $cache->set( $url, q{} ); + $promise->reject; + return; + } + )->wait; + return $promise; +} + sub head_dbdb_p { my ( $self, $url ) = @_; @@ -106,9 +149,8 @@ sub has_cycle_p { sub check_wagonorder_p { my ( $self, $train_no, $wr_link ) = @_; - return Mojo::Promise->new->resolve; - return $self->head_dbdb_p( + return $self->get_dbdb_p( "https://lib.finalrewind.org/dbdb/has_wagonorder/${train_no}/${wr_link}" ); } @@ -119,6 +161,18 @@ sub get_p { my $url = "https://ist-wr.noncd.db.de/wagenreihung/1.0/${train_no}/${api_ts}"; + if ( + my $content = $self->{main_cache}->get( +"https://lib.finalrewind.org/dbdb/has_wagonorder/${train_no}/${api_ts}" + ) + ) + { + if ( $content !~ m{i} and $content =~ m{a} ) { + $url + = "https://www.apps-bahn.de/wr/wagenreihung/1.0/${train_no}/${api_ts}"; + } + } + my $cache = $self->{realtime_cache}; my $promise = Mojo::Promise->new; -- cgit v1.2.3