)
{
chomp;
next if /^\#/;
print "got line $_\n" if $opt_v>1;
/\'([^\']*)\'\s+\'([^\']*)\'\s+\'([^\']*)\'\s+\'([^\']*)\'/ and do {
my $re = $1;
my $nameexp = $2;
my $seasonexp = $3;
my $episodeexp = $4;
print "got line $re $nameexp $seasonexp $episodeexp\n" if $opt_v>1;
$filename =~ /$re/ and do {
$returnData[0] = eval($nameexp);
$returnData[1] = eval($seasonexp);
$returnData[2] = eval($episodeexp);
print "params are $returnData[0], $returnData[1], $returnData[2]\n" if $opt_v>1;
last CFREAD;
};
};
}
}
}
$returnData[0] =~ s/\./ /g;
if ($opt_d) { printf("(DD) Show Name: '%s', Season: '%s' Episode: '%s',\n", $returnData[0], $returnData[1], $returnData[2]);}
return @returnData;
}
sub get_show {
my ($show) = $_[0];
my ($exact ) = $_[1];
my ($episode) = $_[2];
my @ret = "";
my $premiered;
if ( $show ne "" ){
if ($year){
$show = "$show $year";
}
if ($opt_d) { print "Search URL= http://www.tvrage.com/quickinfo.php?show=$show&ep=$episode&exact=$exact\n"; }
my $site = get "http://www.tvrage.com/quickinfo.php?show=".$show."&ep=".$episode."&exact=".$exact;
foreach $line (split("\n",$site) )
{
my ($sec,$val) = split('\@',$line,2);
if ($sec eq "Show Name" )
{
$ret[0] = $val;
my $showname = $val;
}
elsif ( $sec eq "Show URL" )
{
$ret[1] = $val;
}
elsif ( $sec eq "Premiered" )
{
$ret[2] = $val;
}
elsif ($sec eq "Country" )
{
$ret[7] = $val;
}
elsif ( $sec eq "Status" )
{
$ret[8] = $val;
}
elsif ( $sec eq "Classification" )
{
$ret[9] = $val;
}
elsif ( $sec eq "Latest Episode" )
{
my($ep,$title,$airdate) = split('\^',$val);
$ret[3] = $ep.", \"".$title."\" aired on ".$airdate;
}
elsif ( $sec eq "Next Episode" )
{
my($ep,$title,$airdate) = split('\^',$val);
$ret[4] = $ep.", \"".$title."\" airs on ".$airdate;
}
elsif ( $sec eq "Episode Info" )
{
my($ep,$title,$airdate) = split('\^',$val);
$ret[5] = $ep.", \"".$title."\" aired on ".$airdate;
print "Searched Episode= Ep: $ep Title: $title Airdate: $airdate\n";
}
elsif ( $sec eq "Episode URL" )
{
$ret[6] = $val;
}
}
if ( $ret[0] )
{
return @ret;
} else
{
print ("Episode URL Not Found!");
exit (1);
}
}
}
sub getTVRageData {
my ($episodeURL) = $_[0];
my ($showURL) = $_[1];
if ($opt_d) { print "(DD) Episode URL: $episodeURL\n"; }
if ($opt_d) { print "(DD) Show URL: $showURL\n"; }
my $episodeHTML = get $episodeURL;
my $showHTML = get $showURL;
if ($opt_v == 2) { print "(V2) Episodes Stripped:\n$episodeHTML\n"; }
#parse show name
#my $showname = parseBetween( $showHTML, "","
");
#my ($showname) = $showHTML =~ /\]*\>\ | ", " | ");
$title = trim( $title );
#parse season episode number
my $s_episode = parseBetween( $episodeHTML, "Episode #: ", " | ");
#parse season
my $season = $s_episode;
$season =~ s/x[0-9]{1,2}//;
#parse episode number
my $episode_number = $s_episode;
$episode_number =~ s/[0-9]{2}x//;
#parse network
my $network = parseBetween($showHTML, "Network: ", "(");
$network =~ s/<.+?>//g;~ s/<.+?>//g;
#parse production number
my $production_number = parseBetween( $episodeHTML, "Production Number: | ", " | ");
#parse original airdate
my $firstaired = parseBetween( $episodeHTML, "Original Airdate: ", "/td>");
$firstaired=parseBetween( $firstaired," ","<");
$strPoint2 = rindex( $firstaired, ", " );
$strPoint1 = index( $firstaired, " " );
# parse year
my $firstyear = substr( $firstaired, $strPoint2 + 1 );
# parse month
my $monthtemp = substr( $firstaired, 0, $strPoint1 );
my $firstmonth = "00";
use Switch;
switch ($monthtemp) {
case "January" {$firstmonth = "01"}
case "February" {$firstmonth = "02"}
case "March" {$firstmonth = "03"}
case "April" {$firstmonth = "04"}
case "May" {$firstmonth = "05"}
case "June" {$firstmonth = "06"}
case "July" {$firstmonth = "07"}
case "August" {$firstmonth = "08"}
case "September" {$firstmonth = "09"}
case "October" {$firstmonth = "10"}
case "November" {$firstmonth = "11"}
case "December" {$firstmonth = "12"}
}
# parse day
my $firstday = substr( $firstaired, $strPoint1 + 1, $strPoint2 - $strPoint1 - 1 );
$firstday =~ s/\D+//;
#parse director
my $director = parseBetween( $episodeHTML, "Director: | ", " | ");
$writer =~ s/
/,\ /;
$writer =~ s/<.+?>//g;~ s/<.+?>//g;
$writer = stripParensFromName( $writer );
#parse summary
my $summary = parseBetween( $episodeHTML, "", "  " );
chomp($summary);
$summary =~ s/<.+?>//g;~ s/<.+?>//g;
if ($summary =~ /There is no summary added for this episode/)
{
$summary = "There is no summary added for this episode";
}
#parse category/genre
my $category = parseBetween( $showHTML, "Genre: | ", " | ");
while ($category =~ m/ \| / ) {
$category =~ s/\ \|/,/;
}
#parse airtime
my $runtime = parseBetween( $showHTML, "Runtime: ", " Minutes | ");
my $airtime = parseBetween( $showHTML, "Airs: ", " | ");
$airtime = parseBetween( $airtime, "at ", "m");
$strPoint1 = index ($airtime, ":" );
$hour = substr($airtime, 0, $strPoint1 );
$minute = substr($airtime, $strPoint1 + 1, 2 );
if ($airtime =~ m/p/) { $hour = $hour + 12 };
if (($airtime =~ m/a/) & ($hour == 12)) { $hour = 00 };
$airtime = $hour . ":" . $minute . ":00";
$endminute = $minute + $runtime;
$endhour = $hour;
while ($endminute >= 60) {
$endminute = $endminute - 60;
$endhour = $endhour + 1;
}
if ($endhour >= 24 ) {
$endhour = $endhour - 24;
}
if ($endminute <10 ) { $endminute = "0" . $endminute; }
$endtime = $endhour . ":" . $endminute . ":00";
print "Show: $showname\n";
print "Network: $network\n";
print "Season: $season\n";
print "Episode: $episode_number\n";
print "FakeEp: $s_episode\n";
print "Production Number: $production_number\n";
print "Title: $title\n";
print "First Aired: $firstyear / $firstmonth / $firstday\n";
print "Director: $director\n";
print "Plot:\n$summary\n";
print "Runtime: $runtime\n";
print "Airtime: $airtime\n";
print "Endtime: $endtime\n";
print "Writers: $writer\n";
print "Category: $category\n";
@showinfo = ($showname, $network, $season, $s_episode, $episode_number, $production_number, $title, $firstyear, $firstmonth, $firstday, $director, $summary, $runtime, $airtime, $endtime, $writer, $category );
return @showinfo;
}
#
#
# MythTV integration Functions
#
#
sub commercialflag {
my ($inputfile) = @_;
print("Building a seek table should improve FF/RW and JUMP functions when watching this video\n");
print("Do you want to build a seek table for this file? (y/n): ");
chomp(my $do_commflag = );
if ($do_commflag eq "y") {
if (!$test_mode) {
exec("mythcommflag --file $inputfile --rebuild");
} else {
print("Test mode: exec would have done\n");
print(" Exec: 'mythcommflag --file $inputfile --rebuild'\n");
}
} else {
print("Skipping illegal file format: $show\n");
}
}
sub getsetting {
my ($dbh, $ref, $val, $host ) = @_;
my $sql = qq{ SELECT data FROM settings WHERE value = '$val' };
$sql .= " AND hostname = '$host'" if defined $host;
my $sth = $dbh->prepare($sql);
my $rv = $sth->execute();
my $data;
if ($rv)
{
my @row = $sth->fetchrow_array;
print "num rows $rv,$#row, $row[0], $row[1], $row[2]\n" if $opt_v;
$$ref = $row[0] if ($#row >= 0);
}
undef $sth;
}
sub getsettings {
my $dbh = DBI->connect("dbi:mysql:database=$database:host=$dbhost","$user","$pass")
or die "(EE) Cannot connect to database ($!)\n" . exit(-1);
$dbh->{PrintError} = 0;
#$dbh->{PrintWarn} = 1;
#$dbh->{RaiseError} = 0;
getsetting($dbh, \$RecordFilePrefix, "RecordFilePrefix", $host);
getsetting($dbh, \$MasterServerIP, "MasterServerIP");
getsetting($dbh, \$MasterServerPort, "MasterServerPort");
$dbh->disconnect;
}
sub sendcmd {
my ($sock, $cmd) = @_;
my $l = length($cmd);
$cmd = sprintf("%-8d",$l) . $cmd;
#print "sending $cmd\n";
print $sock "$cmd";
$sock->flush();
$sock->recv($response,8,0);
my $l = int $response;
$sock->recv($response,$l,0);
return $response;
}
sub send_recordinglistchanged {
print "notifying server of update\n" if $opt_d;
my $sock = IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => $MasterServerIP,
PeerPort => $MasterServerPort) or return 0;
$sock->autoflush(1);
binmode($sock,":utf8");
my $s = "MESSAGE[]:[]RECORDING_LIST_CHANGE";
my $response = sendcmd($sock, "ANN Playback tvgrabber 0");
#my $response = sendcmd($sock, "ANN SlaveBackend tvgrabber localhost");
print "ANN resp was '$response'\n" if $opt_d;
$response = sendcmd($sock, $s);
print "response was '$response'\n" if $opt_d;
$response = sendcmd($sock, "DONE");
print "DONE resp was '$response'\n" if $opt_d;
undef $sock;
}
## add records to db
sub addrecord {
my @showdata = @_;
my ($showname, $network, $season, $s_episode, $episode_number, $production_number, $title, $firstyear, $firstmonth, $firstday, $director, $summary, $runtime, $airtime, $endtime, $writer, $category, $inputfile, $fullpath ) = @showdata;
my $prevstarttime = "";
print "(V1) Going to add '$showname - $title' to database!\n" if $opt_v;
my $chanid = int( rand(900))+1;
if ( $opt_channel > 0 ) {
$chanid = $opt_channel;
print "Channel: $chanid\n";
}
# else { $chanid = int( rand(900))+1; }
my $fiddledtime = 0;
if (($firstyear+0 == 0) or ($firstmonth+0 == 0) or ($firstday+0 == 0))
{
my @st = stat($fullpath);
my @loctime = localtime($st[9]); # mtime
$firstyear = $loctime[5]+1900;
$firstmonth = $loctime[4]+1;
$firstday = $loctime[3];
print "(V1) time for $fullpath is $firstyear-$firstmonth-$firstday\n" if $opt_v;
$fiddledtime = 1;
}
my $starttime = $firstyear . "-" . $firstmonth . "-" . $firstday . " " . $airtime;
my $endtime = $firstyear . "-" . $firstmonth . "-" . $firstday . " " . $endtime;
my $originalairdate = $firstyear . "-" . $firstmonth . "-" . $firstday;
my $autoexpire = 0;
my $programid;
print "(V2) season '$season' ep '$episode_number' $s_episode\n" if ($opt_v>1);
if ($season ne "")
{
#No Longer Needed?
#if ($season < 10 ) { $season = "0" . $season; }
#if ($episode_number < 10 ) { $episode_number = "0" . $episode_number; }
$programid = "S" . $season . "E" . $episode_number;
$title = "s" . $season . "e" . $episode_number . " " . $title
}
else
{
$title = $episode_number . " " . $title;
$programid = $episode_number;
}
if ($opt_t == 0) {
print "Inserting \'$showname - $title\' into MythTV Database\n";
my $dbh = DBI->connect("dbi:mysql:database=$database:host=$dbhost","$user","$pass")
or die "(EE) Cannot connect to database ($!)\n" . exit(-1);
$dbh->{PrintError} = 0;
#$dbh->{PrintWarn} = 1;
#$dbh->{RaiseError} = 0;
my $sth;
# Check for recording
my $exists = 0;
my $check = "SELECT * FROM recorded WHERE basename = (?)";
my $sths = $dbh->prepare($check);
my $rv = $sths->execute($inputfile);
print "$rv\n" if $opt_d;
if (!$rv)
{
$exists = 0;
print "Show does not exist in database!\n" if $opt_v;
}
else
{
my $x = $sths->fetchrow_hashref;
if (exists $x->{'chanid'})
{
$exists = 1;
if ($opt_v) {print "Show exists in database!\n";}
$chanid = $x->{'chanid'};
$prevstarttime = $x->{'starttime'};
}
else
{
$exists = 0;
print "Show does not exist in Database!\n" if $opt_v;
}
$sths->finish;
}
if ($exists == 0)
{
print "INSERTing show into database!\n" if $opt_v;
my $insert = "INSERT INTO recorded (chanid, starttime, endtime, title, subtitle, description, category, hostname, autoexpire, recgroup, playgroup, programid, originalairdate, basename, progstart, progend) VALUES ((?), (?), (?), (?), (?), (?), (?), (?), (?), (?), (?), (?), (?), (?), (?), (?))";
$sth = $dbh->prepare($insert);
my $rv = $sth->execute($chanid, $starttime, $endtime, $showname, $title, $summary, $category, $host, $autoexpire, $recgroup, $playgroup, $programid, $originalairdate, $inputfile, $starttime, $endtime);
}
if ($exists == 1)
{
print "Show found, REPLACEing instead!\n";
my $replace = "UPDATE recorded SET chanid = (?), starttime = (?), endtime = (?), title = (?), subtitle = (?), description = (?), category = (?), hostname = (?), autoexpire = (?), recgroup = (?), playgroup = (?), programid = (?), originalairdate = (?), basename = (?), progstart = (?), progend = (?) WHERE chanid = (?) AND starttime = (?)";
$sth = $dbh->prepare($replace);
$rv = $sth->execute($chanid, $starttime, $endtime, $showname, $title, $summary, $category, $host, $autoexpire, $recgroup, $playgroup, $programid, $originalairdate, $inputfile, $starttime, $endtime, $chanid, $prevstarttime);
}
print "$rv\n" if $opt_d;
if (!$rv)
{
print "Could not execute ($rv) ",$sth->errstr,"\n";
$sth->finish;
$dbh->disconnect;
print "chanid: $chanid\n";
print "starttime: $starttime\n";
print "endtime: $endtime\n";
print "title: $showname\n";
print "subtitle: $title\n";
print "description: $summary\n";
print "catagory: $category\n";
print "hostname: $host\n";
print "autoexpire: $autoexpire\n";
print "recgroup: $recgroup\n";
print "playgroup: $playgroup\n";
print "programid: $programid\n";
print "originalairdate: $originalairdate\n";
print "basename: $inputfile\n";
print "progstart: $starttime\n";
print "progend: $endtime\n";
print "prevstarttime: $prevstarttime\n";
return 0;
};
$sth->finish;
$dbh->disconnect;
print "Database updated!\n";
return 1;
} else {
print("Test mode: insert would have been done\n");
print(" Query: '$i'\n");
print(" Query params: $plot, $inputfile\n");
return 1;
}
}
#
#
# Generic Functions
#
#
sub parseBetween {
my ($data, $beg, $end)=@_; # grab parameters
my $ldata = lc($data);
my $start = index($ldata, lc($beg)) + length($beg);
my $finish = index($ldata, lc($end), $start);
if ($start != (length($beg) -1) && $finish != -1) {
my $result = substr($data, $start, $finish - $start);
# return w/ decoded numeric character references
# (see http://www.w3.org/TR/html4/charset.html#h-5.3.1)
decode_entities($result);
return $result;
}
return "";
}
sub trim {
my $string = shift;
for ($string) {
s/^\s+//;
s/\s+$//;
}
return $string;
}
sub stripParensFromName {
my ($parensName) = @_; # grab parameters
if ( rindex( $parensName, "(" ) != -1 ) {
return trim( substr( $parensName, 0, rindex( $parensName, "(" ) ) );
} else {
return trim( $parensName );
}
}
# vim: ts=1 sw=1 ai expandtab
## add records to db
sub addmythvideo {
my @showdata = @_;
my ($showname, $network, $season, $s_episode, $episode_number, $production_number, $title, $firstyear, $firstmonth, $firstday, $director, $summary, $runtime, $airtime, $endtime, $writer, $category, $inputfile, $fullpath ) = @showdata;
my $prevstarttime = "";
my $inetref = "99999999";
my $tvset = "100";
print "(V3) Going to add '$showname - $title' to database!\n" if $opt_v;
my $originalairdate = $firstyear . "-" . $firstmonth . "-" . $firstday;
my $programid;
print "(V2) season '$season' ep '$episode_number' $episode_number\n" if ($opt_v>1);
if ($season ne "")
{
#no longer needed?
#if ($season < 10 ) { $season = "0" . $season; }
#if ($episode_number < 10 ) { $episode_number = "0" . $episode_number; }
$programid = "S" . $season . "E" . $episode_number;
$title = "s" . $season . "e" . $episode_number . " " . $title
}
else
{
$title = $s_episode . " " . $title;
$programid = $s_episode;
}
if ($opt_t == 0) {
print "Inserting \'$showname - $title\' into MythTV Database\n";
my $dbh = DBI->connect("dbi:mysql:database=$database:host=$dbhost","$user","$pass")
or die "(EE) Cannot connect to database ($!)\n" . exit(-1);
$dbh->{PrintError} = 0;
#$dbh->{PrintWarn} = 1;
#$dbh->{RaiseError} = 0;
my $sth;
# Check for recording
my $exists = 0;
my $check = "SELECT * FROM videometadata WHERE filename = (?)";
my $sths = $dbh->prepare($check);
my $rv = $sths->execute($fullpath);
print "$rv\n" if $opt_d;
if (!$rv) {
$exists = 0;
print "Show does not exist in database !\n" if $opt_v;
} else {
my $x = $sths->fetchrow_hashref;
if (exists $x->{'intid'}) {
$exists = 1;
if ($opt_v) {print "Show exists in database!\n";}
$intid = $x->{'intid'};
$prevstarttime = $x->{'starttime'};
} else {
$exists = 0;
print "(V1) Show does not exist in Database!\n" if $opt_v;
}
$sths->finish;
}
if ($exists == 0) {
print "INSERTing show into database!\n" if $opt_v;
my $insert = "INSERT INTO videometadata (title, director, plot, year, length, filename, inetref, category, browse, showlevel, coverfile ) VALUES ((?), (?), (?), (?), (?), (?), (?), (?), (?), (?), (?))";
$sth = $dbh->prepare($insert);
$rv = $sth->execute("$showname $title", $director, $summary, $firstyear, $runtime, $fullpath, $inetref, $tvset, 1, 1, $coverfile );
}
if ($exists == 1) {
print "Show found, REPLACEing instead!\n";
my $replace = "UPDATE videometadata SET title = (?), plot = (?), director = (?) , year = (?), length = (?), inetref = (?), category = (?), browse = (?), showlevel = (?), coverfile = (?) WHERE filename = (?)";
$sth = $dbh->prepare($replace);
$rv = $sth->execute("$showname $title", $summary, $director, $firstyear, $runtime, $inetref, $tvset, "1", "1", $coverfile, $fullpath);
}
# update the genre from the catagory...
# get the intid for the file
my $selectint = "SELECT * FROM videometadata WHERE filename = (?)";
my $sths = $dbh->prepare($selectint);
my $rv = $sths->execute($inputfile);
print "$rv\n" if $opt_d;
my $y = $sths->fetchrow_hashref;
if (exists $y->{'intid'}) {
my $intid = $y->{'intid'};
# delete the existing links if they are there
my $delete = "DELETE FROM videometadatagenre WHERE idvideo = (?)";
$sth = $dbh->prepare($delete);
$rv = $sth->execute("$intid");
# loop through the categories and link to the entry
foreach my $cat ( split(/,/, $category)){
$cat =~ s/^\s//g;
$cat =~ s/\s$//g;
if ($opt_v) {print "Adding intid $intid to genre $cat\n";}
# check to see if the catagory exists in the db.
my $genreselect = "SELECT * FROM videogenre where genre like (?)";
my $sths = $dbh->prepare($genreselect);
my $srv = $sths->execute($cat);
if (!($srv)) {
print "INSERTing genre into database!\n" if $opt_v;
my $insert = "INSERT INTO videogenre (genre) VALUES (?)";
$sth = $dbh->prepare($insert);
$irv = $sth->execute($cat );
$genreselect = "SELECT * FROM videogenre where genre like (?)";
$sth = $dbh->prepare($genreselect);
$srv = $sths->execute($cat);
}
my $z = $sths->fetchrow_hashref;
if (exists $z->{'intid'}) {
my $genreid = $z->{'intid'};
if ($opt_v) {print "Inserting $intid, $genreid into videometadatagenre\n";}
my $insert = "INSERT INTO videometadatagenre (idvideo, idgenre) VALUES ((?), (?))";
my $sth = $dbh->prepare($insert);
$irv = $sth->execute($intid, $genreid);
}
}
}
print "$rv\n" if $opt_d;
if (!$rv) {
print "Could not execute ($rv) ",$sth->errstr,"\n";
$sth->finish;
$dbh->disconnect;
print "intid: $intid\n";
print "title: $showname $title\n";
print "plot: $summary\n";
print "catagory: $category\n";
print "originalairdate: $originalairdate\n";
print "filename: $inputfile\n";
print "coverfile: $coverfile\n";
return 0;
};
$sth->finish;
$sths->finish;
$dbh->disconnect;
print "Database updated!\n";
return 1;
} else {
print("Test mode: insert would have been done\n");
print(" Query: '$i'\n");
print(" Query params: $plot, $inputfile\n");
return 1;
}
}
|
|