summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-09-06 11:15:19 +0200
committerDaniel Friesel <derf@finalrewind.org>2020-09-06 11:15:19 +0200
commitb2824fcfb9ef1fcd9b09d74c472aa783d6fe0db5 (patch)
tree06581073223bab6577987be420478c9393f35fb5
parent9bdae29a17e9342eb85db11d97935789492e7dc7 (diff)
add wagonorder helper
-rw-r--r--lib/DBInfoscreen.pm15
-rw-r--r--lib/DBInfoscreen/Controller/Stationboard.pm51
-rw-r--r--lib/DBInfoscreen/Helper/HAFAS.pm12
-rw-r--r--lib/DBInfoscreen/Helper/Wagonorder.pm69
4 files changed, 96 insertions, 51 deletions
diff --git a/lib/DBInfoscreen.pm b/lib/DBInfoscreen.pm
index 700a4c9..b31a1a7 100644
--- a/lib/DBInfoscreen.pm
+++ b/lib/DBInfoscreen.pm
@@ -6,6 +6,7 @@ use Mojo::Base 'Mojolicious';
use Cache::File;
use DBInfoscreen::Helper::HAFAS;
+use DBInfoscreen::Helper::Wagonorder;
use File::Slurp qw(read_file);
use JSON;
use Travel::Status::DE::HAFAS;
@@ -18,6 +19,8 @@ no if $] >= 5.018, warnings => 'experimental::smartmatch';
our $VERSION = qx{git describe --dirty} || '0.05';
+chomp $VERSION;
+
my %default = (
backend => 'iris',
mode => 'app',
@@ -109,6 +112,18 @@ sub startup {
);
$self->helper(
+ wagonorder => sub {
+ my ($self) = @_;
+ state $hafas = DBInfoscreen::Helper::Wagonorder->new(
+ log => $self->app->log,
+ cache => $self->app->cache_iris_main,
+ user_agent => $self->ua,
+ version => $VERSION,
+ );
+ }
+ );
+
+ $self->helper(
'handle_no_results' => sub {
my ( $self, $backend, $station, $errstr ) = @_;
diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm
index c1d952c..f8426c5 100644
--- a/lib/DBInfoscreen/Controller/Stationboard.pm
+++ b/lib/DBInfoscreen/Controller/Stationboard.pm
@@ -105,47 +105,6 @@ sub log_api_access {
return;
}
-sub check_wagonorder_with_wings {
- my ( $ua, $cache, $train, $wr_link ) = @_;
-
- if ( check_wagonorder( $ua, $cache, $train->train_no, $wr_link ) ) {
- return 1;
- }
- elsif ( $train->is_wing ) {
- my $wing = $train->wing_of;
- if ( check_wagonorder( $ua, $cache, $wing->train_no, $wr_link ) ) {
- return 1;
- }
- }
- return;
-}
-
-sub check_wagonorder {
- my ( $ua, $cache, $train_no, $wr_link ) = @_;
-
- my $url
- = "https://lib.finalrewind.org/dbdb/has_wagonorder/${train_no}/${wr_link}";
-
- if ( my $content = $cache->get($url) ) {
- return $content eq 'y' ? 1 : undef;
- }
-
- $ua->request_timeout(2);
- my $res = eval { $ua->head($url)->result };
-
- if ($@) {
- return;
- }
- if ( $res->is_error ) {
- $cache->set( $url, 'n' );
- return;
- }
- else {
- $cache->set( $url, 'y' );
- return 1;
- }
-}
-
sub get_results_for {
my ( $backend, $station, %opt ) = @_;
my $data;
@@ -455,13 +414,9 @@ sub render_train {
$departure->{trip_id} = $self->hafas->get_tripid($result);
- if (
- $departure->{wr_link}
- and not check_wagonorder_with_wings(
- $self->ua, $self->app->cache_iris_main,
- $result, $departure->{wr_link}
- )
- )
+ if ( $departure->{wr_link}
+ and
+ not $self->wagonorder->is_available( $result, $departure->{wr_link} ) )
{
$departure->{wr_link} = undef;
}
diff --git a/lib/DBInfoscreen/Helper/HAFAS.pm b/lib/DBInfoscreen/Helper/HAFAS.pm
index 18a3eda..c9a2ba2 100644
--- a/lib/DBInfoscreen/Helper/HAFAS.pm
+++ b/lib/DBInfoscreen/Helper/HAFAS.pm
@@ -29,9 +29,11 @@ sub hafas_rest_req {
return $content;
}
- my $res = eval { $self->{user_agent}->get($url)->result; };
+ my $res
+ = eval { $self->{user_agent}->get( $url => $self->{header} )->result; };
if ($@) {
+ $self->{log}->debug("hafas_rest_req($url): $@");
return;
}
if ( $res->is_error ) {
@@ -52,9 +54,11 @@ sub hafas_json_req {
return $content;
}
- my $res = eval { $self->{user_agent}->get($url)->result };
+ my $res
+ = eval { $self->{user_agent}->get( $url => $self->{header} )->result };
if ($@) {
+ $self->{log}->debug("hafas_json_req($url): $@");
return;
}
if ( $res->is_error ) {
@@ -82,9 +86,11 @@ sub hafas_xml_req {
return $content;
}
- my $res = eval { $self->{user_agent}->get($url)->result };
+ my $res
+ = eval { $self->{user_agent}->get( $url => $self->{header} )->result };
if ($@) {
+ $self->{log}->debug("hafas_xml_req($url): $@");
return;
}
if ( $res->is_error ) {
diff --git a/lib/DBInfoscreen/Helper/Wagonorder.pm b/lib/DBInfoscreen/Helper/Wagonorder.pm
new file mode 100644
index 0000000..98c22ca
--- /dev/null
+++ b/lib/DBInfoscreen/Helper/Wagonorder.pm
@@ -0,0 +1,69 @@
+package DBInfoscreen::Helper::Wagonorder;
+
+use strict;
+use warnings;
+use 5.020;
+
+use DateTime;
+use Encode qw(decode encode);
+use Mojo::JSON qw(decode_json);
+use XML::LibXML;
+
+sub new {
+ my ( $class, %opt ) = @_;
+
+ my $version = $opt{version};
+
+ $opt{header}
+ = { 'User-Agent' =>
+ "dbf/${version} +https://finalrewind.org/projects/db-fakedisplay" };
+
+ return bless( \%opt, $class );
+
+}
+
+sub is_available {
+ my ( $self, $train, $wr_link ) = @_;
+
+ if ( $self->check_wagonorder( $train->train_no, $wr_link ) ) {
+ return 1;
+ }
+ elsif ( $train->is_wing ) {
+ my $wing = $train->wing_of;
+ if ( $self->check_wagonorder( $wing->train_no, $wr_link ) ) {
+ return 1;
+ }
+ }
+ return;
+}
+
+sub check_wagonorder {
+ my ( $self, $train_no, $wr_link ) = @_;
+
+ my $url
+ = "https://lib.finalrewind.org/dbdb/has_wagonorder/${train_no}/${wr_link}";
+ my $cache = $self->{cache};
+
+ if ( my $content = $self->{cache}->get($url) ) {
+ return $content eq 'y' ? 1 : undef;
+ }
+
+ my $ua = $self->{user_agent}->request_timeout(2);
+
+ my $res = eval { $ua->head( $url => $self->{header} )->result };
+
+ if ($@) {
+ $self->{log}->debug("check_wagonorder($url): $@");
+ return;
+ }
+ if ( $res->is_error ) {
+ $cache->set( $url, 'n' );
+ return;
+ }
+ else {
+ $cache->set( $url, 'y' );
+ return 1;
+ }
+}
+
+1;