scanLocal.php 6.3 KB

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