#!/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= '%rs - %n/%j %g:%i %A - %cc - '.
'%T - %S
'.
'%T %n/%j, %g:%i %A
'.
'%S
%R
';
# 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 < 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") if ($count == 0);
print("$text");
print("
") 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");
}