TV/Movie RAR Sorter

By zenx on Feb 07, 2013

Goes through a folder with scene releases in it and then checks which ones are okay to unrar and then unrars these releases, adds the release to log so it doesnt unrar it again and renames the unrared file to proper release name, useful for those of you who create "home videos" in scene release style (=D)

#!/usr/bin/perl

my $unrarfolder = "/home/me/unrared/"; # Where to unrar
my $downloadfolder = "/home/me/Downloads/"; # Where scene releases are
# ex. 
# Conan.2013.02.04.William.H.Macy.720p.HDTV.x264-BAJSKORV
# Mr.D.S02E05.720p.HDTV.x264-2HD etc..

my $runfile = "/home/me/.unrar"; # Running/not file.
my $unrardb = "/home/me/unrar.db"; # unrardb file

chdir("$downloadfolder");

$SIG{'INT'} = "Handler";
sub Handler {
    open (MYFILE3, "$runfile");
    print MYFILE3 "stopped";
    close (MYFILE3);
    system("killall unrar");
    exit 0;
}

sub addtolog {
    open (udb, ">>$unrardb");
    print udb "$_[0]\n";
    close(udb);
}

if ( ! -e "$runfile" ) {
    open (MYFILE3, ">$runfile");
    print MYFILE3 "stopped";
    close (MYFILE3);
}

open (MYFILE, "$runfile"); 
while (<MYFILE>) { 
    chomp;
    if ($_ =~ /running/) { print "Already running, exiting.."; exit(); }
}
close (MYFILE); 

open (MYFILE3, ">$runfile");
print MYFILE3 "running";
close (MYFILE3);

@unrardb = split("\n", `cat "$unrardb"`);
@find = split("\n", `find */`);
foreach $n (@find) {
    $nskip = 0;
    foreach $x (@unrardb) { if ($x eq $n) { $nskip = 1; } }
    if (($n =~ /.*(\.rar$)/i) && ($n !~ /subs/i) && ($nskip != 1)) {
        if (($n =~ /xvid/i) || ($n =~ /x264/i) || ($n =~ /dvdr[-]/i)) {
            if ($n =~ /.*\.part[0-9]+\.rar/) {
                if (($n =~ /\.part001\.rar/) || ($n =~ /\.part01\.rar/) || ($n =~ /\.part1\.rar/)) {
                    undef @slash; undef $filename; undef $cdc;
                    @slash = split("/", $n);
                    $filename = `unrar lb "$n" |tr -d '\n'`;
                    if ($filename =~ /.*\.mkv/i) { $ext = "mkv"; } elsif ($filename =~ /.*\.mp4/i) { $ext = "mp4"; } elsif ($filename =~ /.*\.avi/i) { $ext = "avi"; }
                    if ($n =~ /\/(CD[0-9]+)\//i) {
                        $cdc = $1;
                        if ( ! -e "$unrarfolder/@slash[-3].$cdc.$ext") {
                            system("unrar -idcdpq -y e $n $unrarfolder/ ; mv $unrarfolder/$filename $unrarfolder/tmp ; mv $unrarfolder/tmp $unrarfolder/@slash[-3].$cdc.$ext");
                            addtolog("$n");
                            print @slash[-3].".".$cdc.".".$ext."\n";
                        }
                    }
                    if ( ! -e "$unrarfolder/@slash[-2].$ext") {
                        if ( ! $cdc ) {
                            system("unrar -idcdpq -y e $n $unrarfolder/ ; mv $unrarfolder/$filename $unrarfolder/tmp ; mv $unrarfolder/tmp $unrarfolder/@slash[-2].$ext");
                                        addtolog("$n");
                            print @slash[-2].".".$ext."\n";
                        }
                    }
                }
            } else {
                undef @slash; undef $filename; undef $cdc;
                @slash = split("/", $n);
                $filename = `unrar lb "$n" |tr -d '\n'`;
                if ($filename =~ /.*\.mkv/i) { $ext = "mkv"; } elsif ($filename =~ /.*\.mp4/i) { $ext = "mp4"; } elsif ($filename =~ /.*\.avi/i) { $ext = "avi"; }

                if ($n =~ /\/(CD[0-9]+)\//i) {
                    $cdc = $1;
                    if ( ! -e "$unrarfolder/@slash[-3].$cdc.$ext") {
                        system("unrar -idcdpq -y e $n $unrarfolder/ ; mv $unrarfolder/$filename $unrarfolder/tmp ; mv $unrarfolder/tmp $unrarfolder/@slash[-3].$cdc.$ext");
                                    addtolog("$n");
                        print @slash[-3].".".$cdc.".".$ext."\n";
                    }
                }
                if ( ! -e "$unrarfolder/@slash[-2].$ext") {
                    if ( ! $cdc ) {
                        system("unrar -idcdpq -y e $n $unrarfolder ; mv $unrarfolder/$filename $unrarfolder/tmp ; mv $unrarfolder/tmp $unrarfolder/@slash[-2].$ext");
                                    addtolog("$n");
                        print @slash[-2].$cdc.".".$ext."\n";
                    }
                }
            }
        }
    }
}

open (MYF, ">$runfile");
print MYF "stopped";
close(MYF);

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.