scanLocal.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/local/bin/php
  2. <?php
  3. # Debug
  4. $DEBUG = false;
  5. if ($_ENV['DEBUG']) {
  6. $DEBUG = true;
  7. }
  8. # Includes
  9. require_once 'opendrive.php';
  10. # Command-line parameters
  11. global $argc;
  12. global $argv;
  13. $SUB_DIR = '';
  14. if (($argc > 1) && (strlen($argv[1]) > 0)) {
  15. $SUB_DIR = trim($argv[1]);
  16. }
  17. $DELAY_DAYS=30;
  18. if (isset($_ENV['DELAY_DAYS'])) {
  19. $DELAY_DAYS = $_ENV['DELAY_DAYS'];
  20. }
  21. $VIDEO_DIR=$_ENV['HOME'] . '/bin/video';
  22. if (isset($_ENV['VIDEO_DIR'])) {
  23. $VIDEO_DIR = $_ENV['VIDEO_DIR'];
  24. }
  25. $BASE_LOCAL='';
  26. if (isset($_ENV['BASE_LOCAL'])) {
  27. $BASE_LOCAL = $_ENV['BASE_LOCAL'];
  28. } else {
  29. $BASE_LOCAL = trim(shell_exec($VIDEO_DIR . '/mediaPath'));
  30. }
  31. # Allow usage with absolute local paths
  32. if (substr($SUB_DIR, 0, 1) == '/') {
  33. $SUB_DIR = preg_replace('/^' . preg_quote($BASE_LOCAL, '/') . '\//', '', $SUB_DIR);
  34. }
  35. # Usage checks
  36. if (strlen($SUB_DIR) < 1 || $DELAY_DAYS < 1) {
  37. die('Usage: ' . $argv[0] . " sub_directory\n");
  38. }
  39. # Sanity checks
  40. $LOCAL = $BASE_LOCAL . '/' . $SUB_DIR;
  41. if (!file_exists($LOCAL) || !is_dir($LOCAL)) {
  42. die('Invalid local directory: ' . $LOCAL . "\n");
  43. }
  44. # Open the DB connection
  45. $dbh = dbOpen();
  46. # Grab the file list -- limit files by mtime, but include all directories
  47. $FIND=tempnam(sys_get_temp_dir(), 'scanLocal-find');
  48. exec('cd ' . escapeshellarg($BASE_LOCAL) . ' && find ' . escapeshellarg($SUB_DIR) .
  49. ' -type f -mtime +' . escapeshellarg($DELAY_DAYS) . ' > ' . escapeshellarg($FIND));
  50. exec('cd ' . escapeshellarg($BASE_LOCAL) . ' && find ' . escapeshellarg($SUB_DIR) .
  51. ' -type d >> ' . escapeshellarg($FIND));
  52. # Sort
  53. exec('cat ' . escapeshellarg($FIND) . ' | sort ', $FILES);
  54. unlink($FIND);
  55. unset($FIND);
  56. # Prepare statements
  57. $select = $dbh->prepare('SELECT base, path, type, mtime, hash, hash_time FROM files WHERE base = :base AND path = :path');
  58. $insert = $dbh->prepare('INSERT INTO files (base, path, type, mtime) VALUES (:base, :path, :type, now())');
  59. $mtime = $dbh->prepare('SELECT EXTRACT(EPOCH FROM mtime) AS mtime FROM files WHERE base = :base AND path = :path');
  60. $set_mtime = $dbh->prepare('UPDATE files SET mtime = now() WHERE base = :base AND path = :path');
  61. $hash_time_check = $dbh->prepare('SELECT hash, EXTRACT(EPOCH FROM hash_time) AS hash_time FROM files WHERE base = :base AND path = :path AND (hash_time IS NULL OR EXTRACT(EPOCH FROM hash_time) < :mtime)');
  62. $set_hash = $dbh->prepare('UPDATE files SET hash = :hash, hash_time = now() WHERE base = :base AND path = :path');
  63. # Loop through all the files
  64. foreach ($FILES as $FILE) {
  65. # Construct the absolute path
  66. $PATH = $BASE_LOCAL . '/' . $FILE;
  67. # Does the path exist
  68. $EXISTS = false;
  69. $select->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE));
  70. $result = $select->fetch();
  71. if (!$result) {
  72. $PARTS = pathinfo($PATH);
  73. $EXT = strtolower($PARTS['extension']);
  74. $NAME = strtolower($PARTS['filename']);
  75. unset($PARTS);
  76. $TYPE = 'other';
  77. if (preg_match('/\/\.git(\/.*)?$/', $PATH)) {
  78. $TYPE = 'ignored';
  79. } else if (preg_match('/\/\._/', $PATH)) {
  80. $TYPE = 'ignored';
  81. } else if ($EXT == 'lastfindrecode' || $NAME == 'placeholder' || $EXT == 'plexignore') {
  82. $TYPE = 'ignored';
  83. } else if ($EXT == 'tmp' || $EXT == 'gitignore' || $EXT == 'ds_store' ||
  84. preg_match('/^\.smbdelete/', $NAME)) {
  85. $TYPE = 'ignored';
  86. } else if (is_dir($PATH)) {
  87. $TYPE = 'folder';
  88. } else if ($EXT == 'm4v' || $EXT == 'mkv' || $EXT == 'mp4' || $EXT == 'mov' ||
  89. $EXT == 'vob' || $EXT == 'iso' || $EXT == 'avi') {
  90. $TYPE = 'video';
  91. } else if ($EXT == 'mp3' || $EXT == 'aac' || $EXT == 'm4a' || $EXT == 'm4b' ||
  92. $EXT == 'm4p' || $EXT == 'wav') {
  93. $TYPE = 'audio';
  94. } else if ($EXT == 'epub' || $EXT == 'pdf') {
  95. $TYPE = 'book';
  96. } else if ($EXT == 'jpg' || $EXT == 'png') {
  97. $TYPE = 'image';
  98. } else if ($EXT == 'gz' || $EXT == 'zip' || $EXT == 'xz') {
  99. $TYPE = 'archive';
  100. } else if ($EXT == 'itc' || $EXT == 'itl' || $EXT == 'strings' || $EXT == 'itdb' ||
  101. $EXT == 'plist' || $EXT == 'ipa' || $EXT == 'ini') {
  102. $TYPE = 'database';
  103. } else if ($EXT == 'clip' || $EXT == 'riff' || $EXT == 'nfo') {
  104. $TYPE = 'metadata';
  105. } else if ($EXT == 'webloc' || $NAME == 'skip' || $NAME == 'season_done' ||
  106. $NAME == 'more_number_formats' || $NAME == 'no_quality_checks' ||
  107. $NAME == 'filler' || $NAME == 'search_name' || $EXT == 'disabled' ||
  108. $NAME == 'must_match' || $EXT == 'fakeshow' || $EXT == 'filler' ||
  109. $NAME == 'excludes' || $NAME == 'search_by_date' || $EXT == 'twopart') {
  110. $TYPE = 'metadata';
  111. } else if ($EXT == 'fake' || $EXT == 'txt' || $EXT == 'json' ||
  112. $EXT == 'bup' || $EXT == 'ifo') {
  113. $TYPE = 'metadata';
  114. }
  115. if ($TYPE == 'other') {
  116. die('Unknown file type: ' . $PATH . ': ' . $NAME . '^' . $EXT . "\n");
  117. }
  118. echo 'Adding: ' . $PATH . ': ' . $TYPE . "\n";
  119. $insert->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE, ':type' => $TYPE));
  120. if ($insert->rowCount() != 1) {
  121. print_r($insert->errorInfo());
  122. die('Unable to insert: ' . $PATH . "\n");
  123. }
  124. } else {
  125. $TYPE = $result['type'];
  126. }
  127. # Skip 'ingnored' files
  128. if ($TYPE == 'ignored') {
  129. continue;
  130. }
  131. # Update the mtime as needed
  132. $MTIME = trim(shell_exec('stat -c "%Y" ' . escapeshellarg($PATH)));
  133. $mtime->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE));
  134. $result = $mtime->fetch(PDO::FETCH_ASSOC);
  135. if (!$result && !$result['mtime'] || !$MTIME) {
  136. die('Unable to find mtime for: ' . $PATH . "\n");
  137. } else if ($result['mtime'] < $MTIME) {
  138. $set_mtime->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE));
  139. }
  140. # Update hashes as needed
  141. if ($TYPE != 'folder') {
  142. $hash_time_check->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE, ':mtime' => $MTIME));
  143. $result = $hash_time_check->fetch(PDO::FETCH_ASSOC);
  144. if ($result) {
  145. echo 'Adding hash: ' . $PATH . "\n";
  146. $HASH = trim(shell_exec('md5sum ' . escapeshellarg($PATH) . ' | cut -d " " -f 1'));
  147. if (strlen($HASH) == 32) {
  148. $set_hash->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE, ':hash' => $HASH));
  149. } else {
  150. die('Invalid hash (' . $HASH . ') for file: ' . $PATH . "\n");
  151. }
  152. } else {
  153. if ($DEBUG) {
  154. echo 'Hash is up-to-date: ' . $PATH . "\n";
  155. }
  156. }
  157. } else {
  158. if ($DEBUG) {
  159. echo 'No hash for folder: ' . $PATH . "\n";
  160. }
  161. }
  162. }
  163. unset($FILES);
  164. # Update priorities
  165. $priority = $dbh->prepare('UPDATE files SET priority = :priority WHERE path LIKE :path');
  166. $priority->execute(array(':priority' => 100, ':path' => 'Movies/%'));
  167. $priority->execute(array(':priority' => 50, ':path' => 'iTunes/%'));
  168. $priority->execute(array(':priority' => -50, ':path' => 'Backups/%'));
  169. $priority->execute(array(':priority' => -100, ':path' => 'TV/%'));
  170. ?>