findTorrent.pl 16 KB

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