Ticket #672: mythname.diff

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

diff of mythname.pl

  • 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
    1112##
    1213## This is a very nasty hack of myth.rebuilddatabase.pl which was nicely
    1314## done by Greg Froese and instructions by Robert Kulagowski. 
     
    1718##
    1819## written by greg froese (g_froese@yahoo.com)
    1920## install instructions by Robert Kulagowski (rkulagow@rocketmail.com)
     21## Much hacking by Behan Webster <behanw@websterwood.com>
     22##   Who also added some code from epair_optimize.pl by xris
    2023##
    2124## use at your own risk, i am not responsible for anything this program may
    2225## or may not do.
     
    2528use DBI;
    2629use Getopt::Long;
    2730use File::Basename;
     31use File::stat qw(:FIELDS);;
    2832
    2933## get command line args
    3034
    31 my ($database, $host, $user, $pass, $verbose, $dir);
     35sub usage() {
     36        print <<END;
     37Usage:  $0 [options] files
    3238
    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);
     39Where [options] are:
     40  --host          - hostname of the mysql server (default: \"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  --subtitle      - Add subtitle to string after a ':'
     46  --sublen        - Maximum subtitle length (only useful with -subtitle)
     47  --legal         - Make sure the filename is legal (no '/', '\', etc.)
     48  --all           - Consider all files
     49  --channel       - Print channel
     50  --codec         - Print codec
     51  --description   - Print description
     52  --extension     - Extension to add to title (defaults to same as filename)
     53  --file          - Print filename
     54  --grep          - Search title:subtitle
     55  --mpeg {2,4}    - Only show files of mpeg2 or mpeg4 encoding
     56  --quiet         - Don't print title
     57  --size          - Show size of show (mins, size, size/h)
     58  --total         - Print total size of listed files
     59END
     60        exit 0;
    5061}
    5162
    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);
     63my $host="127.0.0.1";
     64my $database="mythconverg";
     65my $user="mythtv";
     66my $pass="mythtv";
    5567
    56 if (!$host) { $host="127.0.0.1"; }
    57 if (!$database) { $database="mythconverg"; }
    58 if (!$user) { $user="mythtv"; }
    59 if (!$pass) { $pass="mythtv"; }
     68# Read the mysql.txt file in use by MythTV.
     69# could be in a couple places, so try the usual suspects
     70        my $found = 0;
     71        my @mysql = ('/usr/local/share/mythtv/mysql.txt',
     72                '/usr/share/mythtv/mysql.txt',
     73                '/etc/mythtv/mysql.txt',
     74                '/usr/local/etc/mythtv/mysql.txt',
     75                "$ENV{HOME}/.mythtv/mysql.txt",
     76                'mysql.txt'
     77        );
     78        foreach my $file (@mysql) {
     79                next unless (-e $file);
     80                $found = 1;
     81                open(CONF, $file) or die "Unable to open $file:  $!\n\n";
     82                while (my $line = <CONF>) {
     83                        # Cleanup
     84                        next if ($line =~ /^\s*#/);
     85                        $line =~ s/^str //;
     86                        chomp($line);
    6087
    61 my $dbh =
    62 DBI->connect("dbi:mysql:database=$database:host=$host","$user","$pass") or
    63                  die "Cannot connect to database ($!)\n";
     88                        # Split off the var=val pairs
     89                        my ($var, $val) = split(/\=/, $line, 2);
     90                        next unless ($var && $var =~ /\w/);
     91                        if ($var eq 'DBHostName') {
     92                                $host = $val;
     93                        } elsif ($var eq 'DBUserName') {
     94                                $user = $val;
     95                        } elsif ($var eq 'DBName') {
     96                                $database = $val;
     97                        } elsif ($var eq 'DBPassword') {
     98                                $pass = $val;
     99                        }
     100                }
     101                close CONF;
     102        }
     103        #die "Unable to locate mysql.txt:  $!\n\n" unless ($found && $dbhost);
    64104
    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);
     105usage if !GetOptions('verbose+'=>\$verbose, 'help'=>\$help, 'quiet'=>\$quiet,
     106'database=s'=>\$database, 'host=s'=>\$host, 'user=s'=>\$user, 'pass=s'=>\$pass,
     107'subtitle'=>\$sub, 'sublen=s'=>\$sublen, 'replace=s'=>\$rep, 'legal'=>\$legal,
     108'file'=>\$printfile, 'codec'=>\$printcodec, 'channel'=>\$printchan,
     109'size'=>\$printsize, 'description'=>\$printdesc, 'mpeg=i'=>\$mpeg,
     110'all'=>\$all, 'extension:s'=>\$extension, 'grep=s'=>\$grep, 'total'=>\$printtotal,
     111);
     112usage if $help;
    79113
    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";
     114my @files;
     115if ($all) {
     116        my $dir = '/var/lib/mythtv/';
     117        opendir(DIR, $dir) || die "$dir: $!";
     118        @files = grep {/(mpg|nuv)$/} readdir DIR;
     119        closedir DIR;
     120} else {
     121        @files = @ARGV;
     122}
     123usage unless @files;
    85124
    86 @row=$sth->fetchrow_array;
    87 $title = $row[0];
    88 $subtitle = $row[1];
     125my $dbh = DBI->connect("dbi:mysql:database=$database:host=$host","$user","$pass")
     126        || die "Cannot connect to database ($!)\n";
    89127
    90 if ($rep) {
    91         $subtitle=~s/\ /$rep/gio;
    92         $title=~s/\ /$rep/gio;
    93 }
    94 if ($sublen) {
    95         $subtitle=substr($subtitle,0,$sublen);
    96 }
     128my $sql = 'SELECT title, subtitle, chanid, starttime, endtime, description FROM recorded WHERE basename=?';
     129my $sth = $dbh->prepare($sql);
     130my $chansql = 'SELECT channum, callsign FROM channel WHERE chanid=?';
     131my $sthchan = $dbh->prepare($chansql);
     132for my $file (@files) {
     133        ($show, $path, $suffix)  = fileparse($file , '.nuv');
    97134
    98 if ($legal) {
    99         $nogood = " ";
     135        $sth->execute("$show$suffix") || die "Could not execute ($sql)\n";
     136        my ($title, $subtitle, $chanid, $start, $end, $description) = $sth->fetchrow_array;
     137        $sthchan->execute($chanid) || die "Could not execute ($chansql)\n";
     138        my ($channum, $callsign) = $sthchan->fetchrow_array;
     139
     140        unless ($title) {
     141                print "$file is not in the database\n";
     142                next;
     143        }
     144
    100145        if ($rep) {
    101                 $nogood = $rep;
     146                $subtitle=~s/\ /$rep/gio;
     147                $title=~s/\ /$rep/gio;
    102148        }
    103         $title =~ s/\\/$nogood/gio;
    104         $subtitle =~ s/\\/$nogood/gio;
    105         $title =~ s/\//$nogood/gio;
    106         $subtitle =~ s/\//$nogood/gio;
    107 }
    108149
     150        if ($legal) {
     151                my $good = " ";
     152                if ($rep) {
     153                        $good = $rep;
     154                }
     155                $title =~ s/[\\\/'"()]/$good/gio;
     156                $subtitle =~ s/[\\\/'"()]/$good/gio;
     157        }
    109158
    110 print "$title";
    111 if ($sub) {
    112         print ":$subtitle";
     159        if ($sublen) {
     160                $subtitle=substr($subtitle,0,$sublen);
     161        }
     162       
     163        my $print = 1;
     164        if ($mpeg || $printcodec || $printsize || $printtotal) {
     165                require Time::Piece;
     166                my $before = Time::Piece->strptime($start, '%Y-%m-%d %H:%M:%S');
     167                my $after  = Time::Piece->strptime($end,   '%Y-%m-%d %H:%M:%S');
     168                my $diff = $after - $before;
     169                stat($file) || die "Can't find $file: $!";
     170                $mins = $diff->minutes;
     171                $gb = $st_size/1024/1024/1024;
     172                $gbh = $gb * 60 / $mins;
     173
     174                # This is a bit of a hack, but recording from an ivtv card, and then transcoding it works.
     175                $codec = $gbh >= 1.1 ? 2 : 4;
     176
     177                $print = 0 if $mpeg && $mpeg ne $codec;
     178                #print "MPEG:$mpeg $gbh $print\n";
     179        }
     180
     181        if ($grep) {
     182                $print = 0 unless $title =~ /$grep/ || $subtitle && $subtitle =~ /$grep/;
     183                #print "Grep: $print $grep $title\n";
     184        }
     185
     186        my $ext;
     187        if (defined $extension) {
     188                $file =~ /\.(.*?)$/;
     189                $ext = $extension || $1;
     190        }
     191
     192        if ($print) {
     193                print "$file " if $printfile;
     194                print "mpeg$codec " if $printcodec;
     195                print $title unless $quiet;
     196                print ":$subtitle" if $sub && $subtitle;
     197                print ".$ext" if $ext;
     198                print " on $callsign($chanid)" if $printchan;
     199                printf " (%d mins in %.3f GB %.3f GB/h)", $mins, $gb, $gbh if $printsize;
     200                print "\n";
     201                print " $description\n" if $printdesc;
     202
     203                $tgb += $gb;
     204                $tt  += $mins;
     205                $tn  += 1;
     206        }
    113207}
    114 print "\n";
    115208
     209if ($printtotal) {
     210        printf "%.3f GB for %.1f hours in %d files\n", $tgb, $tt/60, $tn;
     211}
     212
    116213exit(0);
     214
     215# vim: sw=4 ts=4