From 717cc18a403d6705c64a9a6fd43578c1efbb159f Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 6 Aug 2020 16:04:12 +0200 Subject: Move get_departures to a separate IRIS helper --- lib/Travelynx.pm | 95 +++++++++++++---------------------- lib/Travelynx/Command/work.pm | 12 ++++- lib/Travelynx/Controller/Traveling.pm | 7 ++- lib/Travelynx/Helper/IRIS.pm | 71 ++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 63 deletions(-) create mode 100644 lib/Travelynx/Helper/IRIS.pm diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index ea53742..eb5ac63 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -19,6 +19,7 @@ use Travel::Status::DE::DBWagenreihung; use Travel::Status::DE::IRIS; use Travel::Status::DE::IRIS::Stations; use Travelynx::Helper::HAFAS; +use Travelynx::Helper::IRIS; use Travelynx::Helper::Sendmail; use Travelynx::Model::Users; use XML::LibXML; @@ -277,6 +278,18 @@ sub startup { } ); + $self->helper( + iris => sub { + my ($self) = @_; + state $hafas = Travelynx::Helper::IRIS->new( + log => $self->app->log, + main_cache => $self->app->cache_iris_main, + realtime_cache => $self->app->cache_iris_rt, + version => $self->app->config->{version}, + ); + } + ); + $self->helper( pg => sub { my ($self) = @_; @@ -332,62 +345,6 @@ sub startup { } ); - $self->helper( - 'get_departures' => sub { - my ( $self, $station, $lookbehind, $lookahead, $with_related ) = @_; - - $lookbehind //= 180; - $lookahead //= 30; - $with_related //= 0; - - my @station_matches - = Travel::Status::DE::IRIS::Stations::get_station($station); - - if ( @station_matches == 1 ) { - $station = $station_matches[0][0]; - my $status = Travel::Status::DE::IRIS->new( - station => $station, - main_cache => $self->app->cache_iris_main, - realtime_cache => $self->app->cache_iris_rt, - keep_transfers => 1, - lookbehind => 20, - datetime => DateTime->now( time_zone => 'Europe/Berlin' ) - ->subtract( minutes => $lookbehind ), - lookahead => $lookbehind + $lookahead, - lwp_options => { - timeout => 10, - agent => 'travelynx/' . $self->app->config->{version}, - }, - with_related => $with_related, - ); - return { - results => [ $status->results ], - errstr => $status->errstr, - station_ds100 => - ( $status->station ? $status->station->{ds100} : undef ), - station_eva => - ( $status->station ? $status->station->{uic} : undef ), - station_name => - ( $status->station ? $status->station->{name} : undef ), - related_stations => [ $status->related_stations ], - }; - } - elsif ( @station_matches > 1 ) { - return { - results => [], - errstr => 'Mehrdeutiger Stationsname. Mögliche Eingaben: ' - . join( q{, }, map { $_->[1] } @station_matches ), - }; - } - else { - return { - results => [], - errstr => 'Unbekannte Station', - }; - } - } - ); - $self->helper( 'grep_unknown_stations' => sub { my ( $self, @stations ) = @_; @@ -529,7 +486,11 @@ sub startup { $uid //= $self->current_user->{id}; - my $status = $self->get_departures( $station, 140, 40, 0 ); + my $status = $self->iris->get_departures( + station => $station, + lookbehind => 140, + lookahead => 40 + ); if ( $status->{errstr} ) { return ( undef, $status->{errstr} ); } @@ -708,7 +669,11 @@ sub startup { my ( $self, $station, $force, $uid ) = @_; my $db = $self->pg->db; - my $status = $self->get_departures( $station, 120, 120, 0 ); + my $status = $self->iris->get_departures( + station => $station, + lookbehind => 120, + lookahead => 120 + ); $uid //= $self->current_user->{id}; my $user = $self->get_user_status($uid); my $train_id = $user->{train_id}; @@ -750,7 +715,12 @@ sub startup { # While at it, we increase the lookahead to handle long journeys as # well. if ( not $train ) { - $status = $self->get_departures( $station, 120, 180, 1 ); + $status = $self->iris->get_departures( + station => $station, + lookbehind => 120, + lookahead => 180, + with_related => 1 + ); ($train) = List::Util::first { $_->train_id eq $train_id } @{ $status->{results} }; if ( $train @@ -2183,7 +2153,12 @@ sub startup { return; } - my $stationboard = $self->get_departures( $eva, 10, 40, 1 ); + my $stationboard = $self->iris->get_departures( + station => $eva, + lookbehind => 10, + lookahead => 40, + with_related => 1 + ); if ( $stationboard->{errstr} ) { return; } diff --git a/lib/Travelynx/Command/work.pm b/lib/Travelynx/Command/work.pm index 727a694..9c870d8 100644 --- a/lib/Travelynx/Command/work.pm +++ b/lib/Travelynx/Command/work.pm @@ -35,7 +35,11 @@ sub run { eval { if ( $now->epoch - $entry->{real_dep_ts} < 900 ) { - my $status = $self->app->get_departures( $dep, 30, 30 ); + my $status = $self->app->iris->get_departures( + station => $dep, + lookbehind => 30, + lookahead => 30 + ); if ( $status->{errstr} ) { die("get_departures($dep): $status->{errstr}\n"); } @@ -123,7 +127,11 @@ sub run { or $now->epoch - $entry->{real_arr_ts} < 600 ) ) { - my $status = $self->app->get_departures( $arr, 20, 220 ); + my $status = $self->app->iris->get_departures( + station => $arr, + lookbehind => 20, + lookahead => 220 + ); if ( $status->{errstr} ) { die("get_departures($arr): $status->{errstr}\n"); } diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index 05bbccd..e33009f 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -423,7 +423,12 @@ sub station { my $station = $self->stash('station'); my $train = $self->param('train'); - my $status = $self->get_departures( $station, 120, 30, 1 ); + my $status = $self->iris->get_departures( + station => $station, + lookbehind => 120, + lookahead => 30, + with_related => 1 + ); if ( $status->{errstr} ) { $self->render( diff --git a/lib/Travelynx/Helper/IRIS.pm b/lib/Travelynx/Helper/IRIS.pm new file mode 100644 index 0000000..3b98a51 --- /dev/null +++ b/lib/Travelynx/Helper/IRIS.pm @@ -0,0 +1,71 @@ +package Travelynx::Helper::IRIS; + +use strict; +use warnings; +use 5.020; + +use Travel::Status::DE::IRIS; + +sub new { + my ( $class, %opt ) = @_; + + return bless( \%opt, $class ); +} + +sub get_departures { + my ( $self, %opt ) = @_; + my $station = $opt{station}; + my $lookbehind = $opt{lookbehind} // 180; + my $lookahead = $opt{lookahead} // 30; + my $with_related = $opt{with_related} // 0; + + my @station_matches + = Travel::Status::DE::IRIS::Stations::get_station($station); + + if ( @station_matches == 1 ) { + $station = $station_matches[0][0]; + my $status = Travel::Status::DE::IRIS->new( + station => $station, + main_cache => $self->{main_cache}, + realtime_cache => $self->{realtime_cache}, + keep_transfers => 1, + lookbehind => 20, + datetime => DateTime->now( time_zone => 'Europe/Berlin' ) + ->subtract( minutes => $lookbehind ), + lookahead => $lookbehind + $lookahead, + lwp_options => { + timeout => 10, + agent => 'travelynx/' + . $self->{version} + . ' +https://travelynx.de', + }, + with_related => $with_related, + ); + return { + results => [ $status->results ], + errstr => $status->errstr, + station_ds100 => + ( $status->station ? $status->station->{ds100} : undef ), + station_eva => + ( $status->station ? $status->station->{uic} : undef ), + station_name => + ( $status->station ? $status->station->{name} : undef ), + related_stations => [ $status->related_stations ], + }; + } + elsif ( @station_matches > 1 ) { + return { + results => [], + errstr => 'Mehrdeutiger Stationsname. Mögliche Eingaben: ' + . join( q{, }, map { $_->[1] } @station_matches ), + }; + } + else { + return { + results => [], + errstr => 'Unbekannte Station', + }; + } +} + +1; -- cgit v1.2.3