From ca372be9811a183d11a00abd539442a9e2e53cae Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 13 Aug 2021 14:52:32 +0200 Subject: Wagonorder: send request to www.apps-bahn.de when availability is unknown www.apps-bahn.de/wr no longer serves "tryThese" suggestions. So, unless we know from lib.finalrewind.org/dbdb/has_wagonorder (via ist-wr.noncd.db.de) that the trais definitely has a wagon order, we send a wagon order request to www.apps-bahn.de/wr to determine whether it has one or not. We do cache the result though, so if a user clicks on the wagon order link, we don't need to request it again. --- lib/DBInfoscreen/Helper/Wagonorder.pm | 67 ++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 21 deletions(-) (limited to 'lib/DBInfoscreen') diff --git a/lib/DBInfoscreen/Helper/Wagonorder.pm b/lib/DBInfoscreen/Helper/Wagonorder.pm index dbae538..469eda6 100644 --- a/lib/DBInfoscreen/Helper/Wagonorder.pm +++ b/lib/DBInfoscreen/Helper/Wagonorder.pm @@ -108,6 +108,7 @@ sub head_dbdb_p { my $cache = $self->{main_cache}; if ( my $content = $cache->get($url) ) { + $self->{log}->debug("wagonorder->head_dbdb_p($url): cached ($content)"); if ( $content eq 'y' ) { return $promise->resolve; } @@ -121,10 +122,12 @@ sub head_dbdb_p { sub { my ($tx) = @_; if ( $tx->result->is_success ) { + $self->{log}->debug("wagonorder->head_dbdb_p($url): y"); $cache->set( $url, 'y' ); $promise->resolve; } else { + $self->{log}->debug("wagonorder->head_dbdb_p($url): n"); $cache->set( $url, 'n' ); $promise->reject; } @@ -132,6 +135,7 @@ sub head_dbdb_p { } )->catch( sub { + $self->{log}->debug("wagonorder->head_dbdb_p($url): n"); $cache->set( $url, 'n' ); $promise->reject; return; @@ -150,35 +154,50 @@ sub has_cycle_p { sub check_wagonorder_p { my ( $self, $train_no, $wr_link ) = @_; - return $self->get_dbdb_p( + my $promise = Mojo::Promise->new; + + $self->head_dbdb_p( "https://lib.finalrewind.org/dbdb/has_wagonorder/${train_no}/${wr_link}" - ); + )->then( + sub { + $promise->resolve; + return; + } + )->catch( + sub { + $self->get_p( $train_no, $wr_link )->then( + sub { + $promise->resolve; + return; + } + )->catch( + sub { + $promise->reject; + return; + } + )->wait; + return; + } + )->wait; + + return $promise; } sub get_p { my ( $self, $train_no, $api_ts ) = @_; 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}"; - } - } + = "https://www.apps-bahn.de/wr/wagenreihung/1.0/${train_no}/${api_ts}"; my $cache = $self->{realtime_cache}; my $promise = Mojo::Promise->new; if ( my $content = $cache->thaw($url) ) { - $self->{log}->debug("GET $url (cached)"); + $self->{log}->debug("wagonorder->get_p($url): cached"); + if ( $content->{error} ) { + return $promise->reject($content); + } return $promise->resolve($content); } @@ -188,15 +207,21 @@ sub get_p { my ($tx) = @_; if ( my $err = $tx->error ) { - $self->{log}->warn( + my $json = { + error => { + id => $err->{code}, + msg => $err->{message} + } + }; + $self->{log}->debug( "wagonorder->get_p($url): HTTP $err->{code} $err->{message}" ); - $promise->reject( - "GET $url returned HTTP $err->{code} $err->{message}"); + $cache->freeze( $url, $json ); + $promise->reject($json); return; } - $self->{log}->debug("GET $url (OK)"); + $self->{log}->debug("wagonorder->get_p($url): OK"); my $json = $tx->res->json; $cache->freeze( $url, $json ); @@ -206,7 +231,7 @@ sub get_p { )->catch( sub { my ($err) = @_; - $self->{log}->warn("GET $url: $err"); + $self->{log}->warn("wagonorder->get_p($url): $err"); $promise->reject("GET $url: $err"); return; } -- cgit v1.2.3