#!/usr/bin/perl use DBI; $database = 'mythconverg'; $databasetable = 'program'; $databaseuser = 'root'; $databasepass = ''; $config_dir = '/movies'; $dtvstream = '/usr/local/bin/dtvstream'; # cron does not enhert the path connectdatabase(); $current_time = gettime(time()); #$current_time = "2005-06-28 21:30:00"; # uncomment for testing print "time now is $current_time\n"; $record_title=''; open(SHOWS,"<$config_dir/shows.txt") || die("Could not open shows.txt for reading: $!\n"); while(){ unless($_ =~ /^#/){ # commented out chomp(); ($show_name,$priority,$dir) = split(/=/,$_); #print "Looking for $show_name $priority $dir\n"; $sth = $dbh->prepare(qq(select title,subtitle,starttime,endtime,chanid from program where title = "$show_name" and starttime = "$current_time")) or die("could not select from database.\n"); # $sth = $dbh->prepare(qq(select title,subtitle,starttime,endtime,chanid # from program where title = "$show_name")) # or die("could not select from database.\n"); $sth->execute; while(($title,$subtitle,$starttime,$endtime,$chanid) = $sth->fetchrow_array){ $title = make_pretty($title); $subtitle = make_pretty($subtitle); print "found $title-$subtitle with priority $priority\n"; if($starttime =~ /(\d\d):(\d\d):(\d\d)/){ $start_hour = $1; $start_min = $2; $start_sec = $3; }else{ print "ERROR: Could not determine start time from $starttime.\n"; } if($endtime =~ /(\d\d):(\d\d):(\d\d)/){ $end_hour = $1; $end_min = $2; $end_sec = $3; }else{ print "ERROR: Could not determine end time from $endtime.\n"; } $duration = (($end_hour*60)+$end_min) - (($start_hour*60)+$start_min); # record highest priority only if($priority >= $highest_priority){ $highest_priority = $priority; $record_dir = $dir; $record_title = $title; $record_subtitle = $subtitle; $record_starttime = $starttime; $record_endtime = $endtime; $record_chanid= $chanid; $record_duration= $duration; } } } } close(SHOWS); if($record_title){ $sth = $dbh->prepare(qq(select channum,freqid,name,callsign from channel where chanid= "$record_chanid")) or die("could not select from database.\n"); $sth->execute; ($chan_num,$chan_freqid,$chan_name,$chan_callsign) = $sth->fetchrow_array; ($chan_channel,$chan_tsprog) = split(/-/,$chan_freqid); $pid = fork(); if($pid == 0){ # new process to do the recording print "Recording $record_title into $config_dir/$record_dir.\n"; print "$record_title-$record_subtitle $record_starttime,$record_endtime on $chan_channel.$chan_tsprog ($chan_name $chan_callsign) for $record_duration min.\n"; print "time now is $current_time\n"; $record_duration--; exec "$dtvstream -c $chan_channel -p $chan_tsprog -m $record_duration -o \"$config_dir/$record_dir$record_title-$record_subtitle.m2t\" &>/dev/null"; } } sub connectdatabase { $dbh = DBI->connect("dbi:mysql:$database","$databaseuser","$databasepass") or die("Cannot connect: $DBI::errstr"); } sub gettime($){ my $return; #my @days = ("Sun","Mon","Tues","Wed","Thur","Fri","Sat","Sun"); #my @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); my ($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($_[0]); $year = $year + 1900; $mon = $mon + 1; $return = sprintf("%04d-%02d-%02d %02d:%02d:00",$year,$mon,$mday,$hr,$min); return $return; } sub make_pretty($){ my $return = $_[0]; $return =~ s/ //g; $return =~ s/'//g; $return =~ s/"//g; $return =~ s/\///g; $return =~ s/&/And/g; $return =~ s/://g; return $return; }