Unverified Commit 2542ed41 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

add locationSearch support

parent a789b0e2
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -66,6 +66,10 @@ if ( $opt{station} =~ m{ ^ (?<lat> [0-9.]+ ) : (?<lon> [0-9].+ ) $ }x ) {
	};
	delete $opt{station};
}
elsif ( $opt{station} =~ m{ ^ [?] (?<query> .*) $ }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;

+47 −10
Original line number Diff line number Diff line
@@ -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<opt> and returns a new
Travel::Status::DE::HAFAS element with the results.  Dies if the wrong
I<opt> were passed.

I<opt> must contain either a B<station>, a B<geoSearch>, or a B<journey> flag:
I<opt> must contain either a B<station>, B<geoSearch>, B<locationSearch>, or B<journey> flag:

=over

@@ -902,6 +934,11 @@ Results are available via C<< $status->results >>.
Search for stations near I<latitude>, I<longitude>.
Results are available via C<< $status->results >>.

=item B<locationSearch> => I<query>

Search for stations whose name is similar to I<query>.
Results are available via C<< $status->results >>.

=item B<journey> => B<{> B<id> => I<tripid> [, B<name> => I<line> ] B<}>

Request details about the journey identified by I<tripid> and I<line>.
@@ -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<results> => I<count> (geoSearch, station)
=item B<results> => I<count> (geoSearch, locationSearch, station)

Request up to I<count> 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.
+3 −3
Original line number Diff line number Diff line
@@ -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