scanLocal.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. $SORT=tempnam(sys_get_temp_dir(), 'scanLocal-sort');
  54. exec('cat ' . escapeshellarg($FIND) . ' | sort > ' . escapeshellarg($SORT));
  55. unlink($FIND);
  56. unset($FIND);
  57. # Filter junk
  58. exec('grep -v "\/\._" ' . escapeshellarg($SORT) .
  59. ' | grep -v "\/\.DS_Store\$" | grep -v "\/\.git\/"',
  60. $FILES);
  61. unlink($SORT);
  62. unset($SORT);
  63. # Prepare statements
  64. $select = $dbh->prepare('SELECT base, path, type, mtime, hash, hash_time FROM local WHERE base = :base AND path = :path');
  65. $insert = $dbh->prepare('INSERT INTO local (base, path, type, mtime) VALUES (:base, :path, :type, now())');
  66. $mtime = $dbh->prepare('SELECT EXTRACT(EPOCH FROM mtime) AS mtime FROM local WHERE base = :base AND path = :path');
  67. $set_mtime = $dbh->prepare('UPDATE local SET mtime = now() WHERE base = :base AND path = :path');
  68. $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)');
  69. $set_hash = $dbh->prepare('UPDATE local SET hash = :hash, hash_time = now() WHERE base = :base AND path = :path');
  70. # Loop through all the files
  71. foreach ($FILES as $FILE) {
  72. # Construct the absolute path
  73. $PATH = $BASE_LOCAL . '/' . $FILE;
  74. # Does the path exist
  75. $EXISTS = false;
  76. $select->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE));
  77. $result = $select->fetch();
  78. if (!$result) {
  79. $PARTS = pathinfo($PATH);
  80. $EXT = strtolower($PARTS['extension']);
  81. $NAME = strtolower($PARTS['filename']);
  82. unset($PARTS);
  83. $TYPE = 'other';
  84. if (is_dir($PATH)) {
  85. $TYPE = 'folder';
  86. if ($NAME == '.git') {
  87. $TYPE = 'ignored';
  88. }
  89. } else {
  90. if ($EXT == 'm4v' || $EXT == 'mkv' || $EXT == 'mp4' || $EXT == 'mov' ||
  91. $EXT == 'vob' || $EXT == 'iso' || $EXT == 'avi') {
  92. $TYPE = 'video';
  93. } else if ($EXT == 'mp3' || $EXT == 'aac' || $EXT == 'm4a' || $EXT == 'm4b' ||
  94. $EXT == 'm4p' || $EXT == 'wav') {
  95. $TYPE = 'audio';
  96. } else if ($EXT == 'epub' || $EXT == 'pdf') {
  97. $TYPE = 'book';
  98. } else if ($EXT == 'jpg' || $EXT == 'png') {
  99. $TYPE = 'image';
  100. } else if ($EXT == 'gz' || $EXT == 'zip' || $EXT == 'xz') {
  101. $TYPE = 'archive';
  102. } else if ($EXT == 'itc' || $EXT == 'itl' || $EXT == 'strings' || $EXT == 'itdb' ||
  103. $EXT == 'plist' || $EXT == 'ipa' || $EXT == 'ini') {
  104. $TYPE = 'database';
  105. } else if ($EXT == 'clip' || $EXT == 'riff' || $EXT == 'nfo') {
  106. $TYPE = 'metadata';
  107. } else if ($EXT == 'webloc' || $NAME == 'skip' || $NAME == 'season_done' ||
  108. $NAME == 'more_number_formats' || $NAME == 'no_quality_checks' ||
  109. $NAME == 'filler' || $NAME == 'search_name' || $EXT == 'disabled' ||
  110. $NAME == 'must_match' || $EXT == 'fakeshow' || $EXT == 'filler' ||
  111. $NAME == 'excludes' || $NAME == 'search_by_date' || $EXT == 'twopart') {
  112. $TYPE = 'metadata';
  113. } else if ($EXT == 'fake' || $EXT == 'txt' || $EXT == 'json' ||
  114. $EXT == 'bup' || $EXT == 'ifo') {
  115. $TYPE = 'metadata';
  116. } else if ($EXT == 'lastfindrecode' || $NAME == 'placeholder' || $EXT == 'plexignore') {
  117. $TYPE = 'ignored';
  118. } else if ($EXT == 'tmp' || $EXT == 'gitignore' || preg_match('/^\.smbdelete/', $NAME)) {
  119. $TYPE = 'ignored';
  120. }
  121. }
  122. if ($TYPE == 'other') {
  123. die('Unknown file type: ' . $PATH . ': ' . $NAME . '^' . $EXT . "\n");
  124. }
  125. echo 'Adding: ' . $PATH . ': ' . $TYPE . "\n";
  126. $insert->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE, ':type' => $TYPE));
  127. if ($insert->rowCount() != 1) {
  128. print_r($insert->errorInfo());
  129. die('Unable to insert: ' . $PATH . "\n");
  130. }
  131. } else {
  132. $TYPE = $result['type'];
  133. }
  134. # Skip 'ingnored' files
  135. if ($TYPE == 'ignored') {
  136. continue;
  137. }
  138. # Update the mtime as needed
  139. $MTIME = trim(shell_exec('stat -c "%Y" ' . escapeshellarg($PATH)));
  140. $mtime->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE));
  141. $result = $mtime->fetch(PDO::FETCH_ASSOC);
  142. if (!$result && !$result['mtime'] || !$MTIME) {
  143. die('Unable to find mtime for: ' . $PATH . "\n");
  144. } else if ($result['mtime'] < $MTIME) {
  145. $set_mtime->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE));
  146. }
  147. # Update hashes as needed
  148. if ($TYPE != 'folder') {
  149. $hash_time_check->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE, ':mtime' => $MTIME));
  150. $result = $hash_time_check->fetch(PDO::FETCH_ASSOC);
  151. if ($result) {
  152. echo 'Adding hash: ' . $PATH . "\n";
  153. $HASH = trim(shell_exec('md5sum ' . escapeshellarg($PATH) . ' | cut -d " " -f 1'));
  154. if (strlen($HASH) == 32) {
  155. $set_hash->execute(array(':base' => $BASE_LOCAL, ':path' => $FILE, ':hash' => $HASH));
  156. } else {
  157. die('Invalid hash (' . $HASH . ') for file: ' . $PATH . "\n");
  158. }
  159. } else {
  160. if ($DEBUG) {
  161. echo 'Hash is up-to-date: ' . $PATH . "\n";
  162. }
  163. }
  164. } else {
  165. if ($DEBUG) {
  166. echo 'No hash for folder: ' . $PATH . "\n";
  167. }
  168. }
  169. }
  170. unset($FILES);
  171. ?>