summaryrefslogtreecommitdiff
path: root/bin/efa
blob: 5e2f24e1633e3fd7b543c7c5c95efb3a96bb092b (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
#!/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 $connection;
my $groupsize = 8;
my $offset;
my @from;
my @to;

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

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

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

$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(@{$connection->[$offer-1]}, $+{content});
			}
		}
	}
	$offer++;
}

foreach (@$connection) {
	for($i = 0; @{$_} >= (($i+1) * $groupsize); $i++) {
		$offset = $i * $groupsize;
		# skip "Fußweg: x Minuten" messages (they're smaller than $groupsize)
		if ($_->[$offset+2] =~ /ca.*Minute/) {
			splice(@$_, $offset, 5);
		}
		printf("%-5s %-2s %-30s %-20s %s\n", $_->[$offset+0], $_->[$offset+1], $_->[$offset+2], $_->[$offset+3], $_->[$offset+7]);
		printf("%-5s %-2s %-30s\n\n", $_->[$offset+4], $_->[$offset+5], $_->[$offset+6]);
	}
	print "-----\n\n";
}

#print "---\n";
#foreach(@$connection) {
#	print "\n";
#	foreach(@$_) {
#		print "$_\n";
#	}
#}