transmission.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. # Includes
  5. use JSON;
  6. use Date::Parse;
  7. use File::Temp qw/ :mktemp /;
  8. use File::Basename;
  9. use FindBin qw($Bin);
  10. use lib $Bin;
  11. use Run;
  12. use Fetch;
  13. # Prototypes
  14. sub session($);
  15. sub fetch($);
  16. sub getSSE($);
  17. sub getDest($$$$);
  18. sub delTor($);
  19. sub guessExt($);
  20. sub processFile($$);
  21. sub unrar($$);
  22. sub seriesCleanup($);
  23. sub readDir($$);
  24. # Parameters
  25. my $maxAge = 2.5 * 86400;
  26. my $tvDir = '/mnt/media/TV';
  27. my $monitoredExec = '/Users/profplump/bin/video/torrentMonitored.pl';
  28. my $host = 'http://localhost:9091';
  29. my $url = $host . '/transmission/rpc';
  30. my $content = '{"method":"torrent-get","arguments":{"fields":["hashString","id","addedDate","comment","creator","dateCreated","isPrivate","name","totalSize","pieceCount","pieceSize","downloadedEver","error","errorString","eta","haveUnchecked","haveValid","leftUntilDone","metadataPercentComplete","peersConnected","peersGettingFromUs","peersSendingToUs","rateDownload","rateUpload","recheckProgress","sizeWhenDone","status","trackerStats","uploadedEver","uploadRatio","seedRatioLimit","seedRatioMode","downloadDir","files","fileStats"]}}';
  31. my $delContent = '{"method":"torrent-remove","arguments":{"ids":["#_ID_#"], "delete-local-data":"true"}';
  32. my $RAR_MIN_FILES = 4;
  33. # Debug
  34. my $DEBUG = 0;
  35. if ($ENV{'DEBUG'}) {
  36. $DEBUG = 1;
  37. }
  38. # Command-line parameters
  39. my ($force, $maxDays) = @ARGV;
  40. # Init
  41. my $fetch = Fetch->new();
  42. # Check availability of web interface
  43. $fetch->url($host);
  44. if ($DEBUG) {
  45. print STDERR "Checking for torrent web interface\n";
  46. }
  47. $fetch->fetch('nocheck' => 1);
  48. if ($fetch->status_code() != 200) {
  49. if ($DEBUG) {
  50. die("Transmission web interface not available\n");
  51. }
  52. exit(0);
  53. }
  54. # Fetch the list of paused (i.e. completed) torrents
  55. $fetch->url($url);
  56. $fetch->post_content($content);
  57. &fetch($fetch);
  58. my $torrents = decode_json($fetch->content());
  59. $torrents = $torrents->{'arguments'}->{'torrents'};
  60. # Process
  61. if ($DEBUG) {
  62. print STDERR "Processing torrents...\n";
  63. }
  64. foreach my $tor (@{$torrents}) {
  65. # Skip torrents that aren't yet done
  66. if ($tor->{'leftUntilDone'} > 0 || $tor->{'metadataPercentComplete'} < 1) {
  67. next;
  68. }
  69. # Only process recent files
  70. # Torrents created with magnet links have no dateCreated, so fake it with addedDate
  71. if ($tor->{'dateCreated'} < 946684800) {
  72. $tor->{'dateCreated'} = $tor->{'addedDate'};
  73. }
  74. if (!$force && $tor->{'dateCreated'} < time() - $maxAge) {
  75. my $age = (time() - $tor->{'dateCreated'}) / 86400;
  76. if ($DEBUG) {
  77. print STDERR 'Skipping old torrent: ' . $tor->{'name'} . ' (' . sprintf('%.0f', $age) . " days)\n";
  78. }
  79. next;
  80. }
  81. if ($DEBUG) {
  82. print STDERR 'Processing finished torrent: ' . $tor->{'name'} . "\n";
  83. }
  84. # Handle individual files and directories
  85. my $file = '';
  86. my @newFiles = ();
  87. my $path = $tor->{'downloadDir'} . '/' . $tor->{'name'};
  88. if (-d $path) {
  89. # State variables
  90. my $IS_RAR = '';
  91. my $IS_FAKE = 0;
  92. # Look for RAR files
  93. foreach my $file (@{ $tor->{'files'} }) {
  94. if ($file->{'name'} =~ /\.rar$/i) {
  95. $IS_RAR = $tor->{'downloadDir'} . '/' . $file->{'name'};
  96. last;
  97. }
  98. }
  99. # Check for password files
  100. foreach my $file (@{ $tor->{'files'} }) {
  101. if ($file->{'name'} =~ /passw(?:or)?d/i) {
  102. print STDERR 'Password file detected in: ' . $tor->{'name'} . "\n";
  103. $IS_FAKE = 1;
  104. last;
  105. }
  106. }
  107. # Check for single-file RARs
  108. if ($IS_RAR && scalar(@{ $tor->{'files'} }) < $RAR_MIN_FILES) {
  109. print STDERR 'Single-file RAR detected in: ' . $tor->{'name'} . "\n";
  110. $IS_FAKE = 1;
  111. }
  112. # Find media files in valid torrents
  113. if (!$IS_FAKE) {
  114. # Unrar if necessary
  115. if (defined($IS_RAR) && length($IS_RAR) > 0) {
  116. @newFiles = &unrar($IS_RAR, $path);
  117. }
  118. # Find exactly 1 media file
  119. my @files = ();
  120. my @tmpFiles = readDir($path, '/\.(?:avi|mkv|m4v|mov|mp4|ts|wmv)$/i');
  121. foreach my $file (@tmpFiles) {
  122. if ($file =~ /sample/i) {
  123. next;
  124. }
  125. push(@files, $file);
  126. }
  127. if (scalar(@files) != 1) {
  128. print STDERR 'Did not find exactly 1 media file in: ' . $path . "\n";
  129. foreach my $fn (@files) {
  130. print STDERR "\t" . $fn . "\n";
  131. }
  132. next;
  133. }
  134. $file = $files[0];
  135. } else {
  136. $file = '';
  137. }
  138. } else {
  139. $file = $path;
  140. }
  141. # Do all the normal file naming/copying/etc. if we found a file
  142. my $result = -1;
  143. if (defined($file) && length($file) > 0 && -r $file) {
  144. $result = &processFile($file, $path);
  145. }
  146. if (defined($result) && $result == 1) {
  147. if ($DEBUG) {
  148. print STDERR 'Torrent stored successfully: ' . $tor->{'name'} . "\n";
  149. }
  150. } elsif (defined($result) && $result == -1) {
  151. print STDERR 'Deleting bad torrent: ' . $tor->{'name'} . "\n";
  152. } else {
  153. print STDERR 'Error storing file "' . basename($file) . '" from torrent: ' . $tor->{'name'} . "\n";
  154. next;
  155. }
  156. # Delete any files we added
  157. foreach my $file (@newFiles) {
  158. if ($DEBUG) {
  159. print STDERR 'Deleting new file: ' . basename($file) . "\n";
  160. }
  161. unlink($file);
  162. }
  163. # Remove the source torrent (it's either copied or bad by this point)
  164. delTor($tor);
  165. }
  166. # Cleanup
  167. exit(0);
  168. # Grab the session ID
  169. sub session($) {
  170. my ($fetch) = @_;
  171. my %headers = ();
  172. ($headers{'X-Transmission-Session-Id'}) = $fetch->content() =~ /X\-Transmission\-Session\-Id\:\s+(\w+)/;
  173. $fetch->headers(\%headers);
  174. }
  175. sub fetch($) {
  176. my ($fetch) = @_;
  177. $fetch->fetch('nocheck' => 1);
  178. if ($fetch->status_code() != 200) {
  179. &session($fetch);
  180. $fetch->fetch();
  181. if ($fetch->status_code() != 200) {
  182. die('Unable to fetch: ' . $fetch->status_code() . "\n");
  183. }
  184. }
  185. }
  186. sub getSSE($) {
  187. my ($name) = @_;
  188. if ($DEBUG) {
  189. print STDERR 'Finding series/season/episode for: ' . $name . "\n";
  190. }
  191. my $season = 0;
  192. my $episode = 0;
  193. my $seasonBlock = '';
  194. if ($name =~ /(?:\b|_)(S\d{1,2}[_\s\.]?E\d{1,2})(?:\b|_)/i) {
  195. $seasonBlock = $1;
  196. ($season, $episode) = $seasonBlock =~ /S(\d{1,2})[_\s\.]?E(\d{1,2})/i;
  197. $season = int($season);
  198. $episode = sprintf('%02d', int($episode));
  199. } elsif ($name =~ /[\[\_\.](\d{1,2}x\d{2,3})[\]\_\.]/i) {
  200. $seasonBlock = $1;
  201. ($season, $episode) = $seasonBlock =~ /(\d+)x(\d+)/i;
  202. $season = int($season);
  203. $episode = sprintf('%02d', int($episode));
  204. } elsif ($name =~ /(?:\b|_)(20\d\d(?:\.|\-)[01]?\d(?:\.|\-)[0-3]?\d)(?:\b|_)/) {
  205. $seasonBlock = $1;
  206. my ($month, $day);
  207. ($season, $month, $day) = $seasonBlock =~ /(20\d\d)(?:\.|\-)([01]?\d)(?:\.|\-)([0-3]?\d)/;
  208. $season = int($season);
  209. $episode = sprintf('%04d-%02d-%02d', $season, $month, $day);
  210. } elsif ($name =~ /(?:\b|_)([01]?\d[_\s\.]?[0-3]\d)(?:\b|_)/i) {
  211. $seasonBlock = $1;
  212. ($season, $episode) = $seasonBlock =~ /(?:\b|_)([01]?\d)[_\s\.]?([0-3]\d)/i;
  213. $season = int($season);
  214. $episode = sprintf('%02d', int($episode));
  215. }
  216. if (!defined($seasonBlock) || $season < 1 || length($episode) < 1 || $episode eq '0') {
  217. if ($DEBUG) {
  218. print STDERR 'Could not find seasonBlock in: ' . $name . "\n";
  219. }
  220. return;
  221. }
  222. # Assume the series titles comes before the season/episode block
  223. my $series = '';
  224. my $sIndex = index($name, $seasonBlock);
  225. if ($sIndex > 0) {
  226. $series = substr($name, 0, $sIndex - 1);
  227. $series = seriesCleanup($series);
  228. }
  229. if ($DEBUG) {
  230. print STDERR 'Series: ' . $series . ' Season: ' . $season . ' Episode: ' . $episode . "\n";
  231. }
  232. return ($series, $season, $episode);
  233. }
  234. sub getDest($$$$) {
  235. my ($series, $season, $episode, $ext) = @_;
  236. if ($DEBUG) {
  237. print STDERR 'Finding destintion for: ' . $series . ' S' . $season . 'E' . $episode . "\n";
  238. }
  239. # Find all our existing TV series
  240. my @shows = ();
  241. my %showsCan = ();
  242. open(SHOWS, '-|', $monitoredExec, 'STORE')
  243. or die("Unable to fork: ${!}\n");
  244. while (<SHOWS>) {
  245. chomp;
  246. my $orig = $_;
  247. my $clean = seriesCleanup($_);
  248. push(@shows, $clean);
  249. $showsCan{$clean} = $orig;
  250. }
  251. close SHOWS or die("Unable to read monitored series list: ${!} ${?}\n");
  252. # See if we can find a unique series name match
  253. my $sClean = seriesCleanup($series);
  254. my $sMatch = '\b' . $sClean . '\b';
  255. $series = '';
  256. foreach my $show (@shows) {
  257. if ($show =~ /${sMatch}/i) {
  258. # Enforce any secondary matching rules (for ambiguous titles)
  259. my $detail_match = 1;
  260. {
  261. my @lines;
  262. my $file = $tvDir . '/' . $showsCan{$show} . '/must_match';
  263. if (-e $file) {
  264. local ($/, *FH);
  265. open(FH, $file)
  266. or die('Unable to read must_match file: ' . $file . "\n");
  267. my @tmp = <FH>;
  268. close(FH);
  269. push(@lines, @tmp);
  270. }
  271. foreach my $line (@lines) {
  272. chomp($line);
  273. if (!eval('$sClean =~ m' . $line)) {
  274. if ($DEBUG) {
  275. print STDERR 'Skipping ' . $show . ' due to must_match failure for: ' . $line . "\n";
  276. }
  277. $detail_match = 0;
  278. last;
  279. }
  280. }
  281. }
  282. if (!$detail_match) {
  283. next;
  284. }
  285. # Bail if we find more than one matching series
  286. if (length($series) > 0) {
  287. print STDERR 'Matched both ' . $series . ' and ' . $show . "\n";
  288. return;
  289. }
  290. # If we're still around this is the first series title match
  291. $series = $show;
  292. }
  293. }
  294. if (length($series) < 1) {
  295. if ($DEBUG) {
  296. print STDERR 'No series match for: ' . $sMatch . "\n";
  297. }
  298. return;
  299. }
  300. # Lookup the canonical name from the matched name
  301. $series = $showsCan{$series};
  302. # Make sure we have the right season folder
  303. my $seriesDir = $tvDir . '/' . $series;
  304. my $seasonDir = $seriesDir . '/' . 'Season ' . $season;
  305. if (!-d $seasonDir) {
  306. print STDERR 'No season folder for: ' . $series . ' S' . $season . "\n";
  307. return;
  308. }
  309. # Bail if we already have this episode
  310. my @episodes = readDir($seasonDir, '/^\s*[\d\-]+\s*\-\s*/');
  311. foreach my $ep (@episodes) {
  312. $ep = basename($ep);
  313. my ($epNum) = $ep =~ /^\s*([\d\-]+)\s*\-\s*/;
  314. $epNum =~ s/^0+//;
  315. $episode =~ s/^0+//;
  316. if ($epNum eq $episode) {
  317. print STDERR 'Existing episode for: ' . $series . ' S' . $season . 'E' . $episode . "\n";
  318. return;
  319. }
  320. }
  321. # Construct the final path
  322. if (!defined($ext) || length($ext) < 1) {
  323. $ext = 'avi';
  324. }
  325. if ($episode =~ /^\d+$/) {
  326. $episode = sprintf('%02d', $episode);
  327. }
  328. my $dest = sprintf('%s/%s - NoName.%s', $seasonDir, $episode, $ext);
  329. if ($DEBUG) {
  330. print STDERR 'Destination: ' . $dest . "\n";
  331. }
  332. return $dest;
  333. }
  334. sub delTor($) {
  335. my ($tor) = @_;
  336. if ($DEBUG) {
  337. print STDERR 'Deleting torrent: ' . $tor->{'name'} . "\n";
  338. }
  339. # Construct the content
  340. my $id = $tor->{'hashString'};
  341. my $content = $delContent;
  342. $content =~ s/\#_ID_\#/${id}/;
  343. # Send the command
  344. &session($fetch);
  345. $fetch->post_content($content);
  346. &fetch($fetch);
  347. }
  348. sub guessExt($) {
  349. my ($file) = @_;
  350. # Believe most non-avi file extensions without checking
  351. # It's mostly "avi" that lies, and the checks are expensive
  352. my $ext = '';
  353. {
  354. my $orig_ext = basename($file);
  355. $orig_ext =~ s/^.*\.(\w{2,3})$/$1/;
  356. $orig_ext = lc($orig_ext);
  357. if ($orig_ext eq 'mkv' || $orig_ext eq 'ts') {
  358. $ext = $orig_ext;
  359. }
  360. }
  361. # Ask movInfo.pl about the demuxer
  362. if (!$ext) {
  363. my $demux = '';
  364. open(INFO, '-|', $ENV{'HOME'} . '/bin/video/movInfo.pl', $file, 'DEMUXER');
  365. while (<INFO>) {
  366. $demux .= $_;
  367. }
  368. close(INFO);
  369. $demux =~ s/^\s+//;
  370. $demux =~ s/\s+$//;
  371. # Grab a MIME type from file(1)
  372. my $mime = '';
  373. open(FILE, '-|', 'file', '-b', $file);
  374. while (<FILE>) {
  375. $mime .= $_;
  376. }
  377. close(FILE);
  378. $mime =~ s/^\s+//;
  379. $mime =~ s/\s+$//;
  380. $mime =~ s/\;.*$//;
  381. $mime =~ s/^video\///i;
  382. # Try to pick an extension we understand
  383. $ext = 'avi';
  384. if ($demux =~ /mkv/i || $mime =~ /Matroska/i) {
  385. $ext = 'mkv';
  386. } elsif ($demux =~ /asf/i || $mime =~ /\bASF\b/i) {
  387. $ext = 'wmv';
  388. } elsif ($mime =~ /\bZIP/i) {
  389. $ext = 'zip';
  390. } elsif ($mime =~ /\bAVI\b/i) {
  391. $ext = 'avi';
  392. } elsif ($demux =~ /mpegts/i) {
  393. $ext = 'ts';
  394. }
  395. }
  396. return $ext;
  397. }
  398. sub processFile($$) {
  399. my ($file, $path) = @_;
  400. if ($DEBUG) {
  401. print STDERR 'Attempting to process file: ' . basename($file) . "\n";
  402. }
  403. # Guess a file extension
  404. my $ext = &guessExt($file);
  405. # Delete WMV files -- mostly viruses
  406. if ($ext =~ /wmv/i) {
  407. print STDERR 'Declining to save WMV file: ' . basename($file) . "\n";
  408. return -1;
  409. }
  410. # Delete ZIP files -- mostly fake
  411. if ($ext =~ /zip/i) {
  412. print STDERR 'Declining to save ZIP file: ' . basename($file) . "\n";
  413. return -1;
  414. }
  415. # Allow multiple guesses at the series/season/episode
  416. my $dest = '';
  417. my $filename = basename($file);
  418. LOOP: {
  419. # Determine the series, season, and episode number
  420. my ($series, $season, $episode) = &getSSE($filename);
  421. if ( defined($series)
  422. && length($series) > 0
  423. && defined($season)
  424. && $season > 0
  425. && defined($episode)
  426. && length($episode) > 0
  427. && $episode ne '0')
  428. {
  429. # Find the proper destination for this torrent, if any
  430. $dest = &getDest($series, $season, $episode, $ext);
  431. }
  432. # Sanity/loop check
  433. if (!defined($dest) || length($dest) < 1) {
  434. # If there is no match but the torrent is a folder
  435. # retry the guess using the torrent path
  436. if ($file ne $path) {
  437. my $newName = basename($path);
  438. if ($newName ne $filename) {
  439. $filename = basename($path);
  440. redo LOOP;
  441. }
  442. }
  443. # Otherwise we just fail
  444. print STDERR 'No destination for: ' . basename($file) . "\n";
  445. return;
  446. }
  447. }
  448. # Copy with system tools
  449. my $tmp = mktemp($dest . '.XXXXXXXX');
  450. Run::runAndCheck(('cp', $file, $tmp));
  451. rename($tmp, $dest);
  452. # Return success
  453. return 1;
  454. }
  455. sub unrar($$) {
  456. my ($file, $path) = @_;
  457. if ($DEBUG) {
  458. print STDERR 'UnRARing: ' . basename($file) . "\n";
  459. }
  460. # Keep a list of old files, so we can find/delete the output
  461. my @beforeFiles = readDir($path, undef());
  462. # Run the unrar utility
  463. my $pid = fork();
  464. if (!defined($pid)) {
  465. print STDERR "Unable to fork for RAR\n";
  466. return;
  467. } elsif ($pid == 0) {
  468. # Child
  469. close(STDOUT);
  470. close(STDERR);
  471. chdir($path);
  472. my @args = ('unrar', 'e', '-p-', '-y', $file);
  473. exec { $args[0] } @args;
  474. }
  475. # Wait for the child
  476. waitpid($pid, 0);
  477. # Compare the old file list to the new one
  478. my @newFiles = ();
  479. my @afterFiles = readDir($path, undef());
  480. foreach my $file (@afterFiles) {
  481. my $found = 0;
  482. foreach my $file2 (@beforeFiles) {
  483. if ($file eq $file2) {
  484. $found = 1;
  485. last;
  486. }
  487. }
  488. if (!$found) {
  489. push(@newFiles, $file);
  490. }
  491. }
  492. # Return the list of added files
  493. return @newFiles;
  494. }
  495. sub seriesCleanup($) {
  496. my ($name) = @_;
  497. $name =~ s/\b(?:and|\&)\b/ /ig;
  498. $name =~ s/^\s*The\b//ig;
  499. if (!($name =~ /Being\W+Human/i)) {
  500. $name =~ s/\bUS\b?\s*$//ig;
  501. $name =~ s/\([^\)]*\)//g;
  502. }
  503. $name =~ s/\[[^\]]*\]//g;
  504. $name =~ s/\{[^\}]*\}//g;
  505. $name =~ s/[\(\)]//g;
  506. $name =~ s/[\'\"]//g;
  507. $name =~ s/[^\w\s]+/ /g;
  508. $name =~ s/\b20[01][0-9]\s*$//;
  509. $name =~ s/_+/ /g;
  510. $name =~ s/\s+/ /g;
  511. $name =~ s/^\s*//;
  512. $name =~ s/\s*$//;
  513. return $name;
  514. }
  515. sub readDir($$) {
  516. my ($indir, $regex) = @_;
  517. # Allow optional use of a matching regex
  518. my $useRegex = 1;
  519. if (!defined($regex) || length($regex) < 1) {
  520. $useRegex = 0;
  521. }
  522. # Clean up the input directory
  523. $indir =~ s/\/+$//;
  524. my @files = ();
  525. if (!opendir(DIR, $indir)) {
  526. warn('readDir: Unable to read directory: ' . $indir . ': ' . $! . "\n");
  527. return;
  528. }
  529. foreach my $file (readdir(DIR)) {
  530. my $keep = 0;
  531. # Regex filter (if active)
  532. if (!$useRegex) {
  533. $keep = 1;
  534. } elsif (eval('$file =~ m' . $regex)) {
  535. $keep = 1;
  536. }
  537. # Construct a complete file path
  538. $file = $indir . '/' . $file;
  539. # Ignore directories
  540. if (-d $file) {
  541. $keep = 0;
  542. }
  543. # Save matching files
  544. if ($keep) {
  545. push(@files, $file);
  546. }
  547. }
  548. closedir(DIR);
  549. # Sorts the files before returning to the caller
  550. my @orderedFiles = ();
  551. @orderedFiles = sort(@files);
  552. # Return the file list
  553. return (@orderedFiles);
  554. }