From 2542ed41c818ba1cd1e000bc4b5c133972507aea Mon Sep 17 00:00:00 2001 From: Derf Null Date: Mon, 10 Apr 2023 21:42:06 +0200 Subject: add locationSearch support --- bin/hafas-m | 10 +++++++ lib/Travel/Status/DE/HAFAS.pm | 57 +++++++++++++++++++++++++++++++------- lib/Travel/Status/DE/HAFAS/Stop.pm | 6 ++-- 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/bin/hafas-m b/bin/hafas-m index 2e48a90..e53a910 100755 --- a/bin/hafas-m +++ b/bin/hafas-m @@ -66,6 +66,10 @@ if ( $opt{station} =~ m{ ^ (? [0-9.]+ ) : (? [0-9].+ ) $ }x ) { }; delete $opt{station}; } +elsif ( $opt{station} =~ m{ ^ [?] (? .*) $ }x ) { + $opt{locationSearch} = $+{query}; + delete $opt{station}; +} elsif ( $opt{station} =~ m{[|]} ) { $opt{journey} = { id => $opt{station} }; delete $opt{station}; @@ -251,6 +255,12 @@ if ( $opt{geoSearch} ) { } exit 0; } +elsif ( $opt{locationSearch} ) { + for my $result ( $status->results ) { + printf( "%8d %s\n", $result->eva, $result->name ); + } + exit 0; +} elsif ( $opt{journey} ) { my $result = $status->result; diff --git a/lib/Travel/Status/DE/HAFAS.pm b/lib/Travel/Status/DE/HAFAS.pm index 1250857..23aba6e 100644 --- a/lib/Travel/Status/DE/HAFAS.pm +++ b/lib/Travel/Status/DE/HAFAS.pm @@ -193,8 +193,15 @@ sub new { $ua->env_proxy; } - if ( not( $conf{station} or $conf{journey} or $conf{geoSearch} ) ) { - confess('station / journey / geoSearch must be specified'); + if ( + not( $conf{station} + or $conf{journey} + or $conf{geoSearch} + or $conf{locationSearch} ) + ) + { + confess( + 'station / journey / geoSearch / locationSearch must be specified'); } if ( not defined $service ) { @@ -270,6 +277,27 @@ sub new { %{ $hafas_instance{$service}{request} } }; } + elsif ( $conf{locationSearch} ) { + $req = { + svcReqL => [ + { + cfg => { polyEnc => 'GPA' }, + meth => 'LocMatch', + req => { + input => { + loc => { + type => 'S', + name => $conf{locationSearch}, + }, + maxLoc => 10, + field => 'S', + }, + } + } + ], + %{ $hafas_instance{$service}{request} } + }; + } else { my $date = ( $conf{datetime} // $now )->strftime('%Y%m%d'); my $time = ( $conf{datetime} // $now )->strftime('%H%M%S'); @@ -372,8 +400,8 @@ sub new { if ( $conf{journey} ) { $self->parse_journey; } - elsif ( $conf{geoSearch} ) { - $self->parse_geosearch; + elsif ( $conf{geoSearch} or $conf{locationSearch} ) { + $self->parse_search; } else { $self->parse_board; @@ -401,8 +429,8 @@ sub new_p { if ( $conf{journey} ) { $self->parse_journey; } - elsif ( $conf{geoSearch} ) { - $self->parse_geosearch; + elsif ( $conf{geoSearch} or $conf{locationSearch} ) { + $self->parse_search; } else { $self->parse_board; @@ -613,7 +641,7 @@ sub add_message { return $message; } -sub parse_geosearch { +sub parse_search { my ($self) = @_; $self->{results} = []; @@ -625,6 +653,10 @@ sub parse_geosearch { my @refLocL = @{ $self->{raw_json}{svcResL}[0]{res}{common}{locL} // [] }; my @locL = @{ $self->{raw_json}{svcResL}[0]{res}{locL} // [] }; + if ( $self->{raw_json}{svcResL}[0]{res}{match} ) { + @locL = @{ $self->{raw_json}{svcResL}[0]{res}{match}{locL} // [] }; + } + for my $loc (@locL) { push( @{ $self->{results} }, @@ -886,7 +918,7 @@ Requests item(s) as specified by I and returns a new Travel::Status::DE::HAFAS element with the results. Dies if the wrong I were passed. -I must contain either a B, a B, or a B flag: +I must contain either a B, B, B, or B flag: =over @@ -902,6 +934,11 @@ Results are available via C<< $status->results >>. Search for stations near I, I. Results are available via C<< $status->results >>. +=item B => I + +Search for stations whose name is similar to I. +Results are available via C<< $status->results >>. + =item B => B<{> B => I [, B => I ] B<}> Request details about the journey identified by I and I. @@ -953,7 +990,7 @@ Default: -1 (do not limit results by time). Passed on to C<< LWP::UserAgent->new >>. Defaults to C<< { timeout => 10 } >>, pass an empty hashref to call the LWP::UserAgent constructor without arguments. -=item B => I (geoSearch, station) +=item B => I (geoSearch, locationSearch, station) Request up to I results. Default: 30. @@ -1001,7 +1038,7 @@ as string. If no backend error occurred, returns undef. In case of an error in the HTTP request or HAFAS backend, returns a string describing it. If no error occurred, returns undef. -=item $status->results (geoSearch) +=item $status->results (geoSearch, locationSearch) Returns a list of stations. Each list element is a Travel::Status::DE::HAFAS::Stop(3pm) object. diff --git a/lib/Travel/Status/DE/HAFAS/Stop.pm b/lib/Travel/Status/DE/HAFAS/Stop.pm index 1f16550..3c56900 100644 --- a/lib/Travel/Status/DE/HAFAS/Stop.pm +++ b/lib/Travel/Status/DE/HAFAS/Stop.pm @@ -61,10 +61,10 @@ version 4.09 =head1 DESCRIPTION Travel::Status::DE::HAFAS::Stop describes a HAFAS stop. It may be part of a -journey or part of a geoSearch request. +journey or part of a geoSearch / locationSearch request. -geoSearch-specific accessors are annotated with "(geoSearch)" and return -undef for non-geoSearch stops. +geoSearch- and locationSearch-specific accessors are annotated accordingly and +return undef for non-geoSearch / non-locationSearch stops. =head1 METHODS -- cgit v1.2.3