findTorrent.pl 21 KB

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