2020-07-28 11:01:44 +00:00
|
|
|
package Travelynx::Helper::HAFAS;
|
2021-01-05 21:27:46 +00:00
|
|
|
|
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
|
2020-07-28 11:01:44 +00:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use 5.020;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use Encode qw(decode);
|
|
|
|
use JSON;
|
|
|
|
use Mojo::Promise;
|
2022-11-05 18:19:52 +00:00
|
|
|
use Travel::Status::DE::HAFAS;
|
2020-07-28 11:01:44 +00:00
|
|
|
|
2022-11-05 18:19:52 +00:00
|
|
|
sub _epoch {
|
|
|
|
my ($dt) = @_;
|
|
|
|
|
|
|
|
return $dt ? $dt->epoch : 0;
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:01:44 +00:00
|
|
|
sub new {
|
|
|
|
my ( $class, %opt ) = @_;
|
|
|
|
|
|
|
|
my $version = $opt{version};
|
|
|
|
|
2020-07-28 12:19:55 +00:00
|
|
|
$opt{header}
|
|
|
|
= { 'User-Agent' =>
|
2020-09-06 10:41:36 +00:00
|
|
|
"travelynx/${version} on $opt{root_url} +https://finalrewind.org/projects/travelynx"
|
|
|
|
};
|
2020-07-28 11:01:44 +00:00
|
|
|
|
|
|
|
return bless( \%opt, $class );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_json_p {
|
2022-09-24 13:01:24 +00:00
|
|
|
my ( $self, $url, %opt ) = @_;
|
2020-07-28 11:01:44 +00:00
|
|
|
|
|
|
|
my $cache = $self->{main_cache};
|
|
|
|
my $promise = Mojo::Promise->new;
|
|
|
|
|
2022-09-24 13:01:24 +00:00
|
|
|
if ( $opt{realtime} ) {
|
|
|
|
$cache = $self->{realtime_cache};
|
|
|
|
}
|
|
|
|
$opt{encoding} //= 'ISO-8859-15';
|
|
|
|
|
2020-07-28 11:01:44 +00:00
|
|
|
if ( my $content = $cache->thaw($url) ) {
|
2020-09-10 19:20:26 +00:00
|
|
|
return $promise->resolve($content);
|
2020-07-28 11:01:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 12:19:55 +00:00
|
|
|
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
|
|
|
|
->then(
|
2020-07-28 11:01:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-24 13:01:24 +00:00
|
|
|
my $body = decode( $opt{encoding}, $tx->res->body );
|
2020-07-28 11:01:44 +00:00
|
|
|
|
|
|
|
$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);
|
2020-09-10 19:20:26 +00:00
|
|
|
return;
|
2020-07-28 11:01:44 +00:00
|
|
|
}
|
|
|
|
)->catch(
|
|
|
|
sub {
|
|
|
|
my ($err) = @_;
|
2020-10-08 18:05:31 +00:00
|
|
|
$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");
|
2020-09-10 19:20:26 +00:00
|
|
|
return;
|
2020-07-28 11:01:44 +00:00
|
|
|
}
|
|
|
|
)->wait;
|
|
|
|
return $promise;
|
|
|
|
}
|
|
|
|
|
2022-11-09 17:14:07 +00:00
|
|
|
sub get_departures_p {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
2024-03-31 19:22:50 +00:00
|
|
|
my $when = (
|
|
|
|
$opt{timestamp}
|
|
|
|
? $opt{timestamp}->clone
|
|
|
|
: DateTime->now( time_zone => 'Europe/Berlin' )
|
|
|
|
)->subtract( minutes => $opt{lookbehind} );
|
2022-11-09 17:14:07 +00:00
|
|
|
return Travel::Status::DE::HAFAS->new_p(
|
|
|
|
station => $opt{eva},
|
|
|
|
datetime => $when,
|
2024-03-30 08:58:06 +00:00
|
|
|
lookahead => $opt{lookahead} + $opt{lookbehind},
|
2023-03-27 19:03:25 +00:00
|
|
|
results => 300,
|
2022-11-09 17:14:07 +00:00
|
|
|
cache => $self->{realtime_cache},
|
|
|
|
promise => 'Mojo::Promise',
|
|
|
|
user_agent => $self->{user_agent}->request_timeout(5),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-01 17:44:00 +00:00
|
|
|
sub search_location_p {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
|
|
|
return Travel::Status::DE::HAFAS->new_p(
|
|
|
|
locationSearch => $opt{query},
|
|
|
|
cache => $self->{realtime_cache},
|
|
|
|
promise => 'Mojo::Promise',
|
|
|
|
user_agent => $self->{user_agent}->request_timeout(5),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-24 19:11:11 +00:00
|
|
|
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(
|
|
|
|
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(
|
|
|
|
journey => {
|
|
|
|
id => $opt{trip_id},
|
|
|
|
},
|
|
|
|
with_polyline => 0,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-05 18:19:52 +00:00
|
|
|
sub get_route_timestamps_p {
|
|
|
|
my ( $self, %opt ) = @_;
|
2020-07-28 11:01:44 +00:00
|
|
|
|
|
|
|
my $promise = Mojo::Promise->new;
|
2022-11-05 18:19:52 +00:00
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
|
|
|
|
|
|
|
Travel::Status::DE::HAFAS->new_p(
|
|
|
|
journey => {
|
|
|
|
id => $opt{trip_id},
|
|
|
|
|
|
|
|
# name => $opt{train_no},
|
|
|
|
},
|
2022-11-05 21:01:51 +00:00
|
|
|
with_polyline => $opt{with_polyline},
|
|
|
|
cache => $self->{realtime_cache},
|
|
|
|
promise => 'Mojo::Promise',
|
|
|
|
user_agent => $self->{user_agent}->request_timeout(10),
|
2022-11-05 18:19:52 +00:00
|
|
|
)->then(
|
2020-07-28 11:01:44 +00:00
|
|
|
sub {
|
2022-11-05 18:19:52 +00:00
|
|
|
my ($hafas) = @_;
|
|
|
|
my $journey = $hafas->result;
|
|
|
|
my $ret = {};
|
2022-11-05 21:01:51 +00:00
|
|
|
my $polyline;
|
2022-11-05 18:19:52 +00:00
|
|
|
|
|
|
|
my $station_is_past = 1;
|
|
|
|
for my $stop ( $journey->route ) {
|
2023-12-27 09:59:35 +00:00
|
|
|
my $name = $stop->loc->name;
|
|
|
|
$ret->{$name} = $ret->{ $stop->loc->eva } = {
|
|
|
|
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
|
2020-07-28 11:01:44 +00:00
|
|
|
};
|
2024-04-03 11:58:48 +00:00
|
|
|
if ( $stop->tz_offset ) {
|
|
|
|
$ret->{$name}{tz_offset} = $stop->tz_offset;
|
|
|
|
}
|
2023-12-27 09:59:35 +00:00
|
|
|
if ( ( $stop->arr_cancelled or not $stop->sched_arr )
|
|
|
|
and ( $stop->dep_cancelled or not $stop->sched_dep ) )
|
2023-01-15 15:37:32 +00:00
|
|
|
{
|
|
|
|
$ret->{$name}{isCancelled} = 1;
|
|
|
|
}
|
2022-11-05 18:19:52 +00:00
|
|
|
if (
|
|
|
|
$station_is_past
|
|
|
|
and not $ret->{$name}{isCancelled}
|
|
|
|
and $now->epoch < (
|
|
|
|
$ret->{$name}{rt_arr} // $ret->{$name}{rt_dep}
|
|
|
|
// $ret->{$name}{sched_arr}
|
|
|
|
// $ret->{$name}{sched_dep} // $now->epoch
|
|
|
|
)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$station_is_past = 0;
|
|
|
|
}
|
|
|
|
$ret->{$name}{isPast} = $station_is_past;
|
2020-07-28 11:01:44 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 21:01:51 +00:00
|
|
|
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 );
|
|
|
|
|
2022-11-05 21:01:51 +00:00
|
|
|
if ( $iris_stations eq $hafas_stations
|
|
|
|
or index( $hafas_stations, $iris_stations ) != -1 )
|
|
|
|
{
|
|
|
|
$polyline = {
|
2023-12-27 09:59:35 +00:00
|
|
|
from_eva => ( $journey->route )[0]->loc->eva,
|
|
|
|
to_eva => ( $journey->route )[-1]->loc->eva,
|
2022-11-05 21:01:51 +00:00
|
|
|
coords => \@coordinate_list,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
2022-12-12 19:00:48 +00:00
|
|
|
$self->{log}->debug( 'Ignoring polyline for '
|
2022-11-05 21:01:51 +00:00
|
|
|
. $opt{train}->line
|
|
|
|
. ": IRIS route does not agree with HAFAS route: $iris_stations != $hafas_stations"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$promise->resolve( $ret, $journey, $polyline );
|
2020-09-10 19:20:26 +00:00
|
|
|
return;
|
2020-07-28 11:01:44 +00:00
|
|
|
}
|
|
|
|
)->catch(
|
|
|
|
sub {
|
|
|
|
my ($err) = @_;
|
2022-11-05 18:19:52 +00:00
|
|
|
$promise->reject($err);
|
2020-09-10 19:20:26 +00:00
|
|
|
return;
|
2020-07-28 11:01:44 +00:00
|
|
|
}
|
|
|
|
)->wait;
|
2022-11-05 18:19:52 +00:00
|
|
|
|
2020-07-28 11:01:44 +00:00
|
|
|
return $promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|