#!/usr/bin/env perl use Mojolicious::Lite; use Cache::File; use Travel::Status::DE::DeutscheBahn; our $VERSION = '0.01'; sub get_results_for { my ($station) = @_; my $cache = Cache::File->new( cache_root => '/tmp/db-fake', 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 ); } return @{$results}; } sub handle_request { my $self = shift; my $station = $self->stash('station'); my $via = $self->stash('via'); my @platforms = split( /,/, $self->param('platforms') // q{} ); my $template = $self->param('mode') // 'multi'; $self->stash( departures => [] ); $self->stash( title => 'db-fakedisplay' ); $self->stash( version => $VERSION ); if ( not( $template ~~ [qw[multi single]] ) ) { $template = 'multi'; } if ( not $station ) { $self->render($template); return; } my @departures; my @results = get_results_for($station); if ( not @results ) { $self->render( 'multi', error => "Got no results for '$station'" ); return; } 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; } for my $result (@results) { my $platform = ( split( / /, $result->platform ) )[0]; if ($via) { my @route = $result->route; if ( not( grep { $_ =~ m{$via}io } @route ) ) { next; } } if ( @platforms and not grep { $_ eq $platform } @platforms ) { next; } push( @departures, { time => $result->time, train => $result->train, via => [ $result->route_interesting(3) ], destination => $result->destination, platform => $platform, info => $result->info, } ); } $self->render( $template, departures => \@departures, version => $VERSION, title => "departures for ${station}" ); } get '/_redirect' => sub { my $self = shift; my $station = $self->param('station'); my $via = $self->param('via'); my $params = $self->req->params; $params->remove('station'); $params->remove('via'); for my $param (qw(platforms)) { if ( not $params->param($param) ) { $params->remove($param); } elsif ( $param eq 'mode' and $params->param($param) eq 'multi' ) { $params->remove($param); } } $params = $params->to_string; if ($via) { $self->redirect_to("/${station}/${via}?${params}"); } else { $self->redirect_to("/${station}?${params}"); } }; app->defaults( layout => 'default' ); get '/' => \&handle_request; get '/:station' => \&handle_request; get '/:station/:via' => \&handle_request; get '/multi/:station' => \&handle_request; app->config( hypnotoad => { accepts => 10, listen => ['http://*:8092'], pid_file => '/tmp/db-fake.pid', workers => 2, }, ); app->start(); __DATA__ @@ layouts/default.html.ep <%= $title %> <%= content %>
<% if (my $error = stash 'error') { %>

Error: <%= $error %>

<% } %> <%= form_for _redirect => begin %>

Station name <%= text_field 'station' %>
only display routes via <%= text_field 'via' %> (optional)
on platforms <%= text_field 'platforms' %> (optional)
display type <%= select_field mode => [['combined' => 'multi'], ['platform' => 'single']] %> <%= submit_button 'Display' %>

<% end %>
db-fakedisplay v<%= $version %>
@@ multi.html.ep % if (@{$departures}) {
% my $i = 0; % for my $departure (@{$departures}) { % $i++;
%= $departure->{platform}
%= $departure->{time}
%= $departure->{train}
% my $via_max = @{$departure->{via}}; % my $via_cur = 0; % for my $stop (@{$departure->{via}}) { % $via_cur++; <%= $stop %><% if ($via_cur < $via_max) { %> - <% } %> % }
%= $departure->{destination}
% if ($departure->{info}) {
%= $departure->{info}
% }
% }
% } % else {

DB-Fakedisplay displays the next departures at a DB station, just like the big LC display in the station itself.

% } @@ single.html.ep % if (@{$departures}) { % my $i = 0; % for my $departure (@{$departures}) { % $i++;
%= $departure->{platform}
%= $departure->{time}
%= $departure->{train}
% my $via_max = @{$departure->{via}}; % my $via_cur = 0; % for my $stop (@{$departure->{via}}) { % $via_cur++; <%= $stop %><% if ($via_cur < $via_max) { %> - <% } %> % }
%= $departure->{destination}
% if ($departure->{info}) {
%= $departure->{info}
% }
% } % } % else {
Bitte Ansage beachten

DB-Fakedisplay displays the next departures at a DB station, just like the big LC display in the station itself.

% } @@ not_found.html.ep page not found
page not found