findTorrent.pl 22 KB

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