Răsfoiți Sursa

Fold the search-by-date feature into the main findTorrent.pl script.
Fold the relative/raw/season path resolution into the main findTorrent.pl script.
Refactor other scripts to accept these changes.

Zachary Kotlarek 13 ani în urmă
părinte
comite
d286bfc5e8
4 a modificat fișierele cu 157 adăugiri și 154 ștergeri
  1. 3 1
      findAllTorrents.sh
  2. 0 46
      findDate.sh
  3. 153 41
      findTorrent.pl
  4. 1 66
      findTorrent.sh

+ 3 - 1
findAllTorrents.sh

@@ -37,7 +37,9 @@ while read -d $'\0' SERIES ; do
 	fi
 	fi
 
 
 	# Actual search, with timeout to ensure we don't get stuck
 	# Actual search, with timeout to ensure we don't get stuck
-	SILENT=1 ~/bin/video/timeout -t 300 ~/bin/video/findTorrent.sh "${SERIES}"
+	~/bin/video/timeout -t 300 \
+		~/bin/video/findTorrent.pl "${SERIES}" \
+		| ~/bin/download
 
 
 	# Print the series name on error
 	# Print the series name on error
 	if [ $? -ne 0 ]; then
 	if [ $? -ne 0 ]; then

+ 0 - 46
findDate.sh

@@ -1,46 +0,0 @@
-#!/bin/bash
-set -e
-
-# Parameters
-TV_DIR="`~/bin/video/mediaPath`/TV";
-SERIES="${1}"
-SEARCH="${2}"
-DAYS_BACK="${3}"
-MAX_DAYS_BACK="${4}"
-
-# Usage
-if [ -z "${SERIES}" ] || [ -z "${SEARCH}" ] || [ -z "${DAYS_BACK}" ] || \
-	! echo "${DAYS_BACK}" | grep -qE '^[0-9]*$' || [ $DAYS_BACK -lt 0 ] || [ $DAYS_BACK -gt 90 ]; then
-		echo "Usage: `basename "${0}" series search days_back [max_days_back]`" 1>&2
-		exit 1
-fi
-
-# Allow use in recursive mode
-if [ -n "${MAX_DAYS_BACK}" ]; then
-	if ! echo "${MAX_DAYS_BACK}" | grep -qE '^[0-9]*$' || [ $MAX_DAYS_BACK -lt $DAYS_BACK ] || [ $MAX_DAYS_BACK -gt 90 ]; then
-		echo "Usage: `basename "${0}" series search days_back [max_days_back]`" 1>&2
-		exit 1
-	fi
-	while [ $DAYS_BACK -le $MAX_DAYS_BACK ]; do
-		"${0}" "${SERIES}" "${SEARCH}" "${DAYS_BACK}"
-		DAYS_BACK=$(( $DAYS_BACK + 1 ))
-	done
-	exit 0
-fi
-
-# Calculate some dates
-YEAR="`date -v-${DAYS_BACK}d '+%Y'`"
-MONTH="`date -v-${DAYS_BACK}d '+%m'`"
-DAY="`date -v-${DAYS_BACK}d '+%d'`"
-
-# Bail if we already have a matcing file
-FILES="`find "${TV_DIR}/${SERIES}/Season ${YEAR}" -type f -not -name '*.nfo' -not -name '*.tbn' -name "${YEAR}-${MONTH}-${DAY} - *"`"
-if [ -n "${FILES}" ]; then
-	exit 0
-fi
-
-# Otherwise run a search
-SEARCH="`echo "${SEARCH}" | sed "s/%Y/${YEAR}/g"`"
-SEARCH="`echo "${SEARCH}" | sed "s/%m/${MONTH}/g"`"
-SEARCH="`echo "${SEARCH}" | sed "s/%d/${DAY}/g"`"
-~/bin/video/findTorrent.pl "${SERIES}" "${SEARCH}"

+ 153 - 41
findTorrent.pl

@@ -2,13 +2,37 @@
 use strict;
 use strict;
 use warnings;
 use warnings;
 
 
+# Defaults
 my $DEBUG               = 0;
 my $DEBUG               = 0;
 my $NO_QUALITY_CHECKS   = 0;
 my $NO_QUALITY_CHECKS   = 0;
 my $MORE_NUMBER_FORMATS = 0;
 my $MORE_NUMBER_FORMATS = 0;
+my $MIN_DAYS_BACK       = 0;
+my $MAX_DAYS_BACK       = 3;
+
+# Local config
+my $TV_DIR = `~/bin/video/mediaPath` . '/TV';
+
+# Search parameters
+my $PROTOCOL = 'https';
+my %ENABLE_SOURCE = ('TPB' => 1, 'ISO' => 0);
+
+# Selection parameters
+my $MIN_COUNT        = 10;
+my $MIN_SIZE         = 100;
+my $SIZE_BONUS       = 7;
+my $SIZE_PENALTY     = $SIZE_BONUS;
+my $MAX_SEED_RATIO   = .25;
+my $SEED_RATIO_COUNT = 10;
+
+# App config
+my $DELAY   = 4;
+my $TIMEOUT = 15;
+my $UA      = 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1';
 
 
 # Includes
 # Includes
 use JSON;
 use JSON;
 use File::Temp;
 use File::Temp;
+use File::Spec;
 use File::Basename;
 use File::Basename;
 use FindBin qw($Bin);
 use FindBin qw($Bin);
 use lib $Bin;
 use lib $Bin;
@@ -19,7 +43,7 @@ sub findSE($);
 
 
 # Command line
 # Command line
 my ($dir, $search) = @ARGV;
 my ($dir, $search) = @ARGV;
-if (!defined($dir) || (!defined($search) && !-d $dir)) {
+if (!defined($dir)) {
 	die('Usage: ' . basename($0) . " input_directory [search_string]\n");
 	die('Usage: ' . basename($0) . " input_directory [search_string]\n");
 }
 }
 if ($ENV{'DEBUG'}) {
 if ($ENV{'DEBUG'}) {
@@ -31,19 +55,12 @@ if ($ENV{'NO_QUALITY_CHECKS'}) {
 if ($ENV{'MORE_NUMBER_FORMATS'}) {
 if ($ENV{'MORE_NUMBER_FORMATS'}) {
 	$MORE_NUMBER_FORMATS = 1;
 	$MORE_NUMBER_FORMATS = 1;
 }
 }
-
-# Parameters
-my $DELAY            = 4;
-my $MIN_COUNT        = 10;
-my $MIN_SIZE         = 100;
-my $SIZE_BONUS       = 7;
-my $SIZE_PENALTY     = $SIZE_BONUS;
-my $MAX_SEED_RATIO   = .25;
-my $SEED_RATIO_COUNT = 10;
-my $PROTOCOL         = 'https';
-my $UA               = 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1';
-my $TIMEOUT          = 15;
-my %ENABLE_SOURCE    = ('TPB' => 1, 'ISO' => 0);
+if ($ENV{'MIN_DAYS_BACK'}) {
+	$MIN_DAYS_BACK = 1;
+}
+if ($ENV{'MAX_DAYS_BACK'}) {
+	$MAX_DAYS_BACK = 1;
+}
 
 
 # New fetch object
 # New fetch object
 my $cookies = mktemp('/tmp/findTorrent.cookies.XXXXXXXX');
 my $cookies = mktemp('/tmp/findTorrent.cookies.XXXXXXXX');
@@ -130,7 +147,62 @@ my $CUSTOM_SEARCH = 0;
 my $season        = 0;
 my $season        = 0;
 my @need          = ();
 my @need          = ();
 my %need          = ();
 my %need          = ();
-if (defined($search) && length($search) > 0) {
+
+# Clean up the input "directory" path
+{
+	# Allow use of the raw series name
+	if (!($dir =~ /\//)) {
+		$show = $dir;
+		$dir  = $TV_DIR . '/' . $dir;
+	}
+
+	# Allow use of relative paths
+	$dir = File::Spec->rel2abs($dir);
+
+	# Sanity check
+	if (!-d $dir) {
+		die('Invalid input directory: ' . $dir . "\n");
+	}
+
+	# Isolate the season from the path, if provided
+	$dir =~ /\/Season\s+(\d+)\/?$/i;
+	if ($1) {
+		$season = $1;
+		$dir    = dirname($dir);
+	}
+
+	# If no season is provided find the latest
+	if (!$season) {
+		opendir(SERIES, $dir)
+		  or die("Unable to open series directory: ${!}\n");
+		while (my $file = readdir(SERIES)) {
+			if ($file =~ /^Season\s+(\d+)$/i) {
+				if (!$season || $season < $1) {
+					$season = $1;
+				}
+			}
+		}
+		closedir(SERIES);
+	}
+
+	# Isolate and clean the series name
+	if (!$show) {
+		$show = basename($dir);
+	}
+	$show =~ s/[\'\"\.]//g;
+
+	if ($DEBUG) {
+		print STDERR 'Checking directory: ' . $dir . "\n";
+	}
+}
+
+# Allow quality checks to be disabled
+if (-e dirname($dir) . '/no_quality_checks') {
+	$NO_QUALITY_CHECKS = 1;
+}
+
+# Handle custom searches
+if ((scalar(@urls) < 1) && defined($search) && length($search) > 0) {
 
 
 	# Note the custom search string
 	# Note the custom search string
 	$CUSTOM_SEARCH = 1;
 	$CUSTOM_SEARCH = 1;
@@ -143,31 +215,81 @@ if (defined($search) && length($search) > 0) {
 		my $source = $SOURCES{$key};
 		my $source = $SOURCES{$key};
 		push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search . $source->{'search_suffix'});
 		push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search . $source->{'search_suffix'});
 	}
 	}
+}
 
 
-	# Get and clean the show name (for post-search matching)
-	$show = $dir;
-	$show =~ s/[\'\"]//g;
-} else {
+# Handle search-by-date series
+if ((scalar(@urls) < 1) && -e $dir . '/search_by_date') {
 
 
-	# Read the input directory
+	# Note the search-by-date status
+	# Set the CUSTOM_SERACH flag to skip season/epsiode matching
+	$CUSTOM_SEARCH = 1;
 	if ($DEBUG) {
 	if ($DEBUG) {
-		print STDERR 'Checking directory: ' . $dir . "\n";
+		print STDERR "Find by date\n";
+	}
+
+	# Read the find-by-date string
+	my $search_by_date = '';
+	local ($/, *FH);
+	open(FH, $dir . '/search_by_date')
+	  or die('Unable to read search_by_date for show: ' . $show . ': ' . $! . "\n");
+	my $text = <FH>;
+	close(FH);
+	if ($text =~ /^\s*(\S.*\S)\s*$/) {
+		$search_by_date = $1;
+	} else {
+		die('Skipping invalid search_by_date for show: ' . $show . ': ' . $text . "\n");
 	}
 	}
 
 
-	# Allow quality checks to be disabled
-	if (-e dirname($dir) . '/no_quality_checks') {
-		$NO_QUALITY_CHECKS = 1;
+	# Create search strings for each date in the range, unless the related file already exists
+	for (my $days_back = $MIN_DAYS_BACK ; $days_back <= $MAX_DAYS_BACK ; $days_back++) {
+
+		# Calculate the date
+		my (undef(), undef(), undef(), $day, $month, $year) = localtime(time() - (86400 * $days_back));
+
+		# Format as strings
+		$year  = sprintf('%04d', $year + 1900);
+		$month = sprintf('%02d', $month + 1);
+		$day   = sprintf('%02d', $day);
+
+		# Check for an existing file
+		my $exists = 0;
+		{
+			my $season_dir = $dir . '/Season ' . $year;
+			my $prefix     = qr/${year}\-${month}\-${day}\s*\-\s*/;
+			opendir(SEASON, $season_dir)
+			  or die("Unable to open season directory: ${!}\n");
+			while (my $file = readdir(SEASON)) {
+				if ($file =~ $prefix) {
+					$exists = 1;
+					last;
+				}
+			}
+			closedir(SEASON);
+		}
+		if ($exists) {
+			next;
+		}
+
+		# Create the relevent search strings
+		my $search_str = $search_by_date;
+		$search_str =~ s/%Y/${year}/g;
+		$search_str =~ s/%m/${month}/g;
+		$search_str =~ s/%d/${day}/g;
+		foreach my $key (keys(%SOURCES)) {
+			my $source = $SOURCES{$key};
+			push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search_str . $source->{'search_suffix'});
+		}
 	}
 	}
+}
+
+# Handle standard series
+if (scalar(@urls) < 1) {
 
 
 	# Allow use of more number formats
 	# Allow use of more number formats
 	if (-e dirname($dir) . '/more_number_formats') {
 	if (-e dirname($dir) . '/more_number_formats') {
 		$MORE_NUMBER_FORMATS = 1;
 		$MORE_NUMBER_FORMATS = 1;
 	}
 	}
 
 
-	# Get and clean the show name
-	$show = basename(dirname($dir));
-	$show =~ s/[\'\"\.]//g;
-
 	# Allow the show name to be overriden
 	# Allow the show name to be overriden
 	{
 	{
 		my $search_name = dirname($dir) . '/search_name';
 		my $search_name = dirname($dir) . '/search_name';
@@ -188,26 +310,16 @@ if (defined($search) && length($search) > 0) {
 		print STDERR 'Searching with series title: ' . $show . "\n";
 		print STDERR 'Searching with series title: ' . $show . "\n";
 	}
 	}
 
 
-	# Get the season number
-	my $name = basename($dir);
-	if ($name =~ /^\s*Season\s+(\d+)\s*$/i) {
-		$season = $1;
-	}
+	# Validate the season number
 	if (!defined($season) || $season < 1 || $season > 2000) {
 	if (!defined($season) || $season < 1 || $season > 2000) {
-		if (defined($season)) {
-			if ($DEBUG) {
-				print STDERR 'Skipping season ' . $season . "\n";
-			}
-			exit 0;
-		}
-		die('Invalid season number: ' . $show . ' => ' . $name . "\n");
+		die('Invalid season number: ' . $show . ' => ' . $season . "\n");
 	}
 	}
 
 
 	# Get the last episode number
 	# Get the last episode number
 	my $no_next  = 0;
 	my $no_next  = 0;
 	my %episodes = ();
 	my %episodes = ();
 	my $highest  = 0;
 	my $highest  = 0;
-	opendir(SEASON, $dir)
+	opendir(SEASON, $dir . '/Season ' . $season)
 	  or die("Unable to open season directory: ${!}\n");
 	  or die("Unable to open season directory: ${!}\n");
 	while (my $file = readdir(SEASON)) {
 	while (my $file = readdir(SEASON)) {
 
 

+ 1 - 66
findTorrent.sh

@@ -1,68 +1,3 @@
 #!/bin/bash
 #!/bin/bash
 
 
-BASE_DIR="`~/bin/video/mediaPath`/TV"
-
-SERIES="${1}"
-SEASON="${2}"
-
-usage() {
-	echo "Usage: `basename "${0}"` series_name [season]" 1>&2
-	exit 1
-}
-
-# Resolve relative paths
-if echo "${SERIES}" | grep -qE "\.\.?/"; then
-	SERIES="`cd "${SERIES}" && pwd`"
-fi
-
-# Split the season directory, if provided
-if echo "${SERIES}" | grep -qE '\/Season [0-9]*\/?$'; then
-		SEASON="`basename "${SERIES}" | sed 's%^Season %%'`"
-		SERIES="`dirname "${SERIES}"`"
-fi
-
-# Construct the series directory path
-if echo "${SERIES}" | grep -q "/"; then
-	SERIES_DIR="${SERIES}"
-else
-	SERIES_DIR="${BASE_DIR}/${SERIES}"
-fi
-
-# Standardize the series name
-SERIES="`basename "${SERIES_DIR}"`"
-
-# Sanity check
-if [ ! -d "${SERIES_DIR}" ]; then
-	echo "No such series directory: ${SERIES_DIR}" 1>&2
-	usage
-fi
-
-# Find the season directory -- if no season is provided, use the last season in the series directory
-if [ -z "${SEASON}" ]; then
-	SEASON="`ls "${SERIES_DIR}" | awk '$1 == "Season" && $2 ~ "[0-9]*" {print $2}' | sort -n -r | head -n 1`"
-	SEASON=$(( $SEASON + 0 ))
-fi
-SEASON_DIR="${SERIES_DIR}/Season ${SEASON}"
-
-# Sanity check
-if [ ! -d "${SEASON_DIR}" ]; then
-	echo "No such season directory: ${SEASON_DIR}" 1>&2
-	usage
-fi
-
-# Enable DEBUG by default
-DEBUG=1
-if [ -n "${SILENT}" ]; then
-	DEBUG=0
-fi
-export DEBUG
-
-# Special handling for search-by-date folders
-if [ -r "${SERIES_DIR}/search_by_date" ]; then
-	SEARCH_STR=`cat "${SERIES_DIR}/search_by_date"`
-	~/bin/video/findDate.sh "${SERIES}" "${SEARCH_STR}" 0 3 | download
-	exit $?
-fi
-
-# Run the standard command, in debug mode
-~/bin/video/findTorrent.pl "${SEASON_DIR}" | download
+DEBUG=1 ~/bin/video/findTorrent.pl "${1}" | download