Răsfoiți Sursa

Search recursively for media files. Refactor the readDir function for more general use.

Zachary Kotlarek 13 ani în urmă
părinte
comite
2ec062812b
1 a modificat fișierele cu 66 adăugiri și 45 ștergeri
  1. 66 45
      transmission.pl

+ 66 - 45
transmission.pl

@@ -23,18 +23,20 @@ sub unrar($$);
 sub localSeriesCleanup($);
 sub seriesCleanup($);
 sub readDir($$);
+sub findMediaFiles($$);
 sub runAndCheck(@);
 
 # Parameters
-my $maxAge        = 2.5 * 86400;
-my $tvDir         = `~/bin/video/mediaPath` . '/TV';
-my $monitoredExec = '/Users/profplump/bin/video/torrentMonitored.pl';
-my $host          = 'http://localhost:9091';
-my $url           = $host . '/transmission/rpc';
-my $content       = '{"method":"torrent-get","arguments":{"fields":["hashString","id","addedDate","comment","creator","dateCreated","isPrivate","name","totalSize","pieceCount","pieceSize","downloadedEver","error","errorString","eta","haveUnchecked","haveValid","leftUntilDone","metadataPercentComplete","peersConnected","peersGettingFromUs","peersSendingToUs","rateDownload","rateUpload","recheckProgress","sizeWhenDone","status","trackerStats","uploadedEver","uploadRatio","seedRatioLimit","seedRatioMode","downloadDir","files","fileStats"]}}';
-my $delContent    = '{"method":"torrent-remove","arguments":{"ids":["#_ID_#"], "delete-local-data":"true"}';
-my $delSleep      = 10;
-my $RAR_MIN_FILES = 4;
+my $maxAge         = 2.5 * 86400;
+my $tvDir          = `~/bin/video/mediaPath` . '/TV';
+my $monitoredExec  = '/Users/profplump/bin/video/torrentMonitored.pl';
+my $host           = 'http://localhost:9091';
+my $url            = $host . '/transmission/rpc';
+my $content        = '{"method":"torrent-get","arguments":{"fields":["hashString","id","addedDate","comment","creator","dateCreated","isPrivate","name","totalSize","pieceCount","pieceSize","downloadedEver","error","errorString","eta","haveUnchecked","haveValid","leftUntilDone","metadataPercentComplete","peersConnected","peersGettingFromUs","peersSendingToUs","rateDownload","rateUpload","recheckProgress","sizeWhenDone","status","trackerStats","uploadedEver","uploadRatio","seedRatioLimit","seedRatioMode","downloadDir","files","fileStats"]}}';
+my $delContent     = '{"method":"torrent-remove","arguments":{"ids":["#_ID_#"], "delete-local-data":"true"}';
+my $delSleep       = 10;
+my $RAR_MIN_FILES  = 4;
+my $FIND_MAX_DEPTH = 5;
 
 # Debug
 my $DEBUG = 0;
@@ -145,14 +147,8 @@ foreach my $tor (@{$torrents}) {
 			@newFiles = &unrar($IS_RAR, $path);
 		}
 
-		# Find all media files
-		my @tmpFiles = readDir($path, '/\.(?:avi|mkv|m4v|mov|mp4|ts|wmv)$/i');
-		foreach my $file (@tmpFiles) {
-			if ($file =~ /sample/i) {
-				next;
-			}
-			push(@files, $file);
-		}
+		# Find all media files, recursively
+		@files = findMediaFiles($path, 0);
 	}
 
 	# Bail if we found no files
@@ -280,6 +276,9 @@ sub getDest($$$$) {
 		print STDERR 'Finding destintion for: ' . $series . ' S' . $season . 'E' . $episode . "\n";
 	}
 
+	# Input cleanup
+	$episode =~ s/^0+//;
+
 	# Find all our existing TV series
 	my @shows    = ();
 	my %showsCan = ();
@@ -382,12 +381,13 @@ sub getDest($$$$) {
 	}
 
 	# Bail if we already have this episode
-	my @episodes = readDir($seasonDir, '/^\s*[\d\-]+\s*\-\s*/');
+	my @episodes = readDir($seasonDir, 0);
 	foreach my $ep (@episodes) {
-		$ep = basename($ep);
 		my ($epNum) = $ep =~ /^\s*([\d\-]+)\s*\-\s*/;
-		$epNum   =~ s/^0+//;
-		$episode =~ s/^0+//;
+		if (!defined($epNum)) {
+			next;
+		}
+		$epNum =~ s/^0+//;
 		if ($epNum eq $episode) {
 			print STDERR 'Existing episode for: ' . $series . ' S' . $season . 'E' . $episode . "\n";
 			return;
@@ -561,7 +561,7 @@ sub unrar($$) {
 	}
 
 	# Keep a list of old files, so we can find/delete the output
-	my @beforeFiles = readDir($path, undef());
+	my @beforeFiles = readDir($path, 1);
 
 	# Run the unrar utility
 	my $pid = fork();
@@ -583,7 +583,7 @@ sub unrar($$) {
 
 	# Compare the old file list to the new one
 	my @newFiles = ();
-	my @afterFiles = readDir($path, undef());
+	my @afterFiles = readDir($path, 1);
 	foreach my $file (@afterFiles) {
 		my $found = 0;
 		foreach my $file2 (@beforeFiles) {
@@ -630,13 +630,7 @@ sub seriesCleanup($) {
 }
 
 sub readDir($$) {
-	my ($indir, $regex) = @_;
-
-	# Allow optional use of a matching regex
-	my $useRegex = 1;
-	if (!defined($regex) || length($regex) < 1) {
-		$useRegex = 0;
-	}
+	my ($indir, $long) = @_;
 
 	# Clean up the input directory
 	$indir =~ s/\/+$//;
@@ -649,23 +643,18 @@ sub readDir($$) {
 	foreach my $file (readdir(DIR)) {
 		my $keep = 0;
 
-		# Regex filter (if active)
-		if (!$useRegex) {
-			$keep = 1;
-		} elsif (eval('$file =~ m' . $regex)) {
-			$keep = 1;
-		}
-
-		# Construct a complete file path
-		$file = $indir . '/' . $file;
-
-		# Ignore directories
-		if (-d $file) {
-			$keep = 0;
+		# Skip junk
+		if (   $file eq '.'
+			|| $file eq '..'
+			|| $file =~ /\._/)
+		{
+			next;
 		}
 
-		# Save matching files
-		if ($keep) {
+		# Save the full path
+		if ($long) {
+			push(@files, $indir . '/' . $file);
+		} else {
 			push(@files, $file);
 		}
 	}
@@ -679,6 +668,38 @@ sub readDir($$) {
 	return (@orderedFiles);
 }
 
+sub findMediaFiles($$) {
+	my ($path, $depth) = @_;
+	my @files = ();
+
+	# Recursion limit
+	if ($depth > $FIND_MAX_DEPTH) {
+		return ();
+	}
+
+	my @tmpFiles = readDir($path, 1);
+	foreach my $file (@tmpFiles) {
+
+		# Skip sample files
+		if ($file =~ /sample/i) {
+			next;
+		}
+
+		# Recurse on directories
+		if (-d $file) {
+			push(@files, findMediaFiles($file, $depth + 1));
+			next;
+		}
+
+		# Save matching files
+		if ($file =~ /\.(?:avi|mkv|m4v|mov|mp4|ts|wmv)$/i) {
+			push(@files, $file);
+			next;
+		}
+	}
+	return @files;
+}
+
 sub runAndCheck(@) {
 	my (@args) = (@_);