findTorrent.pl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. # Local config
  11. my $TV_DIR = `~/bin/video/mediaPath` . '/TV';
  12. # Search parameters
  13. my $PROTOCOL = 'https';
  14. my %ENABLE_SOURCE = ('TPB' => 1, 'ISO' => 0);
  15. # Selection parameters
  16. my $MIN_COUNT = 10;
  17. my $MIN_SIZE = 100;
  18. my $SIZE_BONUS = 7;
  19. my $SIZE_PENALTY = $SIZE_BONUS;
  20. my $TITLE_PENALTY = $SIZE_BONUS / 2;
  21. my $MAX_SEED_RATIO = .25;
  22. my $SEED_RATIO_COUNT = 10;
  23. # App config
  24. my $DELAY = 4;
  25. my $TIMEOUT = 15;
  26. 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';
  27. # Includes
  28. use JSON;
  29. use File::Temp;
  30. use File::Spec;
  31. use File::Basename;
  32. use FindBin qw($Bin);
  33. use lib $Bin;
  34. use Fetch;
  35. # Prototypes
  36. sub findSE($);
  37. # Command line
  38. my ($dir, $search) = @ARGV;
  39. if (!defined($dir)) {
  40. die('Usage: ' . basename($0) . " input_directory [search_string]\n");
  41. }
  42. if ($ENV{'DEBUG'}) {
  43. $DEBUG = 1;
  44. }
  45. if ($ENV{'NO_QUALITY_CHECKS'}) {
  46. $NO_QUALITY_CHECKS = 1;
  47. }
  48. if ($ENV{'MORE_NUMBER_FORMATS'}) {
  49. $MORE_NUMBER_FORMATS = 1;
  50. }
  51. if ($ENV{'MIN_DAYS_BACK'}) {
  52. $MIN_DAYS_BACK = $ENV{'MIN_DAYS_BACK'};
  53. }
  54. if ($ENV{'MAX_DAYS_BACK'}) {
  55. $MAX_DAYS_BACK = $ENV{'MAX_DAYS_BACK'};
  56. }
  57. # New fetch object
  58. my $cookies = mktemp('/tmp/findTorrent.cookies.XXXXXXXX');
  59. my $fetch = Fetch->new(
  60. 'cookiefile' => $cookies,
  61. 'timeout' => $TIMEOUT,
  62. 'uas' => $UA
  63. );
  64. # Search sources
  65. my %SOURCES = ();
  66. # The Pirate Bay
  67. if ($ENABLE_SOURCE{'TPB'}) {
  68. # Available TPB proxies, in order of preference
  69. my @TPBs = ('thepiratebay.sx/search/', 'tpb.jasohack.com/index.php?loadurl=/search/', 'tpb.allestec.com/index.php?loadurl=/search/');
  70. # Automatically select a TPB proxy that returns a search page
  71. my $search_url = '';
  72. foreach my $url (@TPBs) {
  73. my ($host) = $url =~ /^([^\/]+)/;
  74. $fetch->url($PROTOCOL . '://' . $host);
  75. if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /\bSearch\b/i) {
  76. $search_url = $url;
  77. last;
  78. } elsif ($DEBUG) {
  79. print STDERR 'TPB proxy not available: ' . $host . "\n";
  80. }
  81. }
  82. # Only add TPB if one of the proxies is up
  83. if ($search_url) {
  84. my %tmp = (
  85. 'search_url' => $search_url,
  86. 'search_suffix' => '/0/7/0',
  87. 'weight' => 1.5,
  88. 'quote' => '0'
  89. );
  90. $SOURCES{'TPB'} = \%tmp;
  91. }
  92. }
  93. # ISOhunt
  94. if ($ENABLE_SOURCE{'ISO'}) {
  95. # Available ISOhunt proxies, in order of preference
  96. my @ISOs = ('isohunt.com/js/json.php?rows=10&start=1&ihq=');
  97. # Automatically select a TPB proxy that returns the non-US page
  98. my $search_url = '';
  99. foreach my $url (@ISOs) {
  100. my ($host) = $url =~ /^([^\/]+)/;
  101. $fetch->url($PROTOCOL . '://' . $host);
  102. if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /Last\s+\d+\s+files\s+indexed/i) {
  103. $search_url = $url;
  104. last;
  105. } elsif ($DEBUG) {
  106. print STDERR 'ISOhunt proxy not available: ' . $host . "\n";
  107. }
  108. }
  109. # Only add ISOhunt if one of the proxies is up
  110. if ($search_url) {
  111. my %tmp = (
  112. 'search_url' => 'isohunt.com/js/json.php?rows=10&start=1&ihq=',
  113. 'search_suffix' => '',
  114. 'weight' => 0.30,
  115. 'quote' => 1
  116. );
  117. $SOURCES{'ISO'} = \%tmp;
  118. }
  119. }
  120. # Sanity check
  121. if (scalar(keys(%SOURCES)) < 1) {
  122. die("No sources available\n");
  123. }
  124. # Environment
  125. #if ($DEBUG) {
  126. # print STDERR `printenv` . "\n";
  127. #}
  128. # Figure out what we're searching for
  129. my $show = '';
  130. my @urls = ();
  131. my $CUSTOM_SEARCH = 0;
  132. my $season = 0;
  133. my @need = ();
  134. my %need = ();
  135. # Clean up the input "directory" path
  136. {
  137. # Allow use of the raw series name
  138. if (!($dir =~ /\//)) {
  139. $show = $dir;
  140. $dir = $TV_DIR . '/' . $dir;
  141. }
  142. # Allow use of relative paths
  143. $dir = File::Spec->rel2abs($dir);
  144. # Sanity check
  145. if (!-d $dir) {
  146. die('Invalid input directory: ' . $dir . "\n");
  147. }
  148. # Isolate the season from the path, if provided
  149. $dir =~ /\/Season\s+(\d+)\/?$/i;
  150. if ($1) {
  151. $season = $1;
  152. $dir = dirname($dir);
  153. }
  154. # If no season is provided find the latest
  155. if (!$season) {
  156. opendir(SERIES, $dir)
  157. or die("Unable to open series directory: ${!}\n");
  158. while (my $file = readdir(SERIES)) {
  159. if ($file =~ /^Season\s+(\d+)$/i) {
  160. if (!$season || $season < $1) {
  161. $season = $1;
  162. }
  163. }
  164. }
  165. closedir(SERIES);
  166. }
  167. # Isolate and clean the series name
  168. if (!$show) {
  169. $show = basename($dir);
  170. }
  171. $show =~ s/[\'\"\.]//g;
  172. if ($DEBUG) {
  173. print STDERR 'Checking directory: ' . $dir . "\n";
  174. }
  175. }
  176. # Allow quality checks to be disabled
  177. if (-e $dir . '/no_quality_checks') {
  178. $NO_QUALITY_CHECKS = 1;
  179. }
  180. # Handle custom searches
  181. if ((scalar(@urls) < 1) && defined($search) && length($search) > 0) {
  182. # Note the custom search string
  183. $CUSTOM_SEARCH = 1;
  184. if ($DEBUG) {
  185. print STDERR "Custom search\n";
  186. }
  187. # Create the relevent search strings
  188. foreach my $key (keys(%SOURCES)) {
  189. my $source = $SOURCES{$key};
  190. push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search . $source->{'search_suffix'});
  191. }
  192. }
  193. # Handle search-by-date series
  194. if ((scalar(@urls) < 1) && -e $dir . '/search_by_date') {
  195. # Note the search-by-date status
  196. # Set the CUSTOM_SERACH flag to skip season/epsiode matching
  197. $CUSTOM_SEARCH = 1;
  198. if ($DEBUG) {
  199. print STDERR "Find by date\n";
  200. }
  201. # Read the find-by-date string
  202. my $search_by_date = '';
  203. local ($/, *FH);
  204. open(FH, $dir . '/search_by_date')
  205. or die('Unable to read search_by_date for show: ' . $show . ': ' . $! . "\n");
  206. my $text = <FH>;
  207. close(FH);
  208. if ($text =~ /^\s*(\S.*\S)\s*$/) {
  209. $search_by_date = $1;
  210. } else {
  211. die('Skipping invalid search_by_date for show: ' . $show . ': ' . $text . "\n");
  212. }
  213. # Create search strings for each date in the range, unless the related file already exists
  214. my (%years, %months, %days) = ();
  215. for (my $days_back = $MIN_DAYS_BACK ; $days_back <= $MAX_DAYS_BACK ; $days_back++) {
  216. # Calculate the date
  217. my (undef(), undef(), undef(), $day, $month, $year) = localtime(time() - (86400 * $days_back));
  218. # Format as strings
  219. $year = sprintf('%04d', $year + 1900);
  220. $month = sprintf('%02d', $month + 1);
  221. $day = sprintf('%02d', $day);
  222. # Check for an existing file
  223. my $exists = 0;
  224. {
  225. my $season_dir = $dir . '/Season ' . $year;
  226. my $prefix = qr/${year}\-${month}\-${day}\s*\-\s*/;
  227. opendir(SEASON, $season_dir)
  228. or die("Unable to open season directory: ${!}\n");
  229. while (my $file = readdir(SEASON)) {
  230. if ($file =~ $prefix) {
  231. $exists = 1;
  232. last;
  233. }
  234. }
  235. closedir(SEASON);
  236. }
  237. if ($exists) {
  238. next;
  239. }
  240. # Save all the date string components
  241. $years{$year} = 1;
  242. $months{$month} = 1;
  243. $days{$day} = 1;
  244. # Create the relevent search strings
  245. my $search_str = $search_by_date;
  246. $search_str =~ s/%Y/${year}/g;
  247. $search_str =~ s/%m/${month}/g;
  248. $search_str =~ s/%d/${day}/g;
  249. foreach my $key (keys(%SOURCES)) {
  250. my $source = $SOURCES{$key};
  251. push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search_str . $source->{'search_suffix'});
  252. }
  253. }
  254. # Build a date string matching regex
  255. my $str = '\b(?:' . join('|', keys(%years)) . ')\b';
  256. $str .= '.*';
  257. $str .= '\b(?:' . join('|', keys(%months)) . ')\b';
  258. $str .= '.*';
  259. $str .= '\b(?:' . join('|', keys(%days)) . ')\b';
  260. $CUSTOM_SEARCH = qr/${str}/;
  261. }
  262. # Handle standard series
  263. if (scalar(@urls) < 1) {
  264. # Allow use of more number formats
  265. if (-e $dir . '/more_number_formats') {
  266. $MORE_NUMBER_FORMATS = 1;
  267. }
  268. # Allow the show name to be overriden
  269. {
  270. my $search_name = $dir . '/search_name';
  271. if (-e $search_name) {
  272. local ($/, *FH);
  273. open(FH, $search_name)
  274. or die('Unable to read search_name for show: ' . $show . ': ' . $! . "\n");
  275. my $text = <FH>;
  276. close(FH);
  277. if ($text =~ /^\s*(\S.*\S)\s*$/) {
  278. $show = $1;
  279. } else {
  280. print STDERR 'Skipping invalid search_name for show: ' . $show . ': ' . $text . "\n";
  281. }
  282. }
  283. }
  284. if ($DEBUG) {
  285. print STDERR 'Searching with series title: ' . $show . "\n";
  286. }
  287. # Validate the season number
  288. if (!defined($season) || $season < 1 || $season > 2000) {
  289. die('Invalid season number: ' . $show . ' => ' . $season . "\n");
  290. }
  291. # Get the last episode number
  292. my $no_next = 0;
  293. my %episodes = ();
  294. my $highest = 0;
  295. opendir(SEASON, $dir . '/Season ' . $season)
  296. or die("Unable to open season directory: ${!}\n");
  297. while (my $file = readdir(SEASON)) {
  298. # Skip support files
  299. if ($file =~ /\.(?:png|xml|jpg|gif|tbn|txt|nfo|torrent)\s*$/i) {
  300. next;
  301. }
  302. # Check for a season_done file
  303. if ($file eq 'season_done') {
  304. $no_next = 1;
  305. next;
  306. }
  307. # Extract the episode number
  308. my ($num) = $file =~ /^\s*\.?(\d+)\s*\-\s*/i;
  309. if (defined($num)) {
  310. $num = int($num);
  311. if ($DEBUG) {
  312. print STDERR 'Found episode number: ' . $num . ' in file: ' . $file . "\n";
  313. }
  314. # Record it
  315. $episodes{$num} = 1;
  316. # Track the highest episode number
  317. if ($num > $highest) {
  318. $highest = $num;
  319. }
  320. }
  321. }
  322. close(SEASON);
  323. # Assume we need the next 2 episodes, unless no_next is set (i.e. season_done)
  324. if (!$no_next) {
  325. @need = ($highest + 1, $highest + 2);
  326. }
  327. # Find any missing episodes
  328. for (my $i = 1 ; $i <= $highest ; $i++) {
  329. if (!$episodes{$i}) {
  330. push(@need, $i);
  331. }
  332. }
  333. if ($DEBUG) {
  334. print STDERR 'Needed episodes: ' . join(', ', @need) . "\n";
  335. }
  336. # Reverse the array for later matching
  337. foreach my $episode (@need) {
  338. $need{$episode} = 1;
  339. }
  340. # Check for URL files
  341. if (-e "${dir}/url") {
  342. open(URL, "${dir}/url")
  343. or die("Unable to open URLs file: ${!}\n");
  344. while (<URL>) {
  345. if (/^\s*[a-z]+(\:\S+)\s*$/) {
  346. push(@urls, $PROTOCOL . $1);
  347. }
  348. }
  349. close(URL);
  350. }
  351. # Construct a URL-friendly show name
  352. my $safeShow = $show;
  353. $safeShow =~ s/\s+\&\s+/ and /i;
  354. $safeShow =~ s/^\s*The\b//i;
  355. $safeShow =~ s/\'//g;
  356. $safeShow =~ s/[^\w\"\-]+/ /g;
  357. $safeShow =~ s/^\s+//;
  358. $safeShow =~ s/\s+$//;
  359. $safeShow =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  360. $safeShow =~ s/\%20/\+/g;
  361. # Calculate possible name variations
  362. my @urlShowVarients = ();
  363. {
  364. # Default name
  365. push(@urlShowVarients, $safeShow);
  366. # Search for both "and" and "&"
  367. if ($safeShow =~ /\+and\+/i) {
  368. my $tmp = $safeShow;
  369. $tmp =~ s/\+and\+/\+%26\+/;
  370. push(@urlShowVarients, $tmp);
  371. }
  372. }
  373. # Construct the URL for each title varient of each needed episode
  374. foreach my $urlShow (@urlShowVarients) {
  375. foreach my $episode (@need) {
  376. my $episode_long = sprintf('%02d', $episode);
  377. my $season_long = sprintf('%02d', $season);
  378. foreach my $source (values(%SOURCES)) {
  379. my $quote = '%22';
  380. if (!$source->{'quote'}) {
  381. $quote = '';
  382. }
  383. # SXXEYY
  384. my $url = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote . '+s' . $season_long . 'e' . $episode_long;
  385. if ($source->{'search_suffix'}) {
  386. $url .= $source->{'search_suffix'};
  387. }
  388. push(@urls, $url);
  389. # Extra searches for shows that have lazy/non-standard number formats
  390. if ($MORE_NUMBER_FORMATS) {
  391. # SXEY
  392. if ($season_long ne $season || $episode_long ne $episode) {
  393. $url = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote . '+s' . $season . 'e' . $episode;
  394. if ($source->{'search_suffix'}) {
  395. $url .= $source->{'search_suffix'};
  396. }
  397. }
  398. push(@urls, $url);
  399. # SXX EYY
  400. $url = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote . '+s' . $season_long . '+e' . $episode_long;
  401. if ($source->{'search_suffix'}) {
  402. $url .= $source->{'search_suffix'};
  403. }
  404. push(@urls, $url);
  405. # Season XX Episode YY
  406. $url = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote . '+season+' . $season_long . '+episode+' . $episode_long;
  407. if ($source->{'search_suffix'}) {
  408. $url .= $source->{'search_suffix'};
  409. }
  410. push(@urls, $url);
  411. # Series X Episode Y
  412. $url = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote . '+series+' . $season . '+episode+' . $episode;
  413. if ($source->{'search_suffix'}) {
  414. $url .= $source->{'search_suffix'};
  415. }
  416. push(@urls, $url);
  417. # SxEE
  418. $url = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote . '+' . $season . 'x' . $episode_long;
  419. if ($source->{'search_suffix'}) {
  420. $url .= $source->{'search_suffix'};
  421. }
  422. push(@urls, $url);
  423. }
  424. }
  425. }
  426. }
  427. }
  428. my $content = '';
  429. my @json_content = ();
  430. foreach my $url (@urls) {
  431. # Fetch the page
  432. if ($DEBUG) {
  433. print STDERR 'Searching with URL: ' . $url . "\n";
  434. $fetch->file('/tmp/findTorrent-lastPage.html');
  435. }
  436. sleep($DELAY * (rand(2) + 0.5));
  437. $fetch->url($url);
  438. $fetch->fetch();
  439. # Check for errors
  440. if ($fetch->status_code() != 200) {
  441. print STDERR 'Error fetching URL: ' . $url . "\n";
  442. }
  443. # Save and compact the content
  444. if ($DEBUG) {
  445. print STDERR 'Fetched ' . length($fetch->content()) . " bytes\n";
  446. }
  447. if ($fetch->content() =~ /^\s*{/) {
  448. my $json = eval { decode_json($fetch->content()); };
  449. if (defined($json) && ref($json)) {
  450. push(@json_content, \@{ $json->{'items'}->{'list'} });
  451. } elsif ($DEBUG) {
  452. print STDERR 'JSON parsing failure on: ' . $fetch->content() . "\n";
  453. }
  454. } else {
  455. $content .= ' ' . $fetch->content();
  456. $content =~ s/\s+/ /g;
  457. }
  458. }
  459. # Find each TR element in TPB content
  460. my @tors = ();
  461. my @trs = split(/\<tr(?:\s+[^\>]*)?\>/i, $content);
  462. if ($DEBUG) {
  463. print STDERR 'TR count: ' . scalar(@trs) . "\n";
  464. }
  465. foreach my $tr (@trs) {
  466. # Trim trailing tags
  467. if (!($tr =~ s/\<\/tr\>.*$//is)) {
  468. # Skip things that aren't compelete TRs
  469. if ($DEBUG > 1) {
  470. print STDERR 'Skipping non-TR line: ' . $tr . "\n\n";
  471. }
  472. next;
  473. }
  474. # Find the show title
  475. my ($title) = $tr =~ /title\=\"Details\s+for\s+([^\"]*)\"/i;
  476. if (!defined($title) || length($title) < 1) {
  477. if ($DEBUG) {
  478. print STDERR "Unable to find show title in TR\n";
  479. }
  480. next;
  481. }
  482. $title =~ s/^\s+//;
  483. # Extract the season and episode numbers
  484. my ($fileSeason, $episode) = findSE($title);
  485. # Extract the URL
  486. my ($url) = $tr =~ /\<a\s+href\=\"(magnet\:\?[^\"]+)\"/i;
  487. if (!defined($url) || length($url) < 1) {
  488. if ($DEBUG) {
  489. print STDERR "Skipping TR with no magnet URL\n";
  490. }
  491. next;
  492. }
  493. # Count the sum of seeders and leachers
  494. my $seeds = 0;
  495. my $leaches = 0;
  496. if ($tr =~ /\<td(?:\s+[^\>]*)?\>(\d+)\<\/td\>\s*\<td(?:\s+[^\>]*)?\>(\d+)\<\/td\>\s*$/i) {
  497. $seeds = $1;
  498. $leaches = $2;
  499. }
  500. # Extract the size (from separate column or inline)
  501. my $size = 0;
  502. my $unit = 'M';
  503. if ($tr =~ m/Size (\d+(?:\.\d+)?)\&nbsp\;(G|M)iB/) {
  504. $size = $1;
  505. $unit = $2;
  506. } elsif ($tr =~ m/(\d+(?:\.\d+)?)\&nbsp\;(G|M)iB\<\/[tT][dD]\>/) {
  507. $size = $1;
  508. $unit = $2;
  509. }
  510. if ($unit eq 'G') {
  511. $size *= 1024;
  512. }
  513. $size = int($size);
  514. if ($DEBUG) {
  515. print STDERR 'Found file (' . $title . '): ' . $url . "\n";
  516. }
  517. # Save the extracted data
  518. my %tor = (
  519. 'title' => $title,
  520. 'season' => $fileSeason,
  521. 'episode' => $episode,
  522. 'seeds' => $seeds,
  523. 'leaches' => $leaches,
  524. 'size' => $size,
  525. 'url' => $url,
  526. 'source' => 'TPB'
  527. );
  528. push(@tors, \%tor);
  529. }
  530. # Find each discrete torrent from ISOhunt
  531. foreach my $json (@json_content) {
  532. foreach my $tor (@{$json}) {
  533. # Extract the title
  534. my $title = $tor->{'title'};
  535. $title =~ s/\<\/?b\>//gi;
  536. $title =~ s/^\s+//;
  537. # Extract the season and episode numbers
  538. my ($fileSeason, $episode) = findSE($tor->{'title'});
  539. # Count the sum of seeders and leachers
  540. my $seeds = 0;
  541. my $leaches = 0;
  542. if (defined($tor->{'Seeds'}) && length($tor->{'Seeds'}) > 0) {
  543. $seeds = $tor->{'Seeds'};
  544. }
  545. if (defined($tor->{'leechers'}) && length($tor->{'leechers'}) > 0) {
  546. $leaches = $tor->{'leechers'};
  547. }
  548. # Extract the size
  549. my $size = 0;
  550. if (defined($tor->{'length'}) && length($tor->{'length'}) > 0) {
  551. $size = int($tor->{'length'} / 1048576);
  552. }
  553. if ($DEBUG) {
  554. print STDERR 'Found file (' . $title . '): ' . $tor->{'enclosure_url'} . "\n";
  555. }
  556. # Save the extracted data
  557. my %tmp = (
  558. 'title' => $title,
  559. 'season' => $fileSeason,
  560. 'episode' => $episode,
  561. 'seeds' => $seeds,
  562. 'leaches' => $leaches,
  563. 'size' => $size,
  564. 'url' => $tor->{'enclosure_url'},
  565. 'source' => 'ISO'
  566. );
  567. push(@tors, \%tmp);
  568. }
  569. }
  570. # Filter for size/count/etc.
  571. my %tors = ();
  572. my $showRegex = undef();
  573. {
  574. my $showClean = $show;
  575. $showClean =~ s/[\"\']//g;
  576. $showClean =~ s/[\W_]+/\[\\W_\].*/g;
  577. $showRegex = qr/^${showClean}[\W_]/i;
  578. }
  579. foreach my $tor (@tors) {
  580. # Skip files that don't start with our show title
  581. if (!($tor->{'title'} =~ $showRegex)) {
  582. if ($DEBUG) {
  583. print STDERR 'Skipping file: Title does not match (' . $showRegex . '): ' . $tor->{'title'} . "\n";
  584. }
  585. next;
  586. # Skip pre-air files
  587. } elsif ($tor->{'title'} =~ /preair/i) {
  588. if ($DEBUG) {
  589. print STDERR 'Skipping file: Title contains "preair": ' . $tor->{'title'} . "\n";
  590. }
  591. next;
  592. # Skip SWESUB files
  593. } elsif ($tor->{'title'} =~ /swesub/i) {
  594. if ($DEBUG) {
  595. print STDERR 'Skipping file: Title contains "SWESUB": ' . $tor->{'title'} . "\n";
  596. }
  597. next;
  598. # Skip .rus. files
  599. } elsif ($tor->{'title'} =~ /\.rus\./i) {
  600. if ($DEBUG) {
  601. print STDERR 'Skipping file: Title contains ".rus.": ' . $tor->{'title'} . "\n";
  602. }
  603. next;
  604. # Skip german files
  605. } elsif ($tor->{'title'} =~ /german/i) {
  606. if ($DEBUG) {
  607. print STDERR 'Skipping file: Title contains "german": ' . $tor->{'title'} . "\n";
  608. }
  609. next;
  610. }
  611. # Enforce season and episode number matches for standard searches, or CUSTOM_SEARCH matching (if it's a regex)
  612. if (!$CUSTOM_SEARCH) {
  613. # Skip files that don't contain the right season number
  614. if (!defined($tor->{'season'}) || $tor->{'season'} != $season) {
  615. if ($DEBUG) {
  616. print STDERR 'Skipping file: No match for season number (' . $season . '): ' . $tor->{'title'} . "\n";
  617. }
  618. next;
  619. # Skip files that don't contain the episode number
  620. } elsif ((!defined($tor->{'episode'}) || !$need{ $tor->{'episode'} })) {
  621. if ($DEBUG) {
  622. print STDERR 'Skipping file: No match for episode number (' . $tor->{'episode'} . '): ' . $tor->{'title'} . "\n";
  623. }
  624. next;
  625. }
  626. } elsif (ref($CUSTOM_SEARCH) eq 'Regexp') {
  627. # Skip files that don't match the regex
  628. if (!($tor->{'title'} =~ $CUSTOM_SEARCH)) {
  629. if ($DEBUG) {
  630. print STDERR 'Skipping file: No match for CUSTOM_SEARCH regex (' . $CUSTOM_SEARCH . '): ' . $tor->{'title'} . "\n";
  631. }
  632. next;
  633. }
  634. }
  635. # Only apply the quality rules if NO_QUALITY_CHECKS is not in effect
  636. if (!$NO_QUALITY_CHECKS) {
  637. # Skip torrents with too few seeders/leachers
  638. if (($tor->{'seeds'} + $tor->{'leaches'}) * $SOURCES{ $tor->{'source'} }->{'weight'} < $MIN_COUNT) {
  639. if ($DEBUG) {
  640. print STDERR 'Skipping file: Insufficient seeder/leacher count (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . '): ' . $tor->{'title'} . "\n";
  641. }
  642. next;
  643. # Skip torrents with unusual seed/leach ratios
  644. } elsif ($tor->{'seeds'} > 1
  645. && $tor->{'seeds'} < $SEED_RATIO_COUNT
  646. && $tor->{'seeds'} > $tor->{'leaches'} * $MAX_SEED_RATIO)
  647. {
  648. if ($DEBUG) {
  649. print STDERR 'Skipping file: Unusual seeder/leacher ratio (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . '): ' . $tor->{'title'} . "\n";
  650. }
  651. next;
  652. # Skip torrents that are too small
  653. } elsif ($tor->{'size'} < $MIN_SIZE) {
  654. if ($DEBUG) {
  655. print STDERR 'Skipping file: Insufficient size (' . $tor->{'size'} . ' MiB): ' . $tor->{'title'} . "\n";
  656. }
  657. next;
  658. }
  659. }
  660. # Save good torrents
  661. push(@{ $tors{ $tor->{'episode'} } }, $tor);
  662. if ($DEBUG) {
  663. print STDERR 'Possible URL (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . ' seeds/leaches, ' . $tor->{'size'} . ' MiB): ' . $tor->{'title'} . "\n";
  664. }
  665. }
  666. # Find the average torrent size for each episode
  667. my %avg = ();
  668. foreach my $episode (keys(%tors)) {
  669. my $count = 0;
  670. $avg{$episode} = 0.001;
  671. foreach my $tor (@{ $tors{$episode} }) {
  672. $avg{$episode} += $tor->{'size'};
  673. $count++;
  674. }
  675. if ($count > 0) {
  676. $avg{$episode} /= $count;
  677. }
  678. if ($DEBUG) {
  679. print STDERR 'Episode ' . $episode . ' average size: ' . int($avg{$episode}) . " MiB\n";
  680. }
  681. }
  682. # Calculate an adjusted count based on the peer count, relative file size, and title contents
  683. foreach my $episode (keys(%tors)) {
  684. foreach my $tor (@{ $tors{$episode} }) {
  685. my $count = $tor->{'seeds'} + $tor->{'leaches'};
  686. # Start with the peer count
  687. $tor->{'adj_count'} = $count;
  688. # Adjust based on file size
  689. {
  690. my $size_ratio = $tor->{'size'} / $avg{$episode};
  691. if ($tor->{'size'} >= $avg{$episode}) {
  692. $tor->{'adj_count'} *= $SIZE_BONUS * $size_ratio;
  693. } else {
  694. $tor->{'adj_count'} *= (1 / $SIZE_PENALTY) * $size_ratio;
  695. }
  696. }
  697. # Adjust based on title contents
  698. if ($tor->{'title'} =~ /Subtitulado/i) {
  699. $tor->{'adj_count'} *= 1 / $TITLE_PENALTY;
  700. }
  701. # Truncate to an integer
  702. $tor->{'adj_count'} = int($tor->{'adj_count'});
  703. if ($DEBUG) {
  704. print STDERR 'Possible URL (' . $tor->{'adj_count'} . ' size-adjusted sources): ' . $tor->{'url'} . "\n";
  705. }
  706. }
  707. }
  708. # Pick the best-adjusted-count torrent for each episode
  709. my %urls = ();
  710. foreach my $episode (keys(%tors)) {
  711. my $max = undef();
  712. foreach my $tor (@{ $tors{$episode} }) {
  713. if (!defined($max) || $tor->{'adj_count'} > $max) {
  714. $urls{$episode} = $tor->{'url'};
  715. $max = $tor->{'adj_count'};
  716. if ($DEBUG) {
  717. print STDERR 'New semi-final URL (adjusted count: ' . $tor->{'adj_count'} . '): ' . $tor->{'url'} . "\n";
  718. }
  719. } elsif ($DEBUG) {
  720. print STDERR 'Skipping for non-max adjusted count (' . $tor->{'adj_count'} . '): ' . $tor->{'url'} . "\n";
  721. }
  722. }
  723. }
  724. # Output
  725. foreach my $episode (keys(%tors)) {
  726. if (defined($urls{$episode})) {
  727. print $urls{$episode} . "\n";
  728. if ($DEBUG) {
  729. print STDERR 'Final URL: ' . $urls{$episode} . "\n";
  730. }
  731. } elsif ($DEBUG) {
  732. print STDERR 'No URL found for URL: ' . $episode . "\n";
  733. }
  734. }
  735. # Cleanup
  736. unlink($cookies);
  737. exit(0);
  738. # Extract season
  739. sub findSE($) {
  740. my ($title) = @_;
  741. my $season = 0;
  742. my $episode = 0;
  743. if ($title =~ /[\W_]s(?:eason|eries)?\s*(\d+)/i) {
  744. $season = $1;
  745. } elsif ($title =~ /\b(\d+)x(\d+)\b/i) {
  746. $season = $1;
  747. $episode = $2;
  748. }
  749. if (!$episode && $title =~ /e(?:ipsode)?\s*(\d{1,2})/i) {
  750. $episode = $1;
  751. }
  752. $season = int($season);
  753. $episode = int($episode);
  754. return ($season, $episode);
  755. }