findTorrent.pl 16 KB

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