travelynx/lib/Travelynx/Helper/HAFAS.pm

329 lines
7.1 KiB
Perl
Raw Normal View History

package Travelynx::Helper::HAFAS;
2023-07-03 15:59:25 +00:00
# Copyright (C) 2020-2023 Birte Kristina Friesel
2020-11-27 21:12:56 +00:00
#
2021-01-29 17:32:13 +00:00
# SPDX-License-Identifier: AGPL-3.0-or-later
use strict;
use warnings;
use 5.020;
use DateTime;
use Encode qw(decode);
use JSON;
use Mojo::Promise;
use Travel::Status::DE::HAFAS;
sub _epoch {
my ($dt) = @_;
return $dt ? $dt->epoch : 0;
}
sub new {
my ( $class, %opt ) = @_;
my $version = $opt{version};
2020-07-28 12:19:55 +00:00
$opt{header}
= { 'User-Agent' =>
"travelynx/${version} on $opt{root_url} +https://finalrewind.org/projects/travelynx"
};
return bless( \%opt, $class );
}
2024-07-18 16:37:33 +00:00
sub get_service {
my ( $self, $service ) = @_;
return Travel::Status::DE::HAFAS::get_service($service);
}
sub get_json_p {
my ( $self, $url, %opt ) = @_;
my $cache = $self->{main_cache};
my $promise = Mojo::Promise->new;
if ( $opt{realtime} ) {
$cache = $self->{realtime_cache};
}
$opt{encoding} //= 'ISO-8859-15';
if ( my $content = $cache->thaw($url) ) {
return $promise->resolve($content);
}
2020-07-28 12:19:55 +00:00
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
->then(
sub {
my ($tx) = @_;
2020-07-28 12:19:55 +00:00
if ( my $err = $tx->error ) {
$promise->reject(
2020-09-21 17:47:54 +00:00
"hafas->get_json_p($url) returned HTTP $err->{code} $err->{message}"
);
2020-07-28 12:19:55 +00:00
return;
}
my $body = decode( $opt{encoding}, $tx->res->body );
$body =~ s{^TSLs[.]sls = }{};
$body =~ s{;$}{};
$body =~ s{(}{(}g;
$body =~ s{)}{)}g;
my $json = JSON->new->decode($body);
$cache->freeze( $url, $json );
$promise->resolve($json);
return;
}
)->catch(
sub {
my ($err) = @_;
$self->{log}->info("hafas->get_json_p($url): $err");
2020-09-21 17:47:54 +00:00
$promise->reject("hafas->get_json_p($url): $err");
return;
}
)->wait;
return $promise;
}
sub get_departures_p {
my ( $self, %opt ) = @_;
my $when = (
$opt{timestamp}
? $opt{timestamp}->clone
: DateTime->now( time_zone => 'Europe/Berlin' )
)->subtract( minutes => $opt{lookbehind} );
return Travel::Status::DE::HAFAS->new_p(
Multi-backend support Squashed commit of the following: commit 92518024ba295456358618c0e8180bd8e996fdf1 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:39:46 2024 +0200 add_or_update station: remove superfluos 'new backend id := old backend id' commit df21c20c6e4c86454f8a9ac69121404415217f2a Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:35:51 2024 +0200 revert connection targets min_count to 3 commit be335cef07d0b42874f5fc1de4a1d13396e8e807 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:20:05 2024 +0200 mention backend selection in API documentation commit 9f41828fb4f18fd707e0087def3032e8d4c8d7d8 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:19:23 2024 +0200 use_history: not all backends provide route data in departure monitor commit 09714b4d89684b8331d0e96f564a4c7432318f70 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:44 2024 +0200 disambiguation: pass correct hafas parameter commit 8cdf1120fc32155dc6525be64601b7c10a9c7f52 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:28 2024 +0200 _checked_in: hide Zuglauf link for non-db checkins commit 7455653f541198e0e0a6d11aed421487ffdb6285 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:01:47 2024 +0200 debug output commit b9cda07f85601a58ea32dbdacdd5399f302db52b Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 19:09:07 2024 +0200 fix remaining get_connection_targets / get_connecting_trains_p invocations commit 2759d7258c37c7498905cfe19f6b4c4f6d16bd21 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Wed Jul 24 20:50:12 2024 +0200 support non-DB HAFAS backends (WiP)
2024-07-26 16:55:58 +00:00
service => $opt{service},
station => $opt{eva},
datetime => $when,
lookahead => $opt{lookahead} + $opt{lookbehind},
2023-03-27 19:03:25 +00:00
results => 300,
cache => $self->{realtime_cache},
promise => 'Mojo::Promise',
user_agent => $self->{user_agent}->request_timeout(5),
);
}
sub search_location_p {
my ( $self, %opt ) = @_;
return Travel::Status::DE::HAFAS->new_p(
Multi-backend support Squashed commit of the following: commit 92518024ba295456358618c0e8180bd8e996fdf1 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:39:46 2024 +0200 add_or_update station: remove superfluos 'new backend id := old backend id' commit df21c20c6e4c86454f8a9ac69121404415217f2a Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:35:51 2024 +0200 revert connection targets min_count to 3 commit be335cef07d0b42874f5fc1de4a1d13396e8e807 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:20:05 2024 +0200 mention backend selection in API documentation commit 9f41828fb4f18fd707e0087def3032e8d4c8d7d8 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:19:23 2024 +0200 use_history: not all backends provide route data in departure monitor commit 09714b4d89684b8331d0e96f564a4c7432318f70 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:44 2024 +0200 disambiguation: pass correct hafas parameter commit 8cdf1120fc32155dc6525be64601b7c10a9c7f52 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:28 2024 +0200 _checked_in: hide Zuglauf link for non-db checkins commit 7455653f541198e0e0a6d11aed421487ffdb6285 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:01:47 2024 +0200 debug output commit b9cda07f85601a58ea32dbdacdd5399f302db52b Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 19:09:07 2024 +0200 fix remaining get_connection_targets / get_connecting_trains_p invocations commit 2759d7258c37c7498905cfe19f6b4c4f6d16bd21 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Wed Jul 24 20:50:12 2024 +0200 support non-DB HAFAS backends (WiP)
2024-07-26 16:55:58 +00:00
service => $opt{service},
locationSearch => $opt{query},
cache => $self->{realtime_cache},
promise => 'Mojo::Promise',
user_agent => $self->{user_agent}->request_timeout(5),
);
}
sub get_tripid_p {
my ( $self, %opt ) = @_;
my $promise = Mojo::Promise->new;
my $train = $opt{train};
my $train_desc = $train->type . ' ' . $train->train_no;
$train_desc =~ s{^- }{};
Travel::Status::DE::HAFAS->new_p(
Multi-backend support Squashed commit of the following: commit 92518024ba295456358618c0e8180bd8e996fdf1 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:39:46 2024 +0200 add_or_update station: remove superfluos 'new backend id := old backend id' commit df21c20c6e4c86454f8a9ac69121404415217f2a Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:35:51 2024 +0200 revert connection targets min_count to 3 commit be335cef07d0b42874f5fc1de4a1d13396e8e807 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:20:05 2024 +0200 mention backend selection in API documentation commit 9f41828fb4f18fd707e0087def3032e8d4c8d7d8 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:19:23 2024 +0200 use_history: not all backends provide route data in departure monitor commit 09714b4d89684b8331d0e96f564a4c7432318f70 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:44 2024 +0200 disambiguation: pass correct hafas parameter commit 8cdf1120fc32155dc6525be64601b7c10a9c7f52 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:28 2024 +0200 _checked_in: hide Zuglauf link for non-db checkins commit 7455653f541198e0e0a6d11aed421487ffdb6285 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:01:47 2024 +0200 debug output commit b9cda07f85601a58ea32dbdacdd5399f302db52b Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 19:09:07 2024 +0200 fix remaining get_connection_targets / get_connecting_trains_p invocations commit 2759d7258c37c7498905cfe19f6b4c4f6d16bd21 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Wed Jul 24 20:50:12 2024 +0200 support non-DB HAFAS backends (WiP)
2024-07-26 16:55:58 +00:00
service => $opt{service},
journeyMatch => $train_desc,
datetime => $train->start,
cache => $self->{realtime_cache},
promise => 'Mojo::Promise',
user_agent => $self->{user_agent}->request_timeout(10),
)->then(
sub {
my ($hafas) = @_;
my @results = $hafas->results;
if ( not @results ) {
$promise->reject(
"journeyMatch($train_desc) returned no results");
return;
}
my $result = $results[0];
if ( @results > 1 ) {
for my $journey (@results) {
if ( ( $journey->route )[0]->loc->name eq $train->origin ) {
$result = $journey;
last;
}
}
}
$promise->resolve( $result->id );
return;
}
)->catch(
sub {
my ($err) = @_;
$promise->reject($err);
return;
}
)->wait;
return $promise;
}
2023-08-13 10:51:15 +00:00
sub get_journey_p {
my ( $self, %opt ) = @_;
my $promise = Mojo::Promise->new;
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
Travel::Status::DE::HAFAS->new_p(
Multi-backend support Squashed commit of the following: commit 92518024ba295456358618c0e8180bd8e996fdf1 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:39:46 2024 +0200 add_or_update station: remove superfluos 'new backend id := old backend id' commit df21c20c6e4c86454f8a9ac69121404415217f2a Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:35:51 2024 +0200 revert connection targets min_count to 3 commit be335cef07d0b42874f5fc1de4a1d13396e8e807 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:20:05 2024 +0200 mention backend selection in API documentation commit 9f41828fb4f18fd707e0087def3032e8d4c8d7d8 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:19:23 2024 +0200 use_history: not all backends provide route data in departure monitor commit 09714b4d89684b8331d0e96f564a4c7432318f70 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:44 2024 +0200 disambiguation: pass correct hafas parameter commit 8cdf1120fc32155dc6525be64601b7c10a9c7f52 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:28 2024 +0200 _checked_in: hide Zuglauf link for non-db checkins commit 7455653f541198e0e0a6d11aed421487ffdb6285 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:01:47 2024 +0200 debug output commit b9cda07f85601a58ea32dbdacdd5399f302db52b Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 19:09:07 2024 +0200 fix remaining get_connection_targets / get_connecting_trains_p invocations commit 2759d7258c37c7498905cfe19f6b4c4f6d16bd21 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Wed Jul 24 20:50:12 2024 +0200 support non-DB HAFAS backends (WiP)
2024-07-26 16:55:58 +00:00
service => $opt{service},
2023-08-13 10:51:15 +00:00
journey => {
id => $opt{trip_id},
},
with_polyline => $opt{with_polyline},
2023-08-13 10:51:15 +00:00
cache => $self->{realtime_cache},
promise => 'Mojo::Promise',
user_agent => $self->{user_agent}->request_timeout(10),
)->then(
sub {
my ($hafas) = @_;
my $journey = $hafas->result;
if ($journey) {
$promise->resolve($journey);
return;
}
$promise->reject('no journey');
return;
}
)->catch(
sub {
my ($err) = @_;
$promise->reject($err);
return;
}
)->wait;
return $promise;
}
sub get_route_p {
my ( $self, %opt ) = @_;
my $promise = Mojo::Promise->new;
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
Travel::Status::DE::HAFAS->new_p(
Multi-backend support Squashed commit of the following: commit 92518024ba295456358618c0e8180bd8e996fdf1 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:39:46 2024 +0200 add_or_update station: remove superfluos 'new backend id := old backend id' commit df21c20c6e4c86454f8a9ac69121404415217f2a Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:35:51 2024 +0200 revert connection targets min_count to 3 commit be335cef07d0b42874f5fc1de4a1d13396e8e807 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:20:05 2024 +0200 mention backend selection in API documentation commit 9f41828fb4f18fd707e0087def3032e8d4c8d7d8 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:19:23 2024 +0200 use_history: not all backends provide route data in departure monitor commit 09714b4d89684b8331d0e96f564a4c7432318f70 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:44 2024 +0200 disambiguation: pass correct hafas parameter commit 8cdf1120fc32155dc6525be64601b7c10a9c7f52 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:28 2024 +0200 _checked_in: hide Zuglauf link for non-db checkins commit 7455653f541198e0e0a6d11aed421487ffdb6285 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:01:47 2024 +0200 debug output commit b9cda07f85601a58ea32dbdacdd5399f302db52b Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 19:09:07 2024 +0200 fix remaining get_connection_targets / get_connecting_trains_p invocations commit 2759d7258c37c7498905cfe19f6b4c4f6d16bd21 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Wed Jul 24 20:50:12 2024 +0200 support non-DB HAFAS backends (WiP)
2024-07-26 16:55:58 +00:00
service => $opt{service},
journey => {
id => $opt{trip_id},
# name => $opt{train_no},
},
with_polyline => $opt{with_polyline},
cache => $self->{realtime_cache},
promise => 'Mojo::Promise',
user_agent => $self->{user_agent}->request_timeout(10),
)->then(
sub {
my ($hafas) = @_;
my $journey = $hafas->result;
my $ret = [];
my $polyline;
my $station_is_past = 1;
for my $stop ( $journey->route ) {
my $entry = {
name => $stop->loc->name,
eva => $stop->loc->eva,
sched_arr => _epoch( $stop->sched_arr ),
sched_dep => _epoch( $stop->sched_dep ),
rt_arr => _epoch( $stop->rt_arr ),
rt_dep => _epoch( $stop->rt_dep ),
arr_delay => $stop->arr_delay,
dep_delay => $stop->dep_delay,
load => $stop->load,
lat => $stop->loc->lat,
lon => $stop->loc->lon,
};
if ( $stop->tz_offset ) {
$entry->{tz_offset} = $stop->tz_offset;
}
if ( ( $stop->arr_cancelled or not $stop->sched_arr )
and ( $stop->dep_cancelled or not $stop->sched_dep ) )
{
$entry->{isCancelled} = 1;
}
if (
$station_is_past
and not $entry->{isCancelled}
and $now->epoch < (
$entry->{rt_arr} // $entry->{rt_dep}
// $entry->{sched_arr} // $entry->{sched_dep}
// $now->epoch
)
)
{
$station_is_past = 0;
}
$entry->{isPast} = $station_is_past;
push( @{$ret}, $entry );
}
if ( $journey->polyline ) {
my @station_list;
my @coordinate_list;
for my $coord ( $journey->polyline ) {
if ( $coord->{name} ) {
push( @coordinate_list,
[ $coord->{lon}, $coord->{lat}, $coord->{eva} ] );
push( @station_list, $coord->{name} );
}
else {
push( @coordinate_list,
[ $coord->{lon}, $coord->{lat} ] );
}
}
2022-11-09 17:17:05 +00:00
my $iris_stations = join( '|', $opt{train}->route );
# borders (Gr" as in "Grenze") are only returned by HAFAS.
# They are not stations.
my $hafas_stations
= join( '|', grep { $_ !~ m{(\(Gr\)|\)Gr)$} } @station_list );
if ( $iris_stations eq $hafas_stations
or index( $hafas_stations, $iris_stations ) != -1 )
{
$polyline = {
from_eva => ( $journey->route )[0]->loc->eva,
to_eva => ( $journey->route )[-1]->loc->eva,
coords => \@coordinate_list,
};
}
else {
$self->{log}->debug( 'Ignoring polyline for '
. $opt{train}->line
. ": IRIS route does not agree with HAFAS route: $iris_stations != $hafas_stations"
);
}
}
$promise->resolve( $ret, $journey, $polyline );
return;
}
)->catch(
sub {
my ($err) = @_;
$promise->reject($err);
return;
}
)->wait;
return $promise;
}
1;