findTorrent.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $DEBUG = 0;
  5. my $NO_QUALITY_CHECKS = 0;
  6. my $MORE_NUMBER_FORMATS = 0;
  7. # Includes
  8. use JSON;
  9. use File::Temp;
  10. use File::Basename;
  11. use lib '/usr/local/spidering/bots/utils';
  12. use Fetch;
  13. # Command line
  14. my ($dir, $search) = @ARGV;
  15. if (!defined($dir) || (!defined($search) && !-d $dir)) {
  16. die('Usage: ' . basename($0) . " input_directory [search_string]\n");
  17. }
  18. if ($ENV{'DEBUG'}) {
  19. $DEBUG = 1;
  20. }
  21. if ($ENV{'NO_QUALITY_CHECKS'}) {
  22. $NO_QUALITY_CHECKS = 1;
  23. }
  24. if ($ENV{'MORE_NUMBER_FORMATS'}) {
  25. $MORE_NUMBER_FORMATS = 1;
  26. }
  27. # Parameters
  28. my $DELAY = 4;
  29. my $MIN_COUNT = 10;
  30. my $MIN_SIZE = 100;
  31. my $SIZE_BONUS = 5;
  32. my $SIZE_PENALTY = $SIZE_BONUS;
  33. my $MAX_SEED_RATIO = .25;
  34. my $SEED_RATIO_COUNT = 10;
  35. my $PROTOCOL = 'http';
  36. 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';
  37. # Search sources
  38. my %SOURCES = ();
  39. {
  40. my %tmp = (
  41. 'search_url' => 'thepiratebay.se/search/',
  42. 'search_suffix' => '/0/7/0',
  43. 'weight' => 1.5,
  44. 'quote' => '0'
  45. );
  46. $SOURCES{'TPB'} = \%tmp;
  47. }
  48. {
  49. my %tmp = (
  50. 'search_url' => 'isohunt.com/js/json.php?rows=10&start=1&ihq=',
  51. 'search_suffix' => '',
  52. 'weight' => 0.30,
  53. 'quote' => 1
  54. );
  55. #$SOURCES{'ISO'} = \%tmp;
  56. }
  57. # Environment
  58. #if ($DEBUG) {
  59. # print STDERR `printenv` . "\n";
  60. #}
  61. # New fetch object
  62. my $cookies = mktemp('/tmp/findTorrent.cookies.XXXXXXXX');
  63. my $fetch = Fetch->new(
  64. 'cookiefile' => $cookies,
  65. 'uas' => $UA
  66. );
  67. # Get the season name
  68. my $show = '';
  69. my @urls = ();
  70. my $CUSTOM_SEARCH = 0;
  71. my $season = 0;
  72. my @need = ();
  73. my %need = ();
  74. if (defined($search) && length($search) > 0) {
  75. # Note the custom search string
  76. $CUSTOM_SEARCH = 1;
  77. # Create the relevent search strings
  78. foreach my $key (keys(%SOURCES)) {
  79. my $source = $SOURCES{$key};
  80. push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search . $source->{'search_suffix'});
  81. }
  82. # Get and clean the show name (for post-search matching)
  83. $show = $dir;
  84. $show =~ s/[\'\"]//g;
  85. } else {
  86. # Read the input directory
  87. if ($DEBUG) {
  88. print STDERR 'Checking directory: ' . $dir . "\n";
  89. }
  90. # Allow quality checks to be disabled
  91. if (-e dirname($dir) . '/no_quality_checks') {
  92. $NO_QUALITY_CHECKS = 1;
  93. }
  94. # Allow use of more number formats
  95. if (-e dirname($dir) . '/more_number_formats') {
  96. $MORE_NUMBER_FORMATS = 1;
  97. }
  98. # Get and clean the show name
  99. $show = basename(dirname($dir));
  100. $show =~ s/[\'\"]//g;
  101. # Allow the show name to be overriden
  102. {
  103. my $search_name = dirname($dir) . '/search_name';
  104. if (-e $search_name) {
  105. local ($/, *FH);
  106. open(FH, $search_name)
  107. or die('Unable to read search_name for show: ' . $show . ': ' . $! . "\n");
  108. my $text = <FH>;
  109. close(FH);
  110. if ($text =~ /^\s*(\S.*\S)\s*$/) {
  111. $show = $1;
  112. } else {
  113. print STDERR 'Skipping invalid search_name for show: ' . $show . ': ' . $text . "\n";
  114. }
  115. }
  116. }
  117. if ($DEBUG) {
  118. print STDERR 'Searching with series title: ' . $show . "\n";
  119. }
  120. # Get the season number
  121. my $name = basename($dir);
  122. if ($name =~ /^\s*Season\s+(\d+)\s*$/i) {
  123. $season = $1;
  124. }
  125. if (!defined($season) || $season < 1 || $season > 2000) {
  126. if (defined($season)) {
  127. if ($DEBUG) {
  128. print STDERR 'Skipping season ' . $season . "\n";
  129. }
  130. exit 0;
  131. }
  132. die('Invalid season number: ' . $show . ' => ' . $name . "\n");
  133. }
  134. # Get the last episode number
  135. my $no_next = 0;
  136. my %episodes = ();
  137. my $highest = 0;
  138. opendir(SEASON, $dir)
  139. or die("Unable to open season directory: ${!}\n");
  140. while (my $file = readdir(SEASON)) {
  141. # Skip support files
  142. if ($file =~ /\.(?:png|xml|jpg|gif|tbn|txt|nfo|torrent)\s*$/i) {
  143. next;
  144. }
  145. # Check for a season_done file
  146. if ($file eq 'season_done') {
  147. $no_next = 1;
  148. next;
  149. }
  150. # Extract the episode number
  151. my ($num) = $file =~ /^\s*\.?(\d+)\s*\-\s*/i;
  152. if (defined($num)) {
  153. $num = int($num);
  154. if ($DEBUG) {
  155. print STDERR 'Found episode number: ' . $num . ' in file: ' . $file . "\n";
  156. }
  157. # Record it
  158. $episodes{$num} = 1;
  159. # Track the highest episode number
  160. if ($num > $highest) {
  161. $highest = $num;
  162. }
  163. }
  164. }
  165. close(SEASON);
  166. # Assume we need the next 2 episodes, unless no_next is set (i.e. season_done)
  167. if (!$no_next) {
  168. @need = ($highest + 1, $highest + 2);
  169. }
  170. # Find any missing episodes
  171. for (my $i = 1 ; $i <= $highest ; $i++) {
  172. if (!$episodes{$i}) {
  173. push(@need, $i);
  174. }
  175. }
  176. if ($DEBUG) {
  177. print STDERR 'Needed episodes: ' . join(', ', @need) . "\n";
  178. }
  179. # Reverse the array for later matching
  180. foreach my $episode (@need) {
  181. $need{$episode} = 1;
  182. }
  183. # Check for URL files
  184. if (-e "${dir}/url") {
  185. open(URL, "${dir}/url")
  186. or die("Unable to open URLs file: ${!}\n");
  187. while (<URL>) {
  188. if (/^\s*[a-z]+(\:\S+)\s*$/) {
  189. push(@urls, $PROTOCOL . $1);
  190. }
  191. }
  192. close(URL);
  193. }
  194. # Construct a URL-friendly show name
  195. my $safeShow = $show;
  196. $safeShow =~ s/\s+\&\s+/ and /i;
  197. $safeShow =~ s/^\s*The\b//i;
  198. $safeShow =~ s/[\W_]+/ /g;
  199. $safeShow =~ s/^\s+//;
  200. $safeShow =~ s/\s+$//;
  201. $safeShow =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  202. $safeShow =~ s/\%20/\+/g;
  203. # Calculate possible name variations
  204. my @urlShowVarients = ();
  205. {
  206. # Default name
  207. push(@urlShowVarients, $safeShow);
  208. # Search for both "and" and "&"
  209. if ($safeShow =~ /\+and\+/i) {
  210. my $tmp = $safeShow;
  211. $tmp =~ s/\+and\+/\+%26\+/;
  212. push(@urlShowVarients, $tmp);
  213. }
  214. }
  215. # Construct the URL for each title varient of each needed episode
  216. foreach my $urlShow (@urlShowVarients) {
  217. foreach my $episode (@need) {
  218. my $episode_long = sprintf('%02d', $episode);
  219. my $season_long = sprintf('%02d', $season);
  220. foreach my $source (values(%SOURCES)) {
  221. my $quote = '%22';
  222. if (!$source->{'quote'}) {
  223. $quote = '';
  224. }
  225. # SXXEYY
  226. my $url = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote . '+s' . $season_long . 'e' . $episode_long;
  227. if ($source->{'search_suffix'}) {
  228. $url .= $source->{'search_suffix'};
  229. }
  230. push(@urls, $url);
  231. # Extra searches for shows that have lazy/non-standard number formats
  232. if ($MORE_NUMBER_FORMATS) {
  233. # SXEY
  234. if ($season_long ne $season || $episode_long ne $episode) {
  235. $url = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote . '+s' . $season . 'e' . $episode;
  236. if ($source->{'search_suffix'}) {
  237. $url .= $source->{'search_suffix'};
  238. }
  239. }
  240. push(@urls, $url);
  241. # SXX EYY
  242. $url = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote . '+s' . $season_long . '+e' . $episode_long;
  243. if ($source->{'search_suffix'}) {
  244. $url .= $source->{'search_suffix'};
  245. }
  246. push(@urls, $url);
  247. # Season XX Episode YY
  248. $url = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote . '+season+' . $season_long . '+episode+' . $episode_long;
  249. if ($source->{'search_suffix'}) {
  250. $url .= $source->{'search_suffix'};
  251. }
  252. push(@urls, $url);
  253. # SxEE
  254. $url = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote . '+' . $season . 'x' . $episode_long;
  255. if ($source->{'search_suffix'}) {
  256. $url .= $source->{'search_suffix'};
  257. }
  258. push(@urls, $url);
  259. }
  260. }
  261. }
  262. }
  263. }
  264. my $content = '';
  265. my @json_content = ();
  266. foreach my $url (@urls) {
  267. # Fetch the page
  268. if ($DEBUG) {
  269. print STDERR 'Searching with URL: ' . $url . "\n";
  270. $fetch->file('/tmp/findTorrent-lastPage.html');
  271. }
  272. sleep($DELAY * (rand(2) + 0.5));
  273. $fetch->url($url);
  274. $fetch->fetch();
  275. # Check for errors
  276. if ($fetch->status_code() != 200) {
  277. print STDERR 'Error fetching URL: ' . $url . "\n";
  278. }
  279. # Save and compact the content
  280. if ($DEBUG) {
  281. print STDERR 'Fetched ' . length($fetch->content()) . " bytes\n";
  282. }
  283. if ($fetch->content() =~ /^\s*{/) {
  284. my $json = eval { decode_json($fetch->content()); };
  285. if (defined($json) && ref($json)) {
  286. push(@json_content, \@{ $json->{'items'}->{'list'} });
  287. } elsif ($DEBUG) {
  288. print STDERR 'JSON parsing failure on: ' . $fetch->content() . "\n";
  289. }
  290. } else {
  291. $content .= ' ' . $fetch->content();
  292. $content =~ s/\s+/ /g;
  293. }
  294. }
  295. # Find each TR element in TPB content
  296. my @tors = ();
  297. my @trs = split(/\<tr(?:\s+[^\>]*)?\>/i, $content);
  298. if ($DEBUG) {
  299. print STDERR 'TR count: ' . scalar(@trs) . "\n";
  300. }
  301. foreach my $tr (@trs) {
  302. # Trim trailing tags
  303. if (!($tr =~ s/\<\/tr\>.*$//is)) {
  304. # Skip things that aren't compelete TRs
  305. if ($DEBUG > 1) {
  306. print STDERR 'Skipping non-TR line: ' . $tr . "\n\n";
  307. }
  308. next;
  309. }
  310. # Find the show title
  311. my ($title) = $tr =~ /title\=\"Details\s+for\s+([^\"]*)\"/i;
  312. if (!defined($title) || length($title) < 1) {
  313. if ($DEBUG) {
  314. print STDERR "Unable to find show title in TR\n";
  315. }
  316. next;
  317. }
  318. $title =~ s/^\s+//;
  319. # Extract the season number
  320. my $isWholeSeason = 0;
  321. my $fileSeason = 0;
  322. if ($title =~ /s0?([1-9]\d*)/i) {
  323. $fileSeason = $1;
  324. if ((!defined($fileSeason) || $season < 1) && $title =~ /season\s*0?([1-9]\d*)/i) {
  325. $fileSeason = $1;
  326. $isWholeSeason = 1;
  327. }
  328. }
  329. # Extract the episode number
  330. my $episode = 0;
  331. if ($isWholeSeason) {
  332. $episode = $need[0];
  333. } elsif ($title =~ /e0*([1-9]\d*)/i) {
  334. $episode = $1;
  335. }
  336. # Extract the URL
  337. my ($url) = $tr =~ /\<a\s+href\=\"(magnet\:\?[^\"]+)\"/i;
  338. if (!defined($url) || length($url) < 1) {
  339. if ($DEBUG) {
  340. print STDERR "Skipping TR with no magnet URL\n";
  341. }
  342. next;
  343. }
  344. # Count the sum of seeders and leachers
  345. my $seeds = 0;
  346. my $leaches = 0;
  347. if ($tr =~ /\<td(?:\s+[^\>]*)?\>(\d+)\<\/td\>\s*\<td(?:\s+[^\>]*)?\>(\d+)\<\/td\>\s*$/i) {
  348. $seeds = $1;
  349. $leaches = $2;
  350. }
  351. # Extract the size
  352. my $size = 0;
  353. my $unit = 'M';
  354. if ($tr =~ m/ Size (\d+\.\d+)\&nbsp\;(G|M)iB/) {
  355. $size = $1;
  356. $unit = $2;
  357. if ($unit eq 'G') {
  358. $size *= 1024;
  359. }
  360. }
  361. $size = int($size);
  362. if ($DEBUG) {
  363. print STDERR 'Found file (' . $title . '): ' . $url . "\n";
  364. }
  365. # Save the extracted data
  366. my %tor = (
  367. 'title' => $title,
  368. 'is_season' => $isWholeSeason,
  369. 'season' => $fileSeason,
  370. 'episode' => $episode,
  371. 'seeds' => $seeds,
  372. 'leaches' => $leaches,
  373. 'size' => $size,
  374. 'url' => $url,
  375. 'source' => 'TPB'
  376. );
  377. push(@tors, \%tor);
  378. }
  379. # Find each discrete torrent from ISOhunt
  380. foreach my $json (@json_content) {
  381. foreach my $tor (@{$json}) {
  382. # Extract the title
  383. my $title = $tor->{'title'};
  384. $title =~ s/\<\/?b\>//gi;
  385. $title =~ s/^\s+//;
  386. # Extract the season number
  387. my $isWholeSeason = 0;
  388. my $fileSeason = 0;
  389. if ($tor->{'title'} =~ /s0?([1-9]\d*)/i) {
  390. $fileSeason = $1;
  391. if ((!defined($fileSeason) || $season < 1) && $tor->{'title'} =~ /season\s*0?([1-9]\d*)/i) {
  392. $fileSeason = $1;
  393. $isWholeSeason = 1;
  394. }
  395. }
  396. # Extract the episode number
  397. my $episode = 0;
  398. if ($isWholeSeason) {
  399. $episode = $need[0];
  400. } elsif ($tor->{'title'} =~ /e0*([1-9]\d*)/i) {
  401. $episode = $1;
  402. }
  403. # Count the sum of seeders and leachers
  404. my $seeds = 0;
  405. my $leaches = 0;
  406. if (defined($tor->{'Seeds'}) && length($tor->{'Seeds'}) > 0) {
  407. $seeds = $tor->{'Seeds'};
  408. }
  409. if (defined($tor->{'leechers'}) && length($tor->{'leechers'}) > 0) {
  410. $leaches = $tor->{'leechers'};
  411. }
  412. # Extract the size
  413. my $size = 0;
  414. if (defined($tor->{'length'}) && length($tor->{'length'}) > 0) {
  415. $size = int($tor->{'length'} / 1048576);
  416. }
  417. if ($DEBUG) {
  418. print STDERR 'Found file (' . $title . '): ' . $tor->{'enclosure_url'} . "\n";
  419. }
  420. # Save the extracted data
  421. my %tmp = (
  422. 'title' => $title,
  423. 'is_season' => $isWholeSeason,
  424. 'season' => $fileSeason,
  425. 'episode' => $episode,
  426. 'seeds' => $seeds,
  427. 'leaches' => $leaches,
  428. 'size' => $size,
  429. 'url' => $tor->{'enclosure_url'},
  430. 'source' => 'ISO'
  431. );
  432. push(@tors, \%tmp);
  433. }
  434. }
  435. # Filter for size/count/etc.
  436. my %tors = ();
  437. my $showRegex = $show;
  438. $showRegex =~ s/[\W_]+/\[\\W_\].*/g;
  439. foreach my $tor (@tors) {
  440. # Skip files that don't start with our show title
  441. if (!($tor->{'title'} =~ /^${showRegex}\b/i)) {
  442. if ($DEBUG) {
  443. print STDERR 'Skipping file: Title does not match (' . $showRegex . '): ' . $tor->{'url'} . "\n";
  444. }
  445. next;
  446. # Skip pre-air files
  447. } elsif ($tor->{'title'} =~ /preair/i) {
  448. if ($DEBUG) {
  449. print STDERR 'Skipping file: Title contains "preair": ' . $tor->{'url'} . "\n";
  450. }
  451. next;
  452. # Skip SWESUB files
  453. } elsif ($tor->{'title'} =~ /swesub/i) {
  454. if ($DEBUG) {
  455. print STDERR 'Skipping file: Title contains "SWESUB": ' . $tor->{'url'} . "\n";
  456. }
  457. next;
  458. # Skip .rus. files
  459. } elsif ($tor->{'title'} =~ /\.rus\./i) {
  460. if ($DEBUG) {
  461. print STDERR 'Skipping file: Title contains ".rus.": ' . $tor->{'url'} . "\n";
  462. }
  463. next;
  464. # Skip german files
  465. } elsif ($tor->{'title'} =~ /german/i) {
  466. if ($DEBUG) {
  467. print STDERR 'Skipping file: Title contains "german": ' . $tor->{'url'} . "\n";
  468. }
  469. next;
  470. # Skip torrents that are too small
  471. } elsif ($tor->{'size'} < $MIN_SIZE) {
  472. if ($DEBUG) {
  473. print STDERR 'Skipping file: Insufficient size (' . $tor->{'size'} . ' MiB): ' . $tor->{'url'} . "\n";
  474. }
  475. next;
  476. }
  477. # Only apply season and episode number matches if CUSTOMER_SEARCH is not in effect
  478. if (!$CUSTOM_SEARCH) {
  479. # Skip files that don't contain the right season number
  480. if (!$tor->{'is_season'} && (!defined($tor->{'season'}) || $tor->{'season'} != $season)) {
  481. if ($DEBUG) {
  482. print STDERR 'Skipping file: No match for season number (' . $season . '): ' . $tor->{'url'} . "\n";
  483. }
  484. next;
  485. # Skip files that don't contain the episode number
  486. } elsif ((!defined($tor->{'episode'}) || !$need{ $tor->{'episode'} })) {
  487. if ($DEBUG) {
  488. print STDERR 'Skipping file: No match for episode number (' . $tor->{'episode'} . '): ' . $tor->{'url'} . "\n";
  489. }
  490. next;
  491. }
  492. }
  493. # Only apply the quality rules if NO_QUALITY_CHECKS is not in effect
  494. if (!$NO_QUALITY_CHECKS) {
  495. # Skip torrents with too few seeders/leachers
  496. if (($tor->{'seeds'} + $tor->{'leaches'}) * $SOURCES{ $tor->{'source'} }->{'weight'} < $MIN_COUNT) {
  497. if ($DEBUG) {
  498. print STDERR 'Skipping file: Insufficient seeder/leacher count (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . '): ' . $tor->{'url'} . "\n";
  499. }
  500. next;
  501. # Skip torrents with unusual seed/leach ratios
  502. } elsif ($tor->{'seeds'} > 1
  503. && $tor->{'seeds'} < $SEED_RATIO_COUNT
  504. && $tor->{'seeds'} > $tor->{'leaches'} * $MAX_SEED_RATIO)
  505. {
  506. if ($DEBUG) {
  507. print STDERR 'Skipping file: Unusual seeder/leacher ratio (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . '): ' . $tor->{'url'} . "\n";
  508. }
  509. next;
  510. }
  511. }
  512. # Save good torrents
  513. push(@{ $tors{ $tor->{'episode'} } }, $tor);
  514. if ($DEBUG) {
  515. print STDERR 'Possible URL (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . ' seeds/leaches, ' . $tor->{'size'} . ' MiB): ' . $tor->{'url'} . "\n";
  516. }
  517. }
  518. # Find the average torrent size for each episode
  519. my %avg = ();
  520. foreach my $episode (keys(%tors)) {
  521. my $count = 0;
  522. $avg{$episode} = 0.001;
  523. foreach my $tor (@{ $tors{$episode} }) {
  524. $avg{$episode} += $tor->{'size'};
  525. $count++;
  526. }
  527. if ($count > 0) {
  528. $avg{$episode} /= $count;
  529. }
  530. if ($DEBUG) {
  531. print STDERR 'Episode ' . $episode . ' average size: ' . int($avg{$episode}) . " MiB\n";
  532. }
  533. }
  534. # Calculate an adjusted count based on the original count and the relative file size
  535. foreach my $episode (keys(%tors)) {
  536. foreach my $tor (@{ $tors{$episode} }) {
  537. my $count = $tor->{'seeds'} + $tor->{'leaches'};
  538. if ($tor->{'size'} >= $avg{$episode}) {
  539. $tor->{'adj_count'} = int($count * ($SIZE_BONUS * ($tor->{'size'} / $avg{$episode})));
  540. } else {
  541. $tor->{'adj_count'} = int($count * ((1 / $SIZE_PENALTY) * ($tor->{'size'} / $avg{$episode})));
  542. }
  543. if ($DEBUG) {
  544. print STDERR 'Possible URL (' . $tor->{'adj_count'} . ' size-adjusted sources): ' . $tor->{'url'} . "\n";
  545. }
  546. }
  547. }
  548. # Pick the best-adjusted-count torrent for each episode
  549. my %urls = ();
  550. foreach my $episode (keys(%tors)) {
  551. my $max = undef();
  552. foreach my $tor (@{ $tors{$episode} }) {
  553. if (!defined($max) || $tor->{'adj_count'} > $max) {
  554. $urls{$episode} = $tor->{'url'};
  555. $max = $tor->{'adj_count'};
  556. if ($DEBUG) {
  557. print STDERR 'New semi-final URL (adjusted count: ' . $tor->{'adj_count'} . '): ' . $tor->{'url'} . "\n";
  558. }
  559. } elsif ($DEBUG) {
  560. print STDERR 'Skipping for non-max adjusted count (' . $tor->{'adj_count'} . '): ' . $tor->{'url'} . "\n";
  561. }
  562. }
  563. }
  564. # Output
  565. foreach my $episode (keys(%tors)) {
  566. if (defined($urls{$episode})) {
  567. print $urls{$episode} . "\n";
  568. if ($DEBUG) {
  569. print STDERR 'Final URL: ' . $urls{$episode} . "\n";
  570. }
  571. } elsif ($DEBUG) {
  572. print STDERR 'No URL found for URL: ' . $episode . "\n";
  573. }
  574. }
  575. # Cleanup
  576. unlink($cookies);
  577. exit(0);