diff options
author | Birte Kristina Friesel <derf@finalrewind.org> | 2024-02-24 20:11:11 +0100 |
---|---|---|
committer | Birte Kristina Friesel <derf@finalrewind.org> | 2024-02-24 20:11:11 +0100 |
commit | dafeb838dc245176444acc7c0338290dbbf0307b (patch) | |
tree | 4099604f0903d003cc42f2432d501589987cbe01 /lib/Travelynx/Helper | |
parent | 85c22b559575bddf8965fb29bde89bfb1ba38ac4 (diff) |
Use journeyMatch rather than legacy trainsearch.exe API to find tripIDs
Diffstat (limited to 'lib/Travelynx/Helper')
-rw-r--r-- | lib/Travelynx/Helper/HAFAS.pm | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/Travelynx/Helper/HAFAS.pm b/lib/Travelynx/Helper/HAFAS.pm index d7f2a10..5a20515 100644 --- a/lib/Travelynx/Helper/HAFAS.pm +++ b/lib/Travelynx/Helper/HAFAS.pm @@ -109,6 +109,56 @@ sub search_location_p { ); } +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; +} + sub get_journey_p { my ( $self, %opt ) = @_; |