summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2023-08-29 20:54:11 +0200
committerBirte Kristina Friesel <derf@finalrewind.org>2023-08-29 20:54:54 +0200
commit6a671d45390c883d994defdaa544405223b8dc29 (patch)
tree31d7e142891749e2e3256e1cab6b325fbb360d05 /lib
parentfab688d0da2242a2dc9145c82eafc0a9877d3f74 (diff)
geostop: always show hafas and iris stops
Diffstat (limited to 'lib')
-rw-r--r--lib/DBInfoscreen/Controller/Static.pm15
-rw-r--r--lib/DBInfoscreen/Controller/Stationboard.pm113
2 files changed, 61 insertions, 67 deletions
diff --git a/lib/DBInfoscreen/Controller/Static.pm b/lib/DBInfoscreen/Controller/Static.pm
index 4560d02..22f2034 100644
--- a/lib/DBInfoscreen/Controller/Static.pm
+++ b/lib/DBInfoscreen/Controller/Static.pm
@@ -42,23 +42,8 @@ sub redirect {
sub geostop {
my ($self) = @_;
- my ( $api_link, $api_text, $api_icon );
- if ( $self->param('hafas') ) {
- $api_link = '/_autostop';
- $api_text = 'Auf Bahnverkehr wechseln';
- $api_icon = 'directions_bus';
- }
- else {
- $api_link = '/_autostop?hafas=1';
- $api_text = 'Auf Nahverkehr wechseln';
- $api_icon = 'train';
- }
-
$self->render(
'geostop',
- api_link => $api_link,
- api_text => $api_text,
- api_icon => $api_icon,
with_geostop => 1,
hide_opts => 1
);
diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm
index 1a03b10..d22443e 100644
--- a/lib/DBInfoscreen/Controller/Stationboard.pm
+++ b/lib/DBInfoscreen/Controller/Stationboard.pm
@@ -11,6 +11,7 @@ use DateTime::Format::Strptime;
use Encode qw(decode encode);
use File::Slurp qw(read_file write_file);
use List::Util qw(max uniq);
+use List::UtilsBy qw(uniq_by);
use List::MoreUtils qw();
use Mojo::JSON qw(decode_json encode_json);
use Mojo::Promise;
@@ -1781,63 +1782,71 @@ sub stations_by_coordinates {
if ( not $lon or not $lat ) {
$self->render( json => { error => 'Invalid lon/lat received' } );
+ return;
}
- if ($hafas) {
- $self->render_later;
- Travel::Status::DE::HAFAS->new_p(
- promise => 'Mojo::Promise',
- user_agent => $self->ua,
- geoSearch => {
- lat => $lat,
- lon => $lon
- }
- )->then(
- sub {
- my ($hafas) = @_;
- my @candidates = map {
- {
- name => $_->name,
- eva => $_->eva,
- distance => $_->distance_m / 1000,
- hafas => 1
- }
- } $hafas->results;
- $self->render(
- json => {
- candidates => [@candidates],
- }
- );
- }
- )->catch(
- sub {
- my ($err) = @_;
- $self->render( json => { error => $err } );
- }
- )->wait;
- }
- else {
- my @candidates = map {
- {
- ds100 => $_->[0][0],
- name => $_->[0][1],
- eva => $_->[0][2],
- lon => $_->[0][3],
- lat => $_->[0][4],
- distance => $_->[1],
- hafas => 0,
- }
- } Travel::Status::DE::IRIS::Stations::get_station_by_location( $lon,
- $lat, 10 );
- $self->render(
- json => {
- candidates => [@candidates],
+
+ $self->render_later;
+
+ my @iris = map {
+ {
+ ds100 => $_->[0][0],
+ name => $_->[0][1],
+ eva => $_->[0][2],
+ lon => $_->[0][3],
+ lat => $_->[0][4],
+ distance => $_->[1],
+ hafas => 0,
+ }
+ } Travel::Status::DE::IRIS::Stations::get_station_by_location( $lon,
+ $lat, 10 );
+
+ @iris = uniq_by { $_->{name} } @iris;
+
+ Travel::Status::DE::HAFAS->new_p(
+ promise => 'Mojo::Promise',
+ user_agent => $self->ua,
+ geoSearch => {
+ lat => $lat,
+ lon => $lon
+ }
+ )->then(
+ sub {
+ my ($hafas) = @_;
+ my @hafas = map {
+ {
+ name => $_->name,
+ eva => $_->eva,
+ distance => $_->distance_m / 1000,
+ hafas => 1
+ }
+ } $hafas->results;
+ if ( @hafas > 10 ) {
+ @hafas = @hafas[ 0 .. 9 ];
}
- );
- }
+ my @results = map { $_->[0] }
+ sort { $a->[1] <=> $b->[1] }
+ map { [ $_, $_->{distance} ] } ( @iris, @hafas );
+ $self->render(
+ json => {
+ candidates => [@results],
+ }
+ );
+ }
+ )->catch(
+ sub {
+ my ($err) = @_;
+ $self->render(
+ json => {
+ candidates => [@iris],
+ warning => $err,
+ }
+ );
+ }
+ )->wait;
}
sub autocomplete {
- my $self = shift;
+ my ($self) = @_;
$self->res->headers->cache_control('max-age=31536000, immutable');