transmission.pl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. # Includes
  5. use JSON;
  6. use Date::Parse;
  7. use File::Copy;
  8. use File::Touch;
  9. use File::Temp 'mktemp';
  10. use File::Path 'remove_tree';
  11. use File::Basename;
  12. use Image::ExifTool qw/ :Public/;
  13. use FindBin qw($Bin);
  14. use lib $Bin;
  15. use Fetch;
  16. # Prototypes
  17. sub session($);
  18. sub fetch($);
  19. sub getSSE($);
  20. sub getDest($$$$);
  21. sub storeFiles($$$);
  22. sub readNZB($$);
  23. sub storeNZB($);
  24. sub storeTor($);
  25. sub delNZB($);
  26. sub delTor($);
  27. sub guessExt($);
  28. sub processFile($$);
  29. sub unrar($$);
  30. sub seriesCleanupLocal($);
  31. sub seriesCleanupCore($);
  32. sub seriesCleanup($);
  33. sub readDir($$);
  34. sub findMediaFiles($$);
  35. sub available($);
  36. # Parameters
  37. my $maxAge = 2.5 * 86400;
  38. my $tvDir = `~/bin/video/mediaPath` . '/TV';
  39. my $monitoredExec = $ENV{'HOME'} . '/bin/video/torrentMonitored.pl';
  40. my $content =
  41. '{"method":"torrent-get","arguments":{"fields":["hashString","id","addedDate","comment","creator",'
  42. . '"dateCreated","isPrivate","name","totalSize","pieceCount","pieceSize","downloadedEver","error","errorString",'
  43. . '"eta","haveUnchecked","haveValid","leftUntilDone","metadataPercentComplete","peersConnected","peersGettingFromUs",'
  44. . '"peersSendingToUs","rateDownload","rateUpload","recheckProgress","sizeWhenDone","status","trackerStats",'
  45. . '"uploadedEver","uploadRatio","seedRatioLimit","seedRatioMode","downloadDir","files","fileStats"]}}';
  46. my $delContent = '{"method":"torrent-remove","arguments":{"ids":["#_ID_#"], "delete-local-data":"true"}';
  47. my $history = '{"method":"history","params":[]}';
  48. my $delHistory = '{"method":"editqueue","params":["historyFinalDelete","",[#_ID_#]]}';
  49. my $delSleep = 2;
  50. my $RAR_MIN_FILES = 4;
  51. my $FIND_MAX_DEPTH = 5;
  52. my $CONF_FILE = $ENV{'HOME'} . '/.download.config';
  53. # Environment
  54. my $DEBUG = 0;
  55. if ($ENV{'DEBUG'}) {
  56. if ($ENV{'DEBUG'} =~ /(\d+)/) {
  57. $DEBUG = $1;
  58. } else {
  59. $DEBUG = 1;
  60. }
  61. }
  62. my $DELETE = 0;
  63. if ($ENV{'DELETE'}) {
  64. print STDERR "Failure mode: DELETE\n";
  65. $DELETE = 1;
  66. }
  67. # Command-line parameters
  68. my ($hash) = @ARGV;
  69. # Config file
  70. my %CONFIG = ();
  71. if ($CONF_FILE && -r $CONF_FILE) {
  72. my $fh;
  73. open($fh, '<', $CONF_FILE)
  74. or die('Unable to open config file: ' . $CONF_FILE . ': ' . $! . "\n");
  75. while (<$fh>) {
  76. # Skip blank lines and comments
  77. if (/^\s*#/ || /^\s*$/) {
  78. next;
  79. }
  80. # Assume everything else is bash variables
  81. if (my ($key, undef(), $val) = $_ =~ /^\s*(\S[^\=]+)\=([\'\"])(.*)\g{-2}\s*$/) {
  82. $CONFIG{$key} = $val;
  83. if ($DEBUG > 1) {
  84. print STDERR 'Adding config: ' . $key . ' => ' . $val . "\n";
  85. }
  86. } else {
  87. warn('Ignoring config line: ' . $_ . "\n");
  88. }
  89. }
  90. close($fh);
  91. }
  92. # Init
  93. my $fetch = undef();
  94. # Tranmission
  95. if (available($CONFIG{'TRANS_URL'})) {
  96. # Fetch the list of paused (i.e. completed) torrents
  97. $fetch->url($CONFIG{'TRANS_URL'});
  98. $fetch->post_content($content);
  99. &fetch($fetch);
  100. my $torrents = decode_json(scalar($fetch->content()));
  101. $torrents = $torrents->{'arguments'}->{'torrents'};
  102. # Process
  103. if ($DEBUG) {
  104. print STDERR "Processing torrents...\n";
  105. }
  106. foreach my $tor (@{$torrents}) {
  107. # Skip torrents that aren't yet done
  108. if ($tor->{'leftUntilDone'} > 0 || $tor->{'metadataPercentComplete'} < 1) {
  109. next;
  110. }
  111. # If a hash is provided, process only that file
  112. if (defined($hash) && $tor->{'hashString'} ne $hash) {
  113. if ($DEBUG) {
  114. printf STDERR 'Skipping torrent with non-matching hash: ' . $tor->{'name'} . ' (' . $tor->{'hashString'} . ")\n";
  115. }
  116. next;
  117. }
  118. # Process this torrent
  119. if ($DEBUG) {
  120. print STDERR 'Processing completed torrent: ' . $tor->{'name'} . "\n";
  121. }
  122. if (storeTor($tor) || $DELETE) {
  123. delTor($tor);
  124. } else {
  125. print STDERR 'Unable to store torrent: ' . $tor->{'name'} . "\n";
  126. }
  127. next;
  128. }
  129. # NZB
  130. if (available($CONFIG{'NZB_URL'})) {
  131. # Fetch the list of historical (i.e. completed) NZBs
  132. $fetch->url($CONFIG{'NZB_URL'});
  133. $fetch->post_content($history);
  134. $fetch->fetch();
  135. my $resp = decode_json(scalar($fetch->content()));
  136. if (!defined($resp) || ref($resp) ne 'HASH' || !exists($resp->{'result'}) || ref($resp->{'result'}) ne 'ARRAY') {
  137. print STDERR 'Invalid NZB response: ' . $fetch->content() . "\n";
  138. }
  139. # Process
  140. if ($DEBUG) {
  141. print STDERR "Processing NZBs...\n";
  142. }
  143. foreach my $data (@{ $resp->{'result'} }) {
  144. my $nzb = undef();
  145. if (!readNZB(\$nzb, $data)) {
  146. warn("Unable to parse NZB\n");
  147. next;
  148. }
  149. # If a hash is provided, process only that file
  150. #if (defined($hash) && $nzb->{'has'} ne $hash) {
  151. # if ($DEBUG) {
  152. # printf STDERR 'Skipping NZB with non-matching hash: ' . $nzb->{'name'} . ' (' . $nzb->{'hash'} . ")\n";
  153. # }
  154. # next;
  155. #}
  156. # Delete failed NZBs
  157. if ($nzb->{'status'} =~ /FAILURE\/(?:HEALTH|PAR|UNPACK|FETCH|SCAN)/ || $nzb->{'status'} eq 'DELETED/MANUAL') {
  158. delNZB($nzb);
  159. next;
  160. }
  161. # Process sucessful NZBs
  162. if ($nzb->{'status'} =~ /SUCCESS\/(?:UNPACK|HEALTH)/) {
  163. if (storeNZB($nzb) || $DELETE) {
  164. delNZB($nzb);
  165. } else {
  166. print STDERR 'Unable to store NZB: ' . $nzb->{'name'} . "\n";
  167. }
  168. next;
  169. }
  170. }
  171. }
  172. }
  173. # Cleanup
  174. exit(0);
  175. # Grab the session ID
  176. sub session($) {
  177. my ($fetch) = @_;
  178. my %headers = ();
  179. ($headers{'X-Transmission-Session-Id'}) = $fetch->content() =~ /X\-Transmission\-Session\-Id\:\s+(\w+)/;
  180. $fetch->headers(\%headers);
  181. }
  182. sub fetch($) {
  183. my ($fetch) = @_;
  184. $fetch->fetch('nocheck' => 1);
  185. if ($fetch->status_code() != 200) {
  186. &session($fetch);
  187. $fetch->fetch();
  188. if ($fetch->status_code() != 200) {
  189. die('Unable to fetch: ' . $fetch->status_code() . "\n");
  190. }
  191. }
  192. }
  193. sub getSSE($) {
  194. my ($name) = @_;
  195. if ($DEBUG) {
  196. print STDERR 'Finding series/season/episode for: ' . $name . "\n";
  197. }
  198. my $season = 0;
  199. my $episode = 0;
  200. my $seasonBlock = '';
  201. if ($name =~ /(?:\b|_)(20\d\d(?:\.|\-)[01]?\d(?:\.|\-)[0-3]?\d)(?:\b|_)/) {
  202. $seasonBlock = $1;
  203. my ($month, $day);
  204. ($season, $month, $day) = $seasonBlock =~ /(20\d\d)(?:\.|\-)([01]?\d)(?:\.|\-)([0-3]?\d)/;
  205. $season = int($season);
  206. $episode = sprintf('%04d-%02d-%02d', $season, $month, $day);
  207. } elsif ($name =~ /(?:\b|_)(S(?:eason)?[_\s\.\-]*\d{1,2}[_\s\.\-]*E(?:pisode)?[_\s\.]*\d{1,3}(?:E\d{1,3})?)(?:\b|_)/i) {
  208. $seasonBlock = $1;
  209. ($season, $episode) = $seasonBlock =~ /S(?:eason)?[_\s\.\-]*(\d{1,2})[_\s\.\-]*E(?:pisode)?[_\s\.\-]*(\d{1,3})/i;
  210. $season = int($season);
  211. $episode = sprintf('%02d', int($episode));
  212. } elsif ($name =~ /\b(\d{1,2}x\d{2,3})\b/i) {
  213. $seasonBlock = $1;
  214. ($season, $episode) = $seasonBlock =~ /(\d+)x(\d+)/i;
  215. $season = int($season);
  216. $episode = sprintf('%02d', int($episode));
  217. } elsif ($name =~ /(?:\b|_)([01]?\d[_\s\.]?[0-3]\d)(?:\b|_)/i) {
  218. $seasonBlock = $1;
  219. ($season, $episode) = $seasonBlock =~ /(?:\b|_)([01]?\d)[_\s\.]?([0-3]\d)/i;
  220. $season = int($season);
  221. $episode = sprintf('%02d', int($episode));
  222. }
  223. if (!defined($seasonBlock) || $season < 1 || length($episode) < 1 || $episode eq '0') {
  224. if ($DEBUG) {
  225. print STDERR 'Could not find seasonBlock in: ' . $name . "\n";
  226. }
  227. return;
  228. }
  229. # Assume the series titles comes before the season/episode block
  230. my $series = '';
  231. my $sIndex = index($name, $seasonBlock);
  232. if ($sIndex > 0) {
  233. $series = substr($name, 0, $sIndex - 1);
  234. $series = seriesCleanupCore($series);
  235. }
  236. if ($DEBUG) {
  237. print STDERR 'Series: ' . $series . ' Season: ' . $season . ' Episode: ' . $episode . "\n";
  238. }
  239. return ($series, $season, $episode);
  240. }
  241. sub getDest($$$$) {
  242. my ($series, $season, $episode, $ext) = @_;
  243. if ($DEBUG) {
  244. print STDERR 'Finding destination for: ' . $series . ' S' . $season . 'E' . $episode . "\n";
  245. }
  246. # Input cleanup
  247. $episode =~ s/^0+//;
  248. # Find all our existing TV series
  249. my @shows = ();
  250. my %showsCan = ();
  251. open(SHOWS, '-|', $monitoredExec, 'STORE')
  252. or die("Unable to fork: ${!}\n");
  253. while (<SHOWS>) {
  254. chomp;
  255. my $orig = $_;
  256. my $clean = seriesCleanupLocal($_);
  257. push(@shows, $clean);
  258. $showsCan{$clean} = $orig;
  259. }
  260. close SHOWS or die("Unable to read monitored series list: ${!} ${?}\n");
  261. # See if we can find a unique series name match
  262. my $sClean = seriesCleanup($series);
  263. my $sCleanCore = seriesCleanupCore($series);
  264. my $sMatch = '\b' . $sClean . '\b';
  265. $series = '';
  266. foreach my $show (@shows) {
  267. # Match the folder name or the search_name
  268. my $coarse_match = 0;
  269. if ($show =~ /${sMatch}/i) {
  270. $coarse_match = 1;
  271. } else {
  272. my $search_name = '';
  273. my $file = $tvDir . '/' . $showsCan{$show} . '/search_name';
  274. if (-e $file) {
  275. open(FH, $file)
  276. or die('Unable to read search_name file: ' . $file . "\n");
  277. $search_name = <FH>;
  278. close(FH);
  279. }
  280. if ($search_name) {
  281. $search_name = seriesCleanup($search_name);
  282. if ($search_name =~ /${sMatch}/i) {
  283. $coarse_match = 1;
  284. }
  285. }
  286. }
  287. # If we got a course match, continue checking
  288. if ($coarse_match) {
  289. # Enforce any secondary matching rules (for ambiguous titles)
  290. my $detail_match = 1;
  291. {
  292. my @lines;
  293. my $file = $tvDir . '/' . $showsCan{$show} . '/must_match';
  294. if (-e $file) {
  295. local ($/, *FH);
  296. open(FH, $file)
  297. or die('Unable to read must_match file: ' . $file . "\n");
  298. my @tmp = <FH>;
  299. close(FH);
  300. push(@lines, @tmp);
  301. }
  302. foreach my $line (@lines) {
  303. chomp($line);
  304. if (!eval('$sCleanCore =~ m' . $line)) {
  305. if ($DEBUG) {
  306. print STDERR 'Skipping ' . $show . ' due to must_match failure for: ' . $line . "\n";
  307. }
  308. $detail_match = 0;
  309. last;
  310. }
  311. }
  312. }
  313. if (!$detail_match) {
  314. next;
  315. }
  316. # If we find more than one matching series
  317. if (length($series) > 0) {
  318. # If the old match is a subset of the new match, prefer the new match
  319. # If the new match is a subet of the old match, prefer the old match
  320. # Bail if the matches do not overlap
  321. if ($show =~ m/^${series}\b/) {
  322. if ($DEBUG) {
  323. print STDERR 'Using longer match ('
  324. . $showsCan{$show}
  325. . ') in place of shorter match ('
  326. . $showsCan{$series} . ")\n";
  327. }
  328. $series = $show;
  329. } elsif ($series =~ m/^${show}\b/) {
  330. if ($DEBUG) {
  331. print STDERR 'Using longer match ('
  332. . $showsCan{$series}
  333. . ') in place of shorter match ('
  334. . $showsCan{$show} . ")\n";
  335. }
  336. $series = $series;
  337. } else {
  338. print STDERR 'Matched both ' . $showsCan{$series} . ' and ' . $showsCan{$show} . "\n";
  339. return;
  340. }
  341. } else {
  342. # If we're still around this is the first series title match
  343. $series = $show;
  344. }
  345. }
  346. }
  347. if (length($series) < 1) {
  348. if ($DEBUG) {
  349. print STDERR 'No series match for: ' . $sMatch . "\n";
  350. }
  351. return;
  352. }
  353. # Lookup the canonical name from the matched name
  354. $series = $showsCan{$series};
  355. # Make sure we have the right season folder
  356. my $seriesDir = $tvDir . '/' . $series;
  357. my $seasonDir = $seriesDir . '/' . 'Season ' . $season;
  358. if (!-d $seasonDir) {
  359. print STDERR 'No season folder for: ' . $series . ' S' . $season . "\n";
  360. return;
  361. }
  362. # Bail if we already have this episode
  363. my @episodes = readDir($seasonDir, 0);
  364. foreach my $ep (@episodes) {
  365. my ($epNum) = $ep =~ /^\s*([\d\-]+)\s*\-\s*/;
  366. if (!defined($epNum)) {
  367. next;
  368. }
  369. $epNum =~ s/^0+//;
  370. if ($epNum eq $episode) {
  371. print STDERR 'Existing episode for: ' . $series . ' S' . $season . 'E' . $episode . "\n";
  372. return;
  373. }
  374. }
  375. # Construct the final path
  376. if (!defined($ext) || length($ext) < 1) {
  377. $ext = 'avi';
  378. }
  379. if ($episode =~ /^\d+$/) {
  380. $episode = sprintf('%02d', $episode);
  381. }
  382. my $dest = sprintf('%s/%s - NoName.%s', $seasonDir, $episode, $ext);
  383. if ($DEBUG) {
  384. print STDERR 'Destination: ' . $dest . "\n";
  385. }
  386. return $dest;
  387. }
  388. sub storeFiles($$$) {
  389. my ($path, $files, $name) = @_;
  390. my $failure = 0;
  391. foreach my $file (@{$files}) {
  392. my $result = -1;
  393. if (defined($file) && length($file) > 0 && -r $file) {
  394. $result = &processFile($file, $path);
  395. }
  396. if (defined($result) && $result == 1) {
  397. if ($DEBUG) {
  398. print STDERR 'File stored successfully: ' . $name . "\n";
  399. }
  400. } elsif (defined($result) && $result == -1) {
  401. if ($DEBUG) {
  402. print STDERR 'Refusing to save bad download: ' . $name . "\n";
  403. }
  404. last;
  405. } else {
  406. warn('Error storing file "' . basename($file) . '" from: ' . $name . "\n");
  407. $failure = 1;
  408. }
  409. }
  410. return $failure ? 0 : 1;
  411. }
  412. sub readNZB($$) {
  413. my ($nzb, $data) = @_;
  414. my %tmp = ('id' => undef(), 'name' => undef(), 'status' => undef(), 'path' => undef());
  415. # Sanity
  416. if (ref($data) ne 'HASH') {
  417. print STDERR 'Invalid NZB item: ' . $data . "\n";
  418. return 0;
  419. }
  420. # ID
  421. if (exists($data->{'ID'})) {
  422. $tmp{'id'} = int($data->{'ID'});
  423. }
  424. # Name
  425. if (exists($data->{'Name'})) {
  426. $tmp{'name'} = $data->{'Name'};
  427. }
  428. if (!$tmp{'name'} && exists($data->{'NZBName'})) {
  429. $tmp{'name'} = $data->{'NZBName'};
  430. }
  431. if (!$tmp{'name'} && exists($data->{'NZBNicename'})) {
  432. $tmp{'name'} = $data->{'NZBNicename'};
  433. }
  434. if (!$tmp{'name'}) {
  435. print STDERR 'Unable to retrieve name for: ' . $tmp{'id'} . "\n";
  436. return 0;
  437. }
  438. # Status
  439. if (exists($data->{'Status'})) {
  440. $tmp{'status'} = $data->{'Status'};
  441. }
  442. if (!$tmp{'status'}) {
  443. print STDERR 'Unable to retrieve status for: ' . $tmp{'id'} . "\n";
  444. return 0;
  445. }
  446. # Path
  447. if (exists($data->{'DestDir'})) {
  448. $tmp{'path'} = $data->{'DestDir'};
  449. }
  450. if (!$tmp{'path'} && $DEBUG) {
  451. print STDERR 'Unable to retrieve path for: ' . $tmp{'id'} . "\n";
  452. }
  453. # Debug
  454. if ($DEBUG > 1) {
  455. print STDERR "\n\n";
  456. foreach my $key (keys(%tmp)) {
  457. print STDERR $key . ' => ' . $tmp{$key} . "\n";
  458. }
  459. }
  460. # Store the NZB hash and return success
  461. ${$nzb} = \%tmp;
  462. return 1;
  463. }
  464. sub storeTor($) {
  465. my ($tor) = @_;
  466. if ($DEBUG) {
  467. print STDERR 'Storing torrent: ' . $tor->{'name'} . "\n";
  468. }
  469. # Handle individual files and directories
  470. my @files = ();
  471. my @newFiles = ();
  472. my $path = $tor->{'downloadDir'} . '/' . $tor->{'name'};
  473. if (!-d $path) {
  474. push(@files, $path);
  475. } else {
  476. # State variables
  477. my $IS_RAR = '';
  478. my $IS_FAKE = 0;
  479. # Look for RAR files
  480. foreach my $file (@{ $tor->{'files'} }) {
  481. if ($file->{'name'} =~ /\.rar$/i) {
  482. $IS_RAR = $tor->{'downloadDir'} . '/' . $file->{'name'};
  483. last;
  484. }
  485. }
  486. # Check for password files
  487. foreach my $file (@{ $tor->{'files'} }) {
  488. if ($file->{'name'} =~ /passw(?:or)?d/i) {
  489. print STDERR 'Password file detected in: ' . $tor->{'name'} . "\n";
  490. $IS_FAKE = 1;
  491. last;
  492. }
  493. }
  494. # Check for single-file RARs
  495. if ($IS_RAR && scalar(@{ $tor->{'files'} }) < $RAR_MIN_FILES) {
  496. print STDERR 'Single-file RAR detected in: ' . $tor->{'name'} . "\n";
  497. $IS_FAKE = 1;
  498. }
  499. # Delete fake torrents and bail
  500. if ($IS_FAKE) {
  501. if ($DEBUG) {
  502. print STDERR 'Deleting fake torrent: ' . $tor->{'name'} . "\n";
  503. }
  504. delTor($tor);
  505. next;
  506. }
  507. # Unrar if necessary
  508. if (defined($IS_RAR) && length($IS_RAR) > 0) {
  509. @newFiles = &unrar($IS_RAR, $path);
  510. }
  511. # Find all media files, recursively
  512. @files = findMediaFiles($path, 0);
  513. }
  514. # Process any files we found
  515. if (scalar(@files) < 1) {
  516. print STDERR 'No media files found in torrent: ' . $tor->{'name'} . "\n";
  517. return 0;
  518. }
  519. my $retval = storeFiles($path, \@files, $tor->{'name'});
  520. # Delete any files we added
  521. foreach my $file (@newFiles) {
  522. if ($DEBUG) {
  523. print STDERR 'Deleting new file: ' . basename($file) . "\n";
  524. }
  525. unlink($file);
  526. }
  527. # Return whatever storeFiles() said
  528. return $retval;
  529. }
  530. sub storeNZB($) {
  531. my ($nzb) = @_;
  532. if ($DEBUG) {
  533. print STDERR 'Storing NZB: ' . $nzb->{'name'} . "\n";
  534. }
  535. # Find all media files, recursively
  536. my @files = findMediaFiles($nzb->{'path'}, 0);
  537. # Process any files we found
  538. if (scalar(@files) < 1) {
  539. print STDERR 'No media files found in NZB: ' . $nzb->{'name'} . "\n";
  540. return undef();
  541. }
  542. return storeFiles($nzb->{'path'}, \@files, $nzb->{'name'});
  543. }
  544. sub delNZB($) {
  545. my ($nzb) = @_;
  546. if ($DEBUG) {
  547. print STDERR 'Deleting NZB: ' . $nzb->{'name'} . "\n";
  548. }
  549. # Remove the files, if any
  550. if (-e $nzb->{'path'} && -d $nzb->{'path'}) {
  551. if ($DEBUG) {
  552. print STDERR 'Removing NZB folder: ' . $nzb->{'path'} . "\n";
  553. }
  554. remove_tree($nzb->{'path'});
  555. # Check for deletion
  556. if (-e $nzb->{'path'}) {
  557. print STDERR 'Unable to remove NZB folder: ' . $nzb->{'path'} . "\n";
  558. return 0;
  559. }
  560. }
  561. # Send the command
  562. my $id = $nzb->{'id'};
  563. my $content = $delHistory;
  564. $content =~ s/\#_ID_\#/${id}/;
  565. $fetch->post_content($content);
  566. $fetch->fetch();
  567. # Check the result
  568. if ($fetch->content() =~ /\"result\" \: true/i) {
  569. return 1;
  570. }
  571. return 0;
  572. }
  573. sub delTor($) {
  574. my ($tor) = @_;
  575. if ($DEBUG) {
  576. print STDERR 'Deleting torrent: ' . $tor->{'name'} . "\n";
  577. }
  578. # Sleep to avoid bugs in Transmission (i.e. multiple deletes fail)
  579. sleep($delSleep);
  580. # Construct the content
  581. my $id = $tor->{'hashString'};
  582. my $content = $delContent;
  583. $content =~ s/\#_ID_\#/${id}/;
  584. # Send the command
  585. &session($fetch);
  586. $fetch->post_content($content);
  587. &fetch($fetch);
  588. }
  589. sub guessExt($) {
  590. my ($file) = @_;
  591. my $ext = '';
  592. # Believe most non-avi file extensions without checking
  593. # It's mostly "avi" that lies, and the checks are expensive
  594. {
  595. my $orig_ext = basename($file);
  596. $orig_ext =~ s/^.*\.(\w{2,3})$/$1/;
  597. $orig_ext = lc($orig_ext);
  598. if ($orig_ext eq 'mkv' || $orig_ext eq 'ts') {
  599. $ext = $orig_ext;
  600. }
  601. }
  602. # Deal with the weird case of text files labeled as .txt.mp4
  603. if ($file =~ /\.txt\.mp4$/i) {
  604. $ext = 'txt';
  605. }
  606. # Guess the file type if we don't have one yet
  607. if (!$ext) {
  608. my $mime = '';
  609. my $demux = '';
  610. # Try ExifTool for MIME and demuxer types
  611. if (!$mime || !$demux) {
  612. my $exif = ImageInfo($file);
  613. if (defined($exif) && ref($exif) eq 'HASH') {
  614. if ($exif->{'MIMEType'}) {
  615. $mime = $exif->{'MIMEType'};
  616. }
  617. if ($exif->{'FileType'}) {
  618. $demux = $exif->{'FileType'};
  619. }
  620. }
  621. }
  622. # Try file(1) for MIME types
  623. if (!$mime) {
  624. open(FILE, '-|', 'file', '-b', $file);
  625. while (<FILE>) {
  626. $mime .= $_;
  627. }
  628. close(FILE);
  629. }
  630. # Cleanup the demuxer and MIME types
  631. $demux =~ s/^\s+//;
  632. $demux =~ s/\s+$//;
  633. $mime =~ s/^\s+//;
  634. $mime =~ s/\s+$//;
  635. $mime =~ s/\;.*$//;
  636. $mime =~ s/^video\///i;
  637. # Try to pick an extension we understand
  638. if ($demux =~ /MKV/i || $mime =~ /Matroska/i) {
  639. $ext = 'mkv';
  640. } elsif ($demux =~ /ASF/i || $mime =~ /\bASF\b/i) {
  641. $ext = 'wmv';
  642. } elsif ($demux =~ /MP4/i || $mime =~ /\bMP4\b/i) {
  643. $ext = 'mp4';
  644. } elsif ($mime =~ /\bZIP/i) {
  645. $ext = 'zip';
  646. } elsif ($demux =~ /MPEGTS/i) {
  647. $ext = 'ts';
  648. } else {
  649. $ext = 'avi';
  650. }
  651. }
  652. return $ext;
  653. }
  654. sub processFile($$) {
  655. my ($file, $path) = @_;
  656. if ($DEBUG) {
  657. print STDERR 'Attempting to process file: ' . basename($file) . "\n";
  658. }
  659. # Skip known packaging files files with success code
  660. if ($file =~ /\/RARBG\.com\.(?:mp4|avi|mkv|txt)$/i || $file =~ /\.txt\.(?:mp4|avi|mkv)$/) {
  661. if ($DEBUG) {
  662. print STDERR 'Declining to save packaging file: ' . $file . "\n";
  663. }
  664. return 1;
  665. }
  666. # Guess a file extension and basename
  667. my $ext = &guessExt($file);
  668. my $filename = basename($file);
  669. # Delete WMV files -- mostly viruses/fake
  670. if ($ext =~ /wmv/i) {
  671. if ($DEBUG) {
  672. print STDERR 'Declining to save WMV file: ' . $filename . "\n";
  673. }
  674. return -1;
  675. }
  676. # Delete ZIP files -- mostly fake
  677. if ($ext =~ /zip/i) {
  678. if ($DEBUG) {
  679. print STDERR 'Declining to save ZIP file: ' . $filename . "\n";
  680. }
  681. return -1;
  682. }
  683. # Allow multiple guesses at the series/season/episode
  684. my $dest = '';
  685. LOOP: {
  686. # Determine the series, season, and episode number
  687. my ($series, $season, $episode) = &getSSE($filename);
  688. if ( defined($series)
  689. && length($series) > 0
  690. && defined($season)
  691. && $season > 0
  692. && defined($episode)
  693. && length($episode) > 0
  694. && $episode ne '0')
  695. {
  696. # Find the proper destination for this torrent, if any
  697. $dest = &getDest($series, $season, $episode, $ext);
  698. }
  699. # Sanity/loop check
  700. if (!defined($dest) || length($dest) < 1) {
  701. # If there is no match but the torrent is a folder
  702. # retry the guess using the torrent path
  703. if ($file ne $path) {
  704. my $newName = basename($path);
  705. if ($newName ne $filename) {
  706. $filename = basename($path);
  707. redo LOOP;
  708. }
  709. }
  710. # Otherwise we just fail
  711. print STDERR 'No destination for: ' . basename($file) . "\n";
  712. return;
  713. }
  714. }
  715. # Copy (to let Transmission deal with local cleanup) and rename into place
  716. my $tmp = mktemp($dest . '.XXXXXXXX');
  717. copy($file, $tmp);
  718. rename($tmp, $dest);
  719. # Touch to avoid carrying dates set in the download process
  720. touch($dest);
  721. # Return success
  722. return 1;
  723. }
  724. sub unrar($$) {
  725. my ($file, $path) = @_;
  726. if ($DEBUG) {
  727. print STDERR 'UnRARing: ' . basename($file) . "\n";
  728. }
  729. # Keep a list of old files, so we can find/delete the output
  730. my @beforeFiles = readDir($path, 1);
  731. # Run the unrar utility
  732. my $pid = fork();
  733. if (!defined($pid)) {
  734. print STDERR "Unable to fork for RAR\n";
  735. return;
  736. } elsif ($pid == 0) {
  737. # Child
  738. close(STDOUT);
  739. close(STDERR);
  740. chdir($path);
  741. my @args = ('unrar', 'e', '-p-', '-y', $file);
  742. exec { $args[0] } @args;
  743. }
  744. # Wait for the child
  745. waitpid($pid, 0);
  746. # Compare the old file list to the new one
  747. my @newFiles = ();
  748. my @afterFiles = readDir($path, 1);
  749. foreach my $file (@afterFiles) {
  750. my $found = 0;
  751. foreach my $file2 (@beforeFiles) {
  752. if ($file eq $file2) {
  753. $found = 1;
  754. last;
  755. }
  756. }
  757. if (!$found) {
  758. push(@newFiles, $file);
  759. }
  760. }
  761. # Return the list of added files
  762. return @newFiles;
  763. }
  764. sub seriesCleanupLocal($) {
  765. my ($name) = @_;
  766. $name =~ s/\.//g;
  767. return seriesCleanupCore($name);
  768. }
  769. sub seriesCleanupCore($) {
  770. my ($name) = @_;
  771. $name =~ s/^.*\[[^\]]*\]\s+//;
  772. $name =~ s/\b(?:and|\&)\b/ /ig;
  773. $name =~ s/^\s*The\b//ig;
  774. $name =~ s/\[[^\]]*\]//g;
  775. $name =~ s/\{[^\}]*\}//g;
  776. $name =~ s/[\(\)]//g;
  777. $name =~ s/[\'\"]//g;
  778. $name =~ s/[^\w\s]+/ /g;
  779. $name =~ s/_+/ /g;
  780. $name =~ s/\s+/ /g;
  781. $name =~ s/^\s*//;
  782. $name =~ s/\s*$//;
  783. return $name;
  784. }
  785. sub seriesCleanup($) {
  786. my ($name) = @_;
  787. $name =~ s/\s*\(?US\)?\s*$//;
  788. $name =~ s/\s*\(?20[01][0-9]\)?\s*$//;
  789. return seriesCleanupCore($name);
  790. }
  791. sub readDir($$) {
  792. my ($indir, $long) = @_;
  793. # Clean up the input directory
  794. $indir =~ s/\/+$//;
  795. my @files = ();
  796. if (!opendir(DIR, $indir)) {
  797. warn('readDir: Unable to read directory: ' . $indir . ': ' . $! . "\n");
  798. return;
  799. }
  800. foreach my $file (readdir(DIR)) {
  801. my $keep = 0;
  802. # Skip junk
  803. if ( $file eq '.'
  804. || $file eq '..'
  805. || $file =~ /\._/)
  806. {
  807. next;
  808. }
  809. # Save the full path
  810. if ($long) {
  811. push(@files, $indir . '/' . $file);
  812. } else {
  813. push(@files, $file);
  814. }
  815. }
  816. closedir(DIR);
  817. # Sorts the files before returning to the caller
  818. my @orderedFiles = ();
  819. @orderedFiles = sort(@files);
  820. # Return the file list
  821. return (@orderedFiles);
  822. }
  823. sub findMediaFiles($$) {
  824. my ($path, $depth) = @_;
  825. my @files = ();
  826. # Recursion limit
  827. if ($depth > $FIND_MAX_DEPTH) {
  828. return ();
  829. }
  830. my @tmpFiles = readDir($path, 1);
  831. foreach my $file (@tmpFiles) {
  832. # Skip sample files
  833. if ($file =~ /sample/i) {
  834. next;
  835. }
  836. # Recurse on directories
  837. if (-d $file) {
  838. push(@files, findMediaFiles($file, $depth + 1));
  839. next;
  840. }
  841. # Save matching files
  842. if ($file =~ /\.(?:avi|mkv|m4v|mov|mp4|ts|wmv)$/i) {
  843. push(@files, $file);
  844. next;
  845. }
  846. }
  847. return @files;
  848. }
  849. # Check availability of web interface
  850. sub available($) {
  851. my ($url) = @_;
  852. if ($DEBUG) {
  853. print STDERR "Checking for interface\n";
  854. }
  855. # Reset from the last host
  856. $fetch = Fetch->new();
  857. my ($host) = $url =~ /^(\w+\:\/\/[^\/]+\/?)/;
  858. $fetch->url($host);
  859. if ($fetch->fetch('nocheck' => 1) != 200) {
  860. if ($DEBUG) {
  861. print STDERR 'Code: ' . $fetch->status_code() . "\n";
  862. print STDERR 'Content: ' . $fetch->content() . "\n";
  863. warn('Interface not available: ' . $host . "\n");
  864. }
  865. return 0;
  866. }
  867. return 1;
  868. }