-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTbc.pm
164 lines (121 loc) · 3.66 KB
/
Tbc.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
156
157
158
159
160
161
162
163
164
package Tbc;
use strict;
use utf8;
use Mojolicious::Lite;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
sub lines {
my $class = shift;
my $tx = $ua->get("http://www.infotbc.com/");
my $vals = $tx->res->dom->find('select#edit-navitia-line option:not([value=0])');
my @lignes;
for my $ligne ($vals->each) {
my $ligne_hash;
my $ligne_name = $ligne->text;
my $ligne_id = $ligne->attrs('value');
my @number = split('_', $ligne_id);
$ligne_hash = {
'id' => $ligne_id,
'name' => $ligne_name,
'number' => $number[1],
};
push(@lignes, $ligne_hash);
}
return [@lignes];
}
sub directions {
my $class = shift; # class method, so use $class
my $line = shift;
bless \$line, $class;
$line = uc($line);
my $tx = $ua->get("http://www.infotbc.com/ahah/stoppoint?navitia_line=$line");
my $vals = $tx->res->dom->find('select option:not([value="0"])');
my $hash;
for my $dir ($vals->each) {
my $route = $dir->attrs('value');
my $name = $dir->text;
$$hash{$route} = $name;
};
return $hash;
}
sub stops {
my $class = shift;
my $line = shift;
bless \$line, $class;
$line = uc($line);
my @dirs = ('backward', 'forward');
my $hash;
for my $dir (@dirs) {
my $tx = $ua->get("http://www.infotbc.com/ahah/stoppoint?navitia_line=$line&navitia_direction=$dir");
my $vals = $tx->res->dom->find('select option:not([value="0"])');
# Loop over stops / directions
my @list;
for my $i ($vals->each) {
my $route = $i->attrs('value');
my $name = $i->text;
my $v = {'id'=>$route, 'name'=> $name};
push(@list, $v);
}
$$hash{$dir} = [@list];
};
return $hash;
}
sub now {
my $class = shift; # class method, so use $class
my $line_number = shift;
my $dir = shift;
my $stop_id = shift;
bless \$line_number, $class;
bless \$dir, $class;
bless \$stop_id, $class;
$line_number = uc($line_number);
$stop_id = uc($stop_id);
my $url = "http://www.infotbc.com/nextdeparture/$line_number/stoppoint/$stop_id/$dir#departure";
my $tx = $ua->get($url);
my @list;
for my $tr ($tx->res->dom->find('#navitia-departure-result li')->each) {
my $min = $tr->text;
$min =~ s/^\s+//;
$min =~ s/\s+$//;
if ($min ne "") {
my $date = "$min";
push(@list, $date);
}
}
return [@list];
}
sub dates {
my $class = shift; # class method, so use $class
my $line_number = shift;
my $dir = shift;
my $stop_id = shift;
my $date = shift;
bless \$line_number, $class;
bless \$dir, $class;
bless \$stop_id, $class;
bless \$date, $class;
$line_number = uc($line_number);
$stop_id = uc($stop_id);
my @cal = split('-', $date);
my $url = "http://www.infotbc.com/timetable/$line_number/stoppoint/$stop_id/$dir/$cal[0]/$cal[1]/$cal[2]";
my $tx = $ua->get($url);
my @list;
for my $tr ($tx->res->dom->find('#navitia-timetable-detail tr')->each) {
my $hour = $tr->find('th')->first->text;
for my $td ($tr->find('td abbr:not(.symbol-a)')->each) {
my $min = $td->text;
# my $via_el = $td->find('sup abbr');
$min =~ s/^\s+//;
$min =~ s/\s+$//;
# if ($via_el->first) {
# $via = "- ${$via_el->first->text}";
# }
if ($min ne "") {
my $date = "$hour$min";
push(@list, $date);
}
}
}
return [@list];
}
1;