From b6330217f086360d865163e29285ea2bf6c79320 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 11 Oct 2020 12:04:33 +0200 Subject: calculate tripid from trainsearch.exe. Removes the need for an additional transport.rest request. --- lib/Travelynx/Helper/HAFAS.pm | 89 ------------------------------------------- 1 file changed, 89 deletions(-) (limited to 'lib/Travelynx/Helper') 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 ) = @_; -- cgit v1.2.3