summaryrefslogtreecommitdiff
path: root/bin/efa
blob: d0b21d6ce4dece0d39c33e4c0e8a2af6b06fa074 (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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use utf8;

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

use utf8;

use Encode qw(decode);
use Travel::Routing::DE::EFA;
use Exception::Class;
use Getopt::Long qw/:config no_ignore_case/;
use List::Util qw(first);

our $VERSION = '2.19';
my $ignore_info;
my $efa;
my $efa_url = 'http://efa.vrr.de/vrr/XSLT_TRIP_REQUEST2';
my ( @from, @to, @via, $from_type, $to_type, $via_type );
my $opt = {
	'efa-url'     => \$efa_url,
	'help'        => sub { show_help(0) },
	'ignore-info' => \$ignore_info,
	'from'        => \@from,
	'to'          => \@to,
	'version'     => sub { say "efa version $VERSION"; exit 0 },
	'via'         => \@via,
};

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

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

	say 'Usage: efa [options] <from-city> <from-stop> <to-city> <to-stop>';
	say 'See also: man efa';

	exit $exit_status;
}

sub new_efa_by_url {
	my ($url) = @_;
	my $res = eval {
		Travel::Routing::DE::EFA->new(
			efa_url => $url,

			origin      => [ @from, $from_type ],
			destination => [ @to,   $to_type ],
			via => ( @via ? [ @via, $via_type ] : undef ),

			arrival_time   => $opt->{arrive},
			departure_time => $opt->{depart},
			date           => $opt->{date},
			exclude        => $opt->{exclude},
			train_type     => $opt->{include},
			with_bike      => $opt->{bike},

			select_interchange_by => $opt->{prefer},
			use_near_stops        => $opt->{proximity},
			walk_speed            => $opt->{'walk-speed'},
			max_interchanges      => $opt->{'max-change'},
			num_results           => $opt->{'num-connections'},

			without_solid_stairs => $opt->{without_solid_stairs},
			without_escalators   => $opt->{without_escalators},
			without_elevators    => $opt->{without_elevators},
			with_low_platform    => $opt->{with_low_platform},
			with_wheelchair      => $opt->{with_wheelchair},

			developer_mode => $opt->{devmode},
			lwp_options    => { timeout => $opt->{timeout} },
		);
	};
	return $res;
}

sub handle_efa_exception {
	my ($e) = @_;

	if ( $e->isa('Travel::Routing::DE::EFA::Exception::Setup') ) {
		if ( $e->message ) {
			printf STDERR (
				"User error: %s (option '%s'): %s%s\n",
				$e->description, $e->option, $e->message,
				$e->have ? q{ "} . $e->have . q{"} : q{},
			);
		}
		else {
			printf STDERR (
				"User error: %s (option '%s', got '%s', want '%s')\n",
				$e->description, $e->option, $e->have, $e->want
			);
		}

		exit 1;
	}
	if ( $e->isa('Travel::Routing::DE::EFA::Exception::Net') ) {
		printf STDERR (
			"Network error: %s: %s\n",
			$e->description, $e->http_response->as_string
		);
		exit 2;
	}
	if ( $e->isa('Travel::Routing::DE::EFA::Exception::NoData') ) {
		printf STDERR ( "Backend error: %s\n", $e->description );
		exit 3;
	}
	if ( $e->isa('Travel::Routing::DE::EFA::Exception::Ambiguous') ) {
		printf STDERR (
			"Backend error: The %s '%s' is ambiguous. Try one of %s\n",
			$e->post_key, $e->post_value,, $e->possibilities
		);
		exit 4;
	}
	if ( $e->isa('Travel::Routing::DE::EFA::Exception::Other') ) {
		printf STDERR ( "Backend error: %s: %s\n", $e->description,
			$e->message );
		exit 5;
	}

	printf STDERR ( "Unknown error: %s\n%s", ref($e), $e->trace );
	exit 10;
}

sub check_for_error {
	my ($eval_error) = @_;

	if ( not defined $efa ) {
		if (    $eval_error
			and ref($eval_error)
			and $eval_error->isa('Travel::Routing::DE::EFA::Exception') )
		{
			handle_efa_exception($eval_error);
		}
		elsif ($eval_error) {
			printf STDERR
			  "Unknown Travel::Routing::DE::EFA error:\n${eval_error}";
			exit 10;
		}
		else {
			say STDERR 'Travel::Routing::DE::EFA failed to return an object';
			exit 10;
		}
	}

	return;
}

sub format_footpath {
	my @parts = @_;
	my $str   = q{};

	for my $path_elem (@parts) {
		my ( $type, $level ) = @{$path_elem};
		if ( $level eq 'UP' ) {
			$str .= ' ↗';
		}
		elsif ( $level eq 'DOWN' ) {
			$str .= ' ↘';
		}
		elsif ( $level eq 'LEVEL' ) {
			$str .= ' →';
		}
		else {
			$str .= " [unhandled level, please report a bug : $level]";
		}
	}

	return $str;
}

sub display_routes {
	my (@routes) = @_;

	for my $i ( 0 .. $#routes ) {

		my $route = $routes[$i];

		if ( $opt->{'extended-info'} ) {
			print '# ' . $route->duration;
			if ( $route->ticket_type ) {
				printf( ", Preisstufe %s (%s€ / %s€)\n\n",
					$route->ticket_type, $route->fare_adult,
					$route->fare_child, );
			}
			else {
				print "\n\n";
			}
		}

		for my $c ( $route->parts ) {
			display_connection($c);
		}

		# last one needs to be shown separately
		if ( $opt->{maps} ) {
			my $c = ( $route->parts )[-1];
			for my $m ( $c->arrival_routemaps, $c->arrival_stationmaps ) {
				say "# $m";
			}
		}

		if ( $i != $#routes ) {
			print "---------\n\n";
		}
	}
	return;
}

sub display_connection {
	my ($c) = @_;

	if ( $c->is_cancelled ) {
		say '# FAHRT FÄLLT AUS';
	}
	elsif ( $c->delay ) {
		printf( "# +%d,  Plan: %s -> %s\n",
			$c->delay, $c->departure_stime, $c->arrival_stime );
	}

	for my $note ( $c->regular_notes ) {
		my $text = $note->summary;
		if ( not( length $ignore_info and $text =~ /$ignore_info/i ) ) {
			say "# $text";
		}
	}

	for my $notice ( $c->current_notes ) {
		if ( $notice->subtitle ne $notice->subject ) {
			printf( "# %s - %s\n", $notice->subtitle, $notice->subject );
		}
		else {
			printf( "# %s\n", $notice->subtitle );
		}
	}

	if ( $opt->{maps} ) {
		for my $m ( $c->departure_routemaps, $c->departure_stationmaps ) {
			say "# $m";
		}
	}

	printf(
		"%-5s ab  %-30s %-20s %s\n",
		$c->departure_time,
		$c->departure_stop_and_platform,
		$c->train_line || $c->train_product,
		$c->train_destination,
	);

	if ( $opt->{'full-route'} ) {
		for my $via_stop ( $c->via ) {
			printf( "%-5s     %-30s %s\n",
				$via_stop->[1], $via_stop->[2], $via_stop->[3] );
		}
	}

	printf( "%-5s an  %s\n", $c->arrival_time, $c->arrival_stop_and_platform, );
	print "\n";

	if (    $opt->{'extended-info'}
		and $c->footpath_duration
		and $c->footpath_type ne 'IDEST' )
	{
		printf( "%5d min umsteigen: %s\n\n",
			$c->footpath_duration, format_footpath( $c->footpath_parts ) );
	}

	return;
}

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

#<<<
GetOptions(
	$opt,
	qw{
		accessibility|x=s@
		arrive|a=s
		auto-url|discover-and-print|A
		bike|b
		bike-info|B
		date|d=s
		depart|time|t=s
		devmode
		discover|D
		efa-url|u=s
		exclude|e=s@
		extended-info|E
		from=s@{2}
		full-route|f
		help|h
		ignore-info|I:s
		include|i=s
		list|l
		maps|M
		max-change|m=i
		num-connections|n=i
		prefer|P=s
		proximity|p:10
		service|s=s
		timeout=i
		to=s@{2}
		version|v
		via=s@{2}
		walk-speed|w=s
	},
) or show_help(1);
#>>>

if ( $opt->{accessibility} ) {
	@{ $opt->{accessibility} }
	  = split( qr{,}, join( q{,}, @{ $opt->{accessibility} } ) );
}
else {
	# GetOptions seems to implicitly initialize $opt->{accessibility} to an
	# empty arrayref, but it's best not to rely on that.
	$opt->{accessibility} = [];
}

if ( $opt->{exclude} ) {
	@{ $opt->{exclude} } = split( qr{,}, join( q{,}, @{ $opt->{exclude} } ) );
}

for my $field ( @{ $opt->{accessibility} } ) {
	given ($field) {
		when ( [qw[s no-stairs]] )       { $opt->{without_solid_stairs} = 1 }
		when ( [qw[e no-escalators]] )   { $opt->{without_escalators} = 1 }
		when ( [qw[E no-elevators]] )    { $opt->{without_elevators} = 1 }
		when ( [qw[l nf low-platform]] ) { $opt->{with_low_platform} = 1 }
		when ( [qw[w wheelchair]] )      { $opt->{with_wheelchair} = 1 }
		when ( [qw[i info]] ) { }    # used for ignore_info default
	}
}

if ( $opt->{list} ) {
	printf( "%-40s %-14s %s\n\n", 'service', 'abbr. (-s)', 'url (-u)' );
	for my $service ( Travel::Routing::DE::EFA::get_efa_urls() ) {
		printf( "%-40s %-14s %s\n", @{$service}{qw(name shortname url)} );
	}
	exit 0;
}

if ( $opt->{arrive} and $opt->{depart} ) {
	print STDERR 'Note: The options -a/--arrive and -t/--time/--depart are'
	  . " mutually exclusive\n"
	  . "      Discarding the --arrive option\n\n";
	delete $opt->{arrive};
}

if ( not defined $ignore_info ) {
	$ignore_info = join(
		q{|},
		grep { defined } (
			@{ $opt->{accessibility} } ? undef : 'Einstiegshilfe',
			(
				     $opt->{bike}
				  or $opt->{'bike-info'}
			) ? undef : 'Fahrradmitnahme',
		)
	);
}
elsif ( defined $ignore_info and length($ignore_info) == 0 ) {
	$ignore_info = undef;
}

if ( not( @from and @to ) ) {
	if ( @ARGV == 4 ) {
		( @from[ 0, 1 ], @to[ 0, 1 ] ) = @ARGV;
	}
	elsif ( @ARGV == 6 ) {
		( @from[ 0, 1 ], @via[ 0, 1 ], @to[ 0, 1 ] ) = @ARGV;
	}
	else {
		show_help(1);
	}
}

for my $pair ( [ \@from, \$from_type ], [ \@via, \$via_type ],
	[ \@to, \$to_type ], )
{

	next if ( not defined $pair->[0]->[1] );

	if (
		$pair->[0]->[1] =~ s{ ^ (?<type> [^:]+ ) : \s* (?<target> .+ ) $ }
		{$+{target}}x
	  )
	{
		given ( $+{type} ) {
			when ('addr') { ${ $pair->[1] } = 'address' }
			default       { ${ $pair->[1] } = $+{type} }
		}
	}
}

if ( $opt->{service} ) {
	my $service = first { lc( $_->{shortname} ) eq lc( $opt->{service} ) }
	Travel::Routing::DE::EFA::get_efa_urls();
	if ( not $service ) {
		printf STDERR (
			"Error: Unknown service '%s'. See 'efa -l' for a "
			  . "list of supported service names\n",
			$opt->{service}
		);
		exit 1;
	}
	$efa_url = $service->{url};
}

if ( $opt->{discover} or $opt->{'auto-url'} ) {
	for my $service ( Travel::Routing::DE::EFA::get_efa_urls() ) {
		$efa = new_efa_by_url( $service->{url} );
		if ($efa) {
			if ( $opt->{'auto-url'} ) {
				last;
			}
			printf(
				"%s / %s (%s)\n   ->  efa -s %s %s\n\n",
				@{$service}{qw(name shortname url shortname)},
				join( q{ }, map { "'$_'" } @ARGV ),
			);
		}
	}
	if ( $opt->{'discover'} ) {
		exit 0;
	}
}
else {
	$efa = new_efa_by_url($efa_url);
}

check_for_error($@);

my @routes = $efa->routes;

display_routes( $efa->routes );

__END__

=head1 NAME

efa - Command line client for EFA-based public transit routing services

=head1 SYNOPSIS

=over

=item B<efa> B<--from> I<city> I<stop> B<--to> I<city> I<stop> [ I<additional options> ]

=item B<efa> [ I<options> ] I<from-city> I<from-stop> [ I<via-city> I<via-stop> ] I<to-city> I<to-stop>

=back

=head1 VERSION

version 2.19

=head1 DESCRIPTION

B<efa> is a command line client for EFA-based public transit routing
interfaces, e.g. L<http://efa.vrr.de>.  B<efa> requests connections between two
points and displays the results.

By default, the EFA service at L<http://efa.vrr.de> (operated by
Verkehrsverbund Rhein-Ruhr) is used, which supports most places in Germany.
A list of all supported EFA instances can be retrieved with B<-l> and a custom
instance can be selected using B<-s> I<service> or B<-u> I<url>. It is also
possible to probe all known EFA services for a specific connection using the
B<-A> and B<-D> options.

=head1 OPTIONS

=over

=item B<--from> I<city> I<stop>

Departure place

=item B<--to> I<city> I<stop>

Arrival place

=item B<--via> I<city> I<stop>

Travel via this place

In all cases, if you want I<stop> to be an address or "point of interest", you
can set it to 'addr:something' or 'poi:something'.

=item B<-a>|B<--arrive> I<hh>:I<mm>

Journey end time (overrides --time/--depart)

=item B<-A>|B<--auto-url>|B<--discover-and-print>

Probe all known EFA entry points for the specified connection. 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 this option by default is not recommended, as it puts
EFA services under considerable additional load.

=item B<-b>|B<--bike>

Request connections allowing the passenger to take a bike with them.
Note that this may cause B<efa> to display no routes at all -- In that case,
the backend was unable to find such connections or didn't know about
their bike-support.

=item B<-B>|B<--bike-info>

Show information about bike support, but do not select routes based on it.
In essence, this option simply changes the B<--ignore-info> default. It is
useful when B<--bike> did not return any routes even though most of the
route has bike support.

=item B<-d>|B<--date> I<dd>.I<mm>.[I<yyyy>]

Journey date. Also accepts the shortcut B<tomorrow>.

=item B<-D>|B<--discover>

Probe all known EFA entry points for the specified connection. No routes are
returned in this case. Instead, B<efa> will print the URLs and names of all
entry points which did not return an error.

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

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

=item B<-e>|B<--exclude> I<transports>

Exclude I<transports> (comma separated list).

Possible transports: zug, s-bahn, u-bahn, stadtbahn, tram, stadtbus, regionalbus,
schnellbus, seilbahn, schiff, ast, sonstige

=item B<-E>|B<--extended-info>

Display more than just the basic route information. At the moment, the
following is displayed (if available):

=over

=item * Route duration

=item * Ticket fare

=item * transfer duration and elevation changes (via stairs / escalators)
at each stop

=back

=item B<-f>|B<--full-route>

Display intermediate stops (with time and platform) of each train.  Note that
these are not always available.

=item B<-I>|B<--ignore-info> [ I<regex> ]

Ignore additional information matching I<regex>
(default: /Fahrradmitnahme|Einstiegshilfe/)

If I<regex> is not supplied, removes the default regex (-E<gt> nothing will be ignored)

=item B<-i>|B<--include> I<type>

Include connections using trains of type I<type>, where I<type> may be:

=over

=item * local (default)

only take local trains ("Verbund-/Nahverkehrslinien"). Slow, but the cheapest
method if you're not traveling long distance

=item * ic

Local trains + IC

=item * ice

All trains (local + IC + ICE)

=back

=item B<-l>|B<--list>

List supported EFA services with their URLs (see B<-u>) and abbreviations
(see B<-s>).

=item B<-M>|B<--maps>

Output links to maps of transfer paths and transfer stations where
available.

=item B<-m>|B<--max-change> I<number>

Print connections with at most I<number> interchanges

=item B<-n>|B<--num-connections> I<number>

Return up to I<number> connections.  If unset, the default of the respective
EFA server is used (usually 4 or 5).

=item B<-P>|B<--prefer> I<type>

Prefer connections of I<type>:

=over

=item * speed (default)

The faster, the better

=item * nowait

Prefer connections with less interchanges

=item * nowalk

Prefer connections with less walking (at interchanges)

=back

=item B<-p>|B<--proximity> [I<minutes>]

Take stops close to the stop/start into account.  By default, up to 10 minutes
of walking to/from the route's first/last stop is considered acceptable. You
can specify I<minutes> to use a custom value.

=item B<-s>|B<--service> I<name>

Shortname of the EFA entry point. See Travel::Routing::DE::EFA(3pm) and
the B<-l> option for a list of services.

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

Journey start time

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

Set timeout for HTTP requests. Default: 60 seconds.

=item B<-v>|B<--version>

Print version information

=item B<-w>|B<--walk-speed> I<speed>

Set your walking speed to I<speed>.
Accepted values: normal (default), fast, slow

=item B<-x>|B<--accessibility> I<types>

Request connections satisfying all of the specified accessibility I<types>
(comma-separated list, option may be repeated). Please note that this may cause
the backend to return no results at all, either because no such connection
exists or because the required lines do not have the correct accessibility
attributes set.

The following I<types> are supported:

=over

=item e / no-escalators

Transfers must be possible without taking an escalator.

=item E / no-elevators

Transfers must be possible without using an elevator.

=item i / info

Does not restrict the requested connections. However, this option causes
B<efa> to display additional accessibility information for each route part
(where available). When B<-x w> or B<-x l> do not return any connections,
using B<-x i> might be more helpful (though it cannot guarantee that any of
the results satisfy the respective type).

=item l / nf / low-platform

All vehicles must be low-platform ("Niederflur").

=item s / no-stairs

Transfers must be possible without stairs (e.g. via escalators/elevators or
ramps).

=item w / wheelchair

All vehicles must be wheelchair accessible.

=back

=back

=head1 EXIT STATUS

    0    Everything went well
    1    Invalid arguments, see error message
    2    Network error, unable to send request
    3    efa.vrr.de did not return any parsable data
    4    efa.vrr.de error: ambiguous input
    5    efa.vrr.de error: no connections found
    10   Unknown Travel::Routing::DE::EFA error
    255  Other internal error

=head1 CONFIGURATION

None.

=head1 EXAMPLES

=over

=item efa Do Hbf MH Hbf

Look up a connection from Dortmund (Do) Hbf to ME<uuml>lheim (MH) Hbf

=item efa --include ice Essen Hbf Hamburg Dammtor

Look up a connection with long-distance trains

=item efa --arrive 18:00 -e zug,s-bahn -M E Wickenburgstr D Oststr

Look up a connection from Essen Wickenburgstr to DE<uuml>sseldorf Oststr.
Do not use any trains, make sure to arrive around 18:00 and print links to
maps of all interchange stations.

=item efa -s vvs Stuttgart Hbf Stuttgart Marienplatz

Use the VVS (Verkehrsverbund Stuttgart) EFA service to look up a connection.

=back

=head1 DEPENDENCIES

This script requires perl 5.10 (or higher) with the following modules:

=over

=item * Class::Accessor

=item * Exception::Class

=item * LWP::UserAgent

=item * XML::LibXML

=back

=head1 BUGS AND LIMITATIONS

The EFA backend is not able to calculate "two-way" routes, i.e. from -> via ->
to routes with from == to. If from and to are the same stop, it doesn't even
try to calculate a route ("we recommend walking instead"), if they are close to
each other it may or may not work.  Workaround: Request from -> via using the
normal four-argument efa invocation, read the time, use efa -t time via via to
to to request via -> to.

=head1 AUTHOR

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

=head1 LICENSE

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