1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#!/usr/bin/env perl
use Mojolicious::Lite;
use Cache::File;
use List::MoreUtils qw(any);
use Travel::Status::DE::DeutscheBahn;
use 5.014;
use utf8;
no if $] >= 5.018, warnings => "experimental::smartmatch";
our $VERSION = qx{git describe --dirty} || '0.04';
my $refresh_interval = 180;
sub get_results_for {
my ($station) = @_;
my $cache = Cache::File->new(
cache_root => '/tmp/db-fake',
default_expires => $refresh_interval . ' 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';
my $hide_low_delay = $self->param('hidelowdelay') // 0;
$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( any { $_ =~ m{$via}io } @route ) ) {
next;
}
}
if ( @platforms and not( any { $_ eq $platform } @platforms ) ) {
next;
}
my $info = $result->info;
if ( $info eq '+0' ) {
$info = undef;
}
if ($hide_low_delay and $info) {
$info =~ s{ ^ (?: ca\. \s* )? \+ [ 1 2 3 4 ] $ }{}x;
}
if ($info) {
$info
=~ s{ ^ (?: ca\. \s* )? \+ (\d+) }{Verspätung ca $1 Min.}x;
}
push(
@departures,
{
time => $result->time,
train => $result->train,
via => [ $result->route_interesting(3) ],
destination => $result->destination,
platform => $platform,
info => $info,
}
);
}
$self->render(
$template,
departures => \@departures,
version => $VERSION,
title => "departures for ${station}",
refresh_interval => $refresh_interval + 3,
);
}
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');
if ( $params->param('mode') and $params->param('mode') eq 'multi' ) {
$params->remove('mode');
}
for my $param (qw(platforms)) {
if ( not $params->param($param) ) {
$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();
|