index.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. "use strict";
  2. const fs = require("fs");
  3. const path = require("path");
  4. const minimist = require("minimist");
  5. const low = require("lowdb");
  6. const FileSync = require("lowdb/adapters/FileSync");
  7. const chokidar = require("chokidar");
  8. const hbjs = require("handbrake-js");
  9. const args = minimist(process.argv.slice(2), {
  10. alias: {
  11. h: "help",
  12. v: "version",
  13. c: "config",
  14. i: "ignoreInitial",
  15. t: "test"
  16. }
  17. });
  18. const version = () => {
  19. const pkg = require('./package.json');
  20. return console.log(pkg.version);
  21. }
  22. const help = () => {
  23. let message = `Usage:
  24. \tnpm run movies
  25. \tnpm run tvshows
  26. \tnpm run pr0n
  27. \tnpm run all
  28. \tnpm run help
  29. \tnpm run version\n`
  30. return console.log(message);
  31. }
  32. String.prototype.toTitleCase = function() {
  33. var i, j, str, lowers, uppers;
  34. str = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
  35. return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  36. });
  37. // Certain minor words should be left lowercase unless
  38. // they are the first or last words in the string
  39. lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At',
  40. 'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
  41. for (i = 0, j = lowers.length; i < j; i++)
  42. str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'),
  43. function(txt) {
  44. return txt.toLowerCase();
  45. });
  46. // Certain words such as initialisms or acronyms should be left uppercase
  47. uppers = ['Id', 'Tv'];
  48. for (i = 0, j = uppers.length; i < j; i++)
  49. str = str.replace(new RegExp('\\b' + uppers[i] + '\\b', 'g'),
  50. uppers[i].toUpperCase());
  51. return str;
  52. };
  53. // get the db from the path
  54. const getDbForDir = (dir, opts) => {
  55. let options = opts || Object.assign({}, defaults, paths[dir]); // baseline the options
  56. let adapter = new FileSync(options.database); // init the db adapter
  57. let db = low(adapter); // connect the db
  58. return db;
  59. };
  60. // find a file in the db
  61. const findFile = (db, file) => {
  62. return db.get('files').find({ input:file }).value(); // does it already exist?
  63. };
  64. // set a file in the db
  65. const setFile = (db, file, payload) => {
  66. if (!payload && typeof file === 'object')
  67. return db.get('files').push(file).write();
  68. return db.get('files').find({ input: file }).assign(payload).write();
  69. };
  70. // remove a file from the db
  71. const removeFile = (db, file) => {
  72. return db.get('files').remove({ input: file }).write(); // remove file from database
  73. };
  74. // write process output
  75. const processOutput = (str) => {
  76. process.stdout.clearLine();
  77. process.stdout.cursorTo(0);
  78. process.stdout.write(str);
  79. };
  80. // spawn a handbrake
  81. const processWithHandbrake = (input, output, preset) => {
  82. return new Promise((resolve, reject) => {
  83. const filename = path.basename(input);
  84. hbjs.spawn({
  85. input: input,
  86. output: output,
  87. preset: preset
  88. }).on('start', (err) => {
  89. processOutput(` --> processing "${filename}" to "${output}" with "${preset}"`);
  90. }).on('error', (err) => {
  91. processOutput(` --> processing ${output} error: ${err.message || err}.\n`);
  92. reject(err);
  93. }).on('progress', (progress) => {
  94. processOutput(` --> processing ${output} - ${progress.percentComplete}, ETA: ${progress.eta}`);
  95. }).on('cancelled', () => {
  96. processOutput(` --> processing ${output} cancelled\n`);
  97. reject(new Error(`Processing ${output} cancelled`));
  98. }).on('complete', () => {
  99. processOutput(` --> processing ${output} complete\n`);
  100. resolve(true);
  101. });
  102. });
  103. };
  104. if (args.version) return version();
  105. if (args.help || !args.config) return help();
  106. const ignoreInitial = (args.hasOwnProperty('ignoreInitial')) ? args.ignoreInitial : false; // ignore initial files
  107. const test = (args.hasOwnProperty('test')) ? args.test : false; // do a dry run ... just log
  108. // get our paths
  109. const paths = require(args.config);
  110. // init our defaults
  111. const defaults = {
  112. "preset": "Fast 1080p30",
  113. "clean": {},
  114. "titlecase": false,
  115. "folder": false,
  116. "database": "data/db.json"
  117. }
  118. // setup watcher options
  119. const opts = {
  120. ignored: /(^|[\/\\])\..|([s|S]ample\.*)/,
  121. ignoreInitial: ignoreInitial,
  122. persistent: true,
  123. usePolling: true,
  124. interval: 10000,
  125. depth: 1,
  126. awaitWriteFinish: {
  127. stabilityThreshold: 3000,
  128. pollInterval: 1000
  129. },
  130. ignorePermissionErrors: false
  131. };
  132. // parse the paths to dirs
  133. const dirs = Object.keys(paths);
  134. // initialize watches and db
  135. let watches = []; // array of things to watch
  136. for (let d in dirs) { // loop the dirs
  137. let dir = dirs[d]; // pointer
  138. let options = Object.assign({}, defaults, paths[dir]); // baseline the options
  139. let db = getDbForDir(dir);
  140. db.defaults({ files: [] }).write(); // init the database
  141. for (let e in options.exts) { // loop the exts to watch
  142. let ext = options.exts[e]; // alias the ext
  143. watches.push(`${dir}/**/*.${ext}`); // push the watch
  144. }
  145. }
  146. const watcher = chokidar.watch(watches, opts); // init our watcher
  147. console.log('Watching', watches);
  148. // when a new file is added
  149. watcher.on('add', async (file) => {
  150. let adapter,
  151. db,
  152. options,
  153. preset = 'Fast 1080p30',
  154. clean = {
  155. '.': ' '
  156. },
  157. ext = 'm4v',
  158. filename = path.parse(file).name,
  159. input = file,
  160. output = filename,
  161. titlecase = false,
  162. folder = false,
  163. destination = '';
  164. // determine the presets and clean object
  165. for (let i = 0, l = dirs.length; i < l; i++) {
  166. let dir = dirs[i]; // pointer to the dir
  167. if (file && dir && file.indexOf(dir) > -1) { // is this in this path?
  168. options = Object.assign({}, defaults, paths[dir]); // baseline the options
  169. db = getDbForDir(dir, options); // init the db connection
  170. let found = findFile(db, file); // does it already exist?
  171. if (found && found.status && found.status === 'success') { // was it already processed?
  172. //console.log(`File ${file} has already been successfully processed.`);
  173. return; // break this loop
  174. } else if (!found) { // was it found?
  175. processOutput(`-> ${path.basename(file)} [processing]`);
  176. setFile(db, { input:file, output:'', status:'', date:new Date() }); // push onto the list an entry
  177. } else {
  178. processOutput(`-> ${path.basename(file)} [re-processing]`);
  179. setFile(db, file, { status:'', date:new Date() }); // set the status to blank
  180. }
  181. preset = options.preset; // apply the specifics ...
  182. clean = options.clean; // ...
  183. ext = options.ext; // ...
  184. titlecase = !!options.titlecase; // ...
  185. folder = !!options.folder; // ...
  186. destination = options.destination; // ...
  187. break; // break the loop
  188. }
  189. }
  190. // clean the output name
  191. let cleanKeys = Object.keys(clean);
  192. for (let c in cleanKeys) {
  193. let key = cleanKeys[c];
  194. let val = clean[key];
  195. let re = new RegExp(key, 'gi');
  196. output = output.replace(re, val);
  197. }
  198. output = output.trim(); // trim the whitespace
  199. output = output.replace(/(\d{4})$/, `($1)`); // if there is a date at the end wrap it
  200. if (folder) { // do we have a sub folder option?
  201. let match = output.match(/^(.*?)?\./gm); // get the name for the file before the first .
  202. folder = (match && match.length > 0) ? match[0].slice(0, -1) : false; // get just the stuff before the dot ... or false
  203. }
  204. // do we have title case enabled?
  205. if (options.titlecase) output = output.toTitleCase(); // titlecase that string
  206. let target = destination + ((folder) ? '/' + folder : ''); // setup the target location
  207. output = target + '/' + output + '.' + ext; // update the new name
  208. if (fs.existsSync(output)) {
  209. processOutput(`-> ${path.basename(file)} [skipping ... already processed]\n`);
  210. setFile(db, file, { status: 'success', date:new Date() }); // update database with status
  211. return;
  212. }
  213. processOutput('\n');
  214. setFile(db, file, { output: output }); // update database with output
  215. if (target && !fs.existsSync(target)) {
  216. console.log(' --> creating parent directory:', target);
  217. fs.mkdirSync(target, {
  218. recursive: true
  219. });
  220. }
  221. //console.log(` --> processing "${path.basename(input)}" to "${output}" with "${preset}"`);
  222. // spawn handbrake
  223. if (!test) {
  224. try {
  225. await processWithHandbrake(input, output, preset);
  226. setFile(db, file, { status: 'success', date:new Date() }); // update database with status
  227. } catch (err) {
  228. setFile(db, file, { status: 'failure', date:new Date() }); // update database with status
  229. }
  230. }
  231. }).on('change', (file) => {
  232. console.log(` -> ${file} has been changed`);
  233. }).on('unlink', async (file) => {
  234. console.log(` -> ${file} has been removed`);
  235. let adapter, db, options;
  236. for (let i = 0, l = dirs.length; i < l; i++) {
  237. let dir = dirs[i]; // pointer to the dir
  238. if (file && dir && file.indexOf(dir) > -1) { // is this in this path?
  239. db = getDbForDir(dir); // init the db connection
  240. removeFile(db, file); // remove file form database
  241. }
  242. }
  243. }).on('error', (error) => {
  244. console.error(` -> Error: ${error.message || error}`);
  245. });