summaryrefslogtreecommitdiffstats
path: root/linhes/linhes-system/misc_upcoming_recordings.pl
blob: 26ec1b414dc9a90a7e5fc3f6f71a11350f64ebef (plain)
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
#!/usr/bin/perl -w
#
# Provides notification of upcoming recordings.
#
# Automatically detects database settings.
#

# Includes
    use DBI;
    use Getopt::Long;
    use MythTV;

# Some variables we'll use here
    our ($num_recordings, $heading, $plain_text, $text_format, $usage);
    our ($hours, $minutes, $seconds, $no_conflicts_message);
    our ($scheduled, $duplicates, $deactivated, $conflicts);
    our ($dnum_recordings, $dheading, $dtext_format);
    our ($dhours, $dminutes, $dseconds, $dno_conflicts_message);
    our ($dscheduled, $dduplicates, $ddeactivated, $dconflicts);
    our ($status_text_format, $status_value_format);
    our ($dstatus_text_format, $dstatus_value_format);

# Default number of upcoming recordings to show
    $dnum_recordings = 5;
# Default period in which to show recordings
    $dhours          = -1;
    $dminutes        = -1;
    $dseconds        = -1;
# Default recording status types to show
    $dscheduled      = 1;
    $dduplicates     = 0;
    $ddeactivated    = 0;
    $dconflicts      = 1;
# Default status output heading
    $dheading='Upcoming Recordings:\n';
# Default format of plain-text output
    $dtext_format='%rs\n%n/%j, %g:%i %A - %cc\n%T - %S\n%R\n\n';
# Default "no conflicts" message
    $dno_conflicts_message='No conflicts.\n';
# Default format of status output display text
    $dstatus_text_format= '<a href="javascript:void(0)">%rs - %n/%j %g:%i %A -  %cc - '.
                          '%T - %S<br />'.
                          '<span><strong>%T</strong> %n/%j, %g:%i %A<br />'.
                          '<em>%S</em><br /><br />%R<br /></span></a><hr />';
# Default format of status output value
    $dstatus_value_format = '%n/%j %g:%i %A - %T - %S';

# Provide default values for GetOptions
    $num_recordings       = $dnum_recordings;
    $hours                = $dhours;
    $minutes              = $dminutes;
    $seconds              = $dseconds;
    $scheduled            = $dscheduled;
    $duplicates           = $dduplicates;
    $deactivated          = $ddeactivated;
    $conflicts            = $dconflicts;
    $heading              = $dheading;
    $text_format          = $dtext_format;
    $no_conflicts_message = $dno_conflicts_message;
    $status_text_format   = $dstatus_text_format;
    $status_value_format  = $dstatus_value_format;

# Load the cli options
    GetOptions('num_recordings|recordings=s'         => \$num_recordings,
               'hours|o=i'                           => \$hours,
               'minutes=i'                           => \$minutes,
               'seconds|s=i'                         => \$seconds,
               'show_scheduled|_show_scheduled|scheduled|_scheduled|e!'
                                                     => \$scheduled,
               'show_duplicates|_show_duplicates|duplicates|_duplicates|p!'
                                                     => \$duplicates,
               'show_deactivated|_show_deactivated|deactivated|_deactivated|v!'
                                                     => \$deactivated,
               'show_conflicts|_show_conflicts|conflicts|_conflicts!'
                                                     => \$conflicts,
               'heading=s'                           => \$heading,
               'plain_text'                          => \$plain_text,
               'text_format=s'                       => \$text_format,
               'no_conflicts_message=s'              => \$no_conflicts_message,
               'status_text_format=s'                => \$status_text_format,
               'status_value_format=s'               => \$status_value_format,
               'usage|help'                          => \$usage
              );

# Print usage
    if ($usage) {
    # Make default "--show_*" options readable
        $dscheduled   = ($dscheduled   ? '--show_scheduled' :
                                         '--no_show_scheduled');
        $dduplicates  = ($dduplicates  ? '--show_duplicates' :
                                         '--no_show_duplicates');
        $ddeactivated = ($ddeactivated ? '--show_deactivated' :
                                         '--no_show_deactivated');
        $dconflicts   = ($dconflicts   ? '--show_conflicts' :
                                         '--no_show_conflicts');
        print <<EOF;
$0 usage:

options:

--recordings [number of recordings]

    Outputs information on the next [number of recordings] shows to be recorded
    by MythTV and that match the criteria specified for --scheduled,
    --duplicates, --deactivated, and --conflicts.  To output information on all
    matching recordings, specify -1.

    default:  $dnum_recordings

--hours [number of hours]

    Outputs information on recordings starting in the next [number of hours]
    and that match the criteria specified for --scheduled, --duplicates,
    --deactivated, and --conflicts.  This option may be specified in
    conjunction with --minutes and --seconds.  To output information on all
    matching recordings regardless of start time, specify -1 for --hours,
    --minutes, and --seconds.

    default:  $dhours

--minutes [number of minutes]

    Outputs information on recordings starting in the next [number of minutes]
    and that match the criteria specified for --scheduled, --duplicates,
    --deactivated, and --conflicts.  This option may be specified in
    conjunction with --hours and --seconds.  To output information on all
    matching recordings regardless of start time, specify -1 for --hours,
    --minutes, and --seconds.

    default:  $dminutes

--seconds [number of seconds]

    Outputs information on recordings starting in the next [number of seconds]
    and that match the criteria specified for --scheduled, --duplicates,
    --deactivated, and --conflicts.  This option may be specified in
    conjunction with --hours and --minutes.  To output information on all
    matching recordings regardless of start time, specify -1 for --hours,
    --minutes, and --seconds.

    default:  $dseconds

--show_scheduled|--no_show_scheduled

    Outputs information about scheduled recordings.  Scheduled recordings are
    those that MythTV plans to actually record.

    default:  $dscheduled

--show_duplicates|--no_show_duplicates

    Outputs information about duplicate recordings.  Duplicate recordings are
    those that will not be recorded because of the specified duplicate matching
    policy for the rule.

    default:  $dduplicates

--show_deactivated|--no_show_deactivated

    Outputs information about deactivated recordings.  Deactivated recordings
    are those that MythTV will not record because the schedule is inactive,
    because the showing was set to never record, because the show is being
    recorded in an earlier or later showing, because there are too many
    recordings or not enough disk space to allow the recording, or because
    the show you\'ve specified for recording is not listed in the timeslot
    specified.

    default:  $ddeactivated

--show_conflicts|--no_show_conflicts

    Outputs information about conflicts (those shows that MythTV cannot record
    because of other higher-priority scheduled recordings).

    default:  $dconflicts

--heading [heading]
    Output the [heading] before printing information about recordings.

    default:  \'$dheading\'

--plain_text
    Output information in plain text format (i.e. for inclusion in an e-mail
    notification).

--text_format [format]
    Use the provided [format] to display information on the recordings.  The
    format should use the same format specifiers used by mythlink.pl, but
    may also use \\r and/or \\n for line breaks and %rs for recording status.
    This option is ignored if --plain_text is not used.

    default:  \'$dtext_format\'

--no_conflicts_message [message]
    Use the provided [message] to specify there are no conflicts.  This option
    is used when only information about conflicts is requested and there are
    no conflicts.  I.e. it is only used with the combination of show_*
    options --show_conflicts, --no_show_scheduled, --no_show_deactivated,
    and --no_show_duplicates .

    default:  \'$dno_conflicts_message\'

--help

    Show this help text.

EOF
        exit;
    }

# Determine the period of interest
    my $now = time();
    my $start_before = $now;
    $start_before = $start_before + ($hours * 3600) if ($hours > 0);
    $start_before = $start_before + ($minutes * 60) if ($minutes > 0);
    $start_before = $start_before + $seconds if ($seconds > 0);
    $start_before = 0 if (!($start_before > $now));

# Fix the heading.
    if (defined($plain_text)) {
        $heading =~ s/\\r/\r/g;
        $heading =~ s/\\n/\n/g;
    }
    else {
    # Remove line break format specifiers from heading for status output
        $heading =~ s/(\\r|\\n)//g;
    }

# Connect to mythbackend
    my $Myth = new MythTV();

# Get the list of recordings
    my $count = 0;
    my %rows = $Myth->backend_rows('QUERY_GETALLPENDING', 2);
    my $has_conflicts = $rows{'offset'}[0];
    if ((!$has_conflicts) &&
        (($conflicts) &&
         (!(($scheduled) || ($duplicates) || ($deactivated))))) {
        $no_conflicts_message =~ s/\\r/\r/g;
        $no_conflicts_message =~ s/\\n/\n/g;
        print "$no_conflicts_message";
        exit 0;
    }
    my $num_scheduled = $rows{'offset'}[1];
    our $show;
    foreach my $row (@{$rows{'rows'}}) {
        last unless (($count < $num_recordings) || ($num_recordings < 0));
        $show = new MythTV::Program(@$row);
        last if (($start_before) && ($show->{'recstartts'} > $start_before));
        next if ((!$scheduled) && (is_scheduled($show->{'recstatus'})));
        next if ((!$duplicates) && (is_duplicate($show->{'recstatus'})));
        next if ((!$deactivated) && (is_deactivated($show->{'recstatus'})));
        next if ((!$conflicts) && (is_conflict($show->{'recstatus'})));

    # Print the recording information in the desired format
        if (defined($plain_text)) {
            text_print($count);
        }
        else {
            status_print($count);
        }
        $count++;
    }

# Returns true if the show is scheduled to record
    sub is_scheduled {
        my $recstatus = (shift() or 0);
        return (($MythTV::recstatus_willrecord == $recstatus) ||
                ($MythTV::recstatus_recorded == $recstatus) ||
                ($MythTV::recstatus_recording == $recstatus));
    }

# Returns true if the show is a duplicate
    sub is_duplicate {
        my $recstatus = (shift() or 0);
        return (($MythTV::recstatus_repeat == $recstatus) ||
                ($MythTV::recstatus_previousrecording == $recstatus) ||
                ($MythTV::recstatus_currentrecording == $recstatus));
    }

# Returns true if the recording is deactivated
    sub is_deactivated {
        my $recstatus = (shift() or 0);
        return (($MythTV::recstatus_inactive == $recstatus) ||
                ($MythTV::recstatus_toomanyrecordings == $recstatus) ||
                ($MythTV::recstatus_cancelled == $recstatus) ||
                ($MythTV::recstatus_deleted == $recstatus) ||
                ($MythTV::recstatus_aborted == $recstatus) ||
                ($MythTV::recstatus_notlisted == $recstatus) ||
                ($MythTV::recstatus_dontrecord == $recstatus) ||
                ($MythTV::recstatus_lowdiskspace == $recstatus) ||
                ($MythTV::recstatus_tunerbusy == $recstatus) ||
                ($MythTV::recstatus_neverrecord == $recstatus) ||
                ($MythTV::recstatus_earliershowing == $recstatus) ||
                ($MythTV::recstatus_latershowing == $recstatus));
    }

# Returns true if the show cannot be recorded due to a conflict
    sub is_conflict {
        my $recstatus = (shift() or 0);
        return ($MythTV::recstatus_conflict == $recstatus);
    }

# Print the output for use in the backend status page.
    sub status_print {
        my $count = shift;
        my $text = $show->format_name($status_text_format, ' ', ' ', 1, 0 ,1);
        {
        no warnings 'uninitialized';
	$text =~ s/%rs/$MythTV::RecStatus_Types{$show->{'recstatus'}}/g;
	}
        my $value = $show->format_name($status_value_format, ' ', ' ',
                                       1, 0 ,1);
        $value =~ s/%rs/$MythTV::RecStatus_Types{$show->{'recstatus'}}/g;
        print("$heading<div class=\"schedule\">") if ($count == 0);
        print("$text");
        print("</div>") if ($count == ($num_recordings - 1));
        print("[]:[]recording$count");
        print("[]:[]$value\n");
    }

# Print the output in plain text format
    sub text_print {
        my $count = shift;
        my $text = $show->format_name($text_format, ' ', ' ', 1, 0 ,1);
        {
        no warnings 'uninitialized';
	$text =~ s/%rs/$MythTV::RecStatus_Types{$show->{'recstatus'}}/g;
	}
        $text =~ s/\\r/\r/g;
        $text =~ s/\\n/\n/g;
        print("$heading") if ($count == 0);
        print("$text");
    }