summaryrefslogtreecommitdiff
path: root/bin/efa
blob: 5b6dc847fe5239ffcaa0c4a7a2fcf9490e483495 (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
#!/usr/bin/env perl
## Copyright © 2009 by Daniel Friesel <derf@derf.homelinux.org>
## License: WTFPL <http://sam.zoy.org/wtfpl>
use strict;
use warnings;
use 5.010;
use encoding 'utf8';
use Getopt::Long;
use WWW::Mechanize;

my $firsturl = 'http://efa.vrr.de/vrr/XSLT_TRIP_REQUEST2?language=de&itdLPxx_transpCompany=vrr';
my $posturl = 'http://efa.vrr.de/vrr/XSLT_TRIP_REQUEST2';

my $content;
my %post;
my $www = WWW::Mechanize->new(
	autocheck => 1,
);
my $raw;
my $cons;
my (@from, @to, @via);
my ($from_type, $to_type, $via_type) = ('stop', 'stop', 'stop');
my ($time, $time_depart, $time_arrive);
my $date;
my @exclude;
my $maxinter;
my $restrict;
my $prefer;
my $proximity;
my $walk_speed;
my $with_bike;
my $debug = 0;
my $ignore_info = 'Fahrradmitnahme';
my ($i, $j, $con, $part);

sub check_ambiguous($) {
	my $html = shift;
	my $choose_re = qr/<span class="errorTextBold">Bitte auswählen<\/span>/;
	my $select_re = qr/<select name="(?<what>(place|type|name)_(origin|destination))"/;
	my $option_re = qr/<option value="\d+(:\d+)*"( selected)?>(?<choice>[^<]+)<\/option>/;

	if ($html =~ /$choose_re/s) {
		foreach (split(/$choose_re/s, $html)) {
			if (/$select_re/) {
				print "Ambiguous input for $+{what}\n";
			}
			while (/$option_re/gs) {
				print "\t$+{choice}\n";
			}
		}
		return(1);
	}
	return(0);
}

sub parse_content($) {
	my $raw = shift;
	my $groupsize = 8;
	my $offset;
	my $return;
	my @extra;
	for (my $offer = 0; exists($raw->[$offer]); $offer++) {
		foreach (@{$raw->[$offer]}) {
			s/\s*<br>\s*/, /g;
			s/<[^>]+>//g;
		}
		for (my $i = 0; @{$raw->[$offer]} >= (($i+1) * $groupsize) - 1; $i++) {
			$offset = $i * $groupsize;
			undef(@extra);
			if ($raw->[$offer]->[$offset+2] =~ /^Fußweg/) {
				# These are generic, which means they don't contain a time
				splice(@{$raw->[$offer]}, $offset, 0, '');
				splice(@{$raw->[$offer]}, $offset+4, 0, '');
			}
			if ($raw->[$offer]->[$offset+3] =~ /^Fußweg/) {
				# These messages lack the last element, so inject it
				splice(@{$raw->[$offer]}, $offset+7, 0, '');
			}
			for my $j (0, 4, 8) {
				until (not exists($raw->[$offer]->[$offset+$j]) or $raw->[$offer]->[$offset+$j] =~ /^(\d+:\d+)?$/) {
					last unless (exists($raw->[$offer]->[$offset+$j]));
					last if ($raw->[$offer]->[$offset+$j] eq 'Verspätungen sind berücksichtigt');
					if ($raw->[$offer]->[$offset+$j] =~ /^\s*$/) {
						splice(@{$raw->[$offer]}, $offset+$j, 1);
					} else {
						push(@extra, splice(@{$raw->[$offer]}, $offset+$j, 1));
					}
				}
			}
			$return->[$offer]->[$i] = {
				deptime  => $raw->[$offer]->[$offset],
				dep      => $raw->[$offer]->[$offset+1],
				depstop  => $raw->[$offer]->[$offset+2],
				deptrain => $raw->[$offer]->[$offset+3],
				depdest  => $raw->[$offer]->[$offset+7],
				arrtime  => $raw->[$offer]->[$offset+4],
				arr      => $raw->[$offer]->[$offset+5],
				arrstop  => $raw->[$offer]->[$offset+6],
			};
			@{$return->[$offer]->[$i]->{extra}} = @extra;
		}
	}
	return($return);
}

sub prepare_content($) {
	my $html = shift;
	my $offer = 0;
	my $return;
	# beware of the no-break space (U+00A0)     ↓     ↓
	foreach (split(/<span class="labelTextBold"> \d+\. Fahrt<\/span>/, $html)) {
		unless ($offer) {
			$offer++;
			next;
		}
		foreach(split(/\n/)) {
			if (/<span class="labelText"( valign="center")?>(?<content>.+)<\/span><\/td>/) {
				push(@{$return->[$offer-1]}, $+{content});
			}
		}
		$offer++;
	}
	return($return);
}

GetOptions(
	'arrive=s'  => \$time_arrive,
	'bike'      => \$with_bike,
	'date=s'    => \$date,
	'debug'     => \$debug,
	'depart=s'  => \$time_depart,
	'exclude=s' => \@exclude,
	'from=s{2}' => \@from,
	'from-type=s' => \$from_type,
	'help'      => sub {exec('perldoc', '-F', $0)},
	'ignore-info=s{0,1}' => \$ignore_info,
	'max-change=i'  => \$maxinter,
	'post=s'    => \%post,
	'prefer=s'  => \$prefer,
	'proximity' => \$proximity,
	'restrict=s' => \$restrict,
	'time=s'    => \$time,
	'to=s{2}'   => \@to,
	'to-type=s' => \$to_type,
	'via=s{2}'  => \@via,
	'via-type=s' => \$via_type,
	'walk-speed=s' => \$walk_speed,
);

@exclude = split(/,/, join(',', @exclude));

unless (@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;
	}
}

unless (@to == 2 and @from == 2) {
	print STDERR "Usage: see '$0 --help'\n";
	exit(1);
}

@post{'place_origin','name_origin'} = @from;
@post{'place_destination','name_destination'} = @to;
if (@via == 2) {
	@post{'place_via','name_via'} = @via;
}

foreach ($from_type, $to_type, $via_type) {
	unless ($_ ~~ ['stop', 'address', 'poi']) {
		$_ = 'stop';
		print STDERR "from/to/via type: must be stop, address or poi\n";
	}
}

$post{type_origin} = $from_type;
$post{type_destination} = $to_type;
$post{type_via} = $via_type;

if ($time_arrive) {
	$time = $time_arrive;
	$post{itdTripDateTimeDepArr} = 'arr';
} elsif ($time_depart) {
	$time = $time_depart;
	$post{itdTripDateTimeDepArr} = 'dep';
}

if ($time) {
	@post{'itdTimeHour','itdTimeMinute'} = split(/:/, $time);
}
if ($date) {
	@post{'itdDateDay','itdDateMonth','itdDateYear'} = split(/\./, $date);
	$post{itdDateYear} //= (localtime(time))[5] + 1900;
}

if (@exclude) {
	foreach(@exclude) {
		given($_) {
			when('zug')    { $post{inclMOT_0} = undef }
			when('s-bahn') { $post{inclMOT_1} = undef }
			when('u-bahn') { $post{inclMOT_2} = undef }
			when('stadtbahn') { $post{inclMOT_3} = undef }
			when('tram')   { $post{inclMOT_4} = undef }
			when('stadtbus') { $post{inclMOT_5} = undef }
			when('regionalbus') { $post{inclMOT_6} = undef }
			when('schnellbus') { $post{inclMOT_7} = undef }
			when('seilbahn') { $post{inclMOT_8} = undef }
			when('schiff') { $post{inclMOT_9} = undef }
			when('ast')    { $post{inclMOT_10} = undef }
			when('sonstige') {$post{inclMOT_11} = undef }
			default {
				print STDERR "--exclude: invalid argument\n";
			}
		}
	}
}

if (defined($maxinter)) {
	$post{maxChanges} = $maxinter;
}

if ($prefer) {
	given($prefer) {
		when('speed') { $post{routeType} = 'LEASTTIME' }
		when('nowait') { $post{routeType} = 'LEASTINTERCHANGE' }
		when('nowalk') { $post{routeType} = 'LEASTWALKING' }
		default {
			print STDERR "--prefer usage: speed / nowait / nowalk\n";
		}
	}
}

if ($proximity) {
	$post{useProxFootSearch} = 1;
}

if ($restrict) {
	given ($restrict) {
		when('local') { $post{lineRestriction} = 403 }
		when('ic')    { $post{lineRestriction} = 401 }
		when('ice')   { $post{lineRestriction} = 400 }
		when(/\d+/)   { $post{lineRestriction} = $restrict }
		default {
			print STDERR "--restrict usage: local / ic / ice\n";
		}
	}
}

if ($walk_speed) {
	if ($walk_speed ~~ ['normal', 'fast', 'slow']) {
		$post{changeSpeed} = $walk_speed;
	} else {
		print STDERR "--walk-speed usage: normal / fast / slow\n";
	}
}

if ($with_bike) {
	$ignore_info = undef;
	$post{bikeTakeAlong} = 1;
}

$www->get($firsturl);
$www->submit_form(
	form_name => 'jp',
	fields => \%post,
);
$content = $www->content;


if (check_ambiguous($content)) {
	exit(1);
}

$raw = prepare_content($content);

if ($debug) {
	print STDERR "custom post values used in query:\n";
	foreach(keys(%post)) {
		print STDERR "    $_ => $post{$_}\n";
	}
	print STDERR "\nraw response:\n";
	foreach(@$raw) {
		print STDERR "---\n";
		foreach(@$_) {
			print STDERR "$_\n";
		}
	}
}

$cons = parse_content($raw);

for ($i = 0; $con = $cons->[$i]; $i++) {
	for ($j = 0; $part = $con->[$j]; $j++) {
		foreach (@{$part->{extra}}) {
			unless ($ignore_info and $_ =~ /$ignore_info/i) {
				print "# $_\n";
			}
		}
		printf(
			"%-5s %-2s %-30s %-20s %s\n%-5s %-2s %-30s\n\n",
			$part->{deptime}, $part->{dep}, $part->{depstop}, $part->{deptrain},
			$part->{depdest}, $part->{arrtime}, $part->{arr}, $part->{arrstop}
		);
	}
	if (defined($cons->[$i+1])) {
		print "------\n\n";
	}
}

__END__

=head1 NAME

efa - unofficial efa.vrr.de command line client

=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 DESCRIPTION

B<efa> is a command line client for the L<http://efa.vrr.de> web interface.
It sends the specified information to the online form and displays the results

=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

=item B<--from-type>, B<--to-type>, B<--via-type> I<type>

Designate type of the I<stop> for from/to/via.
Possible I<type>s: B<stop> (default), B<address>, B<poi> (point of interest)

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

Journey start time

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

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

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

Journey date

=item B<--bike>

Choose connections where you can take a bike with you

=item 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<--max-change> I<number>

Print connections with at most I<number> interchanges

=item 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<--proximity>

Take stops close to the stop/start into account and possibly use them instead

=item B<--restrict> I<type>

Only accept 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 travelling long distance

=item * ic

All trains except ICE

=item * ice

All trains

=back

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

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

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

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

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

=item B<--debug>

Display debug information (additional post requests sent to the site,
raw items received from the site)

=item B<--post> I<key>=I<value>

Add I<key> with I<value> to the HTTP POST request sent to the EFA server.
This can be used to use setting B<efa> does not yet cover, like
C<--post lineRestriction=400> to also show IC and ICE trains.
Note that B<--post> will be overridden by the standard efa options, such as
B<--time>.

=back