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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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
392
393
394
395
396
397
398
399
400
401
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
|
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use utf8;
our $VERSION = '1.24';
binmode( STDOUT, ':encoding(utf-8)' );
use Encode qw(decode);
use Getopt::Long qw(:config no_ignore_case bundling);
use List::Util qw(first max none);
use Travel::Status::DE::EFA;
my $efa_url = 'https://efa.vrr.de/vrr/XSLT_DM_REQUEST';
my $efa_encoding;
my ( $date, $time, $input_type, $list_lines, $offset, $relative_times );
my ($full_routes);
my ( $filter_via, $track_via );
my ( $timeout, $developer_mode );
my ( @grep_lines, @grep_platforms, @grep_mots );
my ( %edata, @edata_pre );
my ( $list_services, $service, $discover_and_print, $discover );
my $efa;
@ARGV = map { decode( 'UTF-8', $_ ) } @ARGV;
GetOptions(
'A|auto-url|discover-and-print' => \$discover_and_print,
'd|date=s' => \$date,
'D|discover' => \$discover,
'h|help' => sub { show_help(0) },
'l|line=s@' => \@grep_lines,
'L|linelist' => \$list_lines,
'list' => \$list_services,
'm|mot=s@' => \@grep_mots,
'o|offset=i' => \$offset,
'O|output=s@' => \@edata_pre,
'p|platform=s@' => \@grep_platforms,
'r|relative' => \$relative_times,
's|service=s' => \$service,
't|time=s' => \$time,
'timeout=i' => \$timeout,
'u|efa-url=s' => \$efa_url,
'v|via=s' => \$filter_via,
'V|track-via=s' => sub { $filter_via = $track_via = $_[1] },
'version' => \&show_version,
'devmode' => \$developer_mode,
) or show_help(1);
if ($list_services) {
show_services();
}
if ( @ARGV < 1 or @ARGV > 2 ) {
show_help(1);
}
# --line=foo,bar support
@edata_pre = split( qr{,}, join( q{,}, @edata_pre ) );
@grep_lines = split( qr{,}, join( q{,}, @grep_lines ) );
@grep_mots = split( qr{,}, join( q{,}, @grep_mots ) );
@grep_platforms = split( qr{,}, join( q{,}, @grep_platforms ) );
my ( $place, $input );
if ( @ARGV == 1 ) {
$input = $ARGV[0];
}
else {
( $place, $input ) = @ARGV;
}
if ( $input =~ s{ ^ (?<type> address|poi|stop|stopID) : }{}x ) {
$input_type = $+{type};
}
for my $efield (@edata_pre) {
if ( $efield eq 'a' ) { $edata{route_after} = 1; $full_routes = 1 }
elsif ( $efield eq 'b' ) { $edata{route_before} = 1; $full_routes = 1 }
elsif ( $efield eq 'f' ) { $edata{fullroute} = 1; $full_routes = 1 }
elsif ( $efield eq 'r' ) { $edata{route} = 1; $full_routes = 1 }
elsif ( $efield eq 'm' ) { $edata{messages} = 1 }
else { $edata{$efield} = 1 }
}
if ($filter_via) {
$full_routes = 1;
}
if ($service) {
my $service_ref = first { lc( $_->{shortname} ) eq lc($service) }
Travel::Status::DE::EFA::get_efa_urls();
if ( not $service_ref ) {
printf STDERR (
"Error: Unknown service '%s'. See 'efa-m --list' for a "
. "list of supported service names\n",
$service
);
exit 1;
}
$efa_url = $service_ref->{url};
$efa_encoding = $service_ref->{encoding};
}
sub new_efa_by_url {
my ($url) = @_;
my $res = Travel::Status::DE::EFA->new(
date => $date,
developer_mode => $developer_mode,
efa_url => $url,
efa_encoding => $efa_encoding,
full_routes => $full_routes,
place => $place,
name => $input,
time => $time,
type => $input_type,
timeout => $timeout,
);
return $res;
}
sub show_help {
my ($code) = @_;
print "Usage: efa-m [-d <dd.mm.yyyy>] [-t <hh:mm>] [place] <station>\n"
. "See also: man efa-m\n";
exit $code;
}
sub show_services {
printf( "%-45s %-14s %s\n\n", 'service', 'abbr. (-s)', 'url (-u)' );
for my $service ( Travel::Status::DE::EFA::get_efa_urls() ) {
printf( "%-45s %-14s %s\n", @{$service}{qw(name shortname url)} );
}
exit 0;
}
sub show_version {
say "efa-m version ${VERSION}";
exit 0;
}
sub format_delay {
my ( $delay, $len ) = @_;
if ( $delay and $len ) {
return sprintf( "(%+${len}d)", $delay );
}
return q{};
}
sub format_route {
my (@route) = @_;
my $output = q{};
for my $stop (@route) {
if ( not $stop ) {
say 'BUG';
next;
}
if ( defined $stop->arr and defined $stop->dep ) {
if ( $stop->arr->epoch == $stop->dep->epoch ) {
$output .= sprintf(
" %5s %40s %s\n",
$stop->arr->strftime('%H:%M'),
$stop->name, $stop->platform,
);
}
else {
$output .= sprintf(
"%5s → %5s %40s %s\n",
$stop->arr->strftime('%H:%M'),
$stop->dep->strftime('%H:%M'),
$stop->name, $stop->platform,
);
}
}
elsif ( defined $stop->arr ) {
$output .= sprintf(
"%5s %40s %s\n",
$stop->arr->strftime('%H:%M'),
$stop->name, $stop->platform,
);
}
elsif ( defined $stop->dep ) {
$output .= sprintf(
" %5s %40s %s\n",
$stop->dep->strftime('%H:%M'),
$stop->name, $stop->platform,
);
}
}
return $output;
}
sub display_result {
my (@lines) = @_;
my @line_length;
if ( not @lines ) {
die("Nothing to show\n");
}
for my $i ( 0 .. 4 ) {
$line_length[$i] = max map { length( $_->[$i] ) } @lines;
}
for my $line (@lines) {
if ( $edata{messages} and length( $line->[5] ) ) {
$line->[5] =~ tr{\n\x0d}{ }s;
chomp $line->[5];
print "\n";
for my $info_line ( split( qr{\n}, $line->[5] ) ) {
say "# ${info_line}";
}
}
printf(
join( q{ }, ( map { "%-${_}s" } @line_length ) ) . "\n",
@{$line}[ 0 .. 4 ]
);
if ( $line->[6] ) {
say $line->[6];
}
}
return;
}
sub show_lines {
my @output;
for my $l ( $efa->lines ) {
if ( ( @grep_lines and none { $l->name eq $_ } @grep_lines )
or ( @grep_mots and none { $l->mot_name eq $_ } @grep_mots ) )
{
next;
}
if ( @grep_mots and none { $l->mot_name eq $_ } @grep_mots ) {
next;
}
push( @output,
[ $l->type, $l->name, $l->direction // q{}, q{}, $l->route // q{} ]
);
}
display_result(@output);
return;
}
sub show_results {
my @output;
my $delay_len = 0;
my $delay_fmt = 0;
for my $d ( $efa->results ) {
if ( $d->delay ) {
$delay_len = max( $delay_len, length( $d->delay ) + 1 );
}
}
if ($delay_len) {
$delay_fmt = $delay_len + 3;
}
for my $d ( $efa->results ) {
my @output_line;
my $platform = $d->platform;
my $dtime = (
$relative_times
? sprintf( '%2d min', $d->countdown )
: $d->datetime->strftime('%H:%M')
);
if ( $d->platform_db ) {
$platform .= ' (DB)';
}
if (
( @grep_lines and none { $d->line eq $_ } @grep_lines )
or ( @grep_mots and none { $d->mot_name eq $_ } @grep_mots )
or ( @grep_platforms
and none { $platform eq $_ } @grep_platforms )
or ( $offset and $d->countdown < $offset )
or ( $filter_via
and
not( first { $_->name =~ m{$filter_via}io } $d->route_post ) )
)
{
next;
}
if ( $d->is_cancelled ) {
if ($relative_times) {
next;
}
else {
$dtime = '--:--';
}
}
elsif ($track_via) {
my $via = first { $_->name =~ m{$filter_via}io } $d->route_post;
$dtime .= ' → ' . $via->arr->strftime('%H:%M');
}
if ( $d->delay ) {
$dtime .= ' ' . format_delay( $d->delay, $delay_len );
}
my $line = $d->line;
if ( length($line) > 10 and $d->train_type and $d->train_no ) {
$line = $d->train_type . ' ' . $d->train_no;
}
@output_line
= ( $dtime, $platform, $line, q{}, $d->destination, $d->info );
if ( $edata{route} ) {
$output_line[3]
= join( q{ }, map { $_->name_suf } $d->route_interesting );
}
elsif ( $d->occupancy ) {
if ( $d->occupancy eq 'MANY_SEATS' ) {
$output_line[3] = '_';
}
elsif ( $d->occupancy eq 'FEW_SEATS' ) {
$output_line[3] = '*';
}
elsif ( $d->occupancy eq 'STANDING_ONLY' ) {
$output_line[3] = '!';
}
else {
$output_line[3] = '?';
}
}
if ( $edata{fullroute} ) {
$output_line[6]
= format_route( $d->route_pre )
. ' -' x 30 . "\n"
. format_route( $d->route_post );
}
elsif ( $edata{route_after} ) {
$output_line[6] = format_route( $d->route_post );
}
elsif ( $edata{route_before} ) {
$output_line[6] = format_route( reverse $d->route_pre );
}
push( @output, \@output_line );
}
display_result(@output);
return;
}
if ( $discover or $discover_and_print ) {
for my $service_ref ( Travel::Status::DE::EFA::get_efa_urls() ) {
$efa = new_efa_by_url( $service_ref->{url} );
if ( $efa and not $efa->errstr ) {
if ($discover_and_print) {
last;
}
printf(
"%s / %s (%s)\n -> efa-m -s %s %s\n\n",
@{$service_ref}{qw(name shortname url shortname)},
join( q{ }, map { "'$_'" } @ARGV ),
);
}
}
if ($discover) {
exit 0;
}
}
$efa = new_efa_by_url($efa_url);
if ( my $err = $efa->errstr ) {
say STDERR "Request error: ${err}";
if ( $efa->place_candidates ) {
say 'You might want to try one of the following places:';
say join( "\n", $efa->place_candidates );
}
elsif ( $efa->name_candidates ) {
say 'You might want to try one of the following names:';
say join( "\n", $efa->name_candidates );
}
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.24
=head1 DESCRIPTION
B<efa-m> lists scheduled tram, bus and train departures at the location I<name>.
For each departure, it shows
=over
=item * scheduled departure time,
=item * delay in minutes,
=item * platform,
=item * line,
=item * expected occupation (from _ to !, if available), and
=item * destination.
=back
If I<city> is specified, I<name> refers to a location within I<city>. Otherwise,
I<name> must be self-contained. I.e., both C<< efa Essen Hbf >> and
C<< efa "Essen Hbf" >> are valid. Note, however, than C<< efa E Hbf >> works,
but C<< efa "E Hbf" >> does not.
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<-A>, B<--auto-url>, B<--discover-and-print>
Probe all known EFA entry points for the specified stop. Print the first
result which was not an error.
Note that this may take a while and will not necessarily return the best
result. Also, using thi option by default is not recommended, as it puts EFA
services under considerable additional load.
=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<-D>, B<--discover>
Probe all known EFA entry points for the specified stop. Print the URLs and
names of all entry points which did not return an error.
=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<--list>
List supported EFA services with their URLs (see B<-u>) and abbreviations (see
B<-s>).
=item B<-m>, B<--mot> I<motlist>
Only show departures whose type appears in I<motlist> (comma-separated list,
this option may be repeated).
The following departure types ("modes of transport") are supported:
zug, s-bahn, u-bahn, stadtbahn, tram, stadtbus, regionalbus, schnellbus,
seilbahn, schiff, ast, sonstige
=item B<-o>, B<--offset> I<minutes>
Ignore departures which are less than I<minutes> from now.
=item B<-O>, B<--output> I<outputtypes>
For each result, show additional information as specified by I<outputtypes>.
I<outputtypes> is a comma-separated list, the B<-O>/B<--output> option may
also be repeated. Each output type has both a short and long form, for instance
both "-Or" and "--output=route" are valid.
The following output types are supported:
=over
=item a / route_after
Show each departure's full route (timestamps and stop names) after the
requested station.
=item b / route_before
Show each departure's full route (timestamps and stop names) before the
requested station.
=item f / fullroute
Show each departure's full route (timestamps and stop names) before and
after the requested station.
=item r / route
Show up to three stops between the requested station and the departure's
destination. B<efa-m> tries to display the three most important stops,
however these are heuristically determined and may not be optimal.
=item m / messages
Show free-text messages associated with individual departures. These can
include generic information such is bicycle transportation options or Wi-Fi
availability, delay reasons, and more.
=back
=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>
Show relative departure times in minutes (i.e. the time difference between
the departure and the time of the request). In this case, realtime data is
already included.
=item B<-s>, B<--service> I<name>
Short name of the EFA entry point. See Travel::Status::DE::EFA(3pm) and the
B<--list> option for a list of services.
=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) and the B<--list> option 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<--via> I<station>
Only show trains serving I<station> after the requseted stop. I<station>
is matched against the "I<city> I<stop>" fields in each line's route.
Regular expressions are also supported.
=item B<-V>, B<--track-via> I<station>
Lik B<--via>: Only show trains serving I<station> after the requseted stop.
Also, show the arrival time at I<station> after the departure time at the
current stop.
=item 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 not supported there, and B<efa-m> is not yet able to
choose the appropriate EFA URL for these by itself. In these cases, you should
find an appropriate EFA service using the B<-D>/B<--discover> option and then
use B<-s> I<service> when making requests.
=over
=item * B<--track-via> uses real-time data for departure but schedule data for arrival
=back
=head1 AUTHOR
Copyright (C) 2011-2021 by Birte Kristina Friesel E<lt>derf@finalrewind.orgE<gt>
=head1 LICENSE
This program is licensed under the same terms as Perl itself.
|