forked from Over23/Irssi_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jizdni_rady.pl
221 lines (162 loc) · 6.85 KB
/
jizdni_rady.pl
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/perl
use strict;
use warnings;
use Irssi;
use Irssi::Irc;
use pQuery;
use URI::Escape;
use vars qw($VERSION %IRSSI);
$VERSION = '0.01';
%IRSSI = (
authors => "Petr Vavrin",
contact => "pb.pb\@centrum.cz",
name => "dpo",
description => "description",
);
sub on_public {
my ( $server, $message, $nick, $hostmask, $channel ) = @_;
my $cp = Irssi::settings_get_str ( 'bot_cmd_prefix' );
my $isprivate = !defined $channel;
my $dst = $isprivate ? $nick : $channel;
my $defaultJr = "ostrava";
my $inputJr = "";
my $from = "";
my $to = "";
return unless $message =~ /^${cp}spoj/;
if ( $message =~ /^${cp}spoj\s*(.*)\s*--\s*(.*)\s*--\s*(.*)\s*$/ ){
( $inputJr, $from, $to ) = ( $1, $2, $3 );
} elsif ( $message =~ /^${cp}spoj\s*(.*)\s*--\s*(.*)\s*$/ ){
$inputJr = $defaultJr;
( $from, $to ) = ( $1, $2 );
}
for ( $inputJr, $from, $to ){
s/^\s+|\s+$//g;
}
if ( $from =~ // ){
$server->send_message ( $dst, $nick . ": empty JR", 0 );
&showHelp( $server, $dst, $nick );
return;
}
if ( $from =~ /^\s*$/ ){
$server->send_message ( $dst, $nick . ": empty FROM", 0 );
&showHelp ( $server, $dst, $nick );
return;
}
if ( $to =~ /^\s*$/ ){
$server->send_message ( $dst, $nick . ": empty TO", 0 );
&showHelp ( $server, $dst, $nick );
return;
}
my @jrList = (
"pid praha", "idsjmk", "odis", "idol", "zvon", "vlaky vlak", "autobusy autobus", "vlakyautobusy",
"vlakyautobusymhd", "vlakyautobusymhdvse vse", "letadla",
"adamov", "as", "benesov", "beroun", "bilina", "blansko", "brandys", "brno",
"bruntal", "breclav", "bystrice", "caslav", "ceskalipa", "ceskebudejovice",
"ceskytesin", "dacice", "decin", "domazlice", "duchcov", "dvurkralove",
"frydekmistek", "havirov", "havlickuvbrod", "hodonin", "horice",
"hradeckralove", "hranice", "cheb", "chomutov", "chrudim", "jablonec",
"jachymov", "jicin", "jihlava", "jindrichuvhradec", "kadan", "karlovyvary",
"karvina", "kladno", "klasterecnadohri", "klatovy", "kolin",
"kostelecnadorlici", "kralupy", "krnov", "kromeriz", "kutnahora", "kyjov",
"liberec", "litomerice", "litomysl", "louny", "lovosice", "marianskelazne",
"melnik", "milevsko", "mladaboleslav", "mnisekpodbrdy", "most", "nachod",
"neratovice", "novemestonamorave", "novyjicin", "nymburk", "olomouc", "opava",
"orlova", "ostrava ova dpo", "ostrov", "pardubice", "pelhrimov", "pisek",
"plzen", "policka", "praha", "prostejov", "prelouc", "prerov", "prestice",
"pribram", "rokycany", "roudnice", "rychnov", "slany", "sokolov", "strakonice",
"stribro", "studenka", "spindleruvmlyn", "steti", "sumperk", "tabor",
"tachov", "teplice", "trutnov", "trebic", "trinec", "turnov",
"tynistenadorlici", "uherskehradiste", "ustinadlabem", "valasskemezirici",
"varnsdorf", "velkemezirici", "vimperk", "vlasim", "vrchlabi", "vsetin",
"vyskov", "zabreh", "zlin", "znojmo", "zamberk", "zatec", "zdarnadsazavou" );
my @jrSelected = ();
JRLOOP: foreach my $jrNames ( @jrList ){
my @names = split / /, $jrNames;
foreach my $name ( @names ){
if ( $name =~ /^${inputJr}$/i ){
push ( @jrSelected, $names[0] );
last JRLOOP;
} elsif ( $name =~ /${inputJr}/i ){
push ( @jrSelected, $names[0] );
}
}
}
my $countSelected = scalar @jrSelected;
my $jr = "";
if ( $countSelected == 0 ){
$server->send_message ( $dst, $nick . ": Unknown timetable name - '" . $inputJr . "'", 0 );
} elsif ( $countSelected == 1 ){
$jr = $jrSelected[0];
} else {
$server->send_message ( $dst, $nick . ": Please specify timetable name - '" . ( join ", ", @jrSelected ) . "'", 0 );
return;
}
my @inputParams = ();
push ( @inputParams, "f=" . &sanitize ( $from ) );
push ( @inputParams, "t=" . &sanitize ( $to ) );
my $searchParams = join "&", @inputParams;
my $jrLink = "http://jizdnirady.idnes.cz/" . $jr . "/spojeni/?af=true&submit=1&" . $searchParams;
my $jrPage = pQuery ( $jrLink );
my $warningFrom = $jrPage->find( ".from-box strong.red" )->text() || "";
my $warningTo = $jrPage->find( ".to-box strong.red" )->text() || "";
for ( $warningFrom, $warningTo ){
s/^\s+|\s+$//g;
}
if ( $warningFrom ne "" ){
$server->send_message ( $dst, $nick . ": Odkud - " . $from . " - !!! " . $warningFrom, 0 );
} elsif ( $warningTo ne "" ){
$server->send_message ( $dst, $nick . ": Kam - " . $to . " - !!! " . $warningTo, 0 );
} else {
$jrPage->find ( "table.results" )
->each ( sub {
my $result = pQuery( $_ );
my $date = $result->find( ".date" )->text();
my $responseRow = "";
$result->find( "tr" )->each (
sub {
my $row = pQuery( $_ );
my $station = $row->find( "td:eq(2)" )->text() || "";
my $in = $row->find( "td:eq(3)" )->text() || "";
my $out = $row->find( "td:eq(4)" )->text() || "";
my $desc = $row->find( "td:eq(5)" )->text() || "";
my $info = $row->find( "td:last" )->text() || "";
my $vehicle = $row->find( "td:last img" )->attr('title') || "";
for ( $station, $in, $out, $desc, $info ){
s/^\s*>\s*$//g;
s/^\s*|\s*$//g;
}
if ( $vehicle =~ /esun/ ){
$vehicle = "";
}
my $vehicleInfo = $vehicle . " " . $info;
$vehicleInfo =~ s/^\s+|\s+$//g;
if ( $vehicleInfo ne "" ){
$vehicleInfo = " [" . $vehicleInfo . "]";
}
if ( $in eq "" && $out ne "" ){
$responseRow .= $out . " -> " . $station . $vehicleInfo . " ";
} elsif ( $in ne "" && $out eq "" ){
$responseRow .= $station . " " . $in . $vehicleInfo . " | ";
} elsif ( $in ne "" && $out ne "" ){
$responseRow .= $station . " " . $in . " | " . $out . " -> " . $station . $vehicleInfo . " ";
}
}
);
my $answer = $date . " - " . $responseRow;
$answer =~ s/\s+\|\s+$//g;
$server->send_message ( $dst, $nick . ": " . $answer, 0 );
} );
}
}
sub showHelp {
my ( $server, $dst, $nick ) = @_;
$server->send_message ( $dst, $nick . ": INPUT FORMAT - for selected TIMETABLE - `spoj TIMETABLE -- FROM -- TO", 0 );
$server->send_message ( $dst, $nick . ": INPUT FORMAT - for OSTRAVA - `spoj FROM -- TO", 0 );
}
sub sanitize {
my ( $text ) = @_;
return &uri_escape ( $text );
}
Irssi::signal_add('message public', 'on_public');
Irssi::signal_add('message private', 'on_public');
Irssi::settings_add_str('bot', 'bot_cmd_prefix', '!');