summaryrefslogtreecommitdiff
path: root/lib/DBInfoscreen/Helper
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 /lib/DBInfoscreen/Helper
parent9bdae29a17e9342eb85db11d97935789492e7dc7 (diff)
add wagonorder helper
Diffstat (limited to 'lib/DBInfoscreen/Helper')
-rw-r--r--lib/DBInfoscreen/Helper/HAFAS.pm12
-rw-r--r--lib/DBInfoscreen/Helper/Wagonorder.pm69
2 files changed, 78 insertions, 3 deletions
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;