forked from pasky/papalala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDOS.pm
155 lines (125 loc) · 4.75 KB
/
IDOS.pm
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 IDOS::RouteQuery;
# This class handles IDOS route queries.
# API: http://www.chaps.cz/idos-moznost-vyuziti-odkazu.asp
use Moose;
use Moose::Util::TypeConstraints;
subtype 'Region' => as 'Str' => where { /^[a-z]+$/; };
subtype 'Time' => as 'Str' => where { /^\d\d?:\d\d?$/; };
has 'region' => (is => 'rw', isa => 'Region', required => 1);
has 'origin' => (is => 'rw', isa => 'Str', required => 1);
has 'dest' => (is => 'rw', isa => 'Str', required => 1);
has 'thru' => (is => 'rw', isa => 'Str');
has 'later' => (is => 'rw', isa => 'Num', default => 0); # number of minutes after now
sub execute {
my $self = shift;
my @routes;
my %qpar = (f => $self->origin(), t => $self->dest(), v => $self->thru());
if ($self->later() > 0) {
my @t0 = localtime(time);
my @t1 = localtime(time + $self->later() * 60);
$qpar{date} = sprintf('%d.%d.%04d', $t1[3], $t1[4] + 1, $t1[5] + 1900);
$qpar{time} = sprintf('%d:%02d', $t1[2], $t1[1]);
}
my @qpar = map {
my $val = $qpar{$_};
if (defined $val) {
$val =~ s#/\?&=##g; ("$_=$val")
}
} keys %qpar;
my $uri = "http://www.idos.cz/".$self->region()."/?".join('&', 'af=true', @qpar, 'submit=1');
use LWP::UserAgent;
use HTTP::Request;
my $ua = LWP::UserAgent->new(env_proxy => 1, keep_alive => 1, timeout => 10);
my $request = HTTP::Request->new('GET', $uri);
my $response = $ua->request($request);
$response->is_success or die "Cannot get $uri";
my $content = $response->decoded_content(charset=>'utf8');
# Eschew evilness.
$content =~ s#<table class="unitednote.*?</table>##sg;
my @data = split(/\n/, $content);
# print "ook? @data\n";
until (not $#data or $data[0] =~ /<!-- zobrazeni vysledku start -->/) {
shift @data;
}
my ($starttime, $startdate, $tottime, $totdist, $totcost, $detail, @places);
until (not $#data or $data[0] =~ /<!-- zobrazeni vysledku end -->/) {
if ($data[0] =~ /.*<th class="time.*?>(.*?)<.*/) {
$starttime = $1;
} elsif ($data[0] =~ /<p>Celkov. .as </) {
($tottime, $totdist, $totcost) = ($data[0] =~ m#<p>Celkov. .as <strong>(.*?)</strong>(?:, vzd.lenost <strong>(.*?)</strong>)?(?:, cena <strong>(.*?)</strong>)?#);
$data[0] =~ m#<a href="(/detail/.*?)"#;
$detail = "http://jizdnirady.idnes.cz" . $1;
} elsif ($data[0] =~ /<td class="(check|empty)/) {
my $cl = $1;
$data[0] =~ s#</td>##g;
my ($nfield) = ($data[0] =~ m#<td class="note">#);
my (@data) = split(/\s*<td.*?>\s*/, $data[0]);
shift @data; # drop anything before first <td>
for (@data) {
s/<.*?[^ >]>//g; s/ / /g; s/\s+/ /g; s/^\s*//; s/\s*$//; s/^>$//;
}
if ($cl eq 'check') {
$startdate = $data[1];
}
my $note; $note = splice(@data, 5, 1) if $nfield;
push @places, {
place => $data[2],
arrival => $data[3],
departure => $data[4],
note => $note,
line => $data[6]
};
} elsif ($data[0] =~ /^<\/table>/) {
my @conns = map {
my ($d, $a) = ($places[$_]->{departure}, $places[$_+1]->{arrival});
use Data::Dumper;
#print "this " . Dumper($places[$_]) . "\n";
#print "then " . Dumper($places[$_+1]) . "\n";
if ($places[$_]->{line} =~ /esun/) {
($d, $a) = ($places[$_]->{arrival}, $places[$_+1]->{departure});
}
$places[$_]->{line} ||= 'wtf';
my %r = (
'start' => $d, 'origin' => $places[$_]->{place},
'stop' => $a, 'dest' => $places[$_+1]->{place},
'by' => $places[$_]->{line}
);
defined $places[$_]->{note} and $r{note} = $places[$_]->{note};
IDOS::Connection->new(%r);
} 0..($#places - 1);
my %r = (
'time' => $starttime, 'date' => $startdate,
'traveltime' => $tottime, 'detail' => $detail,
);
defined $totcost and $r{'traveldist'} = $totdist;
defined $totcost and $r{'cost'} = $totcost;
push @routes, IDOS::Route->new(%r, 'connections' => \@conns);
@places = ();
}
} continue {
shift @data;
}
return @routes;
}
1;
package IDOS::Route;
# This class represents single route through the map, composed of several connections.
use Moose;
has 'time' => (is => 'rw', isa => 'Str', required => 1);
has 'date' => (is => 'rw', isa => 'Str', required => 1);
has 'traveltime' => (is => 'rw', isa => 'Str');
has 'traveldist' => (is => 'rw', isa => 'Str');
has 'cost' => (is => 'rw', isa => 'Str');
has 'connections' => (is => 'rw', isa => 'ArrayRef[IDOS::Connection]', required => 1);
has 'detail' => (is => 'rw', isa => 'Str');
1;
package IDOS::Connection;
# This class represents single route through the map, composed of several connections.
use Moose;
has 'start' => (is => 'rw', isa => 'Time', required => 1);
has 'stop' => (is => 'rw', isa => 'Time', required => 1);
has 'origin' => (is => 'rw', isa => 'Str', required => 1);
has 'dest' => (is => 'rw', isa => 'Str', required => 1);
has 'by' => (is => 'rw', isa => 'Str', required => 1);
has 'note' => (is => 'rw', isa => 'Str');
1;