summaryrefslogtreecommitdiff
path: root/bin/efa-m
blob: bda21851d28a54109a568e958e4edba9a2e2b0cb (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
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
224
225
226
227
228
229
230
231
232
233
234
235
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
306
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;

no if $] >= 5.018, warnings => "experimental::smartmatch";

our $VERSION = '1.09';

binmode( STDOUT, ':encoding(utf-8)' );

use Encode qw(decode);
use Getopt::Long qw(:config no_ignore_case);
use List::Util qw(max);
use Travel::Status::DE::EFA;

my $efa_url = 'http://efa.vrr.de/vrr/XSLT_DM_REQUEST';
my ( $date, $time, $input_type, $list_lines, $offset, $relative_times );
my ($full_routes);
my ( $timeout,    $developer_mode );
my ( @grep_lines, @grep_platforms );

@ARGV = map { decode( 'UTF-8', $_ ) } @ARGV;

GetOptions(
	'd|date=s'      => \$date,
	'h|help'        => sub { show_help(0) },
	'l|line=s@'     => \@grep_lines,
	'L|linelist'    => \$list_lines,
	'f|full-route'  => \$full_routes,
	'o|offset=i'    => \$offset,
	'p|platform=s@' => \@grep_platforms,
	'r|relative'    => \$relative_times,
	't|time=s'      => \$time,
	'timeout=i'     => \$timeout,
	'u|efa-url=s'   => \$efa_url,
	'V|version'     => \&show_version,
	'devmode'       => \$developer_mode,

) or show_help(1);

if ( @ARGV != 2 ) {
	show_help(1);
}

# --line=foo,bar support
@grep_lines     = split( qr{,}, join( q{,}, @grep_lines ) );
@grep_platforms = split( qr{,}, join( q{,}, @grep_platforms ) );

my ( $place, $input ) = @ARGV;

if ( $input =~ s{ ^ (?<type> address|poi|stop) : }{}x ) {
	$input_type = $+{type};
}

my $status = Travel::Status::DE::EFA->new(
	date           => $date,
	developer_mode => $developer_mode,
	efa_url        => $efa_url,
	full_routes    => $full_routes,
	place          => $place,
	name           => $input,
	time           => $time,
	type           => $input_type,
	timeout        => $timeout,
);

sub show_help {
	my ($code) = @_;

	print "Usage: efa-m [-d <dd.mm.yyyy>] [-t <hh:mm>] <city> <station>\n"
	  . "See also: man efa-m\n";

	exit $code;
}

sub show_version {
	say "efa-m version ${VERSION}";

	exit 0;
}

sub display_result {
	my (@lines) = @_;

	my @line_length;

	if ( not @lines ) {
		die("Nothing to show\n");
	}

	for my $i ( 0 .. 3 ) {
		$line_length[$i] = max map { length( $_->[$i] ) } @lines;
	}

	for my $line (@lines) {

		if ( length( $line->[4] ) ) {
			$line->[4] =~ tr{\n\x0d}{ }s;
			chomp $line->[4];
			print "\n";
			for my $info_line ( split( qr{\n}, $line->[4] ) ) {
				say "# ${info_line}";
			}
		}

		printf(
			join( q{  }, ( map { "%-${_}s" } @line_length ) ) . "\n",
			@{$line}[ 0 .. 3 ]
		);
	}

	return;
}

sub show_lines {
	my @output;

	for my $l ( $status->lines ) {

		if ( @grep_lines and not( $l->name ~~ \@grep_lines ) ) {
			next;
		}

		push( @output,
			[ $l->type, $l->name, $l->direction // q{}, $l->route // q{} ] );
	}

	display_result(@output);

	return;
}

sub show_results {
	my @output;

	for my $d ( $status->results ) {

		my $platform = $d->platform;
		my $dtime    = (
			$relative_times ? sprintf( '%2d min', $d->countdown ) : $d->time );

		if ( $d->platform_db ) {
			$platform .= ' (DB)';
		}

		if (
			( @grep_lines and not( $d->line ~~ \@grep_lines ) )
			or ( @grep_platforms
				and not( $platform ~~ \@grep_platforms ) )
			or ( $offset and $d->countdown < $offset )
		  )
		{
			next;
		}

		if ( $d->is_cancelled ) {
			if ($relative_times) {
				next;
			}
			else {
				$dtime .= ' CANCELED';
			}
		}
		if ( $d->delay ) {
			$dtime .= ' (+' . $d->delay . ')';
		}

		push( @output,
			[ $dtime, $platform, $d->line, $d->destination, $d->info ] );
	}

	display_result(@output);

	return;
}

if ( my $err = $status->errstr ) {
	say STDERR "Request error: ${err}";
	exit 2;
}

if ($list_lines) {
	show_lines();
}
else {
	show_results();
}

__END__

=head1 NAME

efa-m - Unofficial interface to the efa.vrr.de departure monitor

=head1 SYNOPSIS

B<efa-m> [B<-Lr>] [B<-d> I<dd.mm.yyyy>] [B<-t> I<hh:mm>]
[B<-l> I<lines>] [B<-p> I<platforms>] [B<-u> I<url>]
I<city> [I<type>B<:>]I<name>

=head1 VERSION

version 1.09

=head1 DESCRIPTION

B<efa-m> lists upcoming tram, bus and train departures at the location I<name>
in I<city>.

By default, I<name> refers to a stop, this can be changed by specifying
I<type>.  Supported types are B<address> and B<poi> (point of interest).

=head1 OPTIONS

=over

=item B<-d>, B<--date> I<dd.mm.yyyy>

Show departures for I<date> instead of today.
May also be specified as I<dd.mm.>

=item B<-L>, B<--linelist>

Do not show departures. Instead, list all lines serving the specified place.
Note that this information may be incomplete -- only lines which are in
service either at the time of the B<efa-m> call or at the time specifed
using B<--date> and B<--time> are guaranteed to be included.

=item B<-l>, B<--line> I<lines>

Only show departures of I<lines> (comma-separatad list, option may be
repeated)

=item B<-o>, B<--offset> I<minutes>

Ignore departures which are less than I<minutes> from now.

=item B<-p>, B<--platform> I<platforms>

Only show departures at I<platforms> (comma-separated list, option may be
repeated).  Note that the C<< Bstg. >> / C<< Gleis >> prefix must be omitted.

=item B<-r>, B<--relative>

Use relative departure times.

=item B<-t>, B<--time> I<hh:mm>

Show departures starting at I<time> instead of now.

=item B<-u>, B<--efa-url> I<url>

URL to the EFA entry point, defaults to L<http://efa.vrr.de/vrr/XSLT_DM_REQUEST>.
Depending on your location, some I<url>s may contain more specific data
than others.  See Travel::Status::DE::EFA(3pm) for alternatives.

=item B<--timeout> I<seconds>

Set timeout for HTTP requests. Default: 10 seconds. Set to 0 or a negative
value to disable it.

=item B<-V>, B<--version>

Show version information.

=back

=head1 EXIT STATUS

Normally zero. B<1> means B<efa-m> was called with invalid options,
B<2> indicates a request error from Travel::Status::DE::EFA(3pm).

=head1 CONFIGURATION

None.

=head1 DEPENDENCIES

=over

=item * Class::Accessor(3pm)

=item * LWP::UserAgent(3pm)

=item * Travel::Status::DE::EFA(3pm)

=item * XML::LibXML(3pm)

=back

=head1 BUGS AND LIMITATIONS

B<efa-m> uses the VRR EFA service by default, which seems to contain the
greatest available set of information. However, some cities (e.g. Berlin
or parts of Hamburg) are incomplete. B<efa-m> is not yet able to choose the
appropriate EFA URL for these by itself, it needs to be done manually with
the B<-u>, B<--efa-url> option.

=head1 AUTHOR

Copyright (C) 2011-2014 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>

=head1 LICENSE

This program is licensed under the same terms as Perl itself.