00001 #!/usr/bin/perl -w
00002 #
00003 # Outputs information about the most-recently-recorded shows.
00004 #
00005 # Automatically detects database settings.
00006 #
00007
00008 # Includes
00009 use DBI;
00010 use Getopt::Long;
00011 use MythTV;
00012
00013 # Some variables we'll use here
00014 our ($num_recordings, $live, $heading, $plain_text, $text_format, $usage);
00015 our ($hours, $minutes, $seconds);
00016 our ($dnum_recordings, $dheading, $dtext_format);
00017 our ($dhours, $dminutes, $dseconds);
00018 our ($status_text_format, $status_value_format);
00019 our ($dstatus_text_format, $dstatus_value_format);
00020
00021 # Default number of recent recordings to show
00022 $dnum_recordings = 5;
00023 # Default period in which to show recordings
00024 $dhours = -1;
00025 $dminutes = -1;
00026 $dseconds = -1;
00027 # Default status output heading
00028 $dheading='Recent Recordings:\n';
00029 # Default format of plain-text output
00030 $dtext_format='%n/%j, %g:%i %A - %cc\n%T - %S\n%R\n\n';
00031 # Default format of status output display text
00032 $dstatus_text_format= '<a href="#">%n/%j, %g:%i %A - %cc - %T - %S<br />'.
00033 '<span><strong>%T</strong> %n/%j, %g:%i %A<br />'.
00034 '<em>%S</em><br /><br />%R<br /></span></a><hr />';
00035 # Default format of status output value
00036 $dstatus_value_format = '%n/%j, %g:%i %A - %T - %S';
00037
00038 # Provide default values for GetOptions
00039 $num_recordings = $dnum_recordings;
00040 $hours = $dhours;
00041 $minutes = $dminutes;
00042 $seconds = $dseconds;
00043 $heading = $dheading;
00044 $text_format = $dtext_format;
00045 $status_text_format = $dstatus_text_format;
00046 $status_value_format = $dstatus_value_format;
00047
00048 # Load the cli options
00049 GetOptions('num_recordings|recordings=s' => \$num_recordings,
00050 'hours|o=i' => \$hours,
00051 'minutes=i' => \$minutes,
00052 'seconds|e=i' => \$seconds,
00053 'live' => \$live,
00054 'heading=s' => \$heading,
00055 'plain_text' => \$plain_text,
00056 'text_format=s' => \$text_format,
00057 'status_text_format=s' => \$status_text_format,
00058 'status_value_format=s' => \$status_value_format,
00059 'usage|help' => \$usage
00060 );
00061
00062 # Print usage
00063 if ($usage) {
00064 print <<EOF;
00065 $0 usage:
00066
00067 options:
00068
00069 --recordings [number of recordings]
00070
00071 Outputs information on the last [number of recordings] shows recorded by
00072 MythTV. To output information on all recordings, specify -1.
00073
00074 default: $dnum_recordings
00075
00076 --hours [number of hours]
00077
00078 Outputs information on recordings that occurred within [number of hours].
00079 This option may be specified in conjunction with --minutes and --seconds.
00080 To output information on all matching recordings regardless of start time,
00081 specify -1 for --hours, --minutes, and --seconds.
00082
00083 default: $dhours
00084
00085 --minutes [number of minutes]
00086
00087 Outputs information on recordings that occurred within [number of minutes].
00088 This option may be specified in conjunction with --hours and --seconds.
00089 To output information on all matching recordings regardless of start time,
00090 specify -1 for --hours, --minutes, and --seconds.
00091
00092 default: $dminutes
00093
00094 --seconds [number of seconds]
00095
00096 Outputs information on recordings that occurred within [number of seconds].
00097 This option may be specified in conjunction with --hours and --minutes.
00098 To output information on all matching recordings regardless of start time,
00099 specify -1 for --hours, --minutes, and --seconds.
00100
00101 default: $dseconds
00102
00103 --live
00104 Include information on recent LiveTV recordings.
00105
00106 --heading [heading]
00107 Output the [heading] before printing information about recordings.
00108
00109 default: \'$dheading\'
00110
00111 --plain_text
00112 Output information in plain text format (i.e. for inclusion in an e-mail
00113 notification).
00114
00115 --text_format [format]
00116 Use the provided [format] to display information on the recordings. The
00117 format should use the same format specifiers used by mythrename.pl, but
00118 may also use \\r and/or \\n for line breaks. This option is ignored
00119 if --plain_text is not used.
00120
00121 default: \'$dtext_format\'
00122
00123 --help
00124
00125 Show this help text.
00126
00127 EOF
00128 exit;
00129 }
00130
00131 # Determine the period of interest
00132 my $now = time();
00133 my $start_after = $now;
00134 $start_after = $start_after - ($hours * 3600) if ($hours > 0);
00135 $start_after = $start_after - ($minutes * 60) if ($minutes > 0);
00136 $start_after = $start_after - $seconds if ($seconds > 0);
00137 $start_after = 0 if (!($start_after < $now));
00138
00139 # Fix the heading.
00140 if (defined($plain_text)) {
00141 $heading =~ s/\\r/\r/g;
00142 $heading =~ s/\\n/\n/g;
00143 }
00144 else {
00145 # Remove line break format specifiers from heading for status output
00146 $heading =~ s/(\\r|\\n)//g;
00147 }
00148
00149 # Connect to mythbackend
00150 my $Myth = new MythTV();
00151
00152 # Get the list of recordings
00153 my $count = 0;
00154 my %rows = $Myth->backend_rows('QUERY_RECORDINGS Delete');
00155 our $show;
00156 foreach my $row (@{$rows{'rows'}}) {
00157 last unless (($count < $num_recordings) || ($num_recordings < 0));
00158 $show = new MythTV::Program(@$row);
00159 # Skip LiveTV recordings?
00160 next unless (defined($live) || $show->{'recgroup'} ne 'LiveTV');
00161 # Within the period of interest?
00162 last if (($start_after) && ($show->{'recstartts'} < $start_after));
00163 # Print the recording information in the desired format
00164 if (defined($plain_text)) {
00165 text_print($count);
00166 }
00167 else {
00168 status_print($count);
00169 }
00170 $count++;
00171 }
00172
00173 # Print the output for use in the backend status page.
00174 sub status_print {
00175 my $count = shift;
00176 my $text = $show->format_name($status_text_format, ' ', ' ', 1, 0 ,1);
00177 my $value = $show->format_name($status_value_format, ' ', ' ',
00178 1, 0 ,1);
00179 print("$heading<div class=\"schedule\">") if ($count == 0);
00180 print("$text");
00181 print("</div>") if ($count == ($num_recordings - 1));
00182 print("[]:[]recording$count");
00183 print("[]:[]$value\n");
00184 }
00185
00186 # Print the output in plain text format
00187 sub text_print {
00188 my $count = shift;
00189 my $text = $show->format_name($text_format, ' ', ' ', 1, 0 ,1);
00190 $text =~ s/\\r/\r/g;
00191 $text =~ s/\\n/\n/g;
00192 print("$heading") if ($count == 0);
00193 print("$text");
00194 }