transmission.pl 23 KB

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