Ticket #865: mythrename.diff
File mythrename.diff, 4.8 KB (added by , 20 years ago) |
---|
-
mythrename.pl
22 22 use File::Path; 23 23 24 24 # Some variables we'll use here 25 our ($dest, $format, $usage, $underscores );25 our ($dest, $format, $usage, $underscores, $sub); 26 26 our ($dformat, $dseparator, $dreplacement, $separator, $replacement); 27 27 our ($db_host, $db_user, $db_name, $db_pass, $video_dir); 28 28 our ($hostname, $dbh, $sh, $sh2, $q, $q2, $count); … … 43 43 GetOptions('link|dest|destination|path:s' => \$dest, 44 44 'format=s' => \$format, 45 45 'separator=s' => \$separator, 46 'sub' => \$sub, 46 47 'replacement=s' => \$replacement, 47 48 'usage|help|h' => \$usage, 48 'underscores' => \$underscores 49 'underscores' => \$underscores, 50 'verbose' => \$verbose 49 51 ); 50 52 51 53 # Print usage … … 64 66 65 67 /var/video/show_names/ 66 68 69 --sub 70 71 Only takes effect with --link option. If you would like to make subdirectories 72 per title, specify --sub. In the future you will be able to pick the field 73 to sub on. 74 67 75 --format 68 76 69 77 default: $dformat … … 124 132 125 133 default: No underscores 126 134 135 --verbose 136 137 Print some debug info, otherwise be quiet. 138 127 139 --help 128 140 129 141 Show this help text. … … 215 227 # Double-check the destination 216 228 $dest ||= "$video_dir/show_names"; 217 229 # Alert the user 218 print "Link destination directory: $dest\n"; 230 if ($verbose) { 231 print "Link destination directory: $dest\n"; 232 } 219 233 # Create nonexistent paths 220 234 unless (-e $dest) { 221 235 mkpath($dest, 0, 0755) or die "Failed to create $dest: $!\n"; … … 244 258 while (my $ref = $sh->fetchrow_hashref()) { 245 259 my %info = %{$ref}; 246 260 die "This script requires mythtv >= 0.19\n" unless ($info{'basename'}); 261 next unless (-e "$video_dir/".$info{'basename'}); 247 262 # Default times 248 263 my ($syear, $smonth, $sday, $shour, $sminute, $ssecond) = $info{'starttime'} =~ /(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/; 249 264 my ($eyear, $emonth, $eday, $ehour, $eminute, $esecond) = $info{'endtime'} =~ /(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/; … … 330 345 $name =~ s/(?:(?:$safe_sep)+\s*)+(?=[^\d\s])/$separator /sg; 331 346 $name =~ s/^($safe_sep|$safe_rep|\ )+//s; 332 347 $name =~ s/($safe_sep|$safe_rep|\ )+$//s; 348 # Remove spaces from titles to simplify unlinking (I'm lazy) 349 my $title = $info{'title'}; 350 $title =~ s/\s/\_/sg; 333 351 # Underscores? 334 352 if ($underscores) { 335 353 $name =~ tr/ /_/s; … … 352 370 $name .= ".$count"; 353 371 } 354 372 $name .= $suffix; 355 # Create the link 356 symlink "$video_dir/".$info{'basename'}, "$dest/$name" 357 or die "Can't create symlink $dest/$name: $!\n"; 358 print "$dest/$name\n"; 373 374 # Sub destination 375 if (defined($sub)) { 376 # Double-check the destination 377 $sub = "$dest/$title"; 378 # Alert the user 379 if ($verbose) { 380 print "Link destination sub-directory: $sub\n"; 381 } 382 # Create nonexistent paths 383 unless (-e $sub) { 384 mkpath($sub, 0, 0755) or die "Failed to create $sub: $!\n"; 385 } 386 # Bad path 387 die "$sub is not a directory.\n" unless (-d $sub); 388 # Delete any old links 389 foreach my $file (<$sub/*>) { 390 if ($verbose) { 391 print "unlinking $file\n"; 392 } 393 next unless (-l $file); 394 unlink $file or die "Couldn't remove old symlink $file: $!\n"; 395 } 396 symlink "$video_dir/".$info{'basename'}, "$sub/$name" 397 or die "Can't create symlink $sub/$name: $!\n"; 398 if ($verbose) { 399 print "$sub/$name\n"; 400 } 401 } else { 402 403 # Create the link 404 symlink "$video_dir/".$info{'basename'}, "$dest/$name" 405 or die "Can't create symlink $dest/$name: $!\n"; 406 if ($verbose) { 407 print "$dest/$name\n"; 408 } 409 } 359 410 } 360 411 # Rename the file 361 412 else { … … 378 429 $rows = $sh2->execute($info{'basename'}, $info{'chanid'}, $info{'starttime'}); 379 430 die "Couldn't restore original basename in database for ".$info{'basename'}.": ($q2)\n" unless ($rows == 1); 380 431 } 381 print $info{'basename'}."\t-> $name\n"; 432 if ($verbose) { 433 print $info{'basename'}."\t-> $name\n"; 434 } 382 435 } 383 436 } 384 437 }