calculate tripid from trainsearch.exe.
Removes the need for an additional transport.rest request.
This commit is contained in:
parent
fffe8dcdfa
commit
b6330217f0
2 changed files with 34 additions and 115 deletions
|
@ -1244,27 +1244,12 @@ sub startup {
|
|||
return;
|
||||
}
|
||||
|
||||
if ( not $journey->{data}{trip_id} ) {
|
||||
if ( $journey->{data}{trip_id}
|
||||
and not $journey->{data}{polyline_id} )
|
||||
{
|
||||
my ( $origin_eva, $destination_eva, $polyline_str );
|
||||
$self->hafas->get_tripid_p($train)->then(
|
||||
sub {
|
||||
my ($trip_id) = @_;
|
||||
|
||||
my $res = $db->select( 'in_transit', ['data'],
|
||||
{ user_id => $uid } );
|
||||
my $res_h = $res->expand->hash;
|
||||
my $data = $res_h->{data} // {};
|
||||
|
||||
$data->{trip_id} = $trip_id;
|
||||
|
||||
$db->update(
|
||||
'in_transit',
|
||||
{ data => JSON->new->encode($data) },
|
||||
{ user_id => $uid }
|
||||
);
|
||||
return $self->hafas->get_polyline_p( $train, $trip_id );
|
||||
}
|
||||
)->then(
|
||||
$self->hafas->get_polyline_p( $train,
|
||||
$journey->{data}{trip_id} )->then(
|
||||
sub {
|
||||
my ($ret) = @_;
|
||||
my $polyline = $ret->{polyline};
|
||||
|
@ -1278,7 +1263,7 @@ sub startup {
|
|||
|
||||
$polyline_str = JSON->new->encode($polyline);
|
||||
|
||||
return $db->select_p(
|
||||
my $pl_res = $db->select(
|
||||
'polylines',
|
||||
['id'],
|
||||
{
|
||||
|
@ -1288,10 +1273,7 @@ sub startup {
|
|||
},
|
||||
{ limit => 1 }
|
||||
);
|
||||
}
|
||||
)->then(
|
||||
sub {
|
||||
my ($pl_res) = @_;
|
||||
|
||||
my $polyline_id;
|
||||
if ( my $h = $pl_res->hash ) {
|
||||
$polyline_id = $h->{id};
|
||||
|
@ -1350,7 +1332,8 @@ sub startup {
|
|||
my ($trainsearch) = @_;
|
||||
|
||||
# Fallback: Take first result
|
||||
$trainlink = $trainsearch->{suggestions}[0]{trainLink};
|
||||
my $result = $trainsearch->{suggestions}[0];
|
||||
$trainlink = $result->{trainLink};
|
||||
|
||||
# Try finding a result for the current date
|
||||
for
|
||||
|
@ -1371,6 +1354,7 @@ sub startup {
|
|||
# station seems to be the more generic solution, so we do that
|
||||
# instead.
|
||||
if ( $suggestion->{dep} eq $train->origin ) {
|
||||
$result = $suggestion;
|
||||
$trainlink = $suggestion->{trainLink};
|
||||
last;
|
||||
}
|
||||
|
@ -1381,6 +1365,30 @@ sub startup {
|
|||
$self->app->log->debug("trainlink not found");
|
||||
return Mojo::Promise->reject("trainlink not found");
|
||||
}
|
||||
|
||||
# Calculate and store trip_id.
|
||||
# The trip_id's date part doesn't seem to matter -- so far,
|
||||
# HAFAS is happy as long as the date part starts with a number.
|
||||
# HAFAS-internal tripIDs use this format (withouth leading zero
|
||||
# for day of month < 10) though, so let's stick with it.
|
||||
|
||||
my $res = $db->select( 'in_transit', ['data'],
|
||||
{ user_id => $uid } );
|
||||
my $res_h = $res->expand->hash;
|
||||
my $data = $res_h->{data} // {};
|
||||
|
||||
my $date_map = $date_yyyy;
|
||||
$date_map =~ tr{.}{}d;
|
||||
$data->{trip_id} = sprintf( '1|%d|%d|%d|%s',
|
||||
$result->{id}, $result->{cycle},
|
||||
$result->{pool}, $date_map );
|
||||
|
||||
$db->update(
|
||||
'in_transit',
|
||||
{ data => JSON->new->encode($data) },
|
||||
{ user_id => $uid }
|
||||
);
|
||||
|
||||
my $base2
|
||||
= 'https://reiseauskunft.bahn.de/bin/traininfo.exe/dn';
|
||||
return $self->hafas->get_json_p(
|
||||
|
|
|
@ -111,95 +111,6 @@ sub get_polyline_p {
|
|||
return $promise;
|
||||
}
|
||||
|
||||
sub get_tripid_p {
|
||||
my ( $self, $train ) = @_;
|
||||
|
||||
my $promise = Mojo::Promise->new;
|
||||
my $cache = $self->{main_cache};
|
||||
my $eva = $train->station_uic;
|
||||
|
||||
my $dep_ts = DateTime->now( time_zone => 'Europe/Berlin' );
|
||||
my $url
|
||||
= "https://2.db.transport.rest/stations/${eva}/departures?duration=5&when=$dep_ts";
|
||||
|
||||
if ( $train->sched_departure ) {
|
||||
$dep_ts = $train->sched_departure->epoch;
|
||||
$url
|
||||
= "https://2.db.transport.rest/stations/${eva}/departures?duration=5&when=$dep_ts";
|
||||
}
|
||||
elsif ( $train->sched_arrival ) {
|
||||
$dep_ts = $train->sched_arrival->epoch;
|
||||
$url
|
||||
= "https://2.db.transport.rest/stations/${eva}/arrivals?duration=5&when=$dep_ts";
|
||||
}
|
||||
|
||||
$self->get_rest_p($url)->then(
|
||||
sub {
|
||||
my ($json) = @_;
|
||||
|
||||
for my $result ( @{$json} ) {
|
||||
if ( $result->{line}
|
||||
and $result->{line}{fahrtNr} == $train->train_no )
|
||||
{
|
||||
my $trip_id = $result->{tripId};
|
||||
$promise->resolve($trip_id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
$promise->reject( 'hafas->get_tripid_p: train '
|
||||
. $train->train_no
|
||||
. ' not found' );
|
||||
return;
|
||||
}
|
||||
)->catch(
|
||||
sub {
|
||||
my ($err) = @_;
|
||||
$promise->reject($err);
|
||||
return;
|
||||
}
|
||||
)->wait;
|
||||
|
||||
return $promise;
|
||||
}
|
||||
|
||||
sub get_rest_p {
|
||||
my ( $self, $url ) = @_;
|
||||
|
||||
my $cache = $self->{main_cache};
|
||||
my $promise = Mojo::Promise->new;
|
||||
|
||||
if ( my $content = $cache->thaw($url) ) {
|
||||
return $promise->resolve($content);
|
||||
}
|
||||
|
||||
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
|
||||
->then(
|
||||
sub {
|
||||
my ($tx) = @_;
|
||||
|
||||
if ( my $err = $tx->error ) {
|
||||
$promise->reject(
|
||||
"hafas->get_rest_p($url) returned HTTP $err->{code} $err->{message}"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
my $json = JSON->new->decode( $tx->res->body );
|
||||
$cache->freeze( $url, $json );
|
||||
$promise->resolve($json);
|
||||
return;
|
||||
}
|
||||
)->catch(
|
||||
sub {
|
||||
my ($err) = @_;
|
||||
$self->{log}->info("hafas->get_rest_p($url): $err");
|
||||
$promise->reject("hafas->get_rest_p($url): $err");
|
||||
return;
|
||||
}
|
||||
)->wait;
|
||||
return $promise;
|
||||
}
|
||||
|
||||
sub get_json_p {
|
||||
my ( $self, $url ) = @_;
|
||||
|
||||
|
|
Loading…
Reference in a new issue