diff options
author | Daniel Friesel <derf@finalrewind.org> | 2020-10-11 12:04:33 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2020-10-11 12:04:33 +0200 |
commit | b6330217f086360d865163e29285ea2bf6c79320 (patch) | |
tree | b61c7e8d27f57fa1c4d0cec3cb849e28db975d75 /lib | |
parent | fffe8dcdfa0f5110b0b279e6d9989fbcea853cad (diff) |
calculate tripid from trainsearch.exe.
Removes the need for an additional transport.rest request.
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/Travelynx.pm | 60 | ||||
-rw-r--r-- | lib/Travelynx/Helper/HAFAS.pm | 89 |
2 files changed, 34 insertions, 115 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index e8030f1..7c7bb75 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -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( diff --git a/lib/Travelynx/Helper/HAFAS.pm b/lib/Travelynx/Helper/HAFAS.pm index fbca740..5253a4e 100644 --- a/lib/Travelynx/Helper/HAFAS.pm +++ b/lib/Travelynx/Helper/HAFAS.pm @@ -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 ) = @_; |