From b2824fcfb9ef1fcd9b09d74c472aa783d6fe0db5 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 6 Sep 2020 11:15:19 +0200 Subject: add wagonorder helper --- lib/DBInfoscreen/Helper/Wagonorder.pm | 69 +++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 lib/DBInfoscreen/Helper/Wagonorder.pm (limited to 'lib/DBInfoscreen/Helper/Wagonorder.pm') 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; -- cgit v1.2.3