Ticket #874: mythrename-sub.diff
File mythrename-sub.diff, 5.5 KB (added by , 18 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, $live );25 our ($dest, $format, $usage, $underscores, $live, $sub); 26 26 our ($dformat, $dseparator, $dreplacement, $separator, $replacement); 27 27 our ($db_host, $db_user, $db_name, $db_pass, $video_dir, $verbose); 28 28 our ($hostname, $dbh, $sh, $sh2, $q, $q2, $count); … … 44 44 'format=s' => \$format, 45 45 'live' => \$live, 46 46 'separator=s' => \$separator, 47 'sub' => \$sub, 47 48 'replacement=s' => \$replacement, 48 49 'usage|help|h' => \$usage, 49 50 'underscores' => \$underscores, … … 66 67 67 68 /var/video/show_names/ 68 69 70 --sub 71 72 Only takes effect with --link option. If you would like to make subdirectories 73 per title, specify --sub. In the future you will be able to pick the field 74 to sub on. Any titles with spaces in the name will have the spaces changed to 75 underscores to simplify deleting. 76 77 default: off 78 69 79 --live 70 80 71 81 Include live tv recordings, affects both linking and renaming. This assumes 72 that recgroup = "LiveTV" for live tv recordings. 82 that recgroup = "LiveTV" for live tv recordings. Take care that you include 83 the time in your format if you channel surf alot, otherwise you may get 84 errors when it tries to link to the same file twice. 73 85 74 86 default: do not link/rename live tv recordings 75 87 … … 231 243 $dest ||= "$video_dir/show_names"; 232 244 # Alert the user 233 245 if (defined($verbose)) { 246 print "------------------------------------------------------------\n"; 234 247 print "Link destination directory: $dest\n"; 248 print "------------------------------------------------------------\n"; 235 249 } 236 250 # Create nonexistent paths 237 251 unless (-e $dest) { … … 240 254 # Bad path 241 255 die "$dest is not a directory.\n" unless (-d $dest); 242 256 # Delete any old links 243 foreach my $file (<$dest/*>) { 244 next unless (-l $file); 245 unlink $file or die "Couldn't remove old symlink $file: $!\n"; 257 foreach my $deldir (<$dest/*>) { 258 if (-d $deldir) { 259 if (defined($verbose)) { 260 print "Deleting subdirectory $deldir\n"; 261 } 262 rmtree($deldir); 263 } 264 foreach my $delfile (<$deldir/*>) { 265 if (-l $delfile) { 266 if (defined($verbose)) { 267 print "Removing symlink $delfile\n"; 268 } 269 unlink $delfile or die "Couldn't remove old symlink $delfile: $!\n"; 270 } 271 } 246 272 } 273 if (defined($verbose)) { 274 print "------------------------------------------------------------\n"; 275 } 247 276 } 248 277 249 278 # Prepare a database queries … … 352 381 $name =~ s/(?:(?:$safe_sep)+\s*)+(?=[^\d\s])/$separator /sg; 353 382 $name =~ s/^($safe_sep|$safe_rep|\ )+//s; 354 383 $name =~ s/($safe_sep|$safe_rep|\ )+$//s; 384 if (defined($sub)) { 385 # Remove spaces from titles to simplify unlinking (I'm lazy) 386 $title = $fields{'T'}; 387 $title =~ s/\s/\_/sg; 388 } 355 389 # Underscores? 356 390 if ($underscores) { 357 391 $name =~ tr/ /_/s; … … 374 408 $name .= ".$count"; 375 409 } 376 410 $name .= $suffix; 377 # Create the link 378 symlink "$video_dir/".$info{'basename'}, "$dest/$name" 379 or die "Can't create symlink $dest/$name: $!\n"; 380 if (defined($verbose)) { 381 print "$dest/$name\n"; 411 if (defined($verbose)) { 412 print "Trying to link $dest/$name\n"; 413 } 414 # Sub destination 415 if (defined($sub)) { 416 # Double-check the destination 417 my $subdir = "$dest/$title"; 418 # Alert the user 419 if (defined($verbose)) { 420 print "Link destination sub-directory: $subdir\n"; 421 } 422 # Create nonexistent paths 423 unless (-e $subdir) { 424 mkpath($subdir, 0, 0755) or die "Failed to create $subdir: $!\n"; 425 } 426 # Bad path 427 die "$subdir is not a directory.\n" unless (-d $subdir); 428 symlink "$video_dir/".$info{'basename'}, "$subdir/$name" 429 or die "Can't create symlink $subdir/$name: $!\n"; 430 if (defined($verbose)) { 431 print "Created link $subdir/$name\n"; 432 } 433 } else { 434 435 # Create the link 436 symlink "$video_dir/".$info{'basename'}, "$dest/$name" 437 or die "Can't create symlink $dest/$name: $!\n"; 438 if (defined($verbose)) { 439 print "$dest/$name\n"; 440 } 382 441 } 383 442 } 384 443 # Rename the file … … 407 466 } 408 467 } 409 468 } 469 if (defined($verbose)) { 470 print "------------------------------------------------------------\n"; 471 } 410 472 } 411 473 412 474 $sh->finish;