summaryrefslogtreecommitdiff
path: root/bin/efa
blob: ad801dfdfd52b3a037c3d8a954aace8ca038b728 (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
#!/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 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;
my @to;
my $time;
my $date;
my $debug = 0;

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

GetOptions(
	'from=s{2}' => \@from,
	'to=s{2}'   => \@to,
	'time=s'    => \$time,
	'date=s'    => \$date,
	'debug'     => \$debug,
);

$post{place_origin} = $from[0];
$post{name_origin} = $from[1];
$post{place_destination} = $to[0];
$post{name_destination} = $to[1];

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

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

$content = $www->content;

foreach(split(/<span class="labelTextBold"> \d+\. Fahrt<\/span>/, $content)) {
	unless ($offer) {
		$offer++;
		next;
	}
	foreach(split(/\n/)) {
		if (/<span class="labelText">(?<content>[^<]+)<\/span>/) {
			# Ignore "ca. 3 Minuten" and similar
			# something like /^ca\. \d+ Minute/ does not work here, for some reason
			# (probably related to encoding fuckup)
			if ($+{content} !~ /^ca.*Minute/) {
				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;
		# skip "Fußweg: x Minuten" messages (they're smaller than $groupsize)
		if ($raw->[$offer]->[$offset+2] =~ /ca.*Minute/) {
			splice(@$_, $offset, 5);
		}
		$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";
}