transmission.pl 15 KB

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