[mythtv-users] Script to use Schedules Direct with legacy MythTV installations

Josh Mastronarde jmastron at gmail.com
Thu Aug 23 04:56:23 UTC 2007

I know we're all anxiously awaiting new releases to natively support
Schedules Direct; I'm planning to upgrade as soon as I can, but in the
meantime I made a small script to semi-automatically run a modified version
of XMLTV's tv_grab_na_dd followed by mythfilldatabase, adjusting the new
program ID format to match the old database.  It's not pretty (though it
does nice things like wait for the next suggested run time), and it's
definitely not permanent, but will give me a bit of breathing room to
upgrade on my own schedule.  At the very least, you can extract the manual
commands to roll your own solution; it took me some trial and error to
figure out the right command line parameters.

This assumes XMLTV (and tv_grab_na_dd) is already installed (an older
version without the new URL is okay; see modification instructions); this
was true in my ATRPMS "yum install mythtv-suite" following Jarod's guide.
See the help/comments at the beginning for more instructions.

Good luck; I hope this helps a few people.



sub printhelp {
print <<ENDOFHELP;

SchedulesDirect semi-automatic downloader for legacy MythTV installations
Josh Mastronarde, jmastron at gmail.com

Syntax:  sdfill <options>
-nodownload Don't download data (use last downloaded file)
-nofill Don't run mythfilldatabase
-once Only run once

This script will grab listings data, modify the episode ID field to match
the old data, and run mythfilldatabase.  If -once not specified, it will
waiting for the next suggested time from the last run.

Steps to prepare:

1)  Find XMLTV's tv_grab_na_dd (was in /usr/bin for me); copy to

2)  Modify ./tv_grab_na_sd; replace the "dd_service" line with:
    my $dd_service=

3)  Run: ./tv_grab_na_sd --configure --config-file

4)  Replace "CA54023" with your lineup id (from the above .conf file; leave
    the ":-", I think)

If you want to run by hand, the key commands are in "$grabcmd" and
"$mythfillcmd" below, along with the small section of code after "Modify
episode/program IDs" to shorten the IDs.


use Time::Local;

# Defaults

$xmlfile1 = "sdlist.xml";
$xmlfile2 = "sdlist_fixed.xml";
$grablog = "grab_log";

$grabcmd = "./tv_grab_na_sd --config-file ~/.xmltv/tv_grab_na_sd.conf " .
   "--days 14 --dd-data $xmlfile1 --download-only --list-times";

$mythfillcmd = "mythfilldatabase --dd-file 1 -1 CA54023 $xmlfile2";

# Parse command line arguments
while (@ARGV) {
  $arg = shift(@ARGV);
  if ($arg eq "-nodownload") {
    $nodownload = 1;
  } elsif ($arg eq "-nofill") {
    $nofill = 1;
  } elsif ($arg eq "-h") {
  } elsif ($arg eq "-once") {
    $once = 1;
  } else {
    push(@narg, $arg);

$| = 1;
print "\n---- SDfill started at ", &prlocal(time()), "\n\n";

# Loop (but only once if -once flag set)

$run = 1;
while ($run) {
  $run = 0 if ($once);

  # Check for logfile from last grabber run and look for suggested time

  if (-e $grablog) {
    $suggtime = "";
    open(grablog, "<$grablog") || die "Error: Can't open $grablog\n";
    while (<grablog>) {
      if (($year,$mon,$day,$hour,$min,$sec) = ($_ =~ /^suggestedTime
\|(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+)Z/)) {
        $suggtime = timegm($sec,$min,$hour,$day,$mon-1,$year-1900);
    if ($suggtime) {
      $waitsecs = $suggtime - time();
      $waithrs = int($waitsecs/3600);
      $waitmins = int($waitsecs/60) % 60;

      print "---- Sleeping until suggested ", &prlocal($suggtime), ",
$waithrs:$waitmins from now ----\n\n";

  print "\n---- Grab starting at ", &prlocal(time()), "\n\n";

  unless ($nodownload) {
    print "Running: $grabcmd\n";
    open(f, "$grabcmd 2>&1 |") || die "Error: Can't execute tv_grab\n";
    open(grablog, ">$grablog") || die "Error: Can't write $grablog\n";
    while (<f>) {
      print grablog;

  # Modify episode/program IDs to remove 2 digits to match old format

  print "\n\n---- Fixing Episode Number format\n\n";
  open(xmlin, "<$xmlfile1") || die "Error: Can't open $xmlfile1";
  open(xmlout, ">$xmlfile2") || die "Error: Can't open $xmlfile2";

  while (<xmlin>) {
    s/(\<program id=\'..)00/$1/;
    s/(\<schedule program=\'..)00/$1/;
    s/(\<programGenre program=\'..)00/$1/;
    print xmlout;

  # Run mythfilldatabase

  unless ($nofill) {
    print "Running: $mythfillcmd\n";
    open(f, "$mythfillcmd 2>&1 |") || die "Error: Can't execute
    while (<f>) {

print "\n---- Grab done at ", &prlocal(time()), "\n\n";

# Helper routine to print nicely formatted local time from a timestamp

sub prlocal {
  my($intime) = @_;
  my ($sec,$min,$hour,$mday,$mon,$year) = localtime($intime);
  return(sprintf("%04d/%02d/%02d %02d:%02d:%02d",
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mythtv.org/pipermail/mythtv-users/attachments/20070822/13256235/attachment.htm 

More information about the mythtv-users mailing list