summaryrefslogtreecommitdiff
path: root/lib/Travel/Status/DE/VRR.pm
blob: 8bb1f6d3ae6353ef793adc54e0cf3dd748a584e5 (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
package Travel::Status::DE::VRR;

use strict;
use warnings;
use 5.010;

our $VERSION = '0.00';

use Travel::Status::DE::VRR::Result;
use WWW::Mechanize;
use XML::LibXML;

sub new {
	my ( $class, %opt ) = @_;

	my $mech = WWW::Mechanize->new();
	my @now = localtime( time() );

	my $self = {
		post => {
			command => q{},
			deleteAssignedStops_dm => '1',
			help => 'Hilfe',
			itdDateDay => $now[3],
			itdDateMonth => $now[4] + 1,
			itdDateYear => $now[5] + 1900,
			itdLPxx_id_dm => ':dm',
			itdLPxx_mapState_dm => q{},
			itdLPxx_mdvMap2_dm => q{},
			itdLPxx_mdvMap_dm => '3406199:401077:NAV3',
			itdLPxx_transpCompany => 'vrr',
			itdLPxx_view => q{},
			itdTimeHour => $now[2],
			itdTimeMinute => $now[1],
			language => 'de',
			nameInfo_dm => 'invalid',
			nameState_dm => 'empty',
			name_dm => $opt{name},
			placeInfo_dm => 'invalid',
			placeState_dm => 'empty',
			place_dm => $opt{place},
			ptOptionsActive => '1',
			requestID => '0',
			reset => 'neue Anfrage',
			sessionID => '0',
			submitButton => 'anfordern',
			typeInfo_dm => 'invalid',
			type_dm => 'stop',
			useProxFootSearch => '0',
			useRealtime => '1',
		},
	};

	$mech->post( 'http://efa.vrr.de/vrr/XSLT_DM_REQUEST', $self->{post} );

	if ( $mech->response->is_error ) {
		die( $mech->response->status_line );
	}

	my $form = $mech->form_number(1);

	for my $input ( $form->find_input( 'dmLineSelection', 'option' ) ) {
		$input->check();
	}

	$mech->click('submitButton');

	if ( $mech->response->is_error ) {
		die( $mech->response->status_line );
	}

	$self->{html} = $mech->response->decoded_content;

	$self->{tree} = XML::LibXML->load_html(
		string => $self->{html},
		recover => 2,
		suppress_errors => 1,
		suppress_warnings => 1,
	);

	return bless( $self, $class );
}

sub results {
	my ($self) = @_;
	my @results;

	my $xp_element = XML::LibXML::XPathExpression->new(
		'//td[@colspan="3"]/table/tr[starts-with(@class,"bgColor")]');

	my @parts = (
		['time', './td[2]'],
		['platform', './td[3]'],
		['line', './td[5]'],
		['dest', './td[7]'],
	);

	@parts = map { [ $_->[0], XML::LibXML::XPathExpression->new($_->[1]) ] }
	@parts;

	for my $tr ($self->{tree}->findnodes($xp_element) ) {
		my ($time, $platform, $line, $dest) = map { ($tr->findnodes($_->[1]))[0]->textContent } @parts;
		$platform =~ s{ ^ \S+ \s }{}x;
		push(@results, Travel::Status::DE::VRR::Result->new(
			time => $time,
			platform => $platform,
			line => $line,
			destination => $dest,
		));
	}

	return @results;
}

1;

__END__

=head1 NAME

Travel::Status::DE::VRR - inofficial VRR departure monitor

=head1 SYNOPSIS

=head1 VERSION

version

=head1 DESCRIPTION

=head1 METHODS

=over

=back

=head1 DIAGNOSTICS

=head1 DEPENDENCIES

=over

=back

=head1 BUGS AND LIMITATIONS

=head1 SEE ALSO

=head1 AUTHOR

Copyright (C) 2011 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>

=head1 LICENSE

This module is licensed under the same terms as Perl itself.