]> git.mjollnir.org Git - scripts.git/commitdiff
hacky perl script to rename all the crappy media that comes in
authorPenny Leach <penny@mjollnir.org>
Tue, 26 Jan 2010 04:03:53 +0000 (17:03 +1300)
committerPenny Leach <penny@mjollnir.org>
Tue, 26 Jan 2010 04:03:53 +0000 (17:03 +1300)
bin/renametv.pl [new file with mode: 0755]

diff --git a/bin/renametv.pl b/bin/renametv.pl
new file mode 100755 (executable)
index 0000000..fb336c9
--- /dev/null
@@ -0,0 +1,72 @@
+#!/usr/bin/perl -w
+
+use strict;
+use warnings;
+use Getopt::Long;
+use File::Copy;
+use Cwd;
+
+my $filenames = {};
+my $inputdir = getcwd;
+my $somethingtodo = 0;
+
+my @usualsuspects = qw/
+    xvid xor ffndvd repack preair aaf 720p x264 dimension dvdrip
+    hdtv 0tv fqm 2hd notv ws pdtv lol vtv xii dvdscr sys fov dot
+    vostfr p0w4
+/;
+
+GetOptions(
+    'inputdir=s', \$inputdir,
+);
+
+die "Target directory $inputdir was non empty" unless (<$inputdir/*>);
+
+while (<$inputdir/*>) {
+    my $current = $_;
+    tr/[A-Z]/[a-z]/;
+    s/s(\d+)\.?e(\d+)/$1$2/g;
+    s/ - /./g;
+    s/ /_/g;
+    s/'//g;
+    s/-/_/g;
+    s/[\[\]]//g;
+    s/[S|s]?(\d+)\.?[E|e|x|X]?(\d+)/$1$2/;
+    s/\.0/./g;
+    foreach my $bad (@usualsuspects) {
+        s/(\[|\.|_|-)?$bad(\]|-)?//g;
+    }
+    if (/^(.*\/(?!.*\/))([a-z_.]*)((?:_|\.)[0-9]*)(?:_|\.)+([a-z_.,&]*)$/) {
+        my ($path, $series, $epno, $title) = ($1, $2, $3, $4);
+        $series =~ s/(_|\.)//g;
+        $epno =~ s/.(.*)/$1/g;
+        $title =~ s/\./_/g;
+        $title =~ s/_(avi|mkv|mov|srt)/.$1/;
+        if ($epno =~ /^\d$/) {
+            $epno = "0$epno";
+        }
+        $_ = $path . $series . '.' . $epno . '.' . $title;
+    }
+    next if $current eq $_;
+    $filenames->{$current} = $_;
+    print "$current becomes $_\n";
+    $somethingtodo = 1;
+}
+
+unless ($somethingtodo) { print "Nothing to do!\n"; exit 0; }
+
+print "do you want to continue? [Y/n]";
+my $answer = <STDIN>;
+
+chomp $answer;
+
+if ($answer eq 'Y' || $answer eq 'y' || $answer eq '') {
+    print 'Moving ...';
+    foreach my $key (keys %$filenames) {
+        move($key, $filenames->{$key});
+    }
+    print "Done\n";
+}
+
+exit 0;
+