-
Notifications
You must be signed in to change notification settings - Fork 0
/
replayguide.pl
7502 lines (5989 loc) · 219 KB
/
replayguide.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!C:/Perl/bin/perl.exe
#[/usr/bin/perl
#
# Personal ReplayGuide
# by Lee Thompson <thompsonl@logh.net>
# with bits by Philip Van Baren, Kanji T. Bates and Kevin J. Moye
# $Id: replayguide.pl,v 1.30 2003/11/04 00:29:59 pvanbaren Exp $
#
# Requirements:
# XMLTV or DataDirect
#
#
# NOTE: If you have not read the INSTALL.txt or the prg.conf file please do so
# RIGHT NOW!
#
#------------------------------------------------------------------------------------
# This file is part of Personal ReplayGuide (C) 2003 by Lee Thompson
#
# Personal ReplayGuide is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Personal ReplayGuide is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Personal ReplayGuide; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#------------------------------------------------------------------------------------
# NOTE: The more complex routines have special debug modes that can
# be set by module. Simply look for $specialdebug=0 near the
# top of each routine and change to 1. Logging goes to
# the log file. Warning: It can get *very* chatty.
#------------------------------------------------------------------------------------
use POSIX qw( strftime getcwd );
use CGI qw(:standard);
use Time::Local;
my $_version = "Personal ReplayGuide|Main|1|1|200|Lee Thompson,Philip Van Baren,Kevin J. Moye,Kanji T. Bates";
#------------------------------------------------------------------------------------
# Determine Current Directroy
#------------------------------------------------------------------------------------
$current_dir = getcwd;
$current_dir .= "/";
#------------------------------------------------------------------------------------
# Get script pathname, unfortunately on Apache on Win32 $0 is not reliable since
# for some reason it gets changed to an 8.3 pathname.
#
# IIS and Apache both provide long filename pathnames in environment variables so
# if it's an 8.3 we look there. If we don't find it, we just tough it out.
#------------------------------------------------------------------------------------
$script_pathname = $0;
if ($0 =~ /\~/) {
if (length($ENV{SCRIPT_FILENAME}) > 0 ) {
$script_pathname = $ENV{SCRIPT_FILENAME};
}
if (length($ENV{PATH_TRANSLATED}) > 0 ) {
$script_pathname = $ENV{PATH_TRANSLATED};
}
}
#------------------------------------------------------------------------------------
# Get the path that the script is in
#------------------------------------------------------------------------------------
(my $path,my $basename) = $script_pathname =~ m|^(.*[/\\])([^/\\]+?)$|;
#------------------------------------------------------------------------------------
# PRG_HOME is an override environment variable
#------------------------------------------------------------------------------------
if (length($ENV{PRG_HOME}) > 0) {
$path = $ENV{PRG_HOME};
}
#------------------------------------------------------------------------------------
# Normalize the path
#------------------------------------------------------------------------------------
$path =~ s/\\/\//g;
#------------------------------------------------------------------------------------
# If the current working directory is not where we need to be, change paths.
#------------------------------------------------------------------------------------
$changed_path = -1;
if (($current_dir ne $path) && ($path ne "")) {
if (chdir $path) {
$changed_path = 1;
}else{
$changed_path = 0;
}
}
#------------------------------------------------------------------------------------
# Load Libraries
#------------------------------------------------------------------------------------
require 'rg_common.pl'; # Load common functions
require 'rg_database.pl'; # Load database functions
require 'rg_info.pl'; # Database Info
require 'rg_config.pl'; # Load config functions
#------------------------------------------------------------------------------------
# Set up module identification
#------------------------------------------------------------------------------------
my $module_name = "";
(my $debug_package, my $debug_filename, my $debug_line, my $debug_subroutine, my $debug_hasargs, my $debug_wantarray, my $debug_evaltext, my $debug_isrequire) = caller(0);
if (length($debug_evaltext) > 0) {
$module_name = $debug_evaltext;
}else{
if ($path eq "") {
$basename = $script_pathname;
}
$module_name = $basename;
}
$prg_module{$module_name} = $_version;
#------------------------------------------------------------------------------------
# Set up any arrays
#------------------------------------------------------------------------------------
@productionrole = (
"",
"Actor",
"Guest Star",
"Host",
"Director",
"Producer",
"Executive Producer",
"Writer"
);
#-------------------------------------------------------------------
# Initialize and Load Configuration
#-------------------------------------------------------------------
$verbose = 0;
$scriptname = $script_pathname;
$configfile = "replayguide.conf";
$configstatus = &ReadConfig;
(my $parent,my $desc,my $major,my $minor,my $build,my $authors) = parseModuleData($prg_module{$module_name});
$program_title = $parent;
$program_module = $desc;
$program_author = buildMultiWordList($authors);
$program_version = "$major.$minor";
$program_build = $build;
$prg_start = time;
$now = strftime( "%Y-%m-%d", localtime ) . " " . strftime( "%H:%M:%S", localtime );
CGI::import_names('input');
$RemoteAddress = $ENV{'REMOTE_ADDR'};
if ($RemoteAddress eq $null) {
$RemoteAddress = "console";
}
#-------------------------------------------------------------------
# Check to see if the IP Address in $RemoteAddress has access to RTV
#-------------------------------------------------------------------
if (hasAccess($RemoteAddress)) {
$rtvaccess = 1;
}else{
$rtvaccess = 0;
}
#-------------------------------------------------------------------
# Check to see if the IP Address in $RemoteAddress is set for PDA
# mode
#-------------------------------------------------------------------
if (isPDA($RemoteAddress)) {
$showpdaformat = 1;
}else{
$showpdaformat = 0;
}
if (length($input::SHOWPDAFORMAT) > 0) {
$showpdaformat = int $input::SHOWPDAFORMAT;
}
#-------------------------------------------------------------------------------------------
# Modify Settings to PDA Mode (If Needed)
#-------------------------------------------------------------------------------------------
if ($showpdaformat) {
$size_pdalistings = "H5";
$defaultshowhours = 1; # PDA mode only shows an hour
$showrtvtext = 1; # Force Text
}
#-------------------------------------------------------------------------------------------
# Start it up!
#-------------------------------------------------------------------------------------------
writeDebug("********************************************************");
writeDebug("$program_title v$program_version (Build $program_build)");
writeDebug("Running as $script_pathname with PID $$");
writeDebug("Remote Address: $RemoteAddress ($rtvaccess)");
if ($verbose) {
writeDebug("Console Output: Enabled");
}else{
writeDebug("Console Output: Disabled");
}
if ($ENV{SERVER_SOFTWARE} ne $null) {
writeDebug("Running on $ENV{SERVER_SOFTWARE}");
}
if ($showpdaformat) {
writeDebug("PDA mode is ON");
}
if ($debug) {
writeDebug("Debug Messages are ON");
}
&InitializeDisplay;
#-------------------------------------------------------------------
# Set up Database
#-------------------------------------------------------------------
&InitDB;
&InitDSN;
#-------------------------------------------------------------------
# Log what modules are loaded
#-------------------------------------------------------------------
identifyLoadedModules();
#-------------------------------------------------------------------
# Start Database
#-------------------------------------------------------------------
writeDebug("Start DSN->$DATASOURCE{DSN}");
writeDebug("If this is the last line in your log file you may have incorrectly configured the database.");
writeDebug("(Check prg.conf's [database] section, database.conf or rg_info.pl and the INSTALL.txt file for troubleshooting.)");
$prgdb_handle = &StartDSN;
if ($prgdb_handle ne $null) {
writeDebug("Database Connection Established to $DATASOURCE{DSN} ($prgdb_handle)");
}else{
writeDebug("Database Connection Failed to $DATASOURCE{DSN}");
writeDebug("Reason: " . &GetLastSQLError());
writeDebug("(Check prg.conf's [database] section, database.conf or rg_info.pl and the INSTALL.txt file for troubleshooting.)");
abend("Could not establish DSN ($DATASOURCE{DSN})");
}
#-------------------------------------------------------------------
# Set up Variables
#-------------------------------------------------------------------
$null = "";
$stars = "****";
$url_parms = "";
#-------------------------------------------------------------------
# Parse Input Fields
#-------------------------------------------------------------------
if (length($ENV{'QUERY_STRING'}) > 0) {
$default_mode = "";
}
$inp_startdate = $input::STARTDATE;
$inp_starthour = $input::STARTHOUR;
$inp_showhours = int $input::SHOWHOURS;
$inp_showslots = int $input::SHOWSLOTS;
$inp_firsttune = int $input::FIRSTTUNE;
$inp_lasttune = int $input::LASTTUNE;
$inp_favorite = $input::FAVORITE;
$inp_searchfield = $input::FIELD;
$inp_search = $input::SEARCH;
$inp_todo = filterfield($input::TODO);
$inp_programid = $input::PROGRAMID;
$inp_selectedrtv = $input::SELECTEDRTV;
$inp_recordtype = $input::RECORDTYPE;
$inp_updatedrtv = $input::UPDATE;
$inp_showsrtv = $input::SHOWSHOWS;
$inp_rtvunit = $input::RTVUNIT;
$inp_rtvaction = int $input::RTVACTION;
$inp_showdetail = $input::SHOWDETAIL;
$inp_deletertvshow = $input::DELETESHOW;
$inp_manualrec = $input::MANUALREC;
$inp_inputsource = $input::INPUTSOURCE;
writeDebug("CGI: STARTDATE: $inp_startdate");
writeDebug("CGI: STARTHOUR: $inp_starthour");
writeDebug("CGI: SHOWHOURS: $inp_showhours");
writeDebug("CGI: SHOWSLOTS: $inp_showslots");
writeDebug("CGI: FAVORITE: $inp_favorite");
writeDebug("CGI: SEARCHFIELD: $inp_searchfield");
writeDebug("CGI: FIRSTTUNE: $inp_firsttune");
writeDebug("CGI: LASTTUNE: $inp_lasttune");
writeDebug("CGI: SEARCH: $inp_search");
writeDebug("CGI: PROGRAMID: $inp_programid");
writeDebug("CGI: SELECTEDRTV: $inp_selectedrtv");
writeDebug("CGI: RECORDTYPE: $inp_recordtype");
writeDebug("CGI: UPDATE: $inp_updatedrtv");
writeDebug("CGI: RTVUNIT: $inp_rtvunit");
writeDebug("CGI: RTVACTION: $inp_rtvaction");
writeDebug("CGI: SHOWSHOWS: $inp_showsrtv");
writeDebug("CGI: SHOWDETAIL: $inp_showdetail");
writeDebug("CGI: DELETESHOW: $inp_deletertvshow");
writeDebug("CGI: MANUALREC: $inp_manualrec");
writeDebug("CGI: INPUTSOURCE: $inp_inputsource");
writeDebug("CGI: SHOWPDAFORMAT: $showpdaformat");
#-------------------------------------------------------------------
# If icons are not enabled clear them out
#-------------------------------------------------------------------
if ($showbuttonicons < 1) {
$icon_schedule = "";
$icon_now = "";
$icon_go = "";
$icon_locate = "";
$icon_findall = "";
$icon_findrepeat = "";
$icon_all = "";
$icon_locate = "";
$icon_prevwindow = "";
$icon_nextwindow = "";
$icon_prevchan = "";
$icon_nextchan = "";
$icon_todo = "";
$icon_find = "";
$icon_select = "";
$icon_schedule = "";
$icon_done = "";
}
if ($showchannelicons < 1) {
$image_stereo = "";
$image_repeat = "";
$image_cc = "";
$image_tvg = "";
$image_tvpg = "";
$image_tv14 = "";
$image_tvy = "";
$image_tvy7 = "";
$image_tvma = "";
$image_mpaag = "";
$image_mpaapg = "";
$image_mpaapg13 = "";
$image_mpaar = "";
$image_mpaanc17 = "";
}
#-------------------------------------------------------------------
# Load Schedule Resolver Module (SRM) Plug-In
#-------------------------------------------------------------------
writeDebug("Attempting to load $scheduler SRM module");
if (!fileExists($scheduler)) {
writeDebug("Failed to load $scheduler SRM module");
if (fileExists("rg_null.pl")) {
writeDebug("Defaulting to rg_null.pl SRM module");
$rtvaccess = 0;
}else{
abend("Could not load null SRM");
}
}
if (!$rtvaccess) {
$scheduler = "rg_null.pl"; # No RTV Access
}
writeDebug("Loading $scheduler SRM module");
require $scheduler; # Functions handling scheduled recording display
identifyLoadedModules($scheduler); # Ask SRM to ID itself
writeDebug(&AboutScheduler); #
$rtv_refreshinterval = &SchedulerDefaultRefresh;
if ($defaultrefreshinterval != -1) {
if ($rtv_refreshinterval != $defaultrefreshinterval) {
if ($rtv_refreshinterval > 0) {
writeDebug("$scheduler forced guide refresh interval to $rtv_refreshinterval minutes");
}else{
if (&SchedulerDoBatchUpdate) {
writeDebug("$scheduler disabled automatic guide refresh");
}else{
writeDebug("$scheduler does not support guide data");
}
}
}
}else{
if ($rtv_refreshinterval > 0) {
writeDebug("$scheduler guide refresh interval set to $rtv_refreshinterval minutes");
}else{
writeDebug("$scheduler disabled automatic guide refresh");
}
}
if ($rtv_refreshinterval > 0) {
writeDebug("$scheduler is set to automatically refresh guide data every $rtv_refreshinterval minutes.");
}else{
writeDebug("$scheduler automatic guide refresh is disabled.");
}
#-------------------------------------------------------------------
# Process ReplayTVs...
#-------------------------------------------------------------------
#
# This builds three sets of data structures for dealing with
# ReplayTV units.
#
# $replaylist is a comma delimited list of replayunits IDs. These
# are ALL the ReplayTV units defined in the replayunits table.
#
# $schedulinglist is a semicolon delimited list of replay unit IDs.
# What makes this different from replaylist, is these are units that
# are capable of remote scheduling. (If skipversioncheck is on
# this will always be the same as replaylist.)
#
# In addition there is a set of hash arrays for additional
# ReplayTV unit data.
#
# $rtvlabel{REPLAYID} Network Name (e.g. Office)
# $rtvaddress{REPLAYID} FQDN or IP Address of the RTV
# $rtvport{REPLAYID} Port number to use for the RTV
# $rtvversion{REPLAYID} ReplayTV Version
# $guideversion{REPLAYID} Guide Snapshot Version
# $lastsnapshot{REPLAYID} Epoch Seconds of Last Database Refresh
# $categories{REPLAYID} Semicolon delimited list of Categories
# Cat #,Category Format
# $rtvdefaultquality{ID} Default quality level for ReplayID #
# $rtvdefaultkeep{ID} Default episodes to keep for ReplayID #
#
# $rtvunit{LABEL} Uses the Network Name and gives you
# the replayid.
#
# Note if $rtvaccess is zero, none of this will be set!
#
#-------------------------------------------------------------------
$replaylist = "";
$schedulinglist = "";
if ($rtvaccess > 0) {
writeDebug("Processing ReplayTVs...");
my $need_update = 0;
my $db_handle = &StartDSN;
$Stmt = "SELECT * FROM $db_table_replayunits ORDER BY replayaddress;";
my $sth = sqlStmt($db_handle,$Stmt);
if($sth) {
while ( my $row = $sth->fetchrow_hashref ) {
my $replayid = $row->{'replayid'};
$replaylist = buildcommastring($replaylist,$replayid);
$rtvlabel{$replayid} = $row->{'replayname'};
$rtvunit{$rtvlabel{$replayid}} = $replayid;
$rtvaddress{$replayid} = $row->{'replayaddress'};
$rtvport{$replayid} = $row->{'replayport'};
$rtvversion{$replayid} = $row->{'replayosversion'};
$rtvdefaultquality{$replayid} = int $row->{'defaultquality'};
$rtvdefaultkeep{$replayid} = int $row->{'defaultkeep'};
$guideversion{$replayid} = $row->{'guideversion'};
$lastsnapshot{$replayid} = $row->{'lastsnapshot'};
$categories{$replayid} = $row->{'categories'};
#------------------------------------------
# Any one of these criteria will force an
# update.
#------------------------------------------
if ($row->{'lastsnapshot'} == 0) {
$need_update = 1;
}
if ($rtvversion{$replayid} == 0) {
$need_update = 1;
}
if ($guideversion{$replayid}== 0) {
$need_update = 1;
}
if ($rtvport{$replayid} != 80) {
writeDebug("Warning! $replayid) $rtvlabel{$replayid} at $rtvaddress{$replayid} has non standard port of $rtvport{$replayid}. Integration functions may fail for this unit.");
}
if ($skipversioncheck) {
if (length($schedulinglist) > 0) {
$schedulinglist .= ";";
}
$schedulinglist .= "$replayid";
}else{
if ($guideversion{$replayid} == 2) {
if (length($schedulinglist) > 0) {
$schedulinglist .= ";";
}
$schedulinglist .= "$replayid";
}
}
}
}
endDSN($sth,$db_handle);
undef $db_handle;
undef $sth;
if ($replaylist eq $null) {
writeDebug("No ReplayTVs defined. ReplayTV functionality disabled.");
$rtvaccess = 0;
$inp_rtvaction = 0;
}else{
writeDebug(countArray($replaylist) . " ReplayTV units found:");
for ( split /,/, $replaylist ) {
/,/;
writeDebug("$_) '$rtvlabel{$_}' at $rtvaddress{$_}");
}
if ($need_update) {
$inp_updatedrtv = "ALL";
writeDebug("$db_table_replayunits table has never been refreshed for 1 or more units, forcing update");
}
}
#-------------------------------------------------------------------
# Process RTVUNIT/ACTION
#-------------------------------------------------------------------
if ($inp_rtvaction > 0) {
writeDebug("Processing ReplayTV Actions... ( $inp_rtvaction -> $inp_rtvunit )");
if ($inp_rtvaction == 1) {
#-------------------------------
# Refresh
#-------------------------------
if ($inp_rtvunit > 0) {
if (length($inp_updatertv) > 0) {
$inp_updatedrtv = "ALL";
}else{
$inp_updatedrtv = $rtvaddress{$inp_rtvunit};
}
}else{
$inp_updatedrtv = "ALL";
}
}
if ($inp_rtvaction == 2) {
#-------------------------------
# To-Do List
#-------------------------------
$inp_todo = $inp_rtvunit;
}
if ($inp_rtvaction == 3) {
#-------------------------------
# Manage Shows/Channels
#-------------------------------
$inp_showsrtv = $inp_rtvunit;
}
if ($inp_rtvaction == 4) {
#-------------------------------
# Manual Recording
#-------------------------------
$inp_manualrec = $inp_rtvunit;
}
}
if (length($inp_updatedrtv) > 0) {
if ($rtv_updatesleepseconds) {
writeDebug("Sleeping for $rtv_updatesleepseconds seconds to allow guidesnapshot(s) to show new recording on unit $inp_updatedrtv");
sleep($rtv_updatesleepseconds);
writeDebug("Awake");
}
}
my $lastsnapshot = 0;
my $right_now = time;
my $addr;
if ($rtvaccess > 0) {
writeDebug("Checking ReplayTV guide data...");
for ( split /,/, $replaylist ) {
/,/;
my $replayid = $_;
my $refresh = 0;
#-------------------------------------------------------------------
# If the guide data is stale, get a fresh copy
# if rtv_refreshinterval == 0, then only do manually forced refreshes
# i.e. disable the autorefresh on time interval and after a scheduling change
#-------------------------------------------------------------------
if (($rtv_refreshinterval > 0) && (($right_now - $lastsnapshot{$replayid}) > ($rtv_refreshinterval * 60))) {
writeDebug("$replayid) $rtvlabel{$replayid}: $rtv_refreshinterval is non-zero and snapshot is stale");
$refresh = 1;
}
if (uc $inp_updatedrtv eq uc $rtvaddress{$replayid}) {
writeDebug("$replayid) $rtvlabel{$replayid}: $rtvaddress{$replayid} matches $inp_updatedrtv, forcing getFreshScheduleTable");
$refresh = 1;
}
if ($inp_updatedrtv eq 'ALL') {
writeDebug("$replayid) $rtvlabel{$replayid}: $inp_updatedrtv set to ALL, forcing getFreshScheduleTable");
$refresh = 1;
}
if ( $refresh ) {
displayText("Refreshing $rtvlabel{$replayid} ... ",0,1);
writeDebug("Attempting to refresh $rtvlabel{$replayid}");
if(0 != getFreshScheduleTable($replayid)) {
displayText("Failed\n");
writeDebug("getFreshScheduleTable failed for $rtvlabel{$replayid}");
} else {
displayText("Complete\n");
writeDebug("Refresh Complete");
#-----------------------------------------
# Update the snapshot time in the database
#-----------------------------------------
writeDebug("Attempting to update $rtvlabel{$replayid} snapshot marker");
my $Update = "UPDATE $db_table_replayunits SET lastsnapshot = $right_now WHERE replayid = '$replayid';";
my $db_handle = &StartDSN;
if ( ! sqlStmt($db_handle,$Update) ) {
displayText("ReplayTV database update Failed: <PRE>$Update\n" . &GetLastSQLError() . " \n</PRE>");
writeDebug("Failed to update marker");
}
endDSN("",$db_handle);
undef $db_handle;
writeDebug("Finished processing RTV $rtvlabel{$replayid}");
}
} else {
writeDebug("Refresh not required for $rtvlabel{$replayid}");
if(0 != getCachedScheduleTable($replayid)) {
writeDebug("getCachedScheduleTable failed for $rtvlabel{$replayid}");
displayText("Failed to load cached schedule for $rtvlabel{$replayid}");
}
}
}
}
}else{
writeDebug("No access to ReplayTV data.");
$replaylist = "";
$inp_updatedrtv = "";
}
#-------------------------------------------------------------------
# Prepare Time Calculations
#-------------------------------------------------------------------
$now_startdate = substr($now,0,4) . substr($now,5,2) . substr($now,8,2);
$now_starthour = int substr($now,11,2);
$now_timestring = substr($now,0,4) . substr($now,5,2) . substr($now,8,2) . substr($now,11,2) . substr($now,14,2) . "59";
$now_searchstart = substr($now,0,4) . substr($now,5,2) . substr($now,8,2) . substr($now,11,2) . "0000";
$now_todaystart = substr($now,0,4) . substr($now,5,2) . substr($now,8,2) . "000000";
if ($inp_startdate eq $null) {
$inp_startdate = $now_startdate;
}
if ($inp_starthour eq $null) {
$inp_starthour = $now_starthour;
} else {
$inp_starthour = int $inp_starthour;
}
if ($inp_showhours < 1) {
$inp_showhours = $defaultshowhours;
}
if ($inp_showslots < 1) {
$inp_showslots = $defaultshowslots;
}
if (($primetime_start < 0) || ($primetime_start > 23)) {
$primetime_start = 20; # 8 PM
}
if (($inp_starthour < 0) || ($inp_starthour > 23)) {
$inp_starthour = $now_starthour;
}
$starthour = sprintf("%02d",$inp_starthour);
$starttime = $inp_startdate . $starthour . "0000";
$startseconds = as_epoch_seconds($starttime);
$previousseconds = $startseconds - (($inp_showhours * 60) * 60);
$overlap = $startseconds + ($grid_end_overlap * 60);
$overlap = as_time_string($overlap);
$endseconds = $startseconds + (($inp_showhours * 60) * 60);
$previoustime = as_time_string($previousseconds);
$starttime = as_time_string($startseconds);
$endtime = as_time_string($endseconds);
#-------------------------------------------------------------------
# Ready Grid Settings
#-------------------------------------------------------------------
$maxpos = ($inp_showhours * 60) / $inp_showslots;
$maxpos = int $maxpos;
$colpos = 1;
#-------------------------------------------------------------------
# Get Channel Range
#-------------------------------------------------------------------
$Stmt = "";
$Stmt .= "SELECT * ";
$Stmt .= "FROM $db_table_channels ";
$Stmt .= "WHERE hidden = 0 ";
$Stmt .= "ORDER BY tuning;";
$records = 0;
$first_channel = 0;
$last_channel = 0;
my $db_handle = &StartDSN;
my $sth = sqlStmt($db_handle,$Stmt);
if ( $sth ) {
while ( $row = $sth->fetchrow_hashref ) {
$records++;
if ($records == 1) {
$firstrecord = int $row->{'tuning'};
}
$currentrecord = int $row->{'tuning'};
$currentchannel = $row->{'channel'};
$iconsrc = $row->{'iconsrc'};
if($channelicondir && $iconsrc) {
$iconsrc =~ s|^.*/([^\/]+)$|$channelicondir/$1|;
}
$icon{$currentchannel} = $iconsrc;
$tuningnumber{$currentchannel} = $currentrecord;
$channel[$records] = $currentrecord . " " . $currentchannel;
$tuning[$records] = $currentrecord;
}
$lastrecord = $currentrecord;
$first_channel = $firstrecord;
$last_channel = $lastrecord;
$channelcount = $records;
}else{
abend("Error building channel range");
}
endDSN($sth,$db_handle);
undef $db_handle;
if ($records == 0) {
abend("No channels defined.\nHave you run updatetvdata yet?\nAlso check database configuration and/or datafeed configuration");
}
#------------------------------------------------------------------------
# For more than 12-hour displays, force to a single channel rotated table
#------------------------------------------------------------------------
if ($inp_showhours > 12) {
$inp_lasttune = $inp_firsttune;
}
if ($inp_firsttune > $inp_lasttune) {
$inp_firsttune = $first_channel;
}
if ($inp_lasttune < $inp_firsttune) {
$inp_lasttune = $last_channel;
}
if ($inp_firsttune == 0) {
$inp_firsttune = $first_channel;
}
if ($inp_lasttune == 0) {
$inp_lasttune = $last_channel;
}
#-------------------------------------------------------------------
# Set up channel next/prev buttons
#-------------------------------------------------------------------
$records = 0;
do {
$records++;
if ($tuning[$records] == $inp_firsttune) {
$first_rec = $records;
}
if ($tuning[$records] == $inp_lasttune) {
$last_rec = $records;
}
} while ($records < $channelcount);
$display_rec = ($last_rec - $first_rec) + 1;
if ($display_rec eq $channelcount) {
$next_rec = 0;
$prev_rec = 0;
}else{
$next_chan = $tuning[$last_rec + 1];
$prev_chan = $tuning[$first_rec - 1];
if ($next_chan > $last_channel) {
$next_chan = $last_channel;
}
if ($prev_chan < $first_channel) {
$prev_chan = $first_channel;
}
$range_chan = $display_rec;
}
writeDebug("Channel Range is $first_channel to $last_channel ($channelcount)");
#-------------------------------------------------------------------
# Get First and Last date in the database
#-------------------------------------------------------------------
$selected_channel = $first_channel;
$channel_populated = 1;
my $db_handle = &StartDSN;
my $sth = "";
do {
$Stmt = "";
$Stmt .= "SELECT * ";
$Stmt .= "FROM $db_table_tvlistings ";
$Stmt .= "WHERE tuning = $selected_channel ";
$Stmt .= "ORDER BY starttime;";
$records = 0;
$sth = sqlStmt($db_handle,$Stmt);
if ( $sth ) {
if (!$sth->fetchrow_hashref) {
#------------------------------------------
# Channel has no data, try the next one.
#------------------------------------------
$channel_populated = 0;
$selected_channel++;
}else{
$channel_populated = 1;
}
}else{
abend("Error building date range");
}
if ($selected_channel > $last_channel ) {
abend("Error building date range, no data found.");
}
} while ($channel_populated < 1);
while ( $row = $sth->fetchrow_hashref ) {
$records++;
if ($records == 1) {
$firstrecord = sqltotimestring($row->{'starttime'});
}
$currentrecord = sqltotimestring($row->{'starttime'});
}
$lastrecord = $currentrecord;
$rng_startdate = substr($firstrecord,0,4) . substr($firstrecord,4,2) . substr($firstrecord,6,2);
$rng_stopdate = substr($lastrecord,0,4) . substr($lastrecord,4,2) . substr($lastrecord,6,2);
endDSN($sth,$db_handle);
undef $sth;
undef $db_handle;
#-------------------------------------------------------------------
# Set Date Boundaries
#-------------------------------------------------------------------
$rng_start = as_epoch_seconds($rng_startdate . "000000");
$rng_end = as_epoch_seconds($rng_stopdate . "000000") + 86400;
$prev_time = as_epoch_seconds($previoustime);
$next_time = as_epoch_seconds($endtime);
$dayone = timestringtosql(as_time_string($rng_start));
#-------------------------------------------------------------------
# Final Range Checks
#-------------------------------------------------------------------
if ($prev_time < $rng_start) {
$prevok = 0;
}else{
$prevok = 1;
}
if ($next_time > $rng_end) {
$nextok = 0;
}else{
$nextok = 1;
}
if (length($prev_chan) < 1) {
$prev_chan = $first_channel;
}
if (length($next_chan) < 1) {
$next_chan = $last_channel;
}
if ($prev_chan <= $first_channel) {
$prevchanok = 0;
}else{
$prevchanok = 1;
}
if ($next_chan >= $last_channel) {
$nextchanok = 0;
}else{
$nextchanok = 1;
}
writeDebug("Listings Range is $rng_startdate to $rng_stopdate");
displayText();
#-------------------------------------------------------------------
# See if Cast Crew Database is Populated
#-------------------------------------------------------------------
writeDebug("Checking castcrew table ($db_table_castcrew)");
$Stmt = "SELECT *";
if ($db_driver eq "ODBC") {
$Stmt = "SELECT TOP 1 *";
}
$Stmt .= " FROM $db_table_castcrew";
if (($db_driver eq "SQLite") || ($db_driver eq "mysql")) {
$Stmt .= " LIMIT 1";
}