index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 Queue = require('better-queue');
  7. const FileSync = require('lowdb/adapters/FileSync');
  8. const chokidar = require('chokidar');
  9. const hbjs = require('handbrake-js');
  10. const args = minimist(process.argv.slice(2), {
  11. alias: {
  12. h: 'help',
  13. v: 'version',
  14. c: 'config',
  15. i: 'ignoreInitial',
  16. t: 'test',
  17. },
  18. });
  19. const version = () => {
  20. const pkg = require('./package.json');
  21. return console.log(pkg.version);
  22. };
  23. const help = () => {
  24. let message = `Usage:
  25. \tnpm run movies
  26. \tnpm run tvshows
  27. \tnpm run pr0n
  28. \tnpm run all
  29. \tnpm run help
  30. \tnpm run version\n`;
  31. return console.log(message);
  32. };
  33. String.prototype.toTitleCase = function() {
  34. var i, j, str, lowers, uppers;
  35. str = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
  36. return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  37. });
  38. // Certain minor words should be left lowercase unless
  39. // they are the first or last words in the string
  40. lowers = [
  41. 'A',
  42. 'An',
  43. 'The',
  44. 'And',
  45. 'But',
  46. 'Or',
  47. 'For',
  48. 'Nor',
  49. 'As',
  50. 'At',
  51. 'By',
  52. 'For',
  53. 'From',
  54. 'In',
  55. 'Into',
  56. 'Near',
  57. 'Of',
  58. 'On',
  59. 'Onto',
  60. 'To',
  61. 'With',
  62. ];
  63. for (i = 0, j = lowers.length; i < j; i++)
  64. str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'), function(txt) {
  65. return txt.toLowerCase();
  66. });
  67. // Certain words such as initialisms or acronyms should be left uppercase
  68. uppers = ['Id', 'Tv'];
  69. for (i = 0, j = uppers.length; i < j; i++) str = str.replace(new RegExp('\\b' + uppers[i] + '\\b', 'g'), uppers[i].toUpperCase());
  70. return str;
  71. };
  72. // get the db from the path
  73. const getDbForDir = (dir, opts) => {
  74. let options = opts || Object.assign({}, defaults, paths[dir]); // baseline the options
  75. let adapter = new FileSync(options.database); // init the db adapter
  76. let db = low(adapter); // connect the db
  77. return db;
  78. };
  79. // find a file in the db
  80. const findFile = (db, file) => {
  81. return db
  82. .get('files')
  83. .find({
  84. input: file,
  85. })
  86. .value(); // does it already exist?
  87. };
  88. // set a file in the db
  89. const setFile = (db, file, payload) => {
  90. if (!payload && typeof file === 'object')
  91. return db
  92. .get('files')
  93. .push(file)
  94. .write();
  95. return db
  96. .get('files')
  97. .find({
  98. input: file,
  99. })
  100. .assign(payload)
  101. .write();
  102. };
  103. // remove a file from the db
  104. const removeFile = (db, file) => {
  105. return db
  106. .get('files')
  107. .remove({
  108. input: file,
  109. })
  110. .write(); // remove file from database
  111. };
  112. // write process output
  113. const processOutput = str => {
  114. process.stdout.clearLine();
  115. process.stdout.cursorTo(0);
  116. process.stdout.write(str);
  117. };
  118. // spawn a handbrake
  119. const processWithHandbrake = (input, output, preset) => {
  120. return new Promise((resolve, reject) => {
  121. const inputName = path.basename(input);
  122. const outputName = path.basename(output);
  123. hbjs
  124. .spawn({
  125. input: input,
  126. output: output,
  127. preset: preset,
  128. })
  129. .on('start', err => {
  130. processOutput(` -> processing "${inputName}" to "${outputName}" with "${preset}"`);
  131. })
  132. .on('error', err => {
  133. processOutput(` -> processing ${outputName} error: ${err.message || err}.\n`);
  134. reject(err);
  135. })
  136. .on('progress', progress => {
  137. processOutput(` -> processing ${outputName} - ${progress.percentComplete}%, ETA: ${progress.eta}`);
  138. })
  139. .on('cancelled', () => {
  140. processOutput(` -> processing ${outputName} cancelled\n`);
  141. reject(new Error(`Processing ${outputName} cancelled`));
  142. })
  143. .on('complete', () => {
  144. processOutput(` -> processing ${outputName} complete\n`);
  145. resolve(true);
  146. });
  147. });
  148. };
  149. const processFile = async file => {
  150. let db,
  151. options,
  152. preset = 'Fast 1080p30',
  153. clean = {
  154. '.': ' ',
  155. },
  156. ext = 'm4v',
  157. filename = path.parse(file).name,
  158. input = file,
  159. output = filename,
  160. titlecase = false,
  161. folder = false,
  162. destination = '';
  163. // determine the presets and clean object
  164. for (let i = 0, l = dirs.length; i < l; i++) {
  165. let dir = dirs[i]; // pointer to the dir
  166. if (file && dir && file.indexOf(dir) > -1) {
  167. // 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') {
  172. // was it already processed?
  173. //console.log(`File ${file} has already been successfully processed.`);
  174. return false; // break this loop
  175. } else if (!found) {
  176. // was it found?
  177. processOutput(` -> ${path.basename(file)} [processing]`);
  178. setFile(db, {
  179. input: file,
  180. output: '',
  181. status: '',
  182. date: new Date(),
  183. }); // push onto the list an entry
  184. } else {
  185. processOutput(` -> ${path.basename(file)} [re-processing]`);
  186. setFile(db, file, {
  187. status: '',
  188. date: new Date(),
  189. }); // set the status to blank
  190. }
  191. preset = options.preset; // apply the specifics ...
  192. clean = options.clean; // ...
  193. ext = options.ext; // ...
  194. titlecase = !!options.titlecase; // ...
  195. folder = !!options.folder; // ...
  196. destination = options.destination; // ...
  197. break; // break the loop
  198. }
  199. }
  200. // clean the output name
  201. let cleanKeys = Object.keys(clean);
  202. for (let c in cleanKeys) {
  203. let key = cleanKeys[c];
  204. let val = clean[key];
  205. let re = new RegExp(key, 'gi');
  206. output = output.replace(re, val);
  207. }
  208. // baseline the output name
  209. output = output.trim(); // trim the whitespace
  210. output = output.replace(/(\d{4})$/, `($1)`); // if there is a date at the end wrap it
  211. // do we have a sub folder option?
  212. if (folder) {
  213. let match = output.match(/^(.*?)?\./gm); // get the name for the file before the first .
  214. folder = match && match.length > 0 ? match[0].slice(0, -1) : false; // get just the stuff before the dot ... or false
  215. }
  216. // do we have title case enabled?
  217. if (options.titlecase) output = output.toTitleCase(); // titlecase that string
  218. // baseline the target
  219. let target = destination + (folder ? '/' + folder : ''); // setup the target location
  220. output = target + '/' + output + '.' + ext; // update the new name
  221. // do we already have an existing output that matches?
  222. if (fs.existsSync(output)) {
  223. processOutput(` -> ${path.basename(file)} [skipping ... already processed]\n`);
  224. setFile(db, file, {
  225. output: output,
  226. status: 'success',
  227. date: new Date(),
  228. }); // update database with status
  229. return false;
  230. } else {
  231. process.stdout.write('\n'); // send a new line
  232. }
  233. // update database with output name
  234. setFile(db, file, {
  235. output: output,
  236. });
  237. // create parent if required
  238. if (target && !fs.existsSync(target)) {
  239. processOutput(` -> creating parent directory: ${target}\n`);
  240. fs.mkdirSync(target, {
  241. recursive: true,
  242. });
  243. }
  244. // spawn handbrake
  245. if (!test) {
  246. try {
  247. await processWithHandbrake(input, output, preset);
  248. setFile(db, file, {
  249. status: 'success',
  250. date: new Date(),
  251. }); // update database with status
  252. } catch (err) {
  253. setFile(db, file, {
  254. status: 'failure',
  255. date: new Date(),
  256. }); // update database with status
  257. }
  258. }
  259. return true; // when complete return true
  260. };
  261. // cleanup removes from the db
  262. const cleanup = file => {
  263. let db;
  264. for (let i = 0, l = dirs.length; i < l; i++) {
  265. let dir = dirs[i]; // pointer to the dir
  266. if (file && dir && file.indexOf(dir) > -1) {
  267. // is this in this path?
  268. db = getDbForDir(dir); // init the db connection
  269. removeFile(db, file); // remove file form database
  270. }
  271. }
  272. };
  273. // handle args
  274. if (args.version) return version(); // show version
  275. if (args.help || !args.config) return help(); // show help
  276. const ignoreInitial = args.hasOwnProperty('ignoreInitial') ? args.ignoreInitial : false; // ignore initial files
  277. const test = args.hasOwnProperty('test') ? args.test : false; // do a dry run ... just log
  278. // get our paths
  279. const paths = require(args.config);
  280. // init our defaults
  281. const defaults = {
  282. preset: 'Fast 1080p30',
  283. clean: {},
  284. titlecase: false,
  285. folder: false,
  286. database: 'data/db.json',
  287. };
  288. // setup watcher options
  289. const opts = {
  290. ignored: /(^|[\/\\])\..|([s|S]ample\.*)/,
  291. ignoreInitial: ignoreInitial,
  292. persistent: true,
  293. usePolling: true,
  294. interval: 10000,
  295. depth: 1,
  296. awaitWriteFinish: {
  297. stabilityThreshold: 3000,
  298. pollInterval: 1000,
  299. },
  300. ignorePermissionErrors: false,
  301. atomic: true,
  302. };
  303. // parse the paths to dirs
  304. const dirs = Object.keys(paths);
  305. // initialize watches and db then start the watcher
  306. const main = async () => {
  307. var queue = new Queue(
  308. async (input, cb) => {
  309. // init the queue
  310. let result = await processFile(input); // process the queue
  311. cb(null, result);
  312. },
  313. {
  314. batchSize: 1,
  315. concurrent: 1,
  316. maxRetries: 3,
  317. retryDelay: 5000,
  318. }
  319. );
  320. var watches = []; // array of things to watch
  321. for (let d in dirs) {
  322. // loop the dirs
  323. let dir = dirs[d]; // pointer
  324. let options = Object.assign({}, defaults, paths[dir]); // baseline the options
  325. let db = getDbForDir(dir);
  326. db.defaults({
  327. files: [],
  328. }).write(); // init the database
  329. for (let e in options.exts) {
  330. // loop the exts to watch
  331. let ext = options.exts[e]; // alias the ext
  332. watches.push(`${dir}/**/*.${ext}`); // push the watch
  333. }
  334. }
  335. const watcher = chokidar.watch(watches, opts); // init our watcher
  336. console.log('Watching:', watches);
  337. watcher
  338. .on('add', async file => {
  339. // when a file is added ...
  340. console.log(` -> ${file} has been added`);
  341. queue.push(file); // push the file onto the queue to be processed
  342. })
  343. .on('change', file => {
  344. // when a file changes ...
  345. console.log(` -> ${file} has been changed`);
  346. })
  347. .on('unlink', async file => {
  348. // when a file is removed ...
  349. console.log(` -> ${file} has been removed`);
  350. cleanup(file);
  351. })
  352. .on('error', error => {
  353. console.error(` -> Error: ${error.message || error}`);
  354. });
  355. };
  356. (async () => {
  357. main();
  358. })().catch(err => {
  359. console.log('Error:', err.message || err);
  360. });