summaryrefslogtreecommitdiff
path: root/cgi/index.pl
blob: 424f07cdf7e5de08b4108322c3846590aa1bed28 (plain)
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
#!/usr/bin/env perl
use Mojolicious::Lite;
use Cache::File;
use File::ShareDir qw(dist_file);
use HTML::Template;
use Travel::Status::DE::DeutscheBahn;

our $VERSION = '0.00';

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};
}

get '/' => sub {
	my $self    = shift;
	my $station = $self->param('station');

	$self->stash( 'version', $VERSION );

	if ( not $station ) {
		return $self->render;
	}
	$self->redirect_to("/${station}");
} => 'index';

get '/:station' => sub {
	my $self    = shift;
	my $station = $self->stash('station');

	my @params;
	my $template = HTML::Template->new(
		filename          => dist_file( 'db-fakedisplay', 'multi-lcd.html' ),
		loop_context_vars => 1,
	);
	my @results = get_results_for($station);

	$self->stash( 'version', $VERSION );

	if ( not @results ) {
		$self->render( 'index', error => "Got no results for '$station'", );
		return;
	}

	for my $result (@results) {
		push(
			@params,
			{
				time  => $result->time,
				train => $result->train,
				via => [ map { { stop => $_ } } $result->route_interesting(3) ],
				destination => $result->destination,
				platform    => ( split( / /, $result->platform ) )[0],
				info        => $result->info,
			}
		);
	}
	$template->param(
		departures => \@params,
		version    => $VERSION
	);

	$self->render( text => $template->output );
};

get '/multi/:station' => sub {
	my $self    = shift;
	my $station = $self->stash('station');
	$self->redirect_to("/${station}");
};

app->start();

__DATA__

@@ index.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>DB Fakedisplay</title>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
	<style type="text/css">

body {
	font-family: Sans-Serif;
}

p.about {
	color: #666666;
}

p.about a {
	color: #000066;
	text-decoration: none;
}

	</style>
</head>
<body>
<div>
<p>
DB-Fakedisplay displays the next departures at a DB station, just like the big
LC display in the station itself.
</p>

<% if (my $error = stash 'error') { %>
  Error: <%= $error %><br/>
<% } %>
<%= form_for index => begin %>
<p>
  Station name:<br/>
  <%= text_field 'station' %><br/>
  <%= submit_button 'Display' %>
</p>
<% end %>

<p>
(For example: "Koeln Hbf" or "Essen West")
</p>

<p class="about">
This is <a
href="http://finalrewind.org/projects/db-fakedisplay/">db-fakedisplay</a>
v<%= $version %>
</p>

</div>
</body>
</html>