summaryrefslogtreecommitdiff
path: root/bin/efa
blob: 7d0b7d90127566820b343add56dc80f290c070e9 (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
#!/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 $offer = 0;
my $i = 0;
my $raw;
my $cons;
my $groupsize = 8;
my $offset;
my (@from, @to, @via);
my ($time, $time_depart, $time_arrive);
my $date;
my $restrict;
my $debug = 0;

$post{type_origin} = 'stop';
$post{type_destination} = 'stop';

GetOptions(
	'arrive=s'  => \$time_arrive,
	'date=s'    => \$date,
	'debug'     => \$debug,
	'depart=s'  => \$time_depart,
	'from=s{2}' => \@from,
	'post=s'    => \%post,
	'restrict=s' => \$restrict,
	'time=s'    => \$time,
	'to=s{2}'   => \@to,
	'via=s{2}'  => \@via,
);

unless (@to == 2 and @from == 2) {
	print STDERR "Usage: efa --from <city> <stop> --to <city> <stop> [other options]\n";
	exit(1);
}

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

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

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

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

$content = $www->content;

# ambiguous input
if ($content =~ /<span class="errorTextBold">Bitte auswählen<\/span>/s) {
	print STDERR "Ambiguous input for ";
	if ($content =~ /<select name="(?<what>(place|type|name)_(origin|destination))"/) {
		print STDERR "$+{what}\n";
	} else {
		print STDERR "unknown\n";
	}
	while ($content =~ /<option value="\d+:\d+"( selected)?>(?<choice>[^<]+)<\/option>/gs) {
		print STDERR "\t$+{choice}\n";
	}
	exit(1);
}

foreach (split(/<span class="labelTextBold"> \d+\. Fahrt<\/span>/, $content)) {
	unless ($offer) {
		$offer++;
		next;
	}
	foreach(split(/\n/)) {
		if (/<span class="labelText">(?<content>[^<]+)<\/span>/) {
			push(@{$raw->[$offer-1]}, $+{content});
		}
	}
	$offer++;
}

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

for ($offer = 0; exists($raw->[$offer]); $offer++) {
	for ($i = 0; @{$raw->[$offer]} >= (($i+1) * $groupsize); $i++) {
		$offset = $i * $groupsize;
		# If the first field is not a time we've got some additional information.
		# Sadly, this script does not parse it yet, so it's ignored
		until ($raw->[$offer]->[$offset] =~ /^\d+:\d+$/) {
			last unless exists($raw->[$offer]->[++$offset]);
		}
		$cons->[$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],
		};
	}
}

foreach (@$cons) {
	foreach (@$_) {
		printf(
			"%-5s %-2s %-30s %-20s %s\n%-5s %-2s %-30s\n\n",
			$_->{deptime}, $_->{dep}, $_->{depstop}, $_->{deptrain},
			$_->{depdest}, $_->{arrtime}, $_->{arr}, $_->{arrstop}
		);
	}
	print "------\n\n";
}

__END__

=head1 NAME

efa - unofficial efa.vrr.de command line client

=head1 SYNOPSIS

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

=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> (mandatory)

Departure place

=item B<--to> I<city> I<stop> (mandatory)

Arrival place

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

Travel via this place

=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<--restrict> I<type>

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

=over

=item * local

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

=item * ic

All trains exctep ICE

=item * ice

All trains

=back

=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