-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathontv2.pl
360 lines (330 loc) · 8.71 KB
/
ontv2.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use strict;
use utf8;
use vars qw($VERSION %IRSSI);
use Irssi;
use XML::LibXML::Simple qw(XMLin);
use YAML::XS qw/Dump DumpFile LoadFile/;
use DateTime;
use DateTime::Format::Strptime;
use File::Glob qw/:bsd_glob/;
use Text::Wrap;
$VERSION = '0.02';
%IRSSI = (
authors => 'bw1',
name => 'ontv2',
description => "turns irssi into a tv program guide",
license => 'GPLv2',
url => 'https://scripts.irssi.org/',
changed => '2021-01-10',
modules => 'XML::LibXML::Simple YAML::XS DateTime DateTime::Format::Strptime File::Glob Text::Wrap',
commands=> 'ontv2',
selfcheckcmd=> 'ontv2 check',
);
my $help = << "END";
%9Name%9
$IRSSI{name}
%9Version%9
$VERSION
%9description%9
$IRSSI{description}
/ontv2 (current)
List the current tv program
/ontv2 search <query>
Query the program guide for a show
/ontv2 next
Show what'S next on TV
/ontv2 tonight
List tonight's program
/ontv2 watching <station>
Display what's on <station>
/ontv2 check
helper for self check the script
/ontv2 read
read the prgram from file
/ontv2 #xx
show details
\$ tv_grab_ch_search --output ~/.xmltv/current.xml --days 2
based on an idea by Stefan 'tommie' Tomanek
%9See also%9
http://wiki.xmltv.org/index.php/XMLTVProject
END
my $xmltv_file;
my $data;
my $test_str;
my ($prg_count, @prg_list);
my $tf=DateTime::Format::Strptime->new(pattern=>"%Y%m%d%H%M%S %z");
my $localTZ =DateTime::TimeZone->new( name => 'local' );
sub print_head {
my ($title)= @_;
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'head_theme', $title);
}
sub print_footer {
my ($footer)= @_;
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'footer_theme', $footer);
}
sub clear_prg {
@prg_list=();
$prg_count=0;
}
sub to_prg_short {
my ($num)= @_;
my @cl= (0 .. 9, 'A' .. 'Z');
my $d= scalar @cl;
my $s;
while ($num > 0 ) {
my $di= $num % $d;
$num = int ( $num / $d);
$s .= $cl[$di];
}
$s = "#$s";
return $s;
}
sub to_prg_num {
my ($short)= @_;
substr $short, 0, 1, '';
$short =uc $short;
my $num=0;
my @cl= (0 .. 9, 'A' .. 'Z');
my $d= scalar @cl;
my %cl;
my $c=0;
foreach ( @cl ) {
$cl{$_}=$c;
$c++;
}
while ( length($short) > 0 ) {
my $s = substr $short, -1, 1, '';
$num *= $d;
$num += $cl{$s};
}
return $num;
}
sub print_prg {
my ($witem, $pn)=@_;
my $c=Irssi::active_win()->{width}-20;
local $Text::Wrap::columns = $c;
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'title_theme',
$pn->{begin}->strftime('%H:%M'), $pn->{ende}->strftime('%H:%M'),
$pn->{title}->{content},
$data->{channel}->{$pn->{channel}}->{'display-name'}->{content},
to_prg_short($prg_count),
);
push @prg_list, $pn;
$prg_count++;
if ($pn->{'sub-title'}->{content} ne '') {
my @l = split(/\n/, wrap(' ', ' ' ,$pn->{'sub-title'}->{content}));
foreach my $l (@l) {
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'subtitle_theme', $l,);
}
}
}
sub print_prg_long {
my ($witem, $pn)=@_;
my $c=Irssi::active_win()->{width}-20;
local $Text::Wrap::columns = $c;
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'title_theme',
$pn->{begin}->strftime('%H:%M'), $pn->{ende}->strftime('%H:%M'),
$pn->{title}->{content},
$data->{channel}->{$pn->{channel}}->{'display-name'}->{content},
);
if ($pn->{'sub-title'}->{content} ne '') {
my @l = split(/\n/, wrap(' ', ' ' ,$pn->{'sub-title'}->{content}));
foreach my $l (@l) {
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'subtitle_theme', $l,);
}
}
if ($pn->{'desc'}->{content} ne '') {
my @l = split(/\n/, wrap(' ', ' ' ,$pn->{'desc'}->{content}));
foreach my $l (@l) {
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'subtitle_theme', $l,);
}
}
if ( exists $pn->{category} ) {
if (ref($pn->{category}) eq 'HASH') {
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'tail_theme',
$pn->{category}->{content},
);
}
if (ref($pn->{category}) eq 'ARRAY') {
my $s;
foreach (@{$pn->{category}}) {
$s .= $_->{content}." ";
}
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'tail_theme', $s);
}
}
}
sub cmd_read_xmltv {
my ($args, $server, $witem)=@_;
if ( -e $xmltv_file ) {
$data = XMLin $xmltv_file;
} else {
Irssi::print("ontv: Error file not found ($xmltv_file)");
}
foreach my $pn ( @{ $data->{programme} } ) {
my $start= $tf->parse_datetime($pn->{start});
$pn->{begin}= $start;
my $stop= $tf->parse_datetime($pn->{stop});
$pn->{ende}= $stop;
}
}
sub cmd_search {
my ($args, $server, $witem)=@_;
$args =~ s/^search//;
$args =~ s/^\s+//;
$args =~ s/\s+$//;
clear_prg();
my $now = DateTime->now();
my $nowp = $now->clone->add( hours=>12 );
print_head("ontv2 search ($args)");
foreach my $pn ( @{ $data->{programme} } ) {
if ( $pn->{title}->{content}=~ m/\Q$args\E/i){
if ( $pn->{ende}->is_between( $now ,$nowp ) ||
$pn->{begin}->is_between( $now ,$nowp )) {
print_prg($witem, $pn);
}
}
}
print_footer("ontv2 search");
}
sub cmd_watching {
my ($args, $server, $witem)=@_;
$args =~ s/^watching//;
$args =~ s/^\s+//;
$args =~ s/\s+$//;
clear_prg();
my $now = DateTime->now();
my $nowp = $now->clone->add( hours=>12 );
print_head("ontv2 watching ($args)");
foreach my $pn ( @{ $data->{programme} } ) {
if ( $data->{channel}->{$pn->{channel}}->{'display-name'}->{content}=~ m/\Q$args\E/i){
if ( $pn->{ende}->is_between( $now ,$nowp ) ||
$pn->{begin}->is_between( $now ,$nowp )) {
print_prg($witem, $pn);
}
}
}
print_footer("ontv2 watching");
}
sub cmd_next {
my ($args, $server, $witem)=@_;
clear_prg();
my $now = DateTime->now();
$now->add( minutes=>5);
my $nowp = $now->clone->add( minutes=>30 );
print_head("ontv2 next ($args)");
foreach my $pn ( @{ $data->{programme} } ) {
if ( $pn->{begin}->is_between( $now ,$nowp )) {
print_prg($witem, $pn);
}
}
print_footer("ontv2 next");
}
sub cmd_tonight {
my ($args, $server, $witem)=@_;
clear_prg();
my $now = DateTime->now();
$now->set_time_zone($localTZ);
$now->set_hour(20);
$now->set_minute(10);
my $nowp = $now->clone;
$nowp->set_hour(20);
$nowp->set_minute(30);
print_head("ontv2 tonight ($args)");
foreach my $pn ( @{ $data->{programme} } ) {
if ( $pn->{begin}->is_between( $now ,$nowp )) {
print_prg($witem, $pn);
}
}
print_footer("ontv2 tonight");
}
sub cmd_check {
my ($args, $server, $witem)=@_;
my $s="ok";
my $channelc=scalar keys %{$data->{channel}};
my $prgc=scalar @{$data->{programme}};
Irssi::print("Channel: $channelc");
if ( $channelc < 100 ) {
$s="Error: channel count" ;
Irssi::print($s);
}
Irssi::print("Programme: $prgc");
if ( $prgc < 1000 ) {
$s="Error: programme count";
Irssi::print($s);
}
my $schs_version = $Irssi::Script::selfcheckhelperscript::VERSION;
Irssi::command("selfcheckhelperscript $s") if ( defined $schs_version );
}
sub cmd_now {
my ($args, $server, $witem)=@_;
clear_prg();
my $now = DateTime->now();
print_head("ontv2 now");
foreach my $pn ( @{ $data->{programme} } ) {
if ( $now->is_between( $pn->{begin}, $pn->{ende} ) ){
print_prg($witem, $pn);
}
}
print_footer("ontv2 now");
}
sub cmd_long {
my ($args, $server, $witem)=@_;
$args=~s/\s//g;
my $num= to_prg_num($args);
my $pn=$prg_list[$num];
if (defined $pn ) {
print_prg_long($witem, $pn );
}
}
sub cmd {
my ($args, $server, $witem)=@_;
if ($args =~ m/^read/) {
cmd_read_xmltv($args, $server, $witem);
} elsif ($args =~ m/^search/) {
cmd_search($args, $server, $witem);
} elsif ($args =~ m/^watching/) {
cmd_watching($args, $server, $witem);
} elsif ($args =~ m/^tonight/) {
cmd_tonight($args, $server, $witem);
} elsif ($args =~ m/^next/) {
cmd_next($args, $server, $witem);
} elsif ($args =~ m/^check/) {
cmd_check($args, $server, $witem);
} elsif ($args =~ m/^#/) {
cmd_long($args, $server, $witem);
} else {
cmd_now($args, $server, $witem);
}
}
sub cmd_help {
my ($args, $server, $witem)=@_;
$args=~ s/\s+//g;
if ($IRSSI{name} eq $args) {
Irssi::print($help, MSGLEVEL_CLIENTCRAP);
Irssi::signal_stop();
}
}
sub sig_setup_changed {
$xmltv_file= bsd_glob(Irssi::settings_get_str($IRSSI{name}.'_xmltv_file'));
}
my $twidth= Irssi::active_win()->{width}-3*6-20-4;
Irssi::theme_register([
'title_theme', "\$0 \$1 {hilight \$[$twidth]2} \$[18]3 \$[-4]4",
'subtitle_theme', ' 'x12 .'$0',
'tail_theme', '{hilight $0}',
'head_theme', '%R,--[%n%9%U $0 %U%9%R]%n',
'footer_theme', '%R`--<%n $0 %R>->%n',
]);
Irssi::signal_add('setup changed', \&sig_setup_changed);
Irssi::settings_add_str($IRSSI{name} ,$IRSSI{name}.'_xmltv_file', '~/.xmltv/current.xml');
Irssi::command_bind($IRSSI{name}, \&cmd);
Irssi::command_bind("$IRSSI{name} read", \&cmd);
Irssi::command_bind("$IRSSI{name} search", \&cmd);
Irssi::command_bind("$IRSSI{name} watching", \&cmd);
Irssi::command_bind("$IRSSI{name} tonight", \&cmd);
Irssi::command_bind("$IRSSI{name} next", \&cmd);
Irssi::command_bind('help', \&cmd_help);
sig_setup_changed();
cmd_read_xmltv();