Newer
Older
#!/usr/bin/env perl
use Mojolicious::Lite;
use Travel::Status::DE::DeutscheBahn;
sub get_results_for {
my ($station) = @_;
my $cache = Cache::File->new(
default_expires => '900 sec'
);
my $results = $cache->thaw($station);
if ( not $results ) {
my $status
= Travel::Status::DE::DeutscheBahn->new( station => $station );
$results = [ $status->results ];
$cache->freeze( $station, $results );
my $station = $self->stash('station');
my @platforms = split( /,/, $self->param('platforms') // q{} );
Birte Kristina Friesel
committed
my $template = $self->param('mode') // 'multi';
$self->stash( departures => [] );
$self->stash( title => 'db-fakedisplay' );
$self->stash( version => $VERSION );
if ( not( $template ~~ [qw[multi single]] ) ) {
Birte Kristina Friesel
committed
$template = 'multi';
}
Birte Kristina Friesel
committed
$self->render($template);
Birte Kristina Friesel
committed
my @departures;
my @results = get_results_for($station);
if ( not @results ) {
$self->render( 'multi', error => "Got no results for '$station'" );
if ( $template eq 'single' ) {
if ( not @platforms ) {
for my $result (@results) {
if ( not( $result->platform ~~ \@platforms ) ) {
push( @platforms, $result->platform );
}
}
@platforms = sort { $a <=> $b } @platforms;
}
my %pcnt;
@results = grep { $pcnt{ $_->platform }++ < 1 } @results;
@results = sort { $a->platform <=> $b->platform } @results;
}
my $platform = ( split( / /, $result->platform ) )[0];
if ( not( grep { $_ =~ m{$via}io } @route ) ) {
if ( @platforms and not grep { $_ eq $platform } @platforms ) {
if ( $info eq '+0' ) {
if ( $info and $info =~ m{ ^ \+ (\d+) $ }ox ) {
$info = "Verspaetung ca. $1 Minuten";
}
Birte Kristina Friesel
committed
@departures,
Birte Kristina Friesel
committed
time => $result->time,
train => $result->train,
via => [ $result->route_interesting(3) ],
destination => $result->destination,
Birte Kristina Friesel
committed
$self->render(
Birte Kristina Friesel
committed
$template,
Birte Kristina Friesel
committed
departures => \@departures,
version => $VERSION,
title => "departures for ${station}"
);
get '/_redirect' => sub {
my $station = $self->param('station');
my $params = $self->req->params;
$params->remove('station');
$params->remove('via');
if ( $params->param('mode') and $params->param('mode') eq 'multi' ) {
$params->remove($param);
}
}
$params = $params->to_string;
$self->redirect_to("/${station}/${via}?${params}");
$self->redirect_to("/${station}?${params}");
app->defaults( layout => 'default' );
get '/' => \&handle_request;
get '/:station' => \&handle_request;
get '/multi/:station' => \&handle_request;
accepts => 10,
listen => ['http://*:8092'],