Ticket #672: mythname-2.1.diff

File mythname-2.1.diff, 8.8 KB (added by behanw@…, 20 years ago)

diff of mythname.pl (v2.1)

  • mythname.pl

     
    77## 28 Mar 03    1.0     Hack
    88## 29 Mar 03    1.1     Added --legal to fix filenames for / and \
    99##                      Should probably be fixed for other chars
    10 ##                      but I'm to lazy.
     10##                      but I'm too lazy.
     11## 14 Nov 05    2.0 Make it work with 0.18 and up
     12## 17 Nov 05    2.1 Fix a bug with the path for --all
    1113##
    1214## This is a very nasty hack of myth.rebuilddatabase.pl which was nicely
    1315## done by Greg Froese and instructions by Robert Kulagowski. 
     
    1719##
    1820## written by greg froese (g_froese@yahoo.com)
    1921## install instructions by Robert Kulagowski (rkulagow@rocketmail.com)
     22## Much hacking by Behan Webster <behanw@websterwood.com>
     23##   Who also added some code from epair_optimize.pl by xris
    2024##
    2125## use at your own risk, i am not responsible for anything this program may
    2226## or may not do.
     
    2529use DBI;
    2630use Getopt::Long;
    2731use File::Basename;
     32use File::stat qw(:FIELDS);;
    2833
    2934## get command line args
    3035
    31 my ($database, $host, $user, $pass, $verbose, $dir);
     36sub usage() {
     37        print <<END;
     38Usage:  $0 [options] files
    3239
    33 my $argc=@ARGV;
    34 if ($argc == 0) {
    35    print "usage:  mythname.pl [options]
    36 /path/to/store/1001_20030401190000_20030401200000.nuv
    37 
    38 Where [options] is:
    39 --host          - hostname or IP address of the mysql server (default:
    40 \"127.0.0.1\")
    41 --user          - DBUSERNAME (default: \"mythtv\")
    42 --pass          - DBPASSWORD (default: \"mythtv\")
    43 --database      - DATABASENAME (default: \"mythconverg\")
    44 --replace       - Replace spaces with this string (--rep=. will return Daily.Show)
    45 --sublen        - Maximum subtitle length (only useful with -s)
    46 -s              - Add subtitle to string after a ':'
    47 --legal         - Make sure the filename is legal (no '/', '\', etc.)
    48 ";
    49 exit(0);
     40Where [options] are:
     41  --host          - hostname of the mysql server (default: \"127.0.0.1\")
     42  --user          - DBUSERNAME (default: \"mythtv\")
     43  --pass          - DBPASSWORD (default: \"mythtv\")
     44  --database      - DATABASENAME (default: \"mythconverg\")
     45  --replace       - Replace spaces with this string (--rep=. will return Daily.Show)
     46  --subtitle      - Add subtitle to string after a ':'
     47  --sublen        - Maximum subtitle length (only useful with -subtitle)
     48  --legal         - Make sure the filename is legal (no '/', '\', etc.)
     49  --all           - Consider all files
     50  --channel       - Print channel
     51  --codec         - Print codec
     52  --description   - Print description
     53  --extension     - Extension to add to title (defaults to same as filename)
     54  --file          - Print filename
     55  --grep          - Search title:subtitle
     56  --mpeg {2,4}    - Only show files of mpeg2 or mpeg4 encoding
     57  --quiet         - Don't print title
     58  --size          - Show size of show (mins, size, size/h)
     59  --total         - Print total size of listed files
     60END
     61        exit 0;
    5062}
    5163
    52 GetOptions('verbose+'=>\$verbose, 'database=s'=>\$database, 'host=s'=>\$host,
    53 'user=s'=>\$user, 'pass=s'=>\$pass, 's+'=>\$sub, 'replace=s'=>\$rep,
    54 'sublen=s'=>\$sublen, 'legal+'=>\$legal);
     64my $host="127.0.0.1";
     65my $database="mythconverg";
     66my $user="mythtv";
     67my $pass="mythtv";
     68my $mythdir = '/var/lib/mythtv/';
    5569
    56 if (!$host) { $host="127.0.0.1"; }
    57 if (!$database) { $database="mythconverg"; }
    58 if (!$user) { $user="mythtv"; }
    59 if (!$pass) { $pass="mythtv"; }
     70# Read the mysql.txt file in use by MythTV.
     71# could be in a couple places, so try the usual suspects
     72        my $found = 0;
     73        my @mysql = ('/usr/local/share/mythtv/mysql.txt',
     74                '/usr/share/mythtv/mysql.txt',
     75                '/etc/mythtv/mysql.txt',
     76                '/usr/local/etc/mythtv/mysql.txt',
     77                "$ENV{HOME}/.mythtv/mysql.txt",
     78                'mysql.txt'
     79        );
     80        foreach my $file (@mysql) {
     81                next unless (-e $file);
     82                $found = 1;
     83                open(CONF, $file) or die "Unable to open $file:  $!\n\n";
     84                while (my $line = <CONF>) {
     85                        # Cleanup
     86                        next if ($line =~ /^\s*#/);
     87                        $line =~ s/^str //;
     88                        chomp($line);
    6089
    61 my $dbh =
    62 DBI->connect("dbi:mysql:database=$database:host=$host","$user","$pass") or
    63                  die "Cannot connect to database ($!)\n";
     90                        # Split off the var=val pairs
     91                        my ($var, $val) = split(/\=/, $line, 2);
     92                        next unless ($var && $var =~ /\w/);
     93                        if ($var eq 'DBHostName') {
     94                                $host = $val;
     95                        } elsif ($var eq 'DBUserName') {
     96                                $user = $val;
     97                        } elsif ($var eq 'DBName') {
     98                                $database = $val;
     99                        } elsif ($var eq 'DBPassword') {
     100                                $pass = $val;
     101                        }
     102                }
     103                close CONF;
     104        }
     105        #die "Unable to locate mysql.txt:  $!\n\n" unless ($found && $dbhost);
    64106
    65  ($show, $path, $suffix)  = fileparse(@ARGV[0],"\.nuv");
    66   $channel = substr($show,0,4);
    67   $syear = substr($show, 5,4);
    68   $smonth = substr($show, 9,2);
    69   $sday = substr($show,11,2);
    70   $shour = substr($show,13,2);
    71   $sminute = substr($show,15,2);
    72   $ssecond = substr($show,17,2);
    73   $eyear = substr($show,20,4);
    74   $emonth = substr($show,24,2);
    75   $eday = substr($show,26,2);
    76   $ehour = substr($show,28,2);
    77   $eminute = substr($show,30,2);
    78   $esecond = substr($show,32,2);
     107usage if !GetOptions('verbose+'=>\$verbose, 'help'=>\$help, 'quiet'=>\$quiet,
     108'database=s'=>\$database, 'host=s'=>\$host, 'user=s'=>\$user, 'pass=s'=>\$pass,
     109'subtitle'=>\$sub, 'sublen=s'=>\$sublen, 'replace=s'=>\$rep, 'legal'=>\$legal,
     110'file'=>\$printfile, 'codec'=>\$printcodec, 'channel'=>\$printchan,
     111'size'=>\$printsize, 'description'=>\$printdesc, 'mpeg=i'=>\$mpeg,
     112'all'=>\$all, 'extension:s'=>\$extension, 'grep=s'=>\$grep, 'total'=>\$printtotal,
     113);
     114usage if $help;
    79115
    80  $q = "select title, subtitle, chanid, starttime, endtime from recorded where
    81 chanid=$channel and starttime='$syear$smonth$sday$shour$sminute$ssecond' and
    82 endtime='$eyear$emonth$eday$ehour$eminute$esecond'";
    83  $sth = $dbh->prepare($q);
    84  $sth->execute or die "Could not execute ($q)\n";
     116my @files;
     117if ($all) {
     118        opendir(DIR, $mythdir) || die "$mythdir: $!";
     119        @files = grep {/(mpg|nuv)$/} readdir DIR;
     120        closedir DIR;
     121} else {
     122        @files = @ARGV;
     123}
     124usage unless @files;
    85125
    86 @row=$sth->fetchrow_array;
    87 $title = $row[0];
    88 $subtitle = $row[1];
     126my $dbh = DBI->connect("dbi:mysql:database=$database:host=$host","$user","$pass")
     127        || die "Cannot connect to database ($!)\n";
    89128
    90 if ($rep) {
    91         $subtitle=~s/\ /$rep/gio;
    92         $title=~s/\ /$rep/gio;
    93 }
    94 if ($sublen) {
    95         $subtitle=substr($subtitle,0,$sublen);
    96 }
     129my $sql = 'SELECT title, subtitle, chanid, starttime, endtime, description FROM recorded WHERE basename=?';
     130my $sth = $dbh->prepare($sql);
     131my $chansql = 'SELECT channum, callsign FROM channel WHERE chanid=?';
     132my $sthchan = $dbh->prepare($chansql);
     133for my $file (@files) {
     134        ($show, $path, $suffix)  = fileparse($file , '.nuv');
    97135
    98 if ($legal) {
    99         $nogood = " ";
     136        $sth->execute("$show$suffix") || die "Could not execute ($sql)\n";
     137        my ($title, $subtitle, $chanid, $start, $end, $description) = $sth->fetchrow_array;
     138        $sthchan->execute($chanid) || die "Could not execute ($chansql)\n";
     139        my ($channum, $callsign) = $sthchan->fetchrow_array;
     140
     141        unless ($title) {
     142                print "$file is not in the database\n";
     143                next;
     144        }
     145
    100146        if ($rep) {
    101                 $nogood = $rep;
     147                $subtitle=~s/\ /$rep/gio;
     148                $title=~s/\ /$rep/gio;
    102149        }
    103         $title =~ s/\\/$nogood/gio;
    104         $subtitle =~ s/\\/$nogood/gio;
    105         $title =~ s/\//$nogood/gio;
    106         $subtitle =~ s/\//$nogood/gio;
    107 }
    108150
     151        if ($legal) {
     152                my $good = " ";
     153                if ($rep) {
     154                        $good = $rep;
     155                }
     156                $title =~ s/[\\\/'"()]/$good/gio;
     157                $subtitle =~ s/[\\\/'"()]/$good/gio;
     158        }
    109159
    110 print "$title";
    111 if ($sub) {
    112         print ":$subtitle";
     160        if ($sublen) {
     161                $subtitle=substr($subtitle,0,$sublen);
     162        }
     163       
     164        my $print = 1;
     165        if ($mpeg || $printcodec || $printsize || $printtotal) {
     166                require Time::Piece;
     167                my $before = Time::Piece->strptime($start, '%Y-%m-%d %H:%M:%S');
     168                my $after  = Time::Piece->strptime($end,   '%Y-%m-%d %H:%M:%S');
     169                my $diff = $after - $before;
     170                stat($file) || stat("$mythdir/$file") || die "Can't find $file: $!";
     171                $mins = $diff->minutes;
     172                $gb = $st_size/1024/1024/1024;
     173                $gbh = $gb * 60 / $mins;
     174
     175                # This is a bit of a hack, but recording from an ivtv card, and then transcoding it works.
     176                $codec = $gbh >= 1.1 ? 2 : 4;
     177
     178                $print = 0 if $mpeg && $mpeg ne $codec;
     179                #print "MPEG:$mpeg $gbh $print\n";
     180        }
     181
     182        if ($grep) {
     183                $print = 0 unless $title =~ /$grep/ || $subtitle && $subtitle =~ /$grep/;
     184                #print "Grep: $print $grep $title\n";
     185        }
     186
     187        my $ext;
     188        if (defined $extension) {
     189                $file =~ /\.(.*?)$/;
     190                $ext = $extension || $1;
     191        }
     192
     193        if ($print) {
     194                print "$file " if $printfile;
     195                print "mpeg$codec " if $printcodec;
     196                print $title unless $quiet;
     197                print ":$subtitle" if $sub && $subtitle;
     198                print ".$ext" if $ext;
     199                print " on $callsign($chanid)" if $printchan;
     200                printf " (%d mins in %.3f GB %.3f GB/h)", $mins, $gb, $gbh if $printsize;
     201                print "\n";
     202                print " $description\n" if $printdesc;
     203
     204                $tgb += $gb;
     205                $tt  += $mins;
     206                $tn  += 1;
     207        }
    113208}
    114 print "\n";
    115209
     210if ($printtotal) {
     211        printf "%.3f GB for %.1f hours in %d files\n", $tgb, $tt/60, $tn;
     212}
     213
    116214exit(0);
     215
     216# vim: sw=4 ts=4