Просмотр исходного кода

Merge branch 'master' of github.com:profplump/UberZach-Video

Zachary Kotlarek 12 лет назад
Родитель
Сommit
5316f9b5b6
1 измененных файлов с 200 добавлено и 173 удалено
  1. 200 173
      findTorrent.pl

+ 200 - 173
findTorrent.pl

@@ -14,7 +14,7 @@ my $NEXT_EPISODES       = 3;
 my $TV_DIR = `~/bin/video/mediaPath` . '/TV';
 
 # Search parameters
-my $PROTOCOL = 'http';
+my $PROTOCOL = 'https';
 my %ENABLE_SOURCE = ('TPB' => 0, 'ISO' => 1, 'KICK' => 1, 'Z' => 1);
 
 # Selection parameters
@@ -25,7 +25,7 @@ my $SIZE_PENALTY     = $SIZE_BONUS;
 my $TITLE_PENALTY    = $SIZE_BONUS / 2;
 my $MAX_SEED_RATIO   = .25;
 my $SEED_RATIO_COUNT = 10;
-my $TRACKER_LOOKUP   = 0;
+my $TRACKER_LOOKUP   = 1;
 
 # App config
 my $DELAY   = 4;
@@ -51,6 +51,7 @@ sub resolveSecondary($);
 sub resolveTrackers($);
 sub splitTags($$$);
 sub findSE($);
+sub initSources();
 
 # Command line
 my ($dir, $search) = @ARGV;
@@ -76,7 +77,7 @@ if (defined($ENV{'NEXT_EPISODES'})) {
 	$NEXT_EPISODES = $ENV{'NEXT_EPISODES'};
 }
 
-# New fetch object
+# Fetch object
 my $cookies = mktemp('/tmp/findTorrent.cookies.XXXXXXXX');
 my $fetch   = Fetch->new(
 	'cookiefile' => $cookies,
@@ -84,151 +85,6 @@ my $fetch   = Fetch->new(
 	'uas'        => $UA
 );
 
-# Search sources
-my %SOURCES = ();
-
-# The Pirate Bay
-if ($ENABLE_SOURCE{'TPB'}) {
-
-	# Available TPB proxies, in order of preference
-	my @TPBs = ('thepiratebay.se/search/', 'pirateproxy.se/search/', 'tpb.unblocked.co/search/');
-
-	# Automatically select a proxy that returns a search page
-	my $host       = '';
-	my $search_url = '';
-	foreach my $url (@TPBs) {
-		($host) = $url =~ /^([^\/]+)/;
-		$fetch->url($PROTOCOL . '://' . $host);
-		if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /\bSearch\b/i) {
-			$search_url = $url;
-			last;
-		} elsif ($DEBUG) {
-			print STDERR 'TPB proxy not available: ' . $host . "\n";
-		}
-	}
-
-	# Only add TPB if one of the proxies is up
-	if ($search_url) {
-		my %tmp = (
-			'host'          => $host,
-			'search_url'    => $search_url,
-			'search_suffix' => '/0/7/0',
-			'weight'        => 1.5,
-			'quote'         => 0
-		);
-		$SOURCES{'TPB'} = \%tmp;
-	}
-}
-
-# ISOhunt
-if ($ENABLE_SOURCE{'ISO'}) {
-
-	# Available ISOhunt proxies, in order of preference
-	my @proxies = ('isohunters.net/torrents/?ihq=', 'isohunt.to/torrents/?ihq=');
-
-	# Automatically select a proxy that returns the non-US page
-	my $host       = '';
-	my $search_url = '';
-	foreach my $url (@proxies) {
-		($host) = $url =~ /^([^\/]+)/;
-		$fetch->url($PROTOCOL . '://' . $host);
-		if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /Last\s+\d+\s+files\s+indexed/i) {
-			$search_url = $url;
-			last;
-		} elsif ($DEBUG) {
-			print STDERR 'ISOhunt proxy not available: ' . $host . "\n";
-		}
-	}
-
-	# Only add ISOhunt if one of the proxies is up
-	if ($search_url) {
-		my %tmp = (
-			'host'          => $host,
-			'search_url'    => $search_url,
-			'search_suffix' => '',
-			'weight'        => 0.30,
-			'quote'         => 1
-		);
-		$SOURCES{'ISO'} = \%tmp;
-	}
-}
-
-# Kickass
-if ($ENABLE_SOURCE{'KICK'}) {
-
-	# Available Kickass proxies, in order of preference
-	my @proxies = ('kickass.so/usearch/');
-
-	# Automatically select a proxy that returns the non-US page
-	my $host       = '';
-	my $search_url = '';
-	foreach my $url (@proxies) {
-		($host) = $url =~ /^([^\/]+)/;
-		$fetch->url($PROTOCOL . '://' . $host);
-		if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /torrent name\<\/th\>/i) {
-			$search_url = $url;
-			last;
-		} elsif ($DEBUG) {
-			print STDERR 'Kickass proxy not available: ' . $host . "\n";
-		}
-	}
-
-	# Only add Kickass if one of the proxies is up
-	if ($search_url) {
-		my %tmp = (
-			'host'          => $host,
-			'search_url'    => $search_url,
-			'search_suffix' => '/',
-			'weight'        => 0.30,
-			'quote'         => 1
-		);
-		$SOURCES{'KICK'} = \%tmp;
-	}
-}
-
-# Torrentz
-if ($ENABLE_SOURCE{'Z'}) {
-
-	# Available Torrentz proxies, in order of preference
-	my @proxies = ('torrentz.eu/search?q=', 'torrentz.me/search?q=', 'torrentz.ch/search?q=', 'torrentz.in/search?q=');
-
-	# Automatically select a proxy that returns the non-US page
-	my $host       = '';
-	my $search_url = '';
-	foreach my $url (@proxies) {
-		($host) = $url =~ /^([^\/]+)/;
-		$fetch->url($PROTOCOL . '://' . $host);
-		if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /Indexing [\d\,]+ active torrents/i) {
-			$search_url = $url;
-			last;
-		} elsif ($DEBUG) {
-			print STDERR 'Torrentz proxy not available: ' . $host . "\n";
-		}
-	}
-
-	# Only add Torrentz if one of the proxies is up
-	if ($search_url) {
-		my %tmp = (
-			'host'          => $host,
-			'search_url'    => $search_url,
-			'search_suffix' => '+peer+%3E+' . $MIN_COUNT,
-			'weight'        => 1,
-			'quote'         => 1
-		);
-		$SOURCES{'Z'} = \%tmp;
-	}
-}
-
-my $SOURCE_COUNT = scalar(keys(%SOURCES));
-
-# Sanity check
-if ($SOURCE_COUNT < 1) {
-	die("No sources available\n");
-}
-
-# Adjust the inter-page delay with respect to the number of unique sources
-$DELAY /= $SOURCE_COUNT;
-
 # Environment
 #if ($DEBUG) {
 #	print STDERR `printenv` . "\n";
@@ -318,6 +174,12 @@ if (-e $dir . '/excludes') {
 	$exclude = uri_encode(' ' . $exclude);
 }
 
+# Setup our sources
+my $SOURCES = initSources();
+
+# Adjust the inter-page delay with respect to the number of unique sources
+$DELAY /= scalar(keys(%{$SOURCES}));
+
 # Handle custom searches
 if ((scalar(@urls) < 1) && defined($search) && length($search) > 0) {
 
@@ -328,8 +190,8 @@ if ((scalar(@urls) < 1) && defined($search) && length($search) > 0) {
 	}
 
 	# Create the relevent search strings
-	foreach my $key (keys(%SOURCES)) {
-		my $source = $SOURCES{$key};
+	foreach my $key (keys(%{$SOURCES})) {
+		my $source = $SOURCES->{$key};
 		push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search . $exclude . $source->{'search_suffix'});
 	}
 }
@@ -398,8 +260,8 @@ if ((scalar(@urls) < 1) && -e $dir . '/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};
+		foreach my $key (keys(%{$SOURCES})) {
+			my $source = $SOURCES->{$key};
 			push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search_str . $exclude . $source->{'search_suffix'});
 		}
 	}
@@ -539,7 +401,7 @@ if (scalar(@urls) < 1) {
 		foreach my $episode (@need) {
 			my $episode_long = sprintf('%02d', $episode);
 			my $season_long  = sprintf('%02d', $season);
-			foreach my $source (values(%SOURCES)) {
+			foreach my $source (values(%{$SOURCES})) {
 
 				# Use quotes around the show name if the source needs them
 				my $quote = '%22';
@@ -563,26 +425,31 @@ if (scalar(@urls) < 1) {
 
 					# SXEY
 					if ($season_long ne $season || $episode_long ne $episode) {
-						$url = $prefix . '+s' . $season . 'e' . $episode . $exclude;
+						$url = $prefix . '+s' . $season . 'e' . $episode . $exclude . $suffix;
 						push(@urls, $url);
 					}
 
 					# SXX EYY
-					$url = $prefix . '+s' . $season_long . '+e' . $episode_long . $exclude;
+					$url = $prefix . '+s' . $season_long . '+e' . $episode_long . $exclude . $suffix;
 					push(@urls, $url);
 
 					# Season XX Episode YY
-					$url = $prefix . '+season+' . $season_long . '+episode+' . $episode_long . $exclude;
+					$url = $prefix . '+season+' . $season_long . '+episode+' . $episode_long . $exclude . $suffix;
 					push(@urls, $url);
 
 					# Series X Episode Y
-					$url = $prefix . '+series+' . $season . '+episode+' . $episode . $exclude;
+					$url = $prefix . '+series+' . $season . '+episode+' . $episode . $exclude . $suffix;
 					push(@urls, $url);
 
 					# SxEE
-					$url = $prefix . '+' . $season . 'x' . $episode_long . $exclude;
+					$url = $prefix . '+' . $season . 'x' . $episode_long . $exclude . $suffix;
 					push(@urls, $url);
 
+					# Season X
+					if ($NO_QUALITY_CHECKS) {
+						$url = $prefix . '+Season+' . $season . $exclude . $suffix;
+						push(@urls, $url);
+					}
 				}
 			}
 		}
@@ -764,7 +631,7 @@ foreach my $content (@html_content) {
 			}
 
 			# Fetch the detail page for the URL
-			my $url = $PROTOCOL . '://' . $SOURCES{'ISO'}->{'host'} . '/torrent_details/' . $id . '/';
+			my $url = $PROTOCOL . '://' . $SOURCES->{'ISO'}->{'host'} . '/torrent_details/' . $id . '/';
 
 			if ($DEBUG) {
 				print STDERR 'Found file (' . $title . '): ' . $url . "\n";
@@ -890,11 +757,14 @@ foreach my $content (@html_content) {
 			my ($fileSeason, $episode) = findSE($title);
 
 			# Extract the size
-			my ($size, $unit) = $dl =~ /\<span class\=\"s\">(\d+(?:\.\d+)?) (G|M)B\<\/span\>/i;
-			if ($unit eq 'G') {
-				$size *= 1024;
+			my $size = 0;
+			if ($dl =~ /\<span class\=\"s\">(\d+(?:\.\d+)?) (G|M)B\<\/span\>/i) {
+				$size = $1;
+				if ($2 eq 'G') {
+					$size *= 1024;
+				}
+				$size = int($size);
 			}
-			$size = int($size);
 
 			# Count the sum of seeders and leachers
 			my $seeds = 0;
@@ -908,7 +778,7 @@ foreach my $content (@html_content) {
 
 			# Construct a magnet URL from the hash
 			# Assume the seconary lookup will append a list of trackers
-			my $url = 'magnet:?xt=urn:btih:' . $hash;
+			my $url = 'magnet:?xt=urn:btih:' . $hash . '&dn=' . uri_encode($title, { 'encode_reserved' => 1 });
 
 			if ($DEBUG) {
 				print STDERR 'Found file (' . $title . '): ' . $url . "\n";
@@ -1009,8 +879,10 @@ foreach my $tor (@tors) {
 			}
 			next;
 
-			# Skip files that don't contain the episode number
-		} elsif ((!defined($tor->{'episode'}) || !$need{ $tor->{'episode'} })) {
+			# Skip files that don't contain the episode number, unless MORE_NUMBER_FORMATS is set and NO_SEAONS is not
+		} elsif ((!defined($tor->{'episode'}) || !$need{ $tor->{'episode'} })
+			&& !($MORE_NUMBER_FORMATS && $NO_QUALITY_CHECKS && defined($tor->{'episode'}) && $tor->{'episode'} == 0))
+		{
 			if ($DEBUG) {
 				print STDERR 'Skipping file: No match for episode number (' . $tor->{'episode'} . '): ' . $tor->{'title'} . "\n";
 			}
@@ -1031,7 +903,7 @@ foreach my $tor (@tors) {
 	if (!$NO_QUALITY_CHECKS) {
 
 		# Skip torrents with too few seeders/leachers
-		if (($tor->{'seeds'} + $tor->{'leaches'}) * $SOURCES{ $tor->{'source'} }->{'weight'} < $MIN_COUNT) {
+		if (($tor->{'seeds'} + $tor->{'leaches'}) * $SOURCES->{ $tor->{'source'} }->{'weight'} < $MIN_COUNT) {
 			if ($DEBUG) {
 				print STDERR 'Skipping file: Insufficient seeder/leacher count (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . '): ' . $tor->{'title'} . "\n";
 			}
@@ -1183,14 +1055,18 @@ sub resolveTrackers($) {
 	# Always append a few static trackers to the URI
 	if ($url =~ /^magnet\:/i) {
 		foreach my $tracker (@TRACKERS) {
-			$url .= '&tr=' . uri_encode($tracker, { 'encode_reserved' => 1 });
+			my $arg = '&tr=' . uri_encode($tracker, { 'encode_reserved' => 1 });
+			my $match = quotemeta($arg);
+			if (!($url =~ /${match}/)) {
+				$url .= $arg;
+			}
 		}
 	}
 
 	# Fetch a dynamic tracker list for any hashinfo from TorrentZ
-	if ($TRACKER_LOOKUP && defined($SOURCES{'Z'}) && $url =~ /^magnet\:/ && $url =~ /\bxt\=urn\:btih\:(\w+)/i) {
+	if ($TRACKER_LOOKUP && defined($SOURCES->{'Z'}) && $url =~ /^magnet\:/ && $url =~ /\bxt\=urn\:btih\:(\w+)/i) {
 		my $hash    = $1;
-		my $baseURL = $PROTOCOL . '://' . $SOURCES{'Z'}->{'host'};
+		my $baseURL = $PROTOCOL . '://' . $SOURCES->{'Z'}->{'host'};
 		my $hashURL = $baseURL . '/' . $hash;
 		if ($DEBUG) {
 			print STDERR 'Hashinfo fetch with URL: ' . $hashURL . "\n";
@@ -1231,10 +1107,14 @@ sub resolveTrackers($) {
 			$line =~ s/^\s+//;
 			$line =~ s/\s+$//;
 			if ($line =~ /^(?:http|udp)\:/i) {
-				if ($DEBUG) {
-					print STDERR 'Adding tracker: ' . $line . "\n";
+				my $arg = '&tr=' . uri_encode($line, { 'encode_reserved' => 1 });
+				my $match = quotemeta($arg);
+				if (!($url =~ /${match}/)) {
+					if ($DEBUG) {
+						print STDERR 'Adding tracker: ' . $line . "\n";
+					}
+					$url .= $arg;
 				}
-				$url .= '&tr=' . uri_encode($line, { 'encode_reserved' => 1 });
 			} else {
 				print STDERR 'Unknown tracker type: ' . $line . "\n";
 			}
@@ -1297,3 +1177,150 @@ sub findSE($) {
 	$episode = int($episode);
 	return ($season, $episode);
 }
+
+sub initSources() {
+
+	my %sources = ();
+
+	# The Pirate Bay
+	if ($ENABLE_SOURCE{'TPB'}) {
+
+		# Available TPB proxies, in order of preference
+		my @TPBs = ('thepiratebay.se/search/', 'pirateproxy.se/search/', 'tpb.unblocked.co/search/');
+
+		# Automatically select a proxy that returns a search page
+		my $host       = '';
+		my $search_url = '';
+		foreach my $url (@TPBs) {
+			($host) = $url =~ /^([^\/]+)/;
+			$fetch->url($PROTOCOL . '://' . $host);
+			if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /\bSearch\b/i) {
+				$search_url = $url;
+				last;
+			} elsif ($DEBUG) {
+				print STDERR 'TPB proxy not available: ' . $host . "\n";
+			}
+		}
+
+		# Only add TPB if one of the proxies is up
+		if ($search_url) {
+			my %tmp = (
+				'host'          => $host,
+				'search_url'    => $search_url,
+				'search_suffix' => '/0/7/0',
+				'weight'        => 1.5,
+				'quote'         => 0
+			);
+			$sources{'TPB'} = \%tmp;
+		}
+	}
+
+	# ISOhunt
+	if ($ENABLE_SOURCE{'ISO'}) {
+
+		# Available ISOhunt proxies, in order of preference
+		my @proxies = ('isohunters.net/torrents/?ihq=', 'isohunt.to/torrents/?ihq=');
+
+		# Automatically select a proxy that returns the non-US page
+		my $host       = '';
+		my $search_url = '';
+		foreach my $url (@proxies) {
+			($host) = $url =~ /^([^\/]+)/;
+			$fetch->url($PROTOCOL . '://' . $host);
+			if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /Last\s+\d+\s+files\s+indexed/i) {
+				$search_url = $url;
+				last;
+			} elsif ($DEBUG) {
+				print STDERR 'ISOhunt proxy not available: ' . $host . "\n";
+			}
+		}
+
+		# Only add ISOhunt if one of the proxies is up
+		if ($search_url) {
+			my %tmp = (
+				'host'          => $host,
+				'search_url'    => $search_url,
+				'search_suffix' => '',
+				'weight'        => 0.30,
+				'quote'         => 1
+			);
+			$sources{'ISO'} = \%tmp;
+		}
+	}
+
+	# Kickass
+	if ($ENABLE_SOURCE{'KICK'}) {
+
+		# Available Kickass proxies, in order of preference
+		my @proxies = ('katproxy.com/usearch/', 'kickass.so/usearch/');
+
+		# Automatically select a proxy that returns the non-US page
+		my $host       = '';
+		my $search_url = '';
+		foreach my $url (@proxies) {
+			($host) = $url =~ /^([^\/]+)/;
+			$fetch->url($PROTOCOL . '://' . $host);
+			if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /torrent name\<\/th\>/i) {
+				$search_url = $url;
+				last;
+			} elsif ($DEBUG) {
+				print STDERR 'Kickass proxy not available: ' . $host . "\n";
+			}
+		}
+
+		# Only add Kickass if one of the proxies is up
+		if ($search_url) {
+			my %tmp = (
+				'host'          => $host,
+				'search_url'    => $search_url,
+				'search_suffix' => '/',
+				'weight'        => 0.30,
+				'quote'         => 1
+			);
+			$sources{'KICK'} = \%tmp;
+		}
+	}
+
+	# Torrentz
+	if ($ENABLE_SOURCE{'Z'}) {
+
+		# Available Torrentz proxies, in order of preference
+		my @proxies = ('torrentz.eu/search?q=', 'torrentz.me/search?q=', 'torrentz.ch/search?q=', 'torrentz.in/search?q=');
+
+		# Automatically select a proxy that returns the non-US page
+		my $host       = '';
+		my $search_url = '';
+		foreach my $url (@proxies) {
+			($host) = $url =~ /^([^\/]+)/;
+			$fetch->url($PROTOCOL . '://' . $host);
+			if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /Indexing [\d\,]+ active torrents/i) {
+				$search_url = $url;
+				last;
+			} elsif ($DEBUG) {
+				print STDERR 'Torrentz proxy not available: ' . $host . "\n";
+			}
+		}
+
+		# Only add Torrentz if one of the proxies is up
+		if ($search_url) {
+			my %tmp = (
+				'host'          => $host,
+				'search_url'    => $search_url,
+				'search_suffix' => '',
+				'weight'        => 1,
+				'quote'         => 1
+			);
+			if (!$NO_QUALITY_CHECKS) {
+				$tmp{'search_suffix'} = '+peer+%3E+' . $MIN_COUNT,;
+			}
+			$sources{'Z'} = \%tmp;
+		}
+	}
+
+	# Sanity check
+	if (scalar(keys(%sources)) < 1) {
+		die("No sources available\n");
+	}
+
+	return \%sources;
+}