summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2021-02-04 18:12:57 +0100
committerDaniel Friesel <derf@finalrewind.org>2021-02-04 18:12:57 +0100
commitd2ecb154f2dd7f9ac0e4cae6fb5d7f2be2404e08 (patch)
tree9bfac6f603ad5f9b4bc4c1b25c38a74125bdc609 /lib
parentb52687b08e365610a213de348fbb94a01fcbcd68 (diff)
fall back to apps-bahn if ist-wr does not know about a train3.10.5
Diffstat (limited to 'lib')
-rw-r--r--lib/DBInfoscreen/Helper/Wagonorder.pm62
1 files 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;