summaryrefslogtreecommitdiff
path: root/lib/DBInfoscreen/Helper/HAFAS.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/DBInfoscreen/Helper/HAFAS.pm')
-rw-r--r--lib/DBInfoscreen/Helper/HAFAS.pm495
1 files changed, 257 insertions, 238 deletions
diff --git a/lib/DBInfoscreen/Helper/HAFAS.pm b/lib/DBInfoscreen/Helper/HAFAS.pm
index c9a2ba2..e16bad8 100644
--- a/lib/DBInfoscreen/Helper/HAFAS.pm
+++ b/lib/DBInfoscreen/Helper/HAFAS.pm
@@ -1,13 +1,20 @@
package DBInfoscreen::Helper::HAFAS;
+# Copyright (C) 2011-2022 Birte Kristina Friesel
+#
+# SPDX-License-Identifier: AGPL-3.0-or-later
+
use strict;
use warnings;
use 5.020;
+use utf8;
use DateTime;
use Encode qw(decode encode);
+use Travel::Status::DE::HAFAS;
use Mojo::JSON qw(decode_json);
-use XML::LibXML;
+use Mojo::Promise;
+use Mojo::UserAgent;
sub new {
my ( $class, %opt ) = @_;
@@ -16,284 +23,296 @@ sub new {
$opt{header}
= { 'User-Agent' =>
- "dbf/${version} +https://finalrewind.org/projects/db-fakedisplay" };
+"dbf/${version} on $opt{root_url} +https://finalrewind.org/projects/db-fakedisplay"
+ };
return bless( \%opt, $class );
}
-sub hafas_rest_req {
- my ( $self, $cache, $url ) = @_;
-
- if ( my $content = $cache->thaw($url) ) {
- return $content;
- }
+sub get_coverage {
+ my ( $self, $service ) = @_;
- my $res
- = eval { $self->{user_agent}->get( $url => $self->{header} )->result; };
+ my $service_definition = Travel::Status::DE::HAFAS::get_service($service);
- if ($@) {
- $self->{log}->debug("hafas_rest_req($url): $@");
- return;
- }
- if ( $res->is_error ) {
- return;
+ if ( not $service_definition ) {
+ return {};
}
- my $json = decode_json( $res->body );
-
- $cache->freeze( $url, $json );
-
- return $json;
+ return $service_definition->{coverage}{area} // {};
}
-sub hafas_json_req {
- my ( $self, $cache, $url ) = @_;
-
- if ( my $content = $cache->thaw($url) ) {
- return $content;
- }
-
- my $res
- = eval { $self->{user_agent}->get( $url => $self->{header} )->result };
-
- if ($@) {
- $self->{log}->debug("hafas_json_req($url): $@");
- return;
- }
- if ( $res->is_error ) {
- return;
- }
-
- my $body = encode( 'utf-8', decode( 'ISO-8859-15', $res->body ) );
-
- $body =~ s{^TSLs[.]sls = }{};
- $body =~ s{;$}{};
- $body =~ s{(}{(}g;
- $body =~ s{)}{)}g;
-
- my $json = decode_json($body);
-
- $cache->freeze( $url, $json );
-
- return $json;
-}
-
-sub hafas_xml_req {
- my ( $self, $cache, $url ) = @_;
-
- if ( my $content = $cache->thaw($url) ) {
- return $content;
- }
-
- my $res
- = eval { $self->{user_agent}->get( $url => $self->{header} )->result };
-
- if ($@) {
- $self->{log}->debug("hafas_xml_req($url): $@");
- return;
- }
- if ( $res->is_error ) {
- $cache->freeze( $url, {} );
- return;
- }
-
- my $body = decode( 'ISO-8859-15', $res->body );
+sub get_route_p {
+ my ( $self, %opt ) = @_;
- # <SDay text="... &gt; ..."> is invalid HTML, but present
- # regardless. As it is the last tag, we just throw it away.
- $body =~ s{<SDay [^>]*/>}{}s;
+ my $promise = Mojo::Promise->new;
+ my $now = DateTime->now( time_zone => 'Europe/Berlin' );
- my $tree;
+ my $hafas_promise;
- eval { $tree = XML::LibXML->load_xml( string => $body ) };
+ my $agent = $self->{user_agent};
+ if ( $opt{service} and $opt{service} eq 'PKP' ) {
- if ($@) {
- $cache->freeze( $url, {} );
- return;
+ # PKP needs proxying
+ $agent = Mojo::UserAgent->new;
}
- my $ret = {
- station => {},
- stations => [],
- messages => [],
- };
-
- for my $station ( $tree->findnodes('/Journey/St') ) {
- my $name = $station->getAttribute('name');
- my $adelay = $station->getAttribute('adelay');
- my $ddelay = $station->getAttribute('ddelay');
- push( @{ $ret->{stations} }, $name );
- $ret->{station}{$name} = {
- adelay => $adelay,
- ddelay => $ddelay,
- };
- }
-
- for my $message ( $tree->findnodes('/Journey/HIMMessage') ) {
- my $header = $message->getAttribute('header');
- my $lead = $message->getAttribute('lead');
- my $display = $message->getAttribute('display');
- push(
- @{ $ret->{messages} },
- {
- header => $header,
- lead => $lead,
- display => $display
- }
+ if ( $opt{trip_id} ) {
+ $hafas_promise = Travel::Status::DE::HAFAS->new_p(
+ service => $opt{service} // 'ÖBB',
+ journey => {
+ id => $opt{trip_id},
+ },
+ language => $opt{language},
+ cache => $self->{realtime_cache},
+ promise => 'Mojo::Promise',
+ user_agent => $agent->request_timeout(10)
);
}
-
- $cache->freeze( $url, $ret );
-
- return $ret;
-}
-
-sub get_route_timestamps {
- my ( $self, %opt ) = @_;
-
- my $base
- = 'https://reiseauskunft.bahn.de/bin/trainsearch.exe/dn?L=vs_json&start=yes&rt=1';
- my ( $date_yy, $date_yyyy, $train_no, $train_origin );
-
- if ( $opt{train} ) {
- $date_yy = $opt{train}->start->strftime('%d.%m.%y');
- $date_yyyy = $opt{train}->start->strftime('%d.%m.%Y');
- $train_no = $opt{train}->type . ' ' . $opt{train}->train_no;
- $train_origin = $opt{train}->origin;
+ elsif ( $opt{train} ) {
+ $opt{train_req} = $opt{train}->type . ' ' . $opt{train}->train_no;
+ $opt{train_origin} = $opt{train}->origin;
}
else {
- my $now = DateTime->now( time_zone => 'Europe/Berlin' );
- $date_yy = $now->strftime('%d.%m.%y');
- $date_yyyy = $now->strftime('%d.%m.%Y');
- $train_no = $opt{train_no};
+ $opt{train_req} = $opt{train_type} . ' ' . $opt{train_no};
}
- my $trainsearch = $self->hafas_json_req( $self->{main_cache},
- "${base}&date=${date_yy}&trainname=${train_no}" );
-
- if ( not $trainsearch ) {
- return;
- }
-
- # Fallback: Take first result
- my $trainlink = $trainsearch->{suggestions}[0]{trainLink};
-
- # Try finding a result for the current date
- for my $suggestion ( @{ $trainsearch->{suggestions} // [] } ) {
-
- # Drunken API, sail with care. Both date formats are used interchangeably
- if (
- exists $suggestion->{depDate}
- and ( $suggestion->{depDate} eq $date_yy
- or $suggestion->{depDate} eq $date_yyyy )
- )
- {
- # Train numbers are not unique, e.g. IC 149 refers both to the
- # InterCity service Amsterdam -> Berlin and to the InterCity service
- # Koebenhavns Lufthavn st -> Aarhus. One workaround is making
- # requests with the stationFilter=80 parameter. Checking the origin
- # station seems to be the more generic solution, so we do that
- # instead.
- if ( $train_origin and $suggestion->{dep} eq $train_origin ) {
- $trainlink = $suggestion->{trainLink};
- last;
+ $hafas_promise //= Travel::Status::DE::HAFAS->new_p(
+ service => $opt{service} // 'ÖBB',
+ journeyMatch => $opt{train_req} =~ s{^- }{}r,
+ datetime => ( $opt{train} ? $opt{train}->start : $opt{datetime} ),
+ language => $opt{language},
+ cache => $self->{realtime_cache},
+ promise => 'Mojo::Promise',
+ user_agent => $agent->request_timeout(10)
+ )->then(
+ sub {
+ my ($hafas) = @_;
+ my @results = $hafas->results;
+
+ if ( not @results ) {
+ return Mojo::Promise->reject(
+ "journeyMatch($opt{train_req}) found no results");
}
- }
- }
-
- if ( not $trainlink ) {
- return;
- }
-
- $base = 'https://reiseauskunft.bahn.de/bin/traininfo.exe/dn';
-
- my $traininfo = $self->hafas_json_req( $self->{realtime_cache},
- "${base}/${trainlink}?rt=1&date=${date_yy}&L=vs_json" );
- if ( not $traininfo or $traininfo->{error} ) {
- return;
- }
-
- my $traindelay = $self->hafas_xml_req( $self->{realtime_cache},
- "${base}/${trainlink}?rt=1&date=${date_yy}&L=vs_java3" );
-
- my $ret = {};
+ my $result = $results[0];
+ if ( @results > 1 ) {
+ for my $journey (@results) {
+ if ( $opt{train_origin}
+ and ( $journey->route )[0]->loc->name eq
+ $opt{train_origin} )
+ {
+ $result = $journey;
+ last;
+ }
+ }
+ }
- my $strp = DateTime::Format::Strptime->new(
- pattern => '%d.%m.%y %H:%M',
- time_zone => 'Europe/Berlin',
+ return Travel::Status::DE::HAFAS->new_p(
+ service => $opt{service} // 'ÖBB',
+ journey => {
+ id => $result->id,
+ },
+ language => $opt{language},
+ cache => $self->{realtime_cache},
+ promise => 'Mojo::Promise',
+ user_agent => $agent->request_timeout(10)
+ );
+ }
);
- for my $station ( @{ $traininfo->{suggestions}[0]{locations} // [] } ) {
- my $name = $station->{name};
- my $arr = $station->{arrDate} . ' ' . $station->{arrTime};
- my $dep = $station->{depDate} . ' ' . $station->{depTime};
- $ret->{$name} = {
- sched_arr => scalar $strp->parse_datetime($arr),
- sched_dep => scalar $strp->parse_datetime($dep),
- };
- if ( exists $traindelay->{station}{$name} ) {
- my $delay = $traindelay->{station}{$name};
- if ( $ret->{$name}{sched_arr}
- and $delay->{adelay}
- and $delay->{adelay} =~ m{^\d+$} )
- {
- $ret->{$name}{rt_arr} = $ret->{$name}{sched_arr}
- ->clone->add( minutes => $delay->{adelay} );
+ $hafas_promise->then(
+ sub {
+ my ($hafas) = @_;
+ my $journey = $hafas->result;
+ my @ret;
+ my $station_is_past = 1;
+
+ my $num_names = 0;
+ my $prev_name = q{};
+ my $num_directions = 0;
+ my $prev_direction = q{};
+ my $num_operators = 0;
+ my $prev_operator = q{};
+
+ for my $stop ( $journey->route ) {
+ my $prod = $stop->prod_dep // $stop->prod_arr;
+ if ( $prod and $prod->name and $prod->name ne $prev_name ) {
+ $num_names++;
+ $prev_name = $prod->name;
+ }
+ if ( $prod
+ and $prod->operator
+ and $prod->operator ne $prev_operator )
+ {
+ $num_operators++;
+ $prev_operator = $prod->operator;
+ }
+ if ( $stop->direction and $stop->direction ne $prev_direction )
+ {
+ $num_directions++;
+ $prev_direction = $stop->direction;
+ }
}
- if ( $ret->{$name}{sched_dep}
- and $delay->{ddelay}
- and $delay->{ddelay} =~ m{^\d+$} )
- {
- $ret->{$name}{rt_dep} = $ret->{$name}{sched_dep}
- ->clone->add( minutes => $delay->{ddelay} );
+
+ $prev_name = q{};
+ $prev_direction = q{};
+ $prev_operator = q{};
+
+ for my $stop ( $journey->route ) {
+
+ my $prod = $stop->prod_dep // $stop->prod_arr;
+ my %annotation;
+ if ( $num_names > 1
+ and $prod
+ and $prod->name
+ and $prod->name ne $prev_name )
+ {
+ $prev_name = $annotation{prod_name} = $prod->name;
+ }
+ if ( $num_operators > 1
+ and $prod
+ and $prod->operator
+ and $prod->operator ne $prev_operator )
+ {
+ $prev_operator = $annotation{operator} = $prod->operator;
+ }
+ if ( $num_directions > 1
+ and $stop->direction
+ and $stop->direction ne $prev_direction )
+ {
+ $prev_direction = $annotation{direction} = $stop->direction;
+ }
+
+ if (%annotation) {
+ $annotation{is_annotated} = 1;
+ }
+
+ push(
+ @ret,
+ {
+ name => $stop->loc->name,
+ eva => $stop->loc->eva,
+ sched_arr => $stop->sched_arr,
+ sched_dep => $stop->sched_dep,
+ rt_arr => $stop->rt_arr,
+ rt_dep => $stop->rt_dep,
+ arr_delay => $stop->arr_delay,
+ dep_delay => $stop->dep_delay,
+ arr_cancelled => $stop->arr_cancelled,
+ dep_cancelled => $stop->dep_cancelled,
+ tz_offset => $stop->tz_offset,
+ platform => $stop->platform,
+ sched_platform => $stop->sched_platform,
+ load => $stop->load,
+ isAdditional => $stop->is_additional,
+ isCancelled => (
+ ( $stop->arr_cancelled or not $stop->sched_arr )
+ and
+ ( $stop->dep_cancelled or not $stop->sched_dep )
+ ),
+ %annotation,
+ }
+ );
+ if (
+ $station_is_past
+ and not $ret[-1]{isCancelled}
+ and $now->epoch < (
+ $ret[-1]{rt_arr} // $ret[-1]{rt_dep}
+ // $ret[-1]{sched_arr} // $ret[-1]{sched_dep} // $now
+ )->epoch
+ )
+ {
+ $station_is_past = 0;
+ }
+ $ret[-1]{isPast} = $station_is_past;
+ if ( $stop->tz_offset ) {
+ if ( $stop->sched_arr ) {
+ $ret[-1]{local_sched_arr}
+ = $stop->sched_arr->clone->add(
+ minutes => $stop->tz_offset );
+ }
+ if ( $stop->sched_dep ) {
+ $ret[-1]{local_sched_dep}
+ = $stop->sched_dep->clone->add(
+ minutes => $stop->tz_offset );
+ }
+ if ( $stop->rt_arr ) {
+ $ret[-1]{local_rt_arr} = $stop->rt_arr->clone->add(
+ minutes => $stop->tz_offset );
+ }
+ if ( $stop->rt_dep ) {
+ $ret[-1]{local_rt_dep} = $stop->rt_dep->clone->add(
+ minutes => $stop->tz_offset );
+ }
+ $ret[-1]{local_dt_ad} = $ret[-1]{local_rt_arr}
+ // $ret[-1]{local_sched_arr} // $ret[-1]{local_rt_dep}
+ // $ret[-1]{local_sched_dep};
+ $ret[-1]{local_dt_da} = $ret[-1]{local_rt_dep}
+ // $ret[-1]{local_sched_dep} // $ret[-1]{local_rt_arr}
+ // $ret[-1]{local_sched_arr};
+ }
}
+
+ $promise->resolve( \@ret, $journey, $hafas );
+ return;
}
- }
+ )->catch(
+ sub {
+ my ($err) = @_;
+ $promise->reject($err);
+ return;
+ }
+ )->wait;
- return ( $ret, $traindelay // {} );
+ return $promise;
}
-sub get_tripid {
- my ( $self, $train ) = @_;
+# Input: (HAFAS TripID, line number)
+# Output: Promise returning a Travel::Status::DE::HAFAS::Journey instance on success
+sub get_polyline_p {
+ my ( $self, %opt ) = @_;
- my $cache = $self->{main_cache};
- my $eva = $train->station_uic;
+ my $trip_id = $opt{id};
+ my $line = $opt{line};
+ my $service = $opt{service} // 'ÖBB';
+ my $promise = Mojo::Promise->new;
- my $dep_ts = DateTime->now( time_zone => 'Europe/Berlin' );
- my $url
- = "https://2.db.transport.rest/stations/${eva}/departures?duration=5&when=$dep_ts";
+ my $agent = $self->{user_agent};
+ if ( $opt{service} and $opt{service} eq 'PKP' ) {
- if ( $train->sched_departure ) {
- $dep_ts = $train->sched_departure->epoch;
- $url
- = "https://2.db.transport.rest/stations/${eva}/departures?duration=5&when=$dep_ts";
+ # PKP needs proxying
+ $agent = Mojo::UserAgent->new;
}
- elsif ( $train->sched_arrival ) {
- $dep_ts = $train->sched_arrival->epoch;
- $url
- = "https://2.db.transport.rest/stations/${eva}/arrivals?duration=5&when=$dep_ts";
- }
-
- my $json = $self->hafas_rest_req( $cache, $url );
- #say "looking for " . $train->train_no . " in $url";
- for my $result ( @{ $json // [] } ) {
- my $trip_id = $result->{tripId};
- my $fahrt = $result->{line}{fahrtNr};
-
- #say "checking $fahrt";
- if ( $result->{line} and $result->{line}{fahrtNr} == $train->train_no )
- {
- #say "Trip ID is $trip_id";
- return $trip_id;
+ Travel::Status::DE::HAFAS->new_p(
+ service => $service,
+ journey => {
+ id => $trip_id,
+ name => $line,
+ },
+ with_polyline => 1,
+ cache => $self->{realtime_cache},
+ promise => 'Mojo::Promise',
+ user_agent => $agent->request_timeout(10)
+ )->then(
+ sub {
+ my ($hafas) = @_;
+ my $journey = $hafas->result;
+
+ $promise->resolve($journey);
+ return;
}
- else {
- #say "unmatched Trip ID $trip_id";
+ )->catch(
+ sub {
+ my ($err) = @_;
+ $self->{log}->debug("HAFAS->new_p($trip_id, $line) error: $err");
+ $promise->reject($err);
+ return;
}
- }
- return;
+ )->wait;
+
+ return $promise;
}
1;