findTorrent.pl 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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 = 'http';
  15. my %ENABLE_SOURCE = ('TPB' => 0, 'ISO' => 1, 'KICK' => 1, 'Z' => 1);
  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. my $TRACKER_LOOKUP = 0;
  25. # App config
  26. my $DELAY = 4;
  27. my $TIMEOUT = 15;
  28. my $UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A';
  29. # Static tracker list, appended to all magnet URIs
  30. my @TRACKERS = ('udp://open.demonii.com:1337/announce', 'udp://tracker.publicbt.com:80/announce', 'udp://tracker.openbittorrent.com:80/announce', 'udp://9.rarbg.com:2710/announce');
  31. # Includes
  32. use URI::Encode qw(uri_encode);
  33. use HTML::Strip;
  34. use JSON;
  35. use File::Temp;
  36. use File::Spec;
  37. use File::Basename;
  38. use FindBin qw($Bin);
  39. use lib $Bin;
  40. use Fetch;
  41. # Prototypes
  42. sub resolveSecondary($);
  43. sub resolveTrackers($);
  44. sub splitTags($$$);
  45. sub findSE($);
  46. # Command line
  47. my ($dir, $search) = @ARGV;
  48. if (!defined($dir)) {
  49. die('Usage: ' . basename($0) . " input_directory [search_string]\n");
  50. }
  51. if ($ENV{'DEBUG'}) {
  52. $DEBUG = 1;
  53. }
  54. if ($ENV{'NO_QUALITY_CHECKS'}) {
  55. $NO_QUALITY_CHECKS = 1;
  56. }
  57. if ($ENV{'MORE_NUMBER_FORMATS'}) {
  58. $MORE_NUMBER_FORMATS = 1;
  59. }
  60. if (defined($ENV{'MIN_DAYS_BACK'})) {
  61. $MIN_DAYS_BACK = $ENV{'MIN_DAYS_BACK'};
  62. }
  63. if (defined($ENV{'MAX_DAYS_BACK'})) {
  64. $MAX_DAYS_BACK = $ENV{'MAX_DAYS_BACK'};
  65. }
  66. if (defined($ENV{'NEXT_EPISODES'})) {
  67. $NEXT_EPISODES = $ENV{'NEXT_EPISODES'};
  68. }
  69. # New fetch object
  70. my $cookies = mktemp('/tmp/findTorrent.cookies.XXXXXXXX');
  71. my $fetch = Fetch->new(
  72. 'cookiefile' => $cookies,
  73. 'timeout' => $TIMEOUT,
  74. 'uas' => $UA
  75. );
  76. # Search sources
  77. my %SOURCES = ();
  78. # The Pirate Bay
  79. if ($ENABLE_SOURCE{'TPB'}) {
  80. # Available TPB proxies, in order of preference
  81. my @TPBs = ('thepiratebay.se/search/', 'pirateproxy.se/search/', 'tpb.unblocked.co/search/');
  82. # Automatically select a proxy that returns a search page
  83. my $host = '';
  84. my $search_url = '';
  85. foreach my $url (@TPBs) {
  86. ($host) = $url =~ /^([^\/]+)/;
  87. $fetch->url($PROTOCOL . '://' . $host);
  88. if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /\bSearch\b/i) {
  89. $search_url = $url;
  90. last;
  91. } elsif ($DEBUG) {
  92. print STDERR 'TPB proxy not available: ' . $host . "\n";
  93. }
  94. }
  95. # Only add TPB if one of the proxies is up
  96. if ($search_url) {
  97. my %tmp = (
  98. 'host' => $host,
  99. 'search_url' => $search_url,
  100. 'search_suffix' => '/0/7/0',
  101. 'weight' => 1.5,
  102. 'quote' => 0
  103. );
  104. $SOURCES{'TPB'} = \%tmp;
  105. }
  106. }
  107. # ISOhunt
  108. if ($ENABLE_SOURCE{'ISO'}) {
  109. # Available ISOhunt proxies, in order of preference
  110. my @proxies = ('isohunters.net/torrents/?ihq=', 'isohunt.to/torrents/?ihq=');
  111. # Automatically select a proxy that returns the non-US page
  112. my $host = '';
  113. my $search_url = '';
  114. foreach my $url (@proxies) {
  115. ($host) = $url =~ /^([^\/]+)/;
  116. $fetch->url($PROTOCOL . '://' . $host);
  117. if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /Last\s+\d+\s+files\s+indexed/i) {
  118. $search_url = $url;
  119. last;
  120. } elsif ($DEBUG) {
  121. print STDERR 'ISOhunt proxy not available: ' . $host . "\n";
  122. }
  123. }
  124. # Only add ISOhunt if one of the proxies is up
  125. if ($search_url) {
  126. my %tmp = (
  127. 'host' => $host,
  128. 'search_url' => $search_url,
  129. 'search_suffix' => '',
  130. 'weight' => 0.30,
  131. 'quote' => 1
  132. );
  133. $SOURCES{'ISO'} = \%tmp;
  134. }
  135. }
  136. # Kickass
  137. if ($ENABLE_SOURCE{'KICK'}) {
  138. # Available Kickass proxies, in order of preference
  139. my @proxies = ('kickass.so/usearch/');
  140. # Automatically select a proxy that returns the non-US page
  141. my $host = '';
  142. my $search_url = '';
  143. foreach my $url (@proxies) {
  144. ($host) = $url =~ /^([^\/]+)/;
  145. $fetch->url($PROTOCOL . '://' . $host);
  146. if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /torrent name\<\/th\>/i) {
  147. $search_url = $url;
  148. last;
  149. } elsif ($DEBUG) {
  150. print STDERR 'Kickass proxy not available: ' . $host . "\n";
  151. }
  152. }
  153. # Only add Kickass if one of the proxies is up
  154. if ($search_url) {
  155. my %tmp = (
  156. 'host' => $host,
  157. 'search_url' => $search_url,
  158. 'search_suffix' => '/',
  159. 'weight' => 0.30,
  160. 'quote' => 1
  161. );
  162. $SOURCES{'KICK'} = \%tmp;
  163. }
  164. }
  165. # Torrentz
  166. if ($ENABLE_SOURCE{'Z'}) {
  167. # Available Torrentz proxies, in order of preference
  168. my @proxies = ('torrentz.eu/search?q=', 'torrentz.me/search?q=', 'torrentz.ch/search?q=', 'torrentz.in/search?q=');
  169. # Automatically select a proxy that returns the non-US page
  170. my $host = '';
  171. my $search_url = '';
  172. foreach my $url (@proxies) {
  173. ($host) = $url =~ /^([^\/]+)/;
  174. $fetch->url($PROTOCOL . '://' . $host);
  175. if ($fetch->fetch('nocheck' => 1) == 200 && $fetch->content() =~ /Indexing [\d\,]+ active torrents/i) {
  176. $search_url = $url;
  177. last;
  178. } elsif ($DEBUG) {
  179. print STDERR 'Torrentz proxy not available: ' . $host . "\n";
  180. }
  181. }
  182. # Only add Torrentz if one of the proxies is up
  183. if ($search_url) {
  184. my %tmp = (
  185. 'host' => $host,
  186. 'search_url' => $search_url,
  187. 'search_suffix' => '+peer+%3E+' . $MIN_COUNT,
  188. 'weight' => 1,
  189. 'quote' => 1
  190. );
  191. $SOURCES{'Z'} = \%tmp;
  192. }
  193. }
  194. my $SOURCE_COUNT = scalar(keys(%SOURCES));
  195. # Sanity check
  196. if ($SOURCE_COUNT < 1) {
  197. die("No sources available\n");
  198. }
  199. # Adjust the inter-page delay with respect to the number of unique sources
  200. $DELAY /= $SOURCE_COUNT;
  201. # Environment
  202. #if ($DEBUG) {
  203. # print STDERR `printenv` . "\n";
  204. #}
  205. # Figure out what we're searching for
  206. my $show = '';
  207. my @urls = ();
  208. my $CUSTOM_SEARCH = 0;
  209. my $season = 0;
  210. my @need = ();
  211. my %need = ();
  212. # Clean up the input "directory" path
  213. {
  214. # Allow use of the raw series name
  215. if (!($dir =~ /\//)) {
  216. $show = $dir;
  217. $dir = $TV_DIR . '/' . $dir;
  218. }
  219. # Allow use of relative paths
  220. $dir = File::Spec->rel2abs($dir);
  221. # Sanity check
  222. if (!-d $dir) {
  223. die('Invalid input directory: ' . $dir . "\n");
  224. }
  225. # Isolate the season from the path, if provided
  226. $dir =~ /\/Season\s+(\d+)\/?$/i;
  227. if ($1) {
  228. $season = $1;
  229. $dir = dirname($dir);
  230. }
  231. # If no season is provided find the latest
  232. if (!$season) {
  233. opendir(SERIES, $dir)
  234. or die("Unable to open series directory: ${!}\n");
  235. while (my $file = readdir(SERIES)) {
  236. if ($file =~ /^Season\s+(\d+)$/i) {
  237. if (!$season || $season < $1) {
  238. $season = $1;
  239. }
  240. }
  241. }
  242. closedir(SERIES);
  243. }
  244. # Isolate and clean the series name
  245. if (!$show) {
  246. $show = basename($dir);
  247. }
  248. $show =~ s/[\'\"\.]//g;
  249. if ($DEBUG) {
  250. print STDERR 'Checking directory: ' . $dir . "\n";
  251. }
  252. }
  253. # Allow quality checks to be disabled
  254. if (-e $dir . '/no_quality_checks') {
  255. $NO_QUALITY_CHECKS = 1;
  256. }
  257. # Read the excludes file, if any
  258. my $exclude = '';
  259. if (-e $dir . '/excludes') {
  260. local $/ = undef;
  261. open(EX, $dir . '/excludes')
  262. or die("Unable to open excludes file: ${!}\n");
  263. my $ex = <EX>;
  264. close(EX);
  265. $ex =~ s/^\s+//;
  266. $ex =~ s/^\s+//;
  267. my @excludes = split(/\s*,\s*/, $ex);
  268. foreach my $ex (@excludes) {
  269. if (length($exclude)) {
  270. $exclude .= ' ';
  271. }
  272. $exclude .= '-"' . $ex . '"';
  273. }
  274. $exclude = uri_encode(' ' . $exclude);
  275. }
  276. # Handle custom searches
  277. if ((scalar(@urls) < 1) && defined($search) && length($search) > 0) {
  278. # Note the custom search string
  279. $CUSTOM_SEARCH = 1;
  280. if ($DEBUG) {
  281. print STDERR "Custom search\n";
  282. }
  283. # Create the relevent search strings
  284. foreach my $key (keys(%SOURCES)) {
  285. my $source = $SOURCES{$key};
  286. push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search . $exclude . $source->{'search_suffix'});
  287. }
  288. }
  289. # Handle search-by-date series
  290. if ((scalar(@urls) < 1) && -e $dir . '/search_by_date') {
  291. # Note the search-by-date status
  292. # Set the CUSTOM_SERACH flag to skip season/epsiode matching
  293. $CUSTOM_SEARCH = 1;
  294. if ($DEBUG) {
  295. print STDERR "Find by date\n";
  296. }
  297. # Read the find-by-date string
  298. my $search_by_date = '';
  299. local ($/, *FH);
  300. open(FH, $dir . '/search_by_date')
  301. or die('Unable to read search_by_date for show: ' . $show . ': ' . $! . "\n");
  302. my $text = <FH>;
  303. close(FH);
  304. if ($text =~ /^\s*(\S.*\S)\s*$/) {
  305. $search_by_date = $1;
  306. } else {
  307. die('Skipping invalid search_by_date for show: ' . $show . ': ' . $text . "\n");
  308. }
  309. # Create search strings for each date in the range, unless the related file already exists
  310. my (%years, %months, %days) = ();
  311. for (my $days_back = $MIN_DAYS_BACK ; $days_back <= $MAX_DAYS_BACK ; $days_back++) {
  312. # Calculate the date
  313. my (undef(), undef(), undef(), $day, $month, $year) = localtime(time() - (86400 * $days_back));
  314. # Format as strings
  315. $year = sprintf('%04d', $year + 1900);
  316. $month = sprintf('%02d', $month + 1);
  317. $day = sprintf('%02d', $day);
  318. # Check for an existing file
  319. my $exists = 0;
  320. {
  321. my $season_dir = $dir . '/Season ' . $year;
  322. my $prefix = qr/${year}\-${month}\-${day}\s*\-\s*/;
  323. opendir(SEASON, $season_dir)
  324. or die("Unable to open season directory: ${!}\n");
  325. while (my $file = readdir(SEASON)) {
  326. if ($file =~ $prefix) {
  327. $exists = 1;
  328. last;
  329. }
  330. }
  331. closedir(SEASON);
  332. }
  333. if ($exists) {
  334. next;
  335. }
  336. # Save all the date string components
  337. $years{$year} = 1;
  338. $months{$month} = 1;
  339. $days{$day} = 1;
  340. # Create the relevent search strings
  341. my $search_str = $search_by_date;
  342. $search_str =~ s/%Y/${year}/g;
  343. $search_str =~ s/%m/${month}/g;
  344. $search_str =~ s/%d/${day}/g;
  345. foreach my $key (keys(%SOURCES)) {
  346. my $source = $SOURCES{$key};
  347. push(@urls, $PROTOCOL . '://' . $source->{'search_url'} . $search_str . $exclude . $source->{'search_suffix'});
  348. }
  349. }
  350. # Build a date string matching regex
  351. my $str = '\b(?:' . join('|', keys(%years)) . ')\b';
  352. $str .= '.*';
  353. $str .= '\b(?:' . join('|', keys(%months)) . ')\b';
  354. $str .= '.*';
  355. $str .= '\b(?:' . join('|', keys(%days)) . ')\b';
  356. $CUSTOM_SEARCH = qr/${str}/;
  357. }
  358. # Handle standard series
  359. if (scalar(@urls) < 1) {
  360. # Allow use of more number formats
  361. if (-e $dir . '/more_number_formats') {
  362. $MORE_NUMBER_FORMATS = 1;
  363. }
  364. # Allow the show name to be overriden
  365. {
  366. my $search_name = $dir . '/search_name';
  367. if (-e $search_name) {
  368. local ($/, *FH);
  369. open(FH, $search_name)
  370. or die('Unable to read search_name for show: ' . $show . ': ' . $! . "\n");
  371. my $text = <FH>;
  372. close(FH);
  373. if ($text =~ /^\s*(\S.*\S)\s*$/) {
  374. $show = $1;
  375. } else {
  376. print STDERR 'Skipping invalid search_name for show: ' . $show . ': ' . $text . "\n";
  377. }
  378. }
  379. }
  380. if ($DEBUG) {
  381. print STDERR 'Searching with series title: ' . $show . "\n";
  382. }
  383. # Validate the season number
  384. if (!defined($season) || $season < 1 || $season > 2000) {
  385. die('Invalid season number: ' . $show . ' => ' . $season . "\n");
  386. }
  387. # Get the last episode number
  388. my $no_next = 0;
  389. my %episodes = ();
  390. my $highest = 0;
  391. opendir(SEASON, $dir . '/Season ' . $season)
  392. or die("Unable to open season directory: ${!}\n");
  393. while (my $file = readdir(SEASON)) {
  394. # Skip support files
  395. if ($file =~ /\.(?:png|xml|jpg|gif|tbn|txt|nfo|torrent)\s*$/i) {
  396. next;
  397. }
  398. # Check for a season_done file
  399. if ($file eq 'season_done') {
  400. $no_next = 1;
  401. next;
  402. }
  403. # Extract the episode number
  404. my ($num) = $file =~ /^\s*\.?(\d+)\s*\-\s*/i;
  405. if (defined($num)) {
  406. $num = int($num);
  407. if ($DEBUG) {
  408. print STDERR 'Found episode number: ' . $num . ' in file: ' . $file . "\n";
  409. }
  410. # Record it
  411. $episodes{$num} = 1;
  412. # Track the highest episode number
  413. if ($num > $highest) {
  414. $highest = $num;
  415. }
  416. }
  417. }
  418. close(SEASON);
  419. # Assume we need the next 2 episodes, unless no_next is set (i.e. season_done)
  420. if (!$no_next) {
  421. for (my $i = 1 ; $i <= $NEXT_EPISODES ; $i++) {
  422. push(@need, $highest + $i);
  423. }
  424. }
  425. # Find any missing episodes
  426. for (my $i = 1 ; $i <= $highest ; $i++) {
  427. if (!$episodes{$i}) {
  428. push(@need, $i);
  429. }
  430. }
  431. if ($DEBUG) {
  432. print STDERR 'Needed episodes: ' . join(', ', @need) . "\n";
  433. }
  434. # Reverse the array for later matching
  435. foreach my $episode (@need) {
  436. $need{$episode} = 1;
  437. }
  438. # Construct a URL-friendly show name
  439. my $safeShow = $show;
  440. $safeShow =~ s/\s+\&\s+/ and /i;
  441. $safeShow =~ s/^\s*The\b//i;
  442. $safeShow =~ s/\s+\-\s+/ /g;
  443. $safeShow =~ s/[\'\:]//g;
  444. $safeShow =~ s/[^\w\"\-]+/ /g;
  445. $safeShow =~ s/^\s+//;
  446. $safeShow =~ s/\s+$//;
  447. $safeShow =~ s/\s\s+/ /g;
  448. $safeShow =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  449. $safeShow =~ s/\%20/\+/g;
  450. # Calculate possible name variations
  451. my @urlShowVarients = ();
  452. {
  453. # Default name
  454. push(@urlShowVarients, $safeShow);
  455. # Search for both "and" and "&"
  456. if ($safeShow =~ /\+and\+/i) {
  457. my $tmp = $safeShow;
  458. $tmp =~ s/\+and\+/\+%26\+/;
  459. push(@urlShowVarients, $tmp);
  460. }
  461. }
  462. # Construct the URL for each title varient of each needed episode
  463. foreach my $urlShow (@urlShowVarients) {
  464. foreach my $episode (@need) {
  465. my $episode_long = sprintf('%02d', $episode);
  466. my $season_long = sprintf('%02d', $season);
  467. foreach my $source (values(%SOURCES)) {
  468. # Use quotes around the show name if the source needs them
  469. my $quote = '%22';
  470. if (!$source->{'quote'}) {
  471. $quote = '';
  472. }
  473. # Calculate the compete search prefix and suffix to simplify later concatenations
  474. my $prefix = $PROTOCOL . '://' . $source->{'search_url'} . $quote . $urlShow . $quote;
  475. my $suffix = $exclude;
  476. if ($source->{'search_suffix'}) {
  477. $suffix .= $source->{'search_suffix'};
  478. }
  479. # SXXEYY
  480. my $url = $prefix . '+s' . $season_long . 'e' . $episode_long . $suffix;
  481. push(@urls, $url);
  482. # Extra searches for shows that have lazy/non-standard number formats
  483. if ($MORE_NUMBER_FORMATS) {
  484. # SXEY
  485. if ($season_long ne $season || $episode_long ne $episode) {
  486. $url = $prefix . '+s' . $season . 'e' . $episode . $exclude;
  487. push(@urls, $url);
  488. }
  489. # SXX EYY
  490. $url = $prefix . '+s' . $season_long . '+e' . $episode_long . $exclude;
  491. push(@urls, $url);
  492. # Season XX Episode YY
  493. $url = $prefix . '+season+' . $season_long . '+episode+' . $episode_long . $exclude;
  494. push(@urls, $url);
  495. # Series X Episode Y
  496. $url = $prefix . '+series+' . $season . '+episode+' . $episode . $exclude;
  497. push(@urls, $url);
  498. # SxEE
  499. $url = $prefix . '+' . $season . 'x' . $episode_long . $exclude;
  500. push(@urls, $url);
  501. }
  502. }
  503. }
  504. }
  505. }
  506. my @html_content = ();
  507. my @json_content = ();
  508. foreach my $url (@urls) {
  509. # Fetch the page
  510. if ($DEBUG) {
  511. print STDERR 'Searching with URL: ' . $url . "\n";
  512. $fetch->file('/tmp/findTorrent-lastPage.html');
  513. }
  514. sleep($DELAY * (rand(2) + 0.5));
  515. $fetch->url($url);
  516. $fetch->fetch('nocheck' => 1);
  517. # Check for useful errors
  518. if ($fetch->status_code() == 404) {
  519. if ($DEBUG) {
  520. print STDERR "Skipping content from 404 response\n";
  521. }
  522. next;
  523. }
  524. # Check for errors
  525. if ($fetch->status_code() != 200) {
  526. print STDERR 'Error fetching URL (' . $fetch->status_code() . '): ' . $url . "\n";
  527. next;
  528. }
  529. # Save the content, discriminating by data type
  530. if ($DEBUG) {
  531. print STDERR 'Fetched ' . length($fetch->content()) . " bytes\n";
  532. }
  533. if ($fetch->content() =~ /^\s*{/) {
  534. my $json = eval { decode_json($fetch->content()); };
  535. if (defined($json) && ref($json)) {
  536. push(@json_content, \@{ $json->{'items'}->{'list'} });
  537. } elsif ($DEBUG) {
  538. print STDERR 'JSON parsing failure on: ' . $fetch->content() . "\n";
  539. }
  540. } else {
  541. push(@html_content, scalar($fetch->content()));
  542. }
  543. }
  544. # We need someplace to store parsed torrent data from the various sources
  545. my @tors = ();
  546. # Handle HTML content
  547. foreach my $content (@html_content) {
  548. if ($content =~ /\<title\>The Pirate Bay/i) {
  549. # Find TR elements from TPB
  550. my @trs = splitTags($content, 'TR', 'TH');
  551. foreach my $tr (@trs) {
  552. # Find the show title
  553. my ($title) = $tr =~ /title\=\"Details\s+for\s+([^\"]*)\"/i;
  554. if (!defined($title) || length($title) < 1) {
  555. if ($DEBUG) {
  556. print STDERR "Unable to find show title in TR\n\t" . $tr . "\n";
  557. }
  558. next;
  559. }
  560. $title =~ s/^\s+//;
  561. # Extract the season and episode numbers
  562. my ($fileSeason, $episode) = findSE($title);
  563. # Extract the URL
  564. my ($url) = $tr =~ /\<a\s+href\=\"(magnet\:\?[^\"]+)\"/i;
  565. if (!defined($url) || length($url) < 1) {
  566. if ($DEBUG) {
  567. print STDERR "Skipping TR with no magnet URL\n";
  568. }
  569. next;
  570. }
  571. # Count the sum of seeders and leachers
  572. my $seeds = 0;
  573. my $leaches = 0;
  574. if ($tr =~ /\<td(?:\s+[^\>]*)?\>(\d+)\<\/td\>\s*\<td(?:\s+[^\>]*)?\>(\d+)\<\/td\>\s*$/i) {
  575. $seeds = $1;
  576. $leaches = $2;
  577. }
  578. # Extract the size (from separate column or inline)
  579. my $size = 0;
  580. my $unit = 'M';
  581. if ($tr =~ m/Size (\d+(?:\.\d+)?)\&nbsp\;(G|M)iB/) {
  582. $size = $1;
  583. $unit = $2;
  584. } elsif ($tr =~ m/(\d+(?:\.\d+)?)\&nbsp\;(G|M)iB\<\/[tT][dD]\>/) {
  585. $size = $1;
  586. $unit = $2;
  587. }
  588. if ($unit eq 'G') {
  589. $size *= 1024;
  590. }
  591. $size = int($size);
  592. if ($DEBUG) {
  593. print STDERR 'Found file (' . $title . '): ' . $url . "\n";
  594. }
  595. # Save the extracted data
  596. my %tor = (
  597. 'title' => $title,
  598. 'season' => $fileSeason,
  599. 'episode' => $episode,
  600. 'seeds' => $seeds,
  601. 'leaches' => $leaches,
  602. 'size' => $size,
  603. 'url' => $url,
  604. 'source' => 'TPB'
  605. );
  606. push(@tors, \%tor);
  607. }
  608. } elsif ($content =~ /Isohunt Torrent Search Engine\<\/title\>/i) {
  609. # Find each TR element from ISOHunt
  610. my @trs = splitTags($content, 'TR', 'TH');
  611. foreach my $tr (@trs) {
  612. # Split each TD element from the row
  613. my @tds = split(/\<td(?:\s+[^\>]*)?\>/i, $tr);
  614. my $numCols = scalar(@tds) - 1;
  615. if ($numCols > 9 && $numCols < 8) {
  616. print STDERR 'Invalid ISO TR: ' . $tr . "\n";
  617. next;
  618. }
  619. # Skip sponsored results
  620. if ($tds[1] =~ /Sponsored\<\/td\>/i) {
  621. if ($DEBUG > 1) {
  622. print STDERR "Skipping sponsored result\n";
  623. }
  624. next;
  625. }
  626. # Find the torrent ID and show title
  627. my ($id, $title) = $tds[2] =~ /\<a(?:\s+[^\>]*)?href=\"\/torrent_details\/(\d+)\/[^\"]*\"\>\<span\>([^\>]+)\<\/span\>/i;
  628. if (!defined($id) || length($id) < 1 || !defined($title) || length($title) < 1) {
  629. if ($DEBUG) {
  630. print STDERR "Unable to find show ID or title in TD:\n\t" . $tds[2] . "\n";
  631. }
  632. next;
  633. }
  634. $title =~ s/^\s+//;
  635. # Extract the season and episode numbers
  636. my ($fileSeason, $episode) = findSE($title);
  637. # Extract the size
  638. my $size = 0;
  639. my $unit = 'M';
  640. if ($tds[6] =~ m/(\d+(?:\.\d+)?) (G|M)B<\/td\>/i) {
  641. $size = $1;
  642. $unit = $2;
  643. }
  644. if ($unit eq 'G') {
  645. $size *= 1024;
  646. }
  647. $size = int($size);
  648. # Count the sum of seeders and leachers
  649. my $seeds = 0;
  650. if ($tds[7] =~ /(\d+)\<\/td\>/i) {
  651. $seeds = $1;
  652. }
  653. my $leaches = 0;
  654. if ($tds[8] =~ /(\d+)\<\/td\>/i) {
  655. $leaches = $1;
  656. }
  657. # Fetch the detail page for the URL
  658. my $url = $PROTOCOL . '://' . $SOURCES{'ISO'}->{'host'} . '/torrent_details/' . $id . '/';
  659. if ($DEBUG) {
  660. print STDERR 'Found file (' . $title . '): ' . $url . "\n";
  661. }
  662. # Save the extracted data
  663. my %tor = (
  664. 'title' => $title,
  665. 'season' => $fileSeason,
  666. 'episode' => $episode,
  667. 'seeds' => $seeds,
  668. 'leaches' => $leaches,
  669. 'size' => $size,
  670. 'url' => $url,
  671. 'source' => 'ISO'
  672. );
  673. push(@tors, \%tor);
  674. }
  675. } elsif ($content =~ /KickassTorrents\<\/title\>/i) {
  676. # Find each TR element from Kickass
  677. my @trs = splitTags($content, 'TR', 'TH');
  678. foreach my $tr (@trs) {
  679. # Split each TD element from the row
  680. my @tds = split(/\<td(?:\s+[^\>]*)?\>/i, $tr);
  681. my $numCols = scalar(@tds) - 1;
  682. if ($numCols > 9 && $numCols < 8) {
  683. print STDERR 'Invalid KICK TR: ' . $tr . "\n";
  684. next;
  685. }
  686. # Find the torrent title
  687. my ($title) = $tds[1] =~ /\<a(?:\s+[^\>]*)?href\=\"[^\"]+\?title\=\[[^\]\"]+\]([^\"]+)\"/i;
  688. if (!defined($title) || length($title) < 1) {
  689. if ($DEBUG) {
  690. print STDERR "Unable to find torrent title in TD:\n\t" . $tds[1] . "\n";
  691. }
  692. next;
  693. }
  694. $title =~ s/^\s+//;
  695. # Find the torrent URL
  696. my ($url) = $tds[1] =~ /\<a(?:\s+[^\>]*)?href=\"(magnet:[^\"]+)\"/i;
  697. if (!defined($url) || length($url) < 1) {
  698. if ($DEBUG) {
  699. print STDERR "Unable to find torrent URL in TD:\n\t" . $tds[1] . "\n";
  700. }
  701. next;
  702. }
  703. # Extract the season and episode numbers
  704. my ($fileSeason, $episode) = findSE($title);
  705. # Extract the size
  706. my $size = 0;
  707. my $unit = 'M';
  708. if ($tds[2] =~ m/(\d+(?:\.\d+)?)\s+\<span\>(G|M)B\<\/span\>\<\/td\>/i) {
  709. $size = $1;
  710. $unit = $2;
  711. }
  712. if ($unit eq 'G') {
  713. $size *= 1024;
  714. }
  715. $size = int($size);
  716. # Count the sum of seeders and leachers
  717. my $seeds = 0;
  718. if ($tds[5] =~ /(\d+)\<\/td\>/i) {
  719. $seeds = $1;
  720. }
  721. my $leaches = 0;
  722. if ($tds[6] =~ /(\d+)\<\/td\>/i) {
  723. $leaches = $1;
  724. }
  725. if ($DEBUG) {
  726. print STDERR 'Found file (' . $title . '): ' . $url . "\n";
  727. }
  728. # Save the extracted data
  729. my %tor = (
  730. 'title' => $title,
  731. 'season' => $fileSeason,
  732. 'episode' => $episode,
  733. 'seeds' => $seeds,
  734. 'leaches' => $leaches,
  735. 'size' => $size,
  736. 'url' => $url,
  737. 'source' => 'KICK'
  738. );
  739. push(@tors, \%tor);
  740. }
  741. } elsif ($content =~ /\<a(?:\s+[^\>]*)?\>Torrentz\<\/a\>/i) {
  742. # We'll need to strip HTML
  743. my $hs = HTML::Strip->new();
  744. # Find each DL element from Torrentz
  745. my @dls = splitTags($content, 'DL', undef());
  746. foreach my $dl (@dls) {
  747. # Skip ads
  748. if ($dl =~ /\s+rel\=\"nofollow/i) {
  749. next;
  750. }
  751. # Find the torrent title
  752. my ($hash, $title) = $dl =~ /\<a href\=\"\/(\w+)\">(.*)\<\/a\>/i;
  753. if (!defined($hash) || length($hash) < 1 || !defined($title) || length($title) < 1) {
  754. if ($DEBUG) {
  755. print STDERR "Unable to find torrent title in DL:\n\t" . $dl . "\n";
  756. }
  757. next;
  758. }
  759. $title = $hs->parse($title);
  760. $hs->eof;
  761. $title =~ s/^\s+//;
  762. # Extract the season and episode numbers
  763. my ($fileSeason, $episode) = findSE($title);
  764. # Extract the size
  765. my ($size, $unit) = $dl =~ /\<span class\=\"s\">(\d+(?:\.\d+)?) (G|M)B\<\/span\>/i;
  766. if ($unit eq 'G') {
  767. $size *= 1024;
  768. }
  769. $size = int($size);
  770. # Count the sum of seeders and leachers
  771. my $seeds = 0;
  772. if ($dl =~ /<span class\=\"u\">(\d+)\<\/span\>/i) {
  773. $seeds = $1;
  774. }
  775. my $leaches = 0;
  776. if ($dl =~ /<span class\=\"d\">(\d+)\<\/span\>/i) {
  777. $leaches = $1;
  778. }
  779. # Construct a magnet URL from the hash
  780. # Assume the seconary lookup will append a list of trackers
  781. my $url = 'magnet:?xt=urn:btih:' . $hash;
  782. if ($DEBUG) {
  783. print STDERR 'Found file (' . $title . '): ' . $url . "\n";
  784. }
  785. # Save the extracted data
  786. my %tor = (
  787. 'title' => $title,
  788. 'season' => $fileSeason,
  789. 'episode' => $episode,
  790. 'seeds' => $seeds,
  791. 'leaches' => $leaches,
  792. 'size' => $size,
  793. 'url' => $url,
  794. 'source' => 'Z'
  795. );
  796. push(@tors, \%tor);
  797. }
  798. } else {
  799. print STDERR "Unknown HTML content:\n" . $content . "\n\n";
  800. }
  801. }
  802. # Handle JSON content
  803. foreach my $content (@json_content) {
  804. print STDERR "Unknown JSON content:\n" . $content . "\n\n";
  805. }
  806. # Filter for size/count/etc.
  807. my %tors = ();
  808. my $showRegex = undef();
  809. {
  810. my $showClean = $show;
  811. $showClean =~ s/[\"\']//g;
  812. $showClean =~ s/[\W_]+/\[\\W_\].*/g;
  813. $showRegex = qr/^${showClean}[\W_]/i;
  814. }
  815. foreach my $tor (@tors) {
  816. # Skip files that don't start with our show title
  817. if (!($tor->{'title'} =~ $showRegex)) {
  818. if ($DEBUG) {
  819. print STDERR 'Skipping file: Title does not match (' . $showRegex . '): ' . $tor->{'title'} . "\n";
  820. }
  821. next;
  822. # Skip pre-air files
  823. } elsif ($tor->{'title'} =~ /preair/i) {
  824. if ($DEBUG) {
  825. print STDERR 'Skipping file: Title contains "preair": ' . $tor->{'title'} . "\n";
  826. }
  827. next;
  828. # Skip SWESUB files
  829. } elsif ($tor->{'title'} =~ /swesub/i) {
  830. if ($DEBUG) {
  831. print STDERR 'Skipping file: Title contains "SWESUB": ' . $tor->{'title'} . "\n";
  832. }
  833. next;
  834. # Skip .rus. files
  835. } elsif ($tor->{'title'} =~ /\.rus\./i) {
  836. if ($DEBUG) {
  837. print STDERR 'Skipping file: Title contains ".rus.": ' . $tor->{'title'} . "\n";
  838. }
  839. next;
  840. # Skip German files
  841. } elsif ($tor->{'title'} =~ /german/i) {
  842. if ($DEBUG) {
  843. print STDERR 'Skipping file: Title contains "german": ' . $tor->{'title'} . "\n";
  844. }
  845. next;
  846. # Skip French files
  847. } elsif ($tor->{'title'} =~ /french/i) {
  848. if ($DEBUG) {
  849. print STDERR 'Skipping file: Title contains "french": ' . $tor->{'title'} . "\n";
  850. }
  851. next;
  852. # Skip unedited files
  853. } elsif ($tor->{'title'} =~ /unedited/i) {
  854. if ($DEBUG) {
  855. print STDERR 'Skipping file: Title contains "unedited": ' . $tor->{'title'} . "\n";
  856. }
  857. next;
  858. }
  859. # Enforce season and episode number matches for standard searches, or CUSTOM_SEARCH matching (if it's a regex)
  860. if (!$CUSTOM_SEARCH) {
  861. # Skip files that don't contain the right season number
  862. if (!defined($tor->{'season'}) || $tor->{'season'} != $season) {
  863. if ($DEBUG) {
  864. print STDERR 'Skipping file: No match for season number (' . $season . '): ' . $tor->{'title'} . "\n";
  865. }
  866. next;
  867. # Skip files that don't contain the episode number
  868. } elsif ((!defined($tor->{'episode'}) || !$need{ $tor->{'episode'} })) {
  869. if ($DEBUG) {
  870. print STDERR 'Skipping file: No match for episode number (' . $tor->{'episode'} . '): ' . $tor->{'title'} . "\n";
  871. }
  872. next;
  873. }
  874. } elsif (ref($CUSTOM_SEARCH) eq 'Regexp') {
  875. # Skip files that don't match the regex
  876. if (!($tor->{'title'} =~ $CUSTOM_SEARCH)) {
  877. if ($DEBUG) {
  878. print STDERR 'Skipping file: No match for CUSTOM_SEARCH regex (' . $CUSTOM_SEARCH . '): ' . $tor->{'title'} . "\n";
  879. }
  880. next;
  881. }
  882. }
  883. # Only apply the quality rules if NO_QUALITY_CHECKS is not in effect
  884. if (!$NO_QUALITY_CHECKS) {
  885. # Skip torrents with too few seeders/leachers
  886. if (($tor->{'seeds'} + $tor->{'leaches'}) * $SOURCES{ $tor->{'source'} }->{'weight'} < $MIN_COUNT) {
  887. if ($DEBUG) {
  888. print STDERR 'Skipping file: Insufficient seeder/leacher count (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . '): ' . $tor->{'title'} . "\n";
  889. }
  890. next;
  891. # Skip torrents with unusual seed/leach ratios
  892. } elsif ($tor->{'seeds'} > 1
  893. && $tor->{'seeds'} < $SEED_RATIO_COUNT
  894. && $tor->{'seeds'} > $tor->{'leaches'} * $MAX_SEED_RATIO)
  895. {
  896. if ($DEBUG) {
  897. print STDERR 'Skipping file: Unusual seeder/leacher ratio (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . '): ' . $tor->{'title'} . "\n";
  898. }
  899. next;
  900. # Skip torrents that are too small
  901. } elsif ($tor->{'size'} < $MIN_SIZE) {
  902. if ($DEBUG) {
  903. print STDERR 'Skipping file: Insufficient size (' . $tor->{'size'} . ' MiB): ' . $tor->{'title'} . "\n";
  904. }
  905. next;
  906. }
  907. }
  908. # Save good torrents
  909. push(@{ $tors{ $tor->{'episode'} } }, $tor);
  910. if ($DEBUG) {
  911. print STDERR 'Possible URL (' . $tor->{'seeds'} . '/' . $tor->{'leaches'} . ' seeds/leaches, ' . $tor->{'size'} . ' MiB): ' . $tor->{'title'} . "\n";
  912. }
  913. }
  914. # Find the average torrent size for each episode
  915. my %avg = ();
  916. foreach my $episode (keys(%tors)) {
  917. my $count = 0;
  918. $avg{$episode} = 0.001;
  919. foreach my $tor (@{ $tors{$episode} }) {
  920. $avg{$episode} += $tor->{'size'};
  921. $count++;
  922. }
  923. if ($count > 0) {
  924. $avg{$episode} /= $count;
  925. }
  926. if ($DEBUG) {
  927. print STDERR 'Episode ' . $episode . ' average size: ' . int($avg{$episode}) . " MiB\n";
  928. }
  929. }
  930. # Calculate an adjusted count based on the peer count, relative file size, and title contents
  931. foreach my $episode (keys(%tors)) {
  932. foreach my $tor (@{ $tors{$episode} }) {
  933. my $count = $tor->{'seeds'} + $tor->{'leaches'};
  934. # Start with the peer count
  935. $tor->{'adj_count'} = $count;
  936. # Adjust based on file size
  937. {
  938. my $size_ratio = $tor->{'size'} / $avg{$episode};
  939. if ($tor->{'size'} >= $avg{$episode}) {
  940. $tor->{'adj_count'} *= $SIZE_BONUS * $size_ratio;
  941. } else {
  942. $tor->{'adj_count'} *= (1 / $SIZE_PENALTY) * $size_ratio;
  943. }
  944. }
  945. # Adjust based on title contents
  946. if ($tor->{'title'} =~ /Subtitulado/i) {
  947. $tor->{'adj_count'} *= 1 / $TITLE_PENALTY;
  948. }
  949. # Truncate to an integer
  950. $tor->{'adj_count'} = int($tor->{'adj_count'});
  951. if ($DEBUG) {
  952. print STDERR 'Possible URL (' . $tor->{'adj_count'} . ' size-adjusted sources): ' . $tor->{'url'} . "\n";
  953. }
  954. }
  955. }
  956. # Pick the best-adjusted-count torrent for each episode
  957. my %urls = ();
  958. foreach my $episode (keys(%tors)) {
  959. my @sorted = sort { $b->{'adj_count'} <=> $a->{'adj_count'} } @{ $tors{$episode} };
  960. my $max = undef();
  961. foreach my $tor (@sorted) {
  962. if (!defined($max) || $tor->{'adj_count'} > $max) {
  963. if ($urls{$episode} = resolveSecondary($tor)) {
  964. $max = $tor->{'adj_count'};
  965. }
  966. if ($DEBUG) {
  967. print STDERR 'Semi-final URL (adjusted count: ' . $tor->{'adj_count'} . '): ' . $tor->{'url'} . "\n";
  968. }
  969. } elsif ($DEBUG) {
  970. print STDERR 'Skipping for lesser adjusted count (' . $tor->{'adj_count'} . '): ' . $tor->{'url'} . "\n";
  971. }
  972. }
  973. }
  974. # Output
  975. foreach my $episode (keys(%tors)) {
  976. if (defined($urls{$episode})) {
  977. $urls{$episode} = resolveTrackers($urls{$episode});
  978. print $urls{$episode} . "\n";
  979. if ($DEBUG) {
  980. print STDERR 'Final URL: ' . $urls{$episode} . "\n";
  981. }
  982. } elsif ($DEBUG) {
  983. print STDERR 'No URL for: ' . $episode . "\n";
  984. }
  985. }
  986. # Cleanup
  987. unlink($cookies);
  988. exit(0);
  989. # Resolve secondary URLs
  990. sub resolveSecondary($) {
  991. my ($tor) = @_;
  992. my $url = $tor->{'url'};
  993. if ($tor->{'source'} eq 'ISO') {
  994. if ($DEBUG) {
  995. print STDERR 'Secondary fetch with URL: ' . $tor->{'url'} . "\n";
  996. $fetch->file('/tmp/findTorrent-secondary.html');
  997. }
  998. sleep($DELAY * (rand(2) + 0.5));
  999. $fetch->url($tor->{'url'});
  1000. $fetch->fetch();
  1001. if ($fetch->status_code() != 200) {
  1002. print STDERR 'Error fetching secondary URL: ' . $tor->{'url'} . "\n";
  1003. return undef();
  1004. }
  1005. ($url) = $fetch->content() =~ /\<a\s+href\=\"(magnet\:\?[^\"]+)\"/i;
  1006. if (!defined($url) || length($url) < 1) {
  1007. if ($DEBUG) {
  1008. print STDERR 'No secondary URL available from: ' . $tor->{'url'} . "\n";
  1009. }
  1010. return undef();
  1011. }
  1012. }
  1013. return $url;
  1014. }
  1015. sub resolveTrackers($) {
  1016. my ($url) = @_;
  1017. # Always append a few static trackers to the URI
  1018. if ($url =~ /^magnet\:/i) {
  1019. foreach my $tracker (@TRACKERS) {
  1020. $url .= '&tr=' . uri_encode($tracker, { 'encode_reserved' => 1 });
  1021. }
  1022. }
  1023. # Fetch a dynamic tracker list for any hashinfo from TorrentZ
  1024. if ($TRACKER_LOOKUP && defined($SOURCES{'Z'}) && $url =~ /^magnet\:/ && $url =~ /\bxt\=urn\:btih\:(\w+)/i) {
  1025. my $hash = $1;
  1026. my $baseURL = $PROTOCOL . '://' . $SOURCES{'Z'}->{'host'};
  1027. my $hashURL = $baseURL . '/' . $hash;
  1028. if ($DEBUG) {
  1029. print STDERR 'Hashinfo fetch with URL: ' . $hashURL . "\n";
  1030. $fetch->file('/tmp/findTorrent-hashinfo.html');
  1031. }
  1032. sleep($DELAY * (rand(2) + 0.5));
  1033. $fetch->url($hashURL);
  1034. $fetch->fetch();
  1035. if ($fetch->status_code() != 200) {
  1036. print STDERR 'Error fetching hashinfo URL: ' . $hashURL . "\n";
  1037. return $url;
  1038. }
  1039. # Parse out and fetch the tracker list URL
  1040. my ($list) = $fetch->content() =~ /\<a rel\=\"nofollow\" href\=\"(\/announcelist_\d+)\"\>/;
  1041. if (!defined($list)) {
  1042. print STDERR "Unable to find tracker list link\n";
  1043. return $url;
  1044. }
  1045. my $listURL = $baseURL . $list;
  1046. if ($DEBUG) {
  1047. print STDERR 'Tracker list fetch with URL: ' . $listURL . "\n";
  1048. $fetch->file('/tmp/findTorrent-tracker.html');
  1049. }
  1050. sleep($DELAY * (rand(2) + 0.5));
  1051. $fetch->url($listURL);
  1052. $fetch->fetch();
  1053. if ($fetch->status_code() != 200) {
  1054. print STDERR 'Error fetching tracker list URL: ' . $listURL . "\n";
  1055. return $url;
  1056. }
  1057. # Parse the tracker list and append our URL
  1058. foreach my $line ($fetch->content()) {
  1059. if ($line =~ /^\s*$/) {
  1060. next;
  1061. }
  1062. $line =~ s/^\s+//;
  1063. $line =~ s/\s+$//;
  1064. if ($line =~ /^(?:http|udp)\:/i) {
  1065. if ($DEBUG) {
  1066. print STDERR 'Adding tracker: ' . $line . "\n";
  1067. }
  1068. $url .= '&tr=' . uri_encode($line, { 'encode_reserved' => 1 });
  1069. } else {
  1070. print STDERR 'Unknown tracker type: ' . $line . "\n";
  1071. }
  1072. }
  1073. }
  1074. return $url;
  1075. }
  1076. sub splitTags($$$) {
  1077. my ($content, $tag, $header) = @_;
  1078. # Find each tag element
  1079. my @out = ();
  1080. my @parts = split(/\<${tag}(?:\s+[^\>]*)?\>/i, $content);
  1081. foreach my $part (@parts) {
  1082. # Trim trailing tags and skip things that aren't complete tags
  1083. if (!($part =~ s/\<\/${tag}\>.*$//is)) {
  1084. if ($DEBUG > 1) {
  1085. print STDERR 'Skipping non-tag line: ' . $part . "\n\n";
  1086. }
  1087. next;
  1088. }
  1089. # Skip headers
  1090. if (defined($header) && $part =~ /<${header}(?:\s+[^\>]*)?\>/i) {
  1091. if ($DEBUG > 2) {
  1092. print STDERR 'Skipping header line: ' . $part . "\n\n";
  1093. }
  1094. next;
  1095. }
  1096. # Save good tags
  1097. push(@out, $part);
  1098. }
  1099. return @out;
  1100. }
  1101. # Extract season
  1102. sub findSE($) {
  1103. my ($title) = @_;
  1104. my $season = 0;
  1105. my $episode = 0;
  1106. if ($title =~ /[\W_]s(?:eason|eries)?\s*(\d+)/i) {
  1107. $season = $1;
  1108. } elsif ($title =~ /\b(\d+)x(\d+)\b/i) {
  1109. $season = $1;
  1110. $episode = $2;
  1111. }
  1112. if (!$episode && $title =~ /e(?:ipsode)?\s*(\d{1,2})/i) {
  1113. $episode = $1;
  1114. }
  1115. $season = int($season);
  1116. $episode = int($episode);
  1117. return ($season, $episode);
  1118. }