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{});
$self->stash( departures => [] );
$self->stash( title => 'db-fakedisplay' );
$self->stash( version => $VERSION );
$self->render('multi');
return;
Birte Kristina Friesel
committed
my @departures;
my @results = get_results_for($station);
if ( not @results ) {
$self->render( 'multi', error => "Got no results for '$station'" );
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;
}
Birte Kristina Friesel
committed
@departures,
Birte Kristina Friesel
committed
time => $result->time,
train => $result->train,
via => [ $result->route_interesting(3) ],
destination => $result->destination,
info => $result->info,
}
);
}
Birte Kristina Friesel
committed
$self->render(
'multi',
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');
for my $param (qw(platforms)) {
if (not $params->param($param)) {
$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;
app->config(
hypnotoad => {
accepts => 10,
listen => ['http://*:8092'],
pid_file => '/tmp/db-fake.pid',
app->start();
__DATA__
Birte Kristina Friesel
committed
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title><%= $title %></title>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<style type="text/css">
html {
font-family: Sans-Serif;
}
Birte Kristina Friesel
committed
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
div.outer {
border: 0.2em solid #000066;
width: 55em;
}
div.display {
background-color: #0000ff;
color: white;
font-family: Sans-Serif;
font-weight: bold;
position: relative;
margin-bottom: 0;
margin-top: 0;
padding-top: 0;
padding-bottom: 0;
width: 55em;
height: 1.4em;
}
div.display div {
overflow: hidden;
position: absolute;
height: 100%;
}
div.time {
left: 0;
width: 6%;
font-size: 95%;
}
div.train {
left: 5%;
width: 9%;
background-color: white;
color: #0000ff;
font-size: 95%;
}
div.via {
left: 15%;
width: 35%;
}
div.via span {
margin-right: 0.4em;
font-size: 80%;
}
div.destination {
left: 50%;
width: 25%;
font-size: 120%;
}
div.platform {
left: 75%;
width: 5%;
}
div.info {
left: 80%;
width: 20%;
background-color: white;
color: #0000ff;
font-size: 80%;
line-height: 150%;
}
div.separator {
border-bottom: 0.1em solid #000066;
}
div.about {
font-family: Sans-Serif;
color: #666666;
}
div.about a {
color: #000066;
}
span.fielddesc {
display: block;
float: left;
width: 10em;
text-align: right;
padding-right: 0.5em;
}
input, select {
border: 1px solid #000066;
}
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
div.s_display {
background-color: #0000ff;
color: white;
font-family: Sans-Serif;
font-weight: bold;
position: relative;
margin-left: 1em;
margin-top: 1em;
float: left;
width: 28em;
height: 4.5em;
border: 0.7em solid #000066;
}
div.s_display div {
overflow: hidden;
position: absolute;
}
div.s_no_data {
top: 0.5em;
left: 1em;
font-size: 1.7em;
}
div.s_time {
top: 0em;
left: 0em;
font-size: 1.7em;
}
div.s_train {
left: 0em;
top: 1.8em;
}
div.s_via {
top: 1.5em;
left: 5.8em;
width: 17em;
height: 1em;
}
div.s_via span {
margin-right: 0.4em;
}
div.s_destinatio {
top: 1.32em;
left: 3.2em;
width: 9.2em;
font-size: 1.8em;
height: 1em;
}
div.s_platform {
top: 0em;
right: 0em;
font-size: 3em;
}
div.s_info {
top: 0em;
left: 5.8em;
width: 16.5em;
height: 1em;
background-color: white;
color: #0000ff;
}
Birte Kristina Friesel
committed
</style>
</head>
<body>
<%= content %>
<div class="input-field">
<% if (my $error = stash 'error') { %>
<p>
Error: <%= $error %><br/>
</p>
<% } %>
<%= form_for _redirect => begin %>
<p>
<span class="fielddesc">Station name</span>
<%= text_field 'station' %>
<br/>
<span class="fielddesc fieldoptional">only display routes via</span>
<%= text_field 'via' %>
(optional)
<br/>
<span class="fielddesc fieldoptional">on platforms</span>
<%= text_field 'platforms' %>
(optional)
<%= submit_button 'Display' %>
</p>
<% end %>
</div>
<div class="about">
<a href="http://finalrewind.org/projects/db-fakedisplay/">db-fakedisplay</a>
v<%= $version %>
</div>
</body>
</html>
@@ multi.html.ep
% if (@{$departures}) {
Birte Kristina Friesel
committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<div class="outer">
% my $i = 0;
% for my $departure (@{$departures}) {
% $i++;
<div class="display <% if (($i % 2) == 0) { %> separator<% } %>">
<div class="platform">
%= $departure->{platform}
</div>
<div class="time">
%= $departure->{time}
</div>
<div class="train">
%= $departure->{train}
</div>
<div class="via">
% my $via_max = @{$departure->{via}};
% my $via_cur = 0;
% for my $stop (@{$departure->{via}}) {
% $via_cur++;
<span><%= $stop %><% if ($via_cur < $via_max) { %> - <% } %></span>
% }
</div>
<div class="destination">
%= $departure->{destination}
</div>
% if ($departure->{info}) {
<div class="info">
%= $departure->{info}
</div>
% }
</div> <!-- display -->
% }
</div> <!-- outer -->
% }
% else {
<p>
DB-Fakedisplay displays the next departures at a DB station, just like the big
LC display in the station itself.
</p>
% }
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
@@ single.html.ep
<div class="s_display">
% if (@{$departures}) {
% my $i = 0;
% for my $departure (@{$departures}) {
% $i++;
<div class="s_platform">
%= $departure->{platform}
</div>
<div class="s_time">
%= $departure->{time}
</div>
<div class="s_train">
%= $departure->{train}
</div>
<div class="s_via">
% my $via_max = @{$departure->{via}};
% my $via_cur = 0;
% for my $stop (@{$departure->{via}}) {
% $via_cur++;
<span><%= $stop %><% if ($via_cur < $via_max) { %> - <% } %></span>
% }
</div>
<div class="s_destination">
%= $departure->{destinatio}
</div>
% if ($departure->{info}) {
<div class="s_info">
%= $departure->{info}
</div>
% }
% }
% else {
<div class="s_no_data">
Bitte Ansage beachten
</div>
</div> <!-- s_display -->
<p>
DB-Fakedisplay displays the next departures at a DB station, just like the big
LC display in the station itself.
</p>
% }
@@ not_found.html.ep
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>page not found</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
</head>
<body>
<div>
page not found
</div>
</body>
</html>