findTorrent.pl 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Defaults
  5. my $DEBUG = 0;
  6. my $NO_QUALITY_CHECKS = 0;
  7. my $MORE_NUMBER_FORMATS = 0;
  8. my $MIN_DAYS_BACK = 0;
  9. my $MAX_DAYS_BACK = 3;
  10. my $NEXT_EPISODES = 3;
  11. # Local config
  12. my $TV_DIR = `~/bin/video/mediaPath` . '/TV';
  13. # Search parameters
  14. my $PROTOCOL = 'https';
  15. my %ENABLE_SOURCE = ('TPB' => 0, 'ISO' => 1, 'KICK' => 1, 'Z' => 1);
  16. # Selection parameters
  17. my $MIN_COUNT = 10;
  18. my $MIN_SIZE = 100;
  19. my $SIZE_BONUS = 7;
  20. my $SIZE_PENALTY = $SIZE_BONUS;
  21. my $TITLE_PENALTY = $SIZE_BONUS / 2;
  22. my $MAX_SEED_RATIO = .25;
  23. my $SEED_RATIO_COUNT = 10;
  24. my $TRACKER_LOOKUP = 1;
  25. # App config
  26. my $DELAY = 4;
  27. my $TIMEOUT = 15;
  28. my $UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A';
  29. # Static tracker list, appended to all magnet URIs
  30. my @TRACKERS = ('udp://open.demonii.com:1337/announce', 'udp://tracker.publicbt.com:80/announce', 'udp://tracker.openbittorrent.com:80/announce', 'udp://9.rarbg.com:2710/announce');
  31. # Includes
  32. use URI::Encode qw(uri_encode);
  33. use HTML::Entities;
  34. use HTML::Strip;
  35. use JSON;
  36. use File::Temp;
  37. use File::Spec;
  38. use File::Basename;
  39. use FindBin qw($Bin);
  40. use lib $Bin;
  41. use Fetch;
  42. # Prototypes
  43. sub resolveSecondary($);
  44. sub resolveTrackers($);
  45. sub splitTags($$$);
  46. sub findSE($);
  47. sub initSources();
  48. # Command line
  49. my ($dir, $search) = @ARGV;
  50. if (!defined($dir)) {
  51. die('Usage: ' . basename($0) . " input_directory [search_string]\n");
  52. }
  53. if ($ENV{'DEBUG'}) {
  54. $DEBUG = 1;
  55. }
  56. if ($ENV{'NO_QUALITY_CHECKS'}) {
  57. $NO_QUALITY_CHECKS = 1;
  58. }
  59. if ($ENV{'MORE_NUMBER_FORMATS'}) {
  60. $MORE_NUMBER_FORMATS = 1;
  61. }
  62. if (defined($ENV{'MIN_DAYS_BACK'})) {
  63. $MIN_DAYS_BACK = $ENV{'MIN_DAYS_BACK'};
  64. }
  65. if (defined($ENV{'MAX_DAYS_BACK'})) {
  66. $MAX_DAYS_BACK = $ENV{'MAX_DAYS_BACK'};
  67. }
  68. if (defined($ENV{'NEXT_EPISODES'})) {
  69. $NEXT_EPISODES = $ENV{'NEXT_EPISODES'};
  70. }
  71. # Fetch object
  72. my $cookies = mktemp('/tmp/findTorrent.cookies.XXXXXXXX');
  73. my $fetch = Fetch->new(
  74. 'cookiefile' => $cookies,
  75. 'timeout' => $TIMEOUT,
  76. 'uas' => $UA
  77. );
  78. # Environment
  79. #if ($DEBUG) {
  80. # print STDERR `printenv` . "\n";
  81. #}
  82. # Figure out what we're searching for
  83. my $show = '';
  84. my @urls = ();
  85. my $CUSTOM_SEARCH = 0;
  86. my $season = 0;
  87. my @need = ();
  88. my %need = ();
  89. # Clean up the input "directory" path
  90. {
  91. # Allow use of the raw series name
  92. if (!($dir =~ /\//)) {
  93. $show = $dir;
  94. $dir = $TV_DIR . '/' . $dir;
  95. }
  96. # Allow use of relative paths
  97. $dir = File::Spec->rel2abs($dir);
  98. # Sanity check
  99. if (!-d $dir) {
  100. die('Invalid input directory: ' . $dir . "\n");
  101. }
  102. # Isolate the season from the path, if provided
  103. $dir =~ /\/Season\s+(\d+)\/?$/i;
  104. if ($1) {
  105. $season = $1;
  106. $dir = dirname($dir);
  107. }
  108. # If no season is provided find the latest
  109. if (!$season) {
  110. opendir(SERIES, $dir)
  111. or die("Unable to open series directory: ${!}\n");
  112. while (my $file = readdir(SERIES)) {
  113. if ($file =~ /^Season\s+(\d+)$/i) {
  114. if (!$season || $season < $1) {
  115. $season = $1;
  116. }
  117. }
  118. }
  119. closedir(SERIES);
  120. }
  121. # Isolate and clean the series name
  122. if (!$show) {
  123. $show = basename($dir);
  124. }
  125. $show =~ s/[\'\"\.]//g;
  126. if ($DEBUG) {
  127. print STDERR 'Checking directory: ' . $dir . "\n";
  128. }
  129. }
  130. # Allow quality checks to be disabled
  131. if (-e $dir . '/no_quality_checks') {
  132. $NO_QUALITY_CHECKS = 1;
  133. }
  134. # Read the excludes file, if any
  135. my $exclude = '';
  136. if (-e $dir . '/excludes') {
  137. local $/ = undef;
  138. open(EX, $dir . '/excludes')
  139. or die("Unable to open excludes file: ${!}\n");
  140. my $ex = <EX>;
  141. close(EX);
  142. $ex =~ s/^\s+//;
  143. $ex =~ s/^\s+//;
  144. my @excludes = split(/\s*,\s*/, $ex);
  145. foreach my $ex (@excludes) {
  146. if (length($exclude)) {
  147. $exclude .= ' ';
  148. }
  149. $exclude .= '-"' . $ex . '"';
  150. }
  151. $exclude = uri_encode(' ' . $exclude);
  152. }
  153. # Setup our sources
  154. my $SOURCES = initSources();
  155. # Adjust the inter-page delay with respect to the number of unique sources
  156. $DELAY /= scalar(keys(%{$SOURCES}));
  157. # Handle custom searches
  158. if ((scalar(@urls) < 1) && defined($search) && length($search) > 0) {
  159. # Note the custom search string
  160. $CUSTOM_SEARCH = 1;
  161. if ($DEBUG) {
  162. print STDERR "Custom search\n";
  163. }
  164. # Create the relevent search strings
  165. foreach my $key (keys(%{$SOURCES})) {
  166. my $source = $SOURCES->{$key};
  167. push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search . $exclude . $source->{'search_suffix'});
  168. }
  169. }
  170. # Handle search-by-date series
  171. if ((scalar(@urls) < 1) && -e $dir . '/search_by_date') {
  172. # Note the search-by-date status
  173. # Set the CUSTOM_SERACH flag to skip season/epsiode matching
  174. $CUSTOM_SEARCH = 1;
  175. if ($DEBUG) {
  176. print STDERR "Find by date\n";
  177. }
  178. # Read the find-by-date string
  179. my $search_by_date = '';
  180. local ($/, *FH);
  181. open(FH, $dir . '/search_by_date')
  182. or die('Unable to read search_by_date for show: ' . $show . ': ' . $! . "\n");
  183. my $text = <FH>;
  184. close(FH);
  185. if ($text =~ /^\s*(\S.*\S)\s*$/) {
  186. $search_by_date = $1;
  187. } else {
  188. die('Skipping invalid search_by_date for show: ' . $show . ': ' . $text . "\n");
  189. }
  190. # Create search strings for each date in the range, unless the related file already exists
  191. my (%years, %months, %days) = ();
  192. for (my $days_back = $MIN_DAYS_BACK ; $days_back <= $MAX_DAYS_BACK ; $days_back++) {
  193. # Calculate the date
  194. my (undef(), undef(), undef(), $day, $month, $year) = localtime(time() - (86400 * $days_back));
  195. # Format as strings
  196. $year = sprintf('%04d', $year + 1900);
  197. $month = sprintf('%02d', $month + 1);
  198. $day = sprintf('%02d', $day);
  199. # Check for an existing file
  200. my $exists = 0;
  201. {
  202. my $season_dir = $dir . '/Season ' . $year;
  203. my $prefix = qr/${year}\-${month}\-${day}\s*\-\s*/;
  204. opendir(SEASON, $season_dir)
  205. or die('Unable to open season directory (' . $season_dir . '): ' . $! . "\n");
  206. while (my $file = readdir(SEASON)) {
  207. if ($file =~ $prefix) {
  208. $exists = 1;
  209. last;
  210. }
  211. }
  212. closedir(SEASON);
  213. }
  214. if ($exists) {
  215. next;
  216. }
  217. # Save all the date string components
  218. $years{$year} = 1;
  219. $months{$month} = 1;
  220. $days{$day} = 1;
  221. # Create the relevent search strings
  222. my $search_str = $search_by_date;
  223. $search_str =~ s/%Y/${year}/g;
  224. $search_str =~ s/%m/${month}/g;
  225. $search_str =~ s/%d/${day}/g;
  226. foreach my $key (keys(%{$SOURCES})) {
  227. my $source = $SOURCES->{$key};
  228. push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search_str . $exclude . $source->{'search_suffix'});
  229. }
  230. }
  231. # Build a date string matching regex
  232. my $str = '\b(?:' . join('|', keys(%years)) . ')\b';
  233. $str .= '.*';
  234. $str .= '\b(?:' . join('|', keys(%months)) . ')\b';
  235. $str .= '.*';
  236. $str .= '\b(?:' . join('|', keys(%days)) . ')\b';
  237. $CUSTOM_SEARCH = qr/${str}/;
  238. }
  239. # Handle standard series
  240. if (scalar(@urls) < 1) {
  241. # Allow use of more number formats
  242. if (-e $dir . '/more_number_formats') {
  243. $MORE_NUMBER_FORMATS = 1;
  244. }
  245. # Allow the show name to be overriden
  246. {
  247. my $search_name = $dir . '/search_name';
  248. if (-e $search_name) {
  249. local ($/, *FH);
  250. open(FH, $search_name)
  251. or die('Unable to read search_name for show: ' . $show . ': ' . $! . "\n");
  252. my $text = <FH>;
  253. close(FH);
  254. if ($text =~ /^\s*(\S.*\S)\s*$/) {
  255. $show = $1;
  256. } else {
  257. print STDERR 'Skipping invalid search_name for show: ' . $show . ': ' . $text . "\n";
  258. }
  259. }
  260. }
  261. if ($DEBUG) {
  262. print STDERR 'Searching with series title: ' . $show . "\n";
  263. }
  264. # Validate the season number
  265. if (!defined($season) || $season < 1 || $season > 2000) {
  266. die('Invalid season number: ' . $show . ' => ' . $season . "\n");
  267. }
  268. # Get the last episode number
  269. my $no_next = 0;
  270. my %episodes = ();
  271. my $highest = 0;
  272. opendir(SEASON, $dir . '/Season ' . $season)
  273. or die("Unable to open season directory: ${!}\n");
  274. while (my $file = readdir(SEASON)) {
  275. # Skip support files
  276. if ($file =~ /\.(?:png|xml|jpg|gif|tbn|txt|nfo|torrent)\s*$/i) {
  277. next;
  278. }
  279. # Check for a season_done file
  280. if ($file eq 'season_done') {
  281. $no_next = 1;
  282. next;
  283. }
  284. # Extract the episode number
  285. my ($num) = $file =~ /^\s*\.?(\d+)\s*\-\s*/i;
  286. if (defined($num)) {
  287. $num = int($num);
  288. if ($DEBUG) {
  289. print STDERR 'Found episode number: ' . $num . ' in file: ' . $file . "\n";
  290. }
  291. # Record it
  292. $episodes{$num} = 1;
  293. # Track the highest episode number
  294. if ($num > $highest) {
  295. $highest = $num;
  296. }
  297. }
  298. }
  299. close(SEASON);
  300. # Assume we need the next 2 episodes, unless no_next is set (i.e. season_done)
  301. if (!$no_next) {
  302. for (my $i = 1 ; $i <= $NEXT_EPISODES ; $i++) {
  303. push(@need, $highest + $i);
  304. }
  305. }
  306. # Find any missing episodes
  307. for (my $i = 1 ; $i <= $highest ; $i++) {
  308. if (!$episodes{$i}) {
  309. push(@need, $i);
  310. }
  311. }
  312. if ($DEBUG) {
  313. print STDERR 'Needed episodes: ' . join(', ', @need) . "\n";
  314. }
  315. # Reverse the array for later matching
  316. foreach my $episode (@need) {
  317. $need{$episode} = 1;
  318. }
  319. # Construct a URL-friendly show name
  320. my $safeShow = $show;
  321. $safeShow =~ s/\s+\&\s+/ and /i;
  322. $safeShow =~ s/^\s*The\b//i;
  323. $safeShow =~ s/\s+\-\s+/ /g;
  324. $safeShow =~ s/[\'\:]//g;
  325. $safeShow =~ s/[^\w\"\-]+/ /g;
  326. $safeShow =~ s/^\s+//;
  327. $safeShow =~ s/\s+$//;
  328. $safeShow =~ s/\s\s+/ /g;
  329. $safeShow =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  330. $safeShow =~ s/\%20/\+/g;
  331. # Calculate possible name variations
  332. my @urlShowVarients = ();
  333. {
  334. # Default name
  335. push(@urlShowVarients, $safeShow);
  336. # Search for both "and" and "&"
  337. if ($safeShow =~ /\+and\+/i) {
  338. my $tmp = $safeShow;
  339. $tmp =~ s/\+and\+/\+%26\+/;
  340. push(@urlShowVarients, $tmp);
  341. }
  342. }
  343. # Construct the URL for each title varient of each needed episode
  344. foreach my $urlShow (@urlShowVarients) {
  345. foreach my $episode (@need) {
  346. my $episode_long = sprintf('%02d', $episode);
  347. my $season_long = sprintf('%02d', $season);
  348. foreach my $source (values(%{$SOURCES})) {
  349. # Use quotes around the show name if the source needs them
  350. my $quote = '%22';
  351. if (!$source->{'quote'}) {
  352. $quote = '';
  353. }
  354. # Calculate the compete search prefix and suffix to simplify later concatenations
  355. my $prefix = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote;
  356. my $suffix = $exclude;
  357. if ($source->{'search_suffix'}) {
  358. $suffix .= $source->{'search_suffix'};
  359. }
  360. # SXXEYY
  361. my $url = $prefix . '+s' . $season_long . 'e' . $episode_long . $suffix;
  362. push(@urls, $url);
  363. # Extra searches for shows that have lazy/non-standard number formats
  364. if ($MORE_NUMBER_FORMATS) {
  365. # SXEY
  366. if ($season_long ne $season || $episode_long ne $episode) {
  367. $url = $prefix . '+s' . $season . 'e' . $episode . $exclude . $suffix;
  368. push(@urls, $url);
  369. }
  370. # SXX EYY
  371. $url = $prefix . '+s' . $season_long . '+e' . $episode_long . $exclude . $suffix;
  372. push(@urls, $url);
  373. # Season XX Episode YY
  374. $url = $prefix . '+season+' . $season_long . '+episode+' . $episode_long . $exclude . $suffix;
  375. push(@urls, $url);
  376. # Series X Episode Y
  377. $url = $prefix . '+series+' . $season . '+episode+' . $episode . $exclude . $suffix;
  378. push(@urls, $url);
  379. # SxEE
  380. $url = $prefix . '+' . $season . 'x' . $episode_long . $exclude . $suffix;
  381. push(@urls, $url);
  382. # Season X
  383. if ($NO_QUALITY_CHECKS) {
  384. $url = $prefix . '+Season+' . $season . $exclude . $suffix;
  385. push(@urls, $url);
  386. }
  387. }
  388. }
  389. }
  390. }
  391. }
  392. my @html_content = ();
  393. my @json_content = ();
  394. foreach my $url (@urls) {
  395. # Fetch the page
  396. if ($DEBUG) {
  397. print STDERR 'Searching with URL: ' . $url . "\n";
  398. $fetch->file('/tmp/findTorrent-lastPage.html');
  399. }
  400. sleep($DELAY * (rand(2) + 0.5));
  401. $fetch->url($url);
  402. $fetch->fetch('nocheck' => 1);
  403. # Check for useful errors
  404. if ($fetch->status_code() == 404) {
  405. if ($DEBUG) {
  406. print STDERR "Skipping content from 404 response\n";
  407. }
  408. next;
  409. }
  410. # Check for errors
  411. if ($fetch->status_code() != 200) {
  412. print STDERR 'Error fetching URL (' . $fetch->status_code() . '): ' . $url . "\n";
  413. next;
  414. }
  415. # Save the content, discriminating by data type
  416. if ($DEBUG) {
  417. print STDERR 'Fetched ' . length($fetch->content()) . " bytes\n";
  418. }
  419. if ($fetch->content() =~ /^\s*{/) {
  420. my $json = eval { decode_json($fetch->content()); };
  421. if (defined($json) && ref($json)) {
  422. push(@json_content, \@{ $json->{'items'}->{'list'} });
  423. } elsif ($DEBUG) {
  424. print STDERR 'JSON parsing failure on: ' . $fetch->content() . "\n";
  425. }
  426. } else {
  427. push(@html_content, scalar($fetch->content()));
  428. }
  429. }
  430. # We need someplace to store parsed torrent data from the various sources
  431. my @tors = ();
  432. # Handle HTML content
  433. foreach my $content (@html_content) {
  434. if ($content =~ /\<title\>The Pirate Bay/i) {
  435. # Find TR elements from TPB
  436. my @trs = splitTags($content, 'TR', 'TH');
  437. foreach my $tr (@trs) {
  438. # Find the show title
  439. my ($title) = $tr =~ /title\=\"Details\s+for\s+([^\"]*)\"/i;
  440. if (!defined($title) || length($title) < 1) {
  441. if ($DEBUG) {
  442. print STDERR "Unable to find show title in TR\n\t" . $tr . "\n";
  443. }
  444. next;
  445. }
  446. $title =~ s/^\s+//;
  447. # Extract the season and episode numbers
  448. my ($fileSeason, $episode) = findSE($title);
  449. # Extract the URL
  450. my ($url) = $tr =~ /\<a\s+href\=\"(magnet\:\?[^\"]+)\"/i;
  451. if (!defined($url) || length($url) < 1) {
  452. if ($DEBUG) {
  453. print STDERR "Skipping TR with no magnet URL\n";
  454. }
  455. next;
  456. }
  457. $url = decode_entities($url);
  458. # Count the sum of seeders and leachers
  459. my $seeds = 0;
  460. my $leaches = 0;
  461. if ($tr =~ /\<td(?:\s+[^\>]*)?\>(\d+)\<\/td\>\s*\<td(?:\s+[^\>]*)?\>(\d+)\<\/td\>\s*$/i) {
  462. $seeds = $1;
  463. $leaches = $2;
  464. }
  465. # Extract the size (from separate column or inline)
  466. my $size = 0;
  467. my $unit = 'M';
  468. if ($tr =~ m/Size (\d+(?:\.\d+)?)\&nbsp\;(G|M)iB/) {
  469. $size = $1;
  470. $unit = $2;
  471. } elsif ($tr =~ m/(\d+(?:\.\d+)?)\&nbsp\;(G|M)iB\<\/[tT][dD]\>/) {
  472. $size = $1;
  473. $unit = $2;
  474. }
  475. if ($unit eq 'G') {
  476. $size *= 1024;
  477. }
  478. $size = int($size);
  479. if ($DEBUG) {
  480. print STDERR 'Found file (' . $title . '): ' . $url . "\n";
  481. }
  482. # Save the extracted data
  483. my %tor = (
  484. 'title' => $title,
  485. 'season' => $fileSeason,
  486. 'episode' => $episode,
  487. 'seeds' => $seeds,
  488. 'leaches' => $leaches,
  489. 'size' => $size,
  490. 'url' => $url,
  491. 'source' => 'TPB'
  492. );
  493. push(@tors, \%tor);
  494. }
  495. } elsif ($content =~ /Isohunt Torrent Search Engine\<\/title\>/i) {
  496. # Find each TR element from ISOHunt
  497. my @trs = splitTags($content, 'TR', 'TH');
  498. foreach my $tr (@trs) {
  499. # Split each TD element from the row
  500. my @tds = split(/\<td(?:\s+[^\>]*)?\>/i, $tr);
  501. my $numCols = scalar(@tds) - 1;
  502. if ($numCols > 9 && $numCols < 8) {
  503. print STDERR 'Invalid ISO TR: ' . $tr . "\n";
  504. next;
  505. }
  506. # Skip sponsored results
  507. if ($tds[1] =~ /Sponsored\<\/td\>/i) {
  508. if ($DEBUG > 1) {
  509. print STDERR "Skipping sponsored result\n";
  510. }
  511. next;
  512. }
  513. # Find the torrent ID and show title
  514. my ($id, $title) = $tds[2] =~ /\<a(?:\s+[^\>]*)?href=\"\/torrent_details\/(\d+)\/[^\"]*\"\>\<span\>([^\>]+)\<\/span\>/i;
  515. if (!defined($id) || length($id) < 1 || !defined($title) || length($title) < 1) {
  516. if ($DEBUG) {
  517. print STDERR "Unable to find show ID or title in TD:\n\t" . $tds[2] . "\n";
  518. }
  519. next;
  520. }
  521. $title =~ s/^\s+//;
  522. # Extract the season and episode numbers
  523. my ($fileSeason, $episode) = findSE($title);
  524. # Extract the size
  525. my $size = 0;
  526. my $unit = 'M';
  527. if ($tds[6] =~ m/(\d+(?:\.\d+)?) (G|M)B<\/td\>/i) {
  528. $size = $1;
  529. $unit = $2;
  530. }
  531. if ($unit eq 'G') {
  532. $size *= 1024;
  533. }
  534. $size = int($size);
  535. # Count the sum of seeders and leachers
  536. my $seeds = 0;
  537. if ($tds[7] =~ /(\d+)\<\/td\>/i) {
  538. $seeds = $1;
  539. }
  540. my $leaches = 0;
  541. if ($tds[8] =~ /(\d+)\<\/td\>/i) {
  542. $leaches = $1;
  543. }
  544. # Fetch the detail page for the URL
  545. my $url = $PROTOCOL . '://' . $SOURCES->{'ISO'}->{'host'} . '/torrent_details/' . $id . '/';
  546. if ($DEBUG) {
  547. print STDERR 'Found file (' . $title . '): ' . $url . "\n";
  548. }
  549. # Save the extracted data
  550. my %tor = (
  551. 'title' => $title,
  552. 'season' => $fileSeason,
  553. 'episode' => $episode,
  554. 'seeds' => $seeds,
  555. 'leaches' => $leaches,
  556. 'size' => $size,
  557. 'url' => $url,
  558. 'source' => 'ISO'
  559. );
  560. push(@tors, \%tor);
  561. }
  562. } elsif ($content =~ /KickassTorrents\<\/title\>/i) {
  563. # Find each TR element from Kickass
  564. my @trs = splitTags($content, 'TR', 'TH');
  565. foreach my $tr (@trs) {
  566. # Split each TD element from the row
  567. my @tds = split(/\<td(?:\s+[^\>]*)?\>/i, $tr);
  568. my $numCols = scalar(@tds) - 1;
  569. if ($numCols > 9 && $numCols < 8) {
  570. print STDERR 'Invalid KICK TR: ' . $tr . "\n";
  571. next;
  572. }
  573. # Find the torrent title
  574. my ($title) = $tds[1] =~ /\<a(?:\s+[^\>]*)?href\=\"[^\"]+\?title\=\[[^\]\"]+\]([^\"]+)\"/i;
  575. if (!defined($title) || length($title) < 1) {
  576. if ($DEBUG) {
  577. print STDERR "Unable to find torrent title in TD:\n\t" . $tds[1] . "\n";
  578. }
  579. next;
  580. }
  581. $title =~ s/^\s+//;
  582. # Find the torrent URL
  583. my ($url) = $tds[1] =~ /\<a(?:\s+[^\>]*)?href=\"(magnet:[^\"]+)\"/i;
  584. if (!defined($url) || length($url) < 1) {
  585. if ($DEBUG) {
  586. print STDERR "Unable to find torrent URL in TD:\n\t" . $tds[1] . "\n";
  587. }
  588. next;
  589. }
  590. $url = decode_entities($url);
  591. # Extract the season and episode numbers
  592. my ($fileSeason, $episode) = findSE($title);
  593. # Extract the size
  594. my $size = 0;
  595. my $unit = 'M';
  596. if ($tds[2] =~ m/(\d+(?:\.\d+)?)\s+\<span\>(G|M)B\<\/span\>\<\/td\>/i) {
  597. $size = $1;
  598. $unit = $2;
  599. }
  600. if ($unit eq 'G') {
  601. $size *= 1024;
  602. }
  603. $size = int($size);
  604. # Count the sum of seeders and leachers
  605. my $seeds = 0;
  606. if ($tds[5] =~ /(\d+)\<\/td\>/i) {
  607. $seeds = $1;
  608. }
  609. my $leaches = 0;
  610. if ($tds[6] =~ /(\d+)\<\/td\>/i) {
  611. $leaches = $1;
  612. }
  613. if ($DEBUG) {
  614. print STDERR 'Found file (' . $title . '): ' . $url . "\n";
  615. }
  616. # Save the extracted data
  617. my %tor = (
  618. 'title' => $title,
  619. 'season' => $fileSeason,
  620. 'episode' => $episode,
  621. 'seeds' => $seeds,
  622. 'leaches' => $leaches,
  623. 'size' => $size,
  624. 'url' => $url,
  625. 'source' => 'KICK'
  626. );
  627. push(@tors, \%tor);
  628. }
  629. } elsif ($content =~ /\<a(?:\s+[^\>]*)?\>Torrentz\<\/a\>/i) {
  630. # We'll need to strip HTML
  631. my $hs = HTML::Strip->new();
  632. # Find each DL element from Torrentz
  633. my @dls = splitTags($content, 'DL', undef());
  634. foreach my $dl (@dls) {
  635. # Skip ads
  636. if ($dl =~ /\s+rel\=\"nofollow/i) {
  637. next;
  638. }
  639. # Find the torrent title
  640. my ($hash, $title) = $dl =~ /\<a href\=\"\/(\w+)\">(.*)\<\/a\>/i;
  641. if (!defined($hash) || length($hash) < 1 || !defined($title) || length($title) < 1) {
  642. if ($DEBUG) {
  643. print STDERR "Unable to find torrent title in DL:\n\t" . $dl . "\n";
  644. }
  645. next;
  646. }
  647. $title = $hs->parse($title);
  648. $hs->eof;
  649. $title =~ s/^\s+//;
  650. # Extract the season and episode numbers
  651. my ($fileSeason, $episode) = findSE($title);
  652. # Extract the size
  653. my $size = 0;
  654. if ($dl =~ /\<span class\=\"s\">(\d+(?:\.\d+)?) (G|M)B\<\/span\>/i) {
  655. $size = $1;
  656. if ($2 eq 'G') {
  657. $size *= 1024;
  658. }
  659. $size = int($size);
  660. }
  661. # Count the sum of seeders and leachers
  662. my $seeds = 0;
  663. if ($dl =~ /<span class\=\"u\">(\d+)\<\/span\>/i) {
  664. $seeds = $1;
  665. }
  666. my $leaches = 0;
  667. if ($dl =~ /<span class\=\"d\">(\d+)\<\/span\>/i) {
  668. $leaches = $1;
  669. }
  670. # Construct a magnet URL from the hash
  671. # Assume the seconary lookup will append a list of trackers
  672. my $url = 'magnet:?xt=urn:btih:' . $hash . '&dn=' . uri_encode($title, { 'encode_reserved' => 1 });
  673. if ($DEBUG) {
  674. print STDERR 'Found file (' . $title . '): ' . $url . "\n";
  675. }
  676. # Save the extracted data
  677. my %tor = (
  678. 'title' => $title,
  679. 'season' => $fileSeason,
  680. 'episode' => $episode,
  681. 'seeds' => $seeds,
  682. 'leaches' => $leaches,
  683. 'size' => $size,
  684. 'url' => $url,
  685. 'source' => 'Z'
  686. );
  687. push(@tors, \%tor);
  688. }
  689. } else {
  690. print STDERR "Unknown HTML content:\n" . $content . "\n\n";
  691. }
  692. }
  693. # Handle JSON content
  694. foreach my $content (@json_content) {
  695. print STDERR "Unknown JSON content:\n" . $content . "\n\n";
  696. }
  697. # Filter for size/count/etc.
  698. my %tors = ();
  699. my $showRegex = undef();
  700. {
  701. my $showClean = $show;
  702. $showClean =~ s/[\"\']//g;
  703. $showClean =~ s/[\W_]+/\[\\W_\].*/g;
  704. $showRegex = qr/^${showClean}[\W_]/i;
  705. }
  706. foreach my $tor (@tors) {
  707. # Skip files that don't start with our show title
  708. if (!($tor->{'title'} =~ $showRegex)) {
  709. if ($DEBUG) {
  710. print STDERR 'Skipping file: Title does not match (' . $showRegex . '): ' . $tor->{'title'} . "\n";
  711. }
  712. next;
  713. # Skip pre-air files
  714. } elsif ($tor->{'title'} =~ /preair/i) {
  715. if ($DEBUG) {
  716. print STDERR 'Skipping file: Title contains "preair": ' . $tor->{'title'} . "\n";
  717. }
  718. next;
  719. # Skip SWESUB files
  720. } elsif ($tor->{'title'} =~ /swesub/i) {
  721. if ($DEBUG) {
  722. print STDERR 'Skipping file: Title contains "SWESUB": ' . $tor->{'title'} . "\n";
  723. }
  724. next;
  725. # Skip .rus. files
  726. } elsif ($tor->{'title'} =~ /\.rus\./i) {
  727. if ($DEBUG) {
  728. print STDERR 'Skipping file: Title contains ".rus.": ' . $tor->{'title'} . "\n";
  729. }
  730. next;
  731. # Skip German files
  732. } elsif ($tor->{'title'} =~ /german/i) {
  733. if ($DEBUG) {
  734. print STDERR 'Skipping file: Title contains "german": ' . $tor->{'title'} . "\n";
  735. }
  736. next;
  737. # Skip French files
  738. } elsif ($tor->{'title'} =~ /french/i) {
  739. if ($DEBUG) {
  740. print STDERR 'Skipping file: Title contains "french": ' . $tor->{'title'} . "\n";
  741. }
  742. next;
  743. # Skip unedited files
  744. } elsif ($tor->{'title'} =~ /unedited/i) {
  745. if ($DEBUG) {
  746. print STDERR 'Skipping file: Title contains "unedited": ' . $tor->{'title'} . "\n";
  747. }
  748. next;
  749. }
  750. # Enforce season and episode number matches for standard searches, or CUSTOM_SEARCH matching (if it's a regex)
  751. if (!$CUSTOM_SEARCH) {
  752. # Skip files that don't contain the right season number
  753. if (!defined($tor->{'season'}) || $tor->{'season'} != $season) {
  754. if ($DEBUG) {
  755. print STDERR 'Skipping file: No match for season number (' . $season . '): ' . $tor->{'title'} . "\n";
  756. }
  757. next;
  758. # Skip files that don't contain the episode number, unless MORE_NUMBER_FORMATS is set and NO_SEAONS is not
  759. } elsif ((!defined($tor->{'episode'}) || !$need{ $tor->{'episode'} })
  760. && !($MORE_NUMBER_FORMATS && $NO_QUALITY_CHECKS && defined($tor->{'episode'}) && $tor->{'episode'} == 0))
  761. {
  762. if ($DEBUG) {
  763. print STDERR 'Skipping file: No match for episode number (' . $tor->{'episode'} . '): ' . $tor->{'title'} . "\n";
  764. }
  765. next;
  766. }
  767. } elsif (ref($CUSTOM_SEARCH) eq 'Regexp') {
  768. # Skip files that don't match the regex
  769. if (!($tor->{'title'} =~ $CUSTOM_SEARCH)) {
  770. if ($DEBUG) {
  771. print STDERR 'Skipping file: No match for CUSTOM_SEARCH regex (' . $CUSTOM_SEARCH . '): ' . $tor->{'title'} . "\n";
  772. }
  773. next;
  774. }
  775. }
  776. # Only apply the quality rules if NO_QUALITY_CHECKS is not in effect
  777. if (!$NO_QUALITY_CHECKS) {
  778. # Skip torrents with too few seeders/leachers
  779. if (($tor->{'seeds'} + $tor->{'leaches'}) * $SOURCES->{ $tor->{'source'} }->{'weight'} < $MIN_COUNT) {
  780. if ($DEBUG) {
  781. print STDERR 'Skipping file: Insufficient seeder/leacher count (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . '): ' . $tor->{'title'} . "\n";
  782. }
  783. next;
  784. # Skip torrents with unusual seed/leach ratios
  785. } elsif ($tor->{'seeds'} > 1
  786. && $tor->{'seeds'} < $SEED_RATIO_COUNT
  787. && $tor->{'seeds'} > $tor->{'leaches'} * $MAX_SEED_RATIO)
  788. {
  789. if ($DEBUG) {
  790. print STDERR 'Skipping file: Unusual seeder/leacher ratio (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . '): ' . $tor->{'title'} . "\n";
  791. }
  792. next;
  793. # Skip torrents that are too small
  794. } elsif ($tor->{'size'} < $MIN_SIZE) {
  795. if ($DEBUG) {
  796. print STDERR 'Skipping file: Insufficient size (' . $tor->{'size'} . ' MiB): ' . $tor->{'title'} . "\n";
  797. }
  798. next;
  799. }
  800. }
  801. # Save good torrents
  802. push(@{ $tors{ $tor->{'episode'} } }, $tor);
  803. if ($DEBUG) {
  804. print STDERR 'Possible URL (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . ' seeds/leaches, ' . $tor->{'size'} . ' MiB): ' . $tor->{'title'} . "\n";
  805. }
  806. }
  807. # Find the average torrent size for each episode
  808. my %avg = ();
  809. foreach my $episode (keys(%tors)) {
  810. my $count = 0;
  811. $avg{$episode} = 0.001;
  812. foreach my $tor (@{ $tors{$episode} }) {
  813. $avg{$episode} += $tor->{'size'};
  814. $count++;
  815. }
  816. if ($count > 0) {
  817. $avg{$episode} /= $count;
  818. }
  819. if ($DEBUG) {
  820. print STDERR 'Episode ' . $episode . ' average size: ' . int($avg{$episode}) . " MiB\n";
  821. }
  822. }
  823. # Calculate an adjusted count based on the peer count, relative file size, and title contents
  824. foreach my $episode (keys(%tors)) {
  825. foreach my $tor (@{ $tors{$episode} }) {
  826. my $count = $tor->{'seeds'} + $tor->{'leaches'};
  827. # Start with the peer count
  828. $tor->{'adj_count'} = $count;
  829. # Adjust based on file size
  830. {
  831. my $size_ratio = $tor->{'size'} / $avg{$episode};
  832. if ($tor->{'size'} >= $avg{$episode}) {
  833. $tor->{'adj_count'} *= $SIZE_BONUS * $size_ratio;
  834. } else {
  835. $tor->{'adj_count'} *= (1 / $SIZE_PENALTY) * $size_ratio;
  836. }
  837. }
  838. # Adjust based on title contents
  839. if ($tor->{'title'} =~ /Subtitulado/i) {
  840. $tor->{'adj_count'} *= 1 / $TITLE_PENALTY;
  841. }
  842. # Truncate to an integer
  843. $tor->{'adj_count'} = int($tor->{'adj_count'});
  844. if ($DEBUG) {
  845. print STDERR 'Possible URL (' . $tor->{'adj_count'} . ' size-adjusted sources): ' . $tor->{'url'} . "\n";
  846. }
  847. }
  848. }
  849. # Pick the best-adjusted-count torrent for each episode
  850. my %urls = ();
  851. foreach my $episode (keys(%tors)) {
  852. my @sorted = sort { $b->{'adj_count'} <=> $a->{'adj_count'} } @{ $tors{$episode} };
  853. my $max = undef();
  854. foreach my $tor (@sorted) {
  855. if (!defined($max) || $tor->{'adj_count'} > $max) {
  856. if ($urls{$episode} = resolveSecondary($tor)) {
  857. $max = $tor->{'adj_count'};
  858. }
  859. if ($DEBUG) {
  860. print STDERR 'Semi-final URL (adjusted count: ' . $tor->{'adj_count'} . '): ' . $tor->{'url'} . "\n";
  861. }
  862. } elsif ($DEBUG) {
  863. print STDERR 'Skipping for lesser adjusted count (' . $tor->{'adj_count'} . '): ' . $tor->{'url'} . "\n";
  864. }
  865. }
  866. }
  867. # Output
  868. foreach my $episode (keys(%tors)) {
  869. if (defined($urls{$episode})) {
  870. $urls{$episode} = resolveTrackers($urls{$episode});
  871. print $urls{$episode} . "\n";
  872. if ($DEBUG) {
  873. print STDERR 'Final URL: ' . $urls{$episode} . "\n";
  874. }
  875. } elsif ($DEBUG) {
  876. print STDERR 'No URL for: ' . $episode . "\n";
  877. }
  878. }
  879. # Cleanup
  880. unlink($cookies);
  881. exit(0);
  882. # Resolve secondary URLs
  883. sub resolveSecondary($) {
  884. my ($tor) = @_;
  885. my $url = $tor->{'url'};
  886. if ($tor->{'source'} eq 'ISO') {
  887. if ($DEBUG) {
  888. print STDERR 'Secondary fetch with URL: ' . $tor->{'url'} . "\n";
  889. $fetch->file('/tmp/findTorrent-secondary.html');
  890. }
  891. sleep($DELAY * (rand(2) + 0.5));
  892. $fetch->url($tor->{'url'});
  893. $fetch->fetch();
  894. if ($fetch->status_code() != 200) {
  895. print STDERR 'Error fetching secondary URL: ' . $tor->{'url'} . "\n";
  896. return undef();
  897. }
  898. ($url) = $fetch->content() =~ /\<a\s+href\=\"(magnet\:\?[^\"]+)\"/i;
  899. if (!defined($url) || length($url) < 1) {
  900. if ($DEBUG) {
  901. print STDERR 'No secondary URL available from: ' . $tor->{'url'} . "\n";
  902. }
  903. return undef();
  904. }
  905. $url = decode_entities($url);
  906. }
  907. return $url;
  908. }
  909. sub resolveTrackers($) {
  910. my ($url) = @_;
  911. # Always append a few static trackers to the URI
  912. if ($url =~ /^magnet\:/i) {
  913. foreach my $tracker (@TRACKERS) {
  914. my $arg = '&tr=' . uri_encode($tracker, { 'encode_reserved' => 1 });
  915. my $match = quotemeta($arg);
  916. if (!($url =~ /${match}/)) {
  917. $url .= $arg;
  918. }
  919. }
  920. }
  921. # Fetch a dynamic tracker list for any hashinfo from TorrentZ
  922. if ($TRACKER_LOOKUP && defined($SOURCES->{'Z'}) && $url =~ /^magnet\:/ && $url =~ /\bxt\=urn\:btih\:(\w+)/i) {
  923. my $hash = $1;
  924. my $baseURL = $PROTOCOL . '://' . $SOURCES->{'Z'}->{'host'};
  925. my $hashURL = $baseURL . '/' . $hash;
  926. if ($DEBUG) {
  927. print STDERR 'Hashinfo fetch with URL: ' . $hashURL . "\n";
  928. $fetch->file('/tmp/findTorrent-hashinfo.html');
  929. }
  930. sleep($DELAY * (rand(2) + 0.5));
  931. $fetch->url($hashURL);
  932. $fetch->fetch();
  933. if ($fetch->status_code() != 200) {
  934. print STDERR 'Error fetching hashinfo URL: ' . $hashURL . "\n";
  935. return $url;
  936. }
  937. # Parse out and fetch the tracker list URL
  938. my ($list) = $fetch->content() =~ /\<a rel\=\"nofollow\" href\=\"(\/announcelist_\d+)\"\>/;
  939. if (!defined($list)) {
  940. print STDERR "Unable to find tracker list link\n";
  941. return $url;
  942. }
  943. my $listURL = $baseURL . $list;
  944. if ($DEBUG) {
  945. print STDERR 'Tracker list fetch with URL: ' . $listURL . "\n";
  946. $fetch->file('/tmp/findTorrent-tracker.html');
  947. }
  948. sleep($DELAY * (rand(2) + 0.5));
  949. $fetch->url($listURL);
  950. $fetch->fetch();
  951. if ($fetch->status_code() != 200) {
  952. print STDERR 'Error fetching tracker list URL: ' . $listURL . "\n";
  953. return $url;
  954. }
  955. # Parse the tracker list and append our URL
  956. foreach my $line ($fetch->content()) {
  957. if ($line =~ /^\s*$/) {
  958. next;
  959. }
  960. $line =~ s/^\s+//;
  961. $line =~ s/\s+$//;
  962. if ($line =~ /^(?:http|udp)\:/i) {
  963. my $arg = '&tr=' . uri_encode($line, { 'encode_reserved' => 1 });
  964. my $match = quotemeta($arg);
  965. if (!($url =~ /${match}/)) {
  966. if ($DEBUG) {
  967. print STDERR 'Adding tracker: ' . $line . "\n";
  968. }
  969. $url .= $arg;
  970. }
  971. } else {
  972. print STDERR 'Unknown tracker type: ' . $line . "\n";
  973. }
  974. }
  975. }
  976. return $url;
  977. }
  978. sub splitTags($$$) {
  979. my ($content, $tag, $header) = @_;
  980. # Find each tag element
  981. my @out = ();
  982. my @parts = split(/\<${tag}(?:\s+[^\>]*)?\>/i, $content);
  983. foreach my $part (@parts) {
  984. # Trim trailing tags and skip things that aren't complete tags
  985. if (!($part =~ s/\<\/${tag}\>.*$//is)) {
  986. if ($DEBUG > 1) {
  987. print STDERR 'Skipping non-tag line: ' . $part . "\n\n";
  988. }
  989. next;
  990. }
  991. # Skip headers
  992. if (defined($header) && $part =~ /<${header}(?:\s+[^\>]*)?\>/i) {
  993. if ($DEBUG > 2) {
  994. print STDERR 'Skipping header line: ' . $part . "\n\n";
  995. }
  996. next;
  997. }
  998. # Save good tags
  999. push(@out, $part);
  1000. }
  1001. return @out;
  1002. }
  1003. # Extract season
  1004. sub findSE($) {
  1005. my ($title) = @_;
  1006. my $season = 0;
  1007. my $episode = 0;
  1008. if ($title =~ /[\W_]s(?:eason|eries)?\s*(\d+)/i) {
  1009. $season = $1;
  1010. } elsif ($title =~ /\b(\d+)x(\d+)\b/i) {
  1011. $season = $1;
  1012. $episode = $2;
  1013. }
  1014. if (!$episode && $title =~ /e(?:ipsode)?\s*(\d{1,2})/i) {
  1015. $episode = $1;
  1016. }
  1017. $season = int($season);
  1018. $episode = int($episode);
  1019. return ($season, $episode);
  1020. }
  1021. sub initSources() {
  1022. my %sources = ();
  1023. # The Pirate Bay
  1024. if ($ENABLE_SOURCE{'TPB'}) {
  1025. # Available TPB proxies, in order of preference
  1026. my @TPBs = ('thepiratebay.se/search/', 'pirateproxy.se/search/', 'tpb.unblocked.co/search/');
  1027. # Automatically select a proxy that returns a search page
  1028. my $host = '';
  1029. my $search_url = '';
  1030. foreach my $url (@TPBs) {
  1031. ($host) = $url =~ /^([^\/]+)/;
  1032. $fetch->url($PROTOCOL . '://' . $host);
  1033. if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /\bSearch\b/i) {
  1034. $search_url = $url;
  1035. last;
  1036. } elsif ($DEBUG) {
  1037. print STDERR 'TPB proxy not available: ' . $host . "\n";
  1038. }
  1039. }
  1040. # Only add TPB if one of the proxies is up
  1041. if ($search_url) {
  1042. my %tmp = (
  1043. 'host' => $host,
  1044. 'search_url' => $search_url,
  1045. 'search_suffix' => '/0/7/0',
  1046. 'weight' => 1.5,
  1047. 'quote' => 0
  1048. );
  1049. $sources{'TPB'} = \%tmp;
  1050. }
  1051. }
  1052. # ISOhunt
  1053. if ($ENABLE_SOURCE{'ISO'}) {
  1054. # Available ISOhunt proxies, in order of preference
  1055. my @proxies = ('isohunters.net/torrents/?ihq=', 'isohunt.to/torrents/?ihq=');
  1056. # Automatically select a proxy that returns the non-US page
  1057. my $host = '';
  1058. my $search_url = '';
  1059. foreach my $url (@proxies) {
  1060. ($host) = $url =~ /^([^\/]+)/;
  1061. $fetch->url($PROTOCOL . '://' . $host);
  1062. if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /Last\s+\d+\s+files\s+indexed/i) {
  1063. $search_url = $url;
  1064. last;
  1065. } elsif ($DEBUG) {
  1066. print STDERR 'ISOhunt proxy not available: ' . $host . "\n";
  1067. }
  1068. }
  1069. # Only add ISOhunt if one of the proxies is up
  1070. if ($search_url) {
  1071. my %tmp = (
  1072. 'host' => $host,
  1073. 'search_url' => $search_url,
  1074. 'search_suffix' => '',
  1075. 'weight' => 0.30,
  1076. 'quote' => 1
  1077. );
  1078. $sources{'ISO'} = \%tmp;
  1079. }
  1080. }
  1081. # Kickass
  1082. if ($ENABLE_SOURCE{'KICK'}) {
  1083. # Available Kickass proxies, in order of preference
  1084. my @proxies = ('katproxy.com/usearch/', 'kickass.so/usearch/');
  1085. # Automatically select a proxy that returns the non-US page
  1086. my $host = '';
  1087. my $search_url = '';
  1088. foreach my $url (@proxies) {
  1089. ($host) = $url =~ /^([^\/]+)/;
  1090. $fetch->url($PROTOCOL . '://' . $host);
  1091. if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /torrent name\<\/th\>/i) {
  1092. $search_url = $url;
  1093. last;
  1094. } elsif ($DEBUG) {
  1095. print STDERR 'Kickass proxy not available: ' . $host . "\n";
  1096. }
  1097. }
  1098. # Only add Kickass if one of the proxies is up
  1099. if ($search_url) {
  1100. my %tmp = (
  1101. 'host' => $host,
  1102. 'search_url' => $search_url,
  1103. 'search_suffix' => '/',
  1104. 'weight' => 0.30,
  1105. 'quote' => 1
  1106. );
  1107. $sources{'KICK'} = \%tmp;
  1108. }
  1109. }
  1110. # Torrentz
  1111. if ($ENABLE_SOURCE{'Z'}) {
  1112. # Available Torrentz proxies, in order of preference
  1113. my @proxies = ('torrentz.eu/search?q=', 'torrentz.me/search?q=', 'torrentz.ch/search?q=', 'torrentz.in/search?q=');
  1114. # Automatically select a proxy that returns the non-US page
  1115. my $host = '';
  1116. my $search_url = '';
  1117. foreach my $url (@proxies) {
  1118. ($host) = $url =~ /^([^\/]+)/;
  1119. $fetch->url($PROTOCOL . '://' . $host);
  1120. if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /Indexing [\d\,]+ active torrents/i) {
  1121. $search_url = $url;
  1122. last;
  1123. } elsif ($DEBUG) {
  1124. print STDERR 'Torrentz proxy not available: ' . $host . "\n";
  1125. }
  1126. }
  1127. # Only add Torrentz if one of the proxies is up
  1128. if ($search_url) {
  1129. my %tmp = (
  1130. 'host' => $host,
  1131. 'search_url' => $search_url,
  1132. 'search_suffix' => '',
  1133. 'weight' => 1,
  1134. 'quote' => 1
  1135. );
  1136. if (!$NO_QUALITY_CHECKS) {
  1137. $tmp{'search_suffix'} = '+peer+%3E+' . $MIN_COUNT,;
  1138. }
  1139. $sources{'Z'} = \%tmp;
  1140. }
  1141. }
  1142. # Sanity check
  1143. if (scalar(keys(%sources)) < 1) {
  1144. die("No sources available\n");
  1145. }
  1146. return \%sources;
  1147. }