From 9223ba5fa1a3ab203bba1fb54e9d2a5772f0b0d8 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 6 Feb 2021 12:31:35 +0100 Subject: fall back to apps-bahn if ist-wr does not know about a train --- lib/Travelynx.pm | 3 ++- lib/Travelynx/Helper/DBDB.pm | 23 +++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index ac3ae8d..882aab6 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -1242,7 +1242,8 @@ sub startup { $self->dbdb->has_wagonorder_p( $train->sched_departure, $train->train_no )->then( sub { - return $self->dbdb->get_wagonorder_p( + my ($api) = @_; + return $self->dbdb->get_wagonorder_p( $api, $train->sched_departure, $train->train_no ); } )->then( diff --git a/lib/Travelynx/Helper/DBDB.pm b/lib/Travelynx/Helper/DBDB.pm index 916b4cb..4baf3ed 100644 --- a/lib/Travelynx/Helper/DBDB.pm +++ b/lib/Travelynx/Helper/DBDB.pm @@ -1,4 +1,5 @@ package Travelynx::Helper::DBDB; + # Copyright (C) 2020 Daniel Friesel # # SPDX-License-Identifier: AGPL-3.0-or-later @@ -34,21 +35,22 @@ sub has_wagonorder_p { my $promise = Mojo::Promise->new; if ( my $content = $cache->get($url) ) { - if ( $content eq 'y' ) { - return $promise->resolve; - } - elsif ( $content eq 'n' ) { + if ( $content eq 'n' ) { return $promise->reject; } + else { + return $promise->resolve($content); + } } - $self->{user_agent}->request_timeout(5)->head_p( $url => $self->{header} ) + $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) ->then( sub { my ($tx) = @_; if ( $tx->result->is_success ) { - $cache->set( $url, 'y' ); - $promise->resolve; + my $body = $tx->result->body; + $cache->set( $url, $body ); + $promise->resolve($body); } else { $cache->set( $url, 'n' ); @@ -67,11 +69,16 @@ sub has_wagonorder_p { } sub get_wagonorder_p { - my ( $self, $ts, $train_no ) = @_; + my ( $self, $api, $ts, $train_no ) = @_; my $api_ts = $ts->strftime('%Y%m%d%H%M'); my $url = "https://ist-wr.noncd.db.de/wagenreihung/1.0/${train_no}/${api_ts}"; + if ( $api !~ m{i} and $api =~ m{a} ) { + $url + = "https://www.apps-bahn.de/wr/wagenreihung/1.0/${train_no}/${api_ts}"; + } + my $cache = $self->{cache}; my $promise = Mojo::Promise->new; -- cgit v1.2.3