Torrent management: main script (process finished torrents.pl): Difference between revisions

From Bitpost wiki
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 10: Line 10:
# Just count the script processes, we only want 1.
# Just count the script processes, we only want 1.
my $runcount = `pgrep -f "bash process_finished_torrents.pl"|wc -l`;
my $runcount = `pgrep -f "bash process_finished_torrents.pl"|wc -l`;
chomp( $runcount );
# Funny perl note - to make sure it sees a var as a number, do math on it!
# Funny perl note - to make sure it sees a var as a number, do math on it!
# Just add zero, haha.  I've also read that you can just chomp it?
chomp( $runcount );
$runcount = $runcount + 0;
$runcount = $runcount + 0;
if ( $runcount > 1 )
if ( $runcount > 1 )
Line 32: Line 31:
# checks should go first!
# checks should go first!
my %titlesearch = (
my %titlesearch = (
     '[ -_\.]earl[ -_\.]' => '.tv/MyNameIsEarl',
     'untu[ -\[\]\(\)\.]' => '.linux/Ubuntu',
    'the[ -_\.]office[ -_\.]' => '.tv/TheOffice',
     'centos[ -\[\]\(\)\.]' => '.linux/CentOS',
    '[ -\[\]\(\)\.]dvdrip[ -\[\]\(\)\.]' => '.movies',
     'your[ -_\.]show[ -_\.]' => '.tv/YourShow',
    '[ -\[\]\(\)\.]dvdscr[ -\[\]\(\)\.]' => '.movies',
     '[.]png$' => '.images',
    '[ -\[\]\(\)\.]bluray[ -\[\]\(\)\.]' => '.movies',
    '^soundtrack' => '.music/Soundtracks',
     '[^a-z]OST[ -\[\]\(\)\.]*$' => '.music/Soundtracks',
     'motion[ -\[\]\(\)\.]picture[ -\[\]\(\)\.]soundtrack' => '.music/Soundtracks',
    '^various' => '.music/Singles',
    '^va[ .-_]' => '.music/Singles',
    '[ -\[\]\(\)\.]mp3[ -\[\]\(\)\.]' => '.music/Albums',
    '[.]avi$' => '.movies',
     '[.]mkv$' => '.movies',
);
);
# Here, we search the files within the top-level dir.
# Here, we search the files within the top-level dir.
Line 50: Line 40:
# NOTE: we haven't needed to implement this yet...
# NOTE: we haven't needed to implement this yet...
my %dirsearch = (
my %dirsearch = (
     '[.]mp3$' => '.music/Albums',
     '[.]png$' => '.images',
);
);
# ======== SEARCH EXPRESSIONS end =====
# ======== SEARCH EXPRESSIONS end =====

Latest revision as of 14:22, 8 April 2009

#!/usr/bin/perl -w
use strict;
$|++;

use CGI qw(:standard);

my $scriptdir = "/home/user/scripts/torrent";

# If we're already running, get outta Dodge...
# Just count the script processes, we only want 1.
my $runcount = `pgrep -f "bash process_finished_torrents.pl"|wc -l`;
chomp( $runcount );
# Funny perl note - to make sure it sees a var as a number, do math on it!
$runcount = $runcount + 0;
if ( $runcount > 1 )
{
    print "\n[process_finished_torrents.pl] is already running, quitting...\n";
    exit 1;
}

my $mode = param('mode');
if ( !$mode || ( $mode ne "test" && $mode ne "auto" ) )
{
    print "\n  Usage: process_finished_torrents.pl mode=[auto|test]\n\n";
    exit 2;
}

# ======== SEARCH EXPRESSIONS =========
# Here, we check the top-level file or dir name in [finished],
# as well as the torrent name.  First hit wins, so more specific/exact
# checks should go first!
my %titlesearch = (
    'untu[ -\[\]\(\)\.]' => '.linux/Ubuntu',
    'centos[ -\[\]\(\)\.]' => '.linux/CentOS',
    'your[ -_\.]show[ -_\.]' => '.tv/YourShow',
    '[.]png$' => '.images',
);
# Here, we search the files within the top-level dir.
# We do this only if all titlesearches fail.  First hit wins.
# NOTE: we haven't needed to implement this yet...
my %dirsearch = (
    '[.]png$' => '.images',
);
# ======== SEARCH EXPRESSIONS end =====


# Loop through all "finished" directories.
my @finished = `alias ls=ls && cd ~/download/torrents/finished && ls -1 2>/dev/null`;
my @alltorrents = `cd ~/download/torrents && ls -1 *.torrent 2>/dev/null`;
my $count = 0;
foreach ( @finished )
{
    my $fin = $_;
    chomp $fin;

    # Make a nice count variable for printing.
    my $countprint = sprintf( "%3d", $count );

    # Loop through torrents to find $finished...
    my $found_torrent = '';
    my $torcount = 0;
    foreach ( @alltorrents )
    {
        # print "$_";
        my $tor = $_;
        chomp $tor;
        my $torfile = `cd ~/download/torrents && php "$scriptdir/extract_torrent_dir.php" "$tor"`;
        #print $torfile;
        if ( $torfile eq $fin )
        {
            $found_torrent = $tor;

            # Might as well truncate the torrent list as we go,
            # for better performance when there are lots of torrents...
            splice(@alltorrents,$torcount,1);

            last;
        }
        $torcount++;
    }

    if ( $found_torrent eq '' )
    {
        print "$countprint* No torrent found for $fin!\n";

    } else
    {
        # Make a guess as to what to do.
        # Put the result in the symlink array.
        my $symlink = '';

        # Some search terms are for title, others for contents.
        # Search the title first, it's faster.
        my $searchstr;
        foreach $searchstr ( keys( %titlesearch ))
        {
            # Here's how to loop through a hash and get each key and value.  Whoop!
            # print $searchstr," => ",$titlesearch{$searchstr},"\n";

            if ( $fin =~ m/$searchstr/i || $found_torrent =~ m/$searchstr/i )
            {
                $symlink = $titlesearch{$searchstr};
                last;
            }
        }

        # Haven't needed to go this far yet...
        #if ( $symlink eq '' )
        #{
        #    # Next we'll search within the directory.
        #    foreach $searchstr ( keys( %dirsearch ))
        #    {
        #    }
        #}

        if ( $symlink eq '' )
        {
            print "$countprint* Can't determine type for $fin!\n";

        } else
        {
            my $resultcode = " ";
            if ( $mode eq "test" )
            {
                $resultcode = "T";

            } else
            {
                # Are we ready to move the torrent?
                # Let's make sure the symlink is good, and the destination doesn't already exist...
                # NOTE: stupid perl can't resolve home dirs [~] !?
                # NOTE: $symlink may not be an actual symlink if it points to a dir UNDER a symlink.  Use -e not -h.
                my $symtest = "/home/user/download/torrents/archived/symlinks/$symlink";
                if ( ! -e"$symtest" or -e"$symtest/$fin" )  
                {
                    $resultcode = "* Can't move:";

                } else
                {
                    # Now do the deed...
                    my $scriptresult = `$scriptdir/archive_finished_torrent "$found_torrent" "$fin" $symlink`;

                    # Man there are a lot of perlvars!  See [http://perldoc.perl.org/search.html?q=perlvar].
                    # This one gives us the error code from the last backtick operation.
                    if ( $? )
                    {
                        print "\n\n$scriptresult\n\n";
                        $resultcode = "* Archive script failed:";
                    }
                }
            }

            # Print the result.
            print "$countprint$resultcode $fin | $found_torrent | $symlink\n";
        }
    }
    $count += 1;        
}

exit;