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
|
package WWW::Efa;
use strict;
use warnings;
use 5.010;
use Carp qw/croak confess/;
use XML::LibXML;
use WWW::Mechanize;
my $VERSION = '1.3+git';
sub post_time {
my ($post, $conf) = @_;
my $time;
if ($conf->{'depart'} || $conf->{'time'}) {
$post->{'itdTripDateTimeDepArr'} = 'dep';
$time = $conf->{'depart'} || $conf->{'time'};
}
else {
$post->{'itdTripDateTimeDepArr'} = 'arr';
$time = $conf->{'arrive'};
}
if ($time !~ / ^ [0-2]? \d : [0-5]? \d $ /x) {
confess('conf: time invalid. Use HH:MM');
}
@{$post}{'itdTimeHour', 'itdTimeMinute'} = split(/:/, $time);
}
sub post_date {
my ($post, $date) = @_;
if ($date !~ /^ [0-3]? \d \. [01]? \d \. (?: \d{4} )? $/x) {
confess('conf: date invalid DD.MM.[YYYY]');
}
@{$post}{'itdDateDay', 'itdDateMonth', 'itdDateYear'} = split(/\./, $date);
$post->{'itdDateYear'} //= (localtime(time))[5] + 1900;
}
sub post_exclude {
my ($post, @exclude) = @_;
my @mapping = qw{
zug s-bahn u-bahn stadtbahn tram stadtbus regionalbus
schnellbus seilbahn schiff ast sonstige
};
foreach my $exclude_type (@exclude) {
my $ok = 0;
for my $map_id (0 .. $#mapping) {
if ($exclude_type eq $mapping[$map_id]) {
$post->{"inclMOT_${map_id}"} = undef;
$ok = 1;
}
}
if (not $ok) {
confess("conf: exclude: Invalid element $exclude_type");
}
}
}
sub post_prefer {
my ($post, $prefer) = (@_);
given($prefer) {
when('speed') { $post->{'routeType'} = 'LEASTTIME' }
when('nowait') { $post->{'routeType'} = 'LEASTINTERCHANGE' }
when('nowalk') { $post->{'routeType'} = 'LEASTWALKING' }
default {
confess("conf: prefer: Invalid argument $prefer");
}
}
}
sub post_include {
my ($post, $include) = @_;
given ($include) {
when ('local') { $post->{'lineRestriction'} = 403 }
when ('ic') { $post->{'lineRestriction'} = 401 }
when ('ice') { $post->{'lineRestriction'} = 400 }
default {
confess('conf: invalid include');
}
}
}
sub post_walk_speed {
my ($post, $walk_speed) = @_;
if ($walk_speed ~~ ['normal', 'fast', 'slow']) {
$post->{'changeSpeed'} = $walk_speed;
}
else {
confess('conf: walk_speed invalid');
}
}
sub post_place {
my ($post, $which, $place, $stop, $type) = @_;
if (not ($place and $stop)) {
confess("conf: ${which}: Need at least two elements");
}
$type //= 'stop';
@{$post}{"place_${which}", "name_${which}"} = ($place, $stop);
if ($type ~~ [qw[address poi stop]]) {
$post->{"type_${which}"} = $type;
}
}
sub create_post {
my ($conf) = @_;
my $post = {};
post_place($post, 'origin', @{$conf->{'from'}});
post_place($post, 'destination', @{$conf->{'to'}});
if ($conf->{'via'}) {
post_place($post, 'via', @{$conf->{'via'}});
}
if ($conf->{'arrive'} || $conf->{'depart'} || $conf->{'time'}) {
post_time($post, $conf);
}
if ($conf->{'date'}) {
post_date($post, $conf->{'date'});
}
if ($conf->{'exclude'}) {
post_exclude($post, @{$conf->{'exclude'}});
}
if ($conf->{'max_interchanges'}) {
$post->{'maxChanges'} = $conf->{'max_interchanges'};
}
if ($conf->{'prefer'}) {
post_prefer($post, $conf->{'prefer'});
}
if ($conf->{'proximity'}) {
$post->{'useProxFootSearch'} = 1;
}
if ($conf->{'include'}) {
post_include($post, $conf->{'include'});
}
if ($conf->{'walk_speed'}) {
post_walk_speed($post, $conf->{'walk_speed'});
}
if ($conf->{'bike'}) {
$post->{'bikeTakeAlong'} = 1;
}
return $post;
}
sub parse_initial {
my ($tree) = @_;
my $con_part = 0;
my $con_no;
my $cons;
my $xp_td = XML::LibXML::XPathExpression->new('//table//table/tr/td');
my $xp_img = XML::LibXML::XPathExpression->new('./img');
foreach my $td (@{$tree->findnodes($xp_td)}) {
my $colspan = $td->getAttribute('colspan') // 0;
my $class = $td->getAttribute('class') // q{};
if ( $colspan != 8 and $class !~ /^bgColor2?$/ ) {
next;
}
if ($colspan == 8) {
if ($td->textContent() =~ / (?<no> \d+ ) \. .+ Fahrt /x) {
$con_no = $+{'no'} - 1;
$con_part = 0;
next;
}
}
if ($class =~ /^bgColor2?$/) {
if ($class eq 'bgColor' and ($con_part % 2) == 1) {
$con_part++;
}
elsif ($class eq 'bgColor2' and ($con_part % 2) == 0) {
$con_part++;
}
}
if (
defined $con_no and not $td->exists($xp_img)
and $td->textContent() !~ /^\s*$/
)
{
push(@{$cons->[$con_no]->[$con_part]}, $td->textContent());
}
}
if (defined $con_no) {
return $cons;
}
else {
confess('efa.vrr.de returned no connections, check your input data');
}
}
sub parse_pretty {
my ($con_parts) = @_;
my $elements;
my @next_extra;
for my $con (@{$con_parts}) {
my $hash;
# Note: Changes @{$con} elements
foreach my $str (@{$con}) {
$str =~ s/[\s\n\t]+/ /gs;
$str =~ s/^ //;
$str =~ s/ $//;
}
if (@{$con} < 5) {
@next_extra = @{$con};
next;
}
# @extra may contain undef values
foreach my $extra (@next_extra) {
if ($extra) {
push(@{$hash->{'extra'}}, $extra);
}
}
@next_extra = undef;
if ($con->[0] !~ / \d{2} : \d{2} /ox) {
splice(@{$con}, 0, 0, q{});
splice(@{$con}, 4, 0, q{});
$con->[7] = q{};
}
elsif ($con->[4] =~ / Plan: \s ab /ox) {
push(@{$hash->{'extra'}}, splice(@{$con}, 4, 1));
}
foreach my $extra (splice(@{$con}, 8, -1)) {
push (@{$hash->{'extra'}}, $extra);
}
$hash->{'dep_time'} = $con->[0];
# always "ab" $con->[1];
$hash->{'dep_stop'} = $con->[2];
$hash->{'train_line'} = $con->[3];
$hash->{'arr_time'} = $con->[4];
# always "an" $con->[5];
$hash->{'arr_stop'} = $con->[6];
$hash->{'train_dest'} = $con->[7];
push(@{$elements}, $hash);
}
return($elements);
}
sub new {
my ($obj, %conf) = @_;
my $ref = {};
$ref->{'config'} = \%conf;
$ref->{'post'} = create_post(\%conf);
return bless($ref, $obj);
}
sub submit {
my ($self, %conf) = @_;
my $firsturl
= 'http://efa.vrr.de/vrr/XSLT_TRIP_REQUEST2?language=de&itdLPxx_transpCompany=vrr';
$self->{'mech'} = WWW::Mechanize->new(
autocheck => 1,
timeout => $conf{'timeout'} // 10,
);
$self->{'mech'}->get($firsturl);
$self->{'mech'}->submit_form(
form_name => 'jp',
fields => $self->{'post'},
);
# XXX (workaround)
# The content actually is iso-8859-1. But HTML::Message doesn't actually
# decode character strings when they have that encoding. However, it
# doesn't check for latin-1, which is an alias for iso-8859-1.
$self->{'html_reply'} = $self->{'mech'}->response()->decoded_content(
charset => 'latin-1'
);
}
sub parse {
my ($self) = @_;
my $tree = XML::LibXML->load_html(
string => $self->{'html_reply'},
);
my $raw_cons = parse_initial($tree);
for my $raw_con (@{$raw_cons}) {
push(@{$self->{'connections'}}, parse_pretty($raw_con));
}
$self->{'tree'} = $tree;
}
sub check_ambiguous {
my ($self) = @_;
my $ambiguous = 0;
my $tree = $self->{'tree'};
my $xp_select = XML::LibXML::XPathExpression->new('//select');
my $xp_option = XML::LibXML::XPathExpression->new('./option');
foreach my $select (@{$tree->findnodes($xp_select)}) {
$ambiguous = 1;
printf {*STDERR} (
"Ambiguous input for %s\n",
$select->getAttribute('name'),
);
foreach my $val ($select->findnodes($xp_option)) {
print {*STDERR} "\t";
say {*STDERR} $val->textContent();
}
}
if ($ambiguous) {
exit 1;
}
}
sub check_no_connections {
my ($self) = @_;
my $tree = $self->{'tree'};
my $xp_err_img = XML::LibXML::XPathExpression->new(
'//td/img[@src="images/ausrufezeichen.jpg"]');
my $err_node = $tree->findnodes($xp_err_img)->[0];
if ($err_node) {
say {*STDERR} 'Looks like efa.vrr.de showed an error.';
say {*STDERR} 'I will now try to dump the error message:';
say {*STDERR} $err_node->parentNode()->parentNode()->textContent();
exit 2;
}
}
sub connections {
my ($self) = @_;
return(@{$self->{'connections'}});
}
1;
|