From c3c7a0c78b30cc7ef2b836cca4eb0a4e110955da Mon Sep 17 00:00:00 2001 From: Derf Null Date: Mon, 27 Mar 2023 21:03:25 +0200 Subject: [PATCH] stationboard: add HAFAS support --- lib/Travelynx/Controller/Traveling.pm | 61 +++++++++++++++++++++------ lib/Travelynx/Helper/HAFAS.pm | 2 +- templates/_departures_hafas.html.ep | 44 +++++++++++++++++++ templates/_departures_iris.html.ep | 52 +++++++++++++++++++++++ templates/departures.html.ep | 58 +++---------------------- templates/exception.html.ep | 2 +- 6 files changed, 153 insertions(+), 66 deletions(-) create mode 100644 templates/_departures_hafas.html.ep create mode 100644 templates/_departures_iris.html.ep diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index 00cc02a..e9a8bb5 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -795,22 +795,56 @@ sub station { my $train = $self->param('train'); $self->render_later; - $self->iris->get_departures_p( - station => $station, - lookbehind => 120, - lookahead => 30, - with_related => 1 - )->then( + + my $use_hafas = $self->param('hafas'); + my $promise; + if ($use_hafas) { + $promise = $self->hafas->get_departures_p( + eva => $station, + lookbehind => 120, + lookahead => 30, + ); + } + else { + $promise = $self->iris->get_departures_p( + station => $station, + lookbehind => 120, + lookahead => 30, + with_related => 1, + ); + } + $promise->then( sub { my ($status) = @_; - # You can't check into a train which terminates here - my @results = grep { $_->departure } @{ $status->{results} }; + my @results; + if ($use_hafas) { + my $now = $self->now->epoch; + @results = map { $_->[0] } + sort { $b->[1] <=> $a->[1] } + map { [ $_, $_->datetime->epoch ] } + grep { + ( $_->datetime // $_->sched_datetime )->epoch + < $now + 30 * 60 + } $status->results; + $status = { + station_eva => $status->station->{eva}, + station_name => + List::Util::reduce { length($a) < length($b) ? $a : $b } + @{ $status->station->{names} }, + related_stations => [], + }; + } + else { + # You can't check into a train which terminates here + @results = grep { $_->departure } @{ $status->{results} }; - @results = map { $_->[0] } - sort { $b->[1] <=> $a->[1] } - map { [ $_, $_->departure->epoch // $_->sched_departure->epoch ] } - @results; + @results = map { $_->[0] } + sort { $b->[1] <=> $a->[1] } + map { + [ $_, $_->departure->epoch // $_->sched_departure->epoch ] + } @results; + } my $connections_p; if ($train) { @@ -842,6 +876,7 @@ sub station { 'departures', eva => $status->{station_eva}, results => \@results, + hafas => $use_hafas, station => $status->{station_name}, related_stations => $status->{related_stations}, connections => $connecting_trains, @@ -856,6 +891,7 @@ sub station { 'departures', eva => $status->{station_eva}, results => \@results, + hafas => $use_hafas, station => $status->{station_name}, related_stations => $status->{related_stations}, title => "travelynx: $status->{station_name}", @@ -870,6 +906,7 @@ sub station { 'departures', eva => $status->{station_eva}, results => \@results, + hafas => $use_hafas, station => $status->{station_name}, related_stations => $status->{related_stations}, title => "travelynx: $status->{station_name}", diff --git a/lib/Travelynx/Helper/HAFAS.pm b/lib/Travelynx/Helper/HAFAS.pm index 9bff723..a418b61 100644 --- a/lib/Travelynx/Helper/HAFAS.pm +++ b/lib/Travelynx/Helper/HAFAS.pm @@ -91,7 +91,7 @@ sub get_departures_p { station => $opt{eva}, datetime => $when, duration => $opt{lookahead}, - results => 120, + results => 300, cache => $self->{realtime_cache}, promise => 'Mojo::Promise', user_agent => $self->{user_agent}->request_timeout(5), diff --git a/templates/_departures_hafas.html.ep b/templates/_departures_hafas.html.ep new file mode 100644 index 0000000..5b1a057 --- /dev/null +++ b/templates/_departures_hafas.html.ep @@ -0,0 +1,44 @@ + + +% my $orientation_bar_shown = param('train'); +% my $now_epoch = now()->epoch; +% for my $result (@{$results}) { + % my $td_class = ''; + % my $link_class = 'action-checkin'; + % if ($result->is_cancelled) { + % $td_class = "cancelled"; + % $link_class = 'action-cancelled-from'; + % } + % if (not $orientation_bar_shown and $result->datetime->epoch < $now_epoch) { + % $orientation_bar_shown = 1; + + + + + + % } + + + + + +% } + +
+ + — Anfragezeitpunkt — + +
diff --git a/templates/_departures_iris.html.ep b/templates/_departures_iris.html.ep new file mode 100644 index 0000000..b24c3f1 --- /dev/null +++ b/templates/_departures_iris.html.ep @@ -0,0 +1,52 @@ + + +% my $orientation_bar_shown = param('train'); +% my $now_epoch = now()->epoch; +% for my $result (@{$results}) { + % my $td_class = ''; + % my $link_class = 'action-checkin'; + % if ($result->departure_is_cancelled) { + % $td_class = "cancelled"; + % $link_class = 'action-cancelled-from'; + % } + % if (not $orientation_bar_shown and $result->departure->epoch < $now_epoch) { + % $orientation_bar_shown = 1; + + + + + + % } + + + + + +% } + +
+ + — Anfragezeitpunkt — + +
diff --git a/templates/departures.html.ep b/templates/departures.html.ep index 25e752a..857be74 100644 --- a/templates/departures.html.ep +++ b/templates/departures.html.ep @@ -63,58 +63,12 @@ und maximal 120 Minuten nach Abfahrt möglich. % }

- - - % my $orientation_bar_shown = param('train'); - % my $now_epoch = now()->epoch; - % for my $result (@{$results}) { - % my $td_class = ''; - % my $link_class = 'action-checkin'; - % if ($result->departure_is_cancelled) { - % $td_class = "cancelled"; - % $link_class = 'action-cancelled-from'; - % } - % if (not $orientation_bar_shown and $result->departure->epoch < $now_epoch) { - % $orientation_bar_shown = 1; - - - - - - % } - - - - - - % } - -
- - — Anfragezeitpunkt — - -
+ % if ($hafas) { + %= include '_departures_hafas', results => $results; + % } + % else { + %= include '_departures_iris', results => $results; + % } diff --git a/templates/exception.html.ep b/templates/exception.html.ep index 290efc5..ec01ad2 100644 --- a/templates/exception.html.ep +++ b/templates/exception.html.ep @@ -21,7 +21,7 @@ %= DateTime->now(time_zone => 'Europe/Berlin')->strftime("%d/%b/%Y:%H:%M:%S %z")

Message: - %= (split(qr{\n}, $exception->message))[0] + %= ref($exception) ? (split(qr{\n}, $exception->message))[0] : $exception