transmission.pl 15 KB

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