diff --git a/badger-am.js b/badger-am.js index 99256ab..e7446ae 100755 --- a/badger-am.js +++ b/badger-am.js @@ -8,14 +8,15 @@ const fse = require('fs-extra'); const async = require('async'); const commander = require('commander'); const recursive = require('recursive-readdir'); -const metadata = require('musicmetadata'); -const progress = require('progress'); + const app = require('./package.json'); +const audio = require('./lib/audio'); +const util = require('./lib/util'); // generate timestamp const start = process.hrtime(); -printLogo(); +util.printLogo(); // general options commander @@ -56,7 +57,7 @@ function sort(input, output, options) { function (files, asyncCallback) { console.log(files.length + ' \'' + options.format + '\' files found'); // display progressbar - const bar = createProgressBar(files.length); + const bar = util.createProgressBar(files.length); // handle each file async.eachLimit(files, concurrency, function (file, eachCallback) { processFile(output, file, function (err) { @@ -74,7 +75,7 @@ function sort(input, output, options) { }); } ], function (err, result) { - exit(err); + util.exit(err, start); }); function ignoreFilter(file, stats) { @@ -86,13 +87,13 @@ function sort(input, output, options) { function processFile(output, sourceFile, callback) { async.waterfall([ function (asyncCallback) { - extractMetadata(sourceFile, asyncCallback) + audio.extractMetadata(sourceFile, asyncCallback) }, function (sourceFile, metadata, asyncCallback) { - pathFromMetadata(sourceFile, output, metadata, asyncCallback); + util.pathFromMetadata(sourceFile, output, metadata, asyncCallback); }, function (targetFile, asyncCallback) { - moveFile(sourceFile, targetFile, asyncCallback); + util.moveFile(sourceFile, targetFile, asyncCallback); } ], function (err, results) { if (err) { @@ -102,112 +103,5 @@ function processFile(output, sourceFile, callback) { }); } -// extract metadata for further processing -function extractMetadata(sourceFile, callback) { - const stream = fs.createReadStream(sourceFile); - metadata(stream, function (err, metadata) { - if (err) { - return callback(err); - } - stream.close(); - callback(null, sourceFile, metadata) - }); -} -// create path for target file -function pathFromMetadata(file, output, metadata, callback) { - // define directory - let filePath = path.normalize(output); - if (metadata.albumartist && metadata.albumartist.length > 0) { - let tmp; - const artistCount = metadata.albumartist.length; - for (let counter = 0; counter < artistCount; counter++) { - if (counter > 0) { - tmp += ' - ' + metadata.albumartist[counter]; - } else { - tmp = metadata.albumartist[counter]; - } - } - filePath = path.join(filePath, tmp); - } else { - filePath = path.join(filePath, metadata.artist[0]); - } - if (metadata.album) { - filePath = path.join(filePath, metadata.album); - } - // define filename - let fileName = ''; - if (metadata.disk.no) { - fileName += frontFill(metadata.disk.no, '0', 2); - } - if (metadata.track.no) { - if (fileName) { - fileName += '-' + frontFill(metadata.track.no, '0', 2) + ' '; - } else { - fileName += frontFill(metadata.track.no, '0', 2) + ' '; - } - } - if (metadata.artist) { - fileName += metadata.artist[0] + ' - '; - } - if (metadata.title) { - fileName += metadata.title; - } - // append extension - fileName += path.extname(file); - // join directory and name - callback(null, path.join(filePath, fileName)); -} -// create a ascii progressbar -function createProgressBar(total) { - return new progress(':bar | progress: :current/:total (:percent) | elapsed: :elapseds | eta: :etas', { - total: total, - width: 32 - }); -} - -// create target directory and move the file -function moveFile(source, target, callback) { - async.series([ - function (asyncCallback) { - fse.mkdirs(path.dirname(target), asyncCallback); - }, - function (asyncCallback) { - fse.move(source, target, asyncCallback); - } - ], function (err, results) { - if (err) { - return callback(err); - } - callback(); - }); -} - -// fill a string beginning from the front -function frontFill(string, fill, length) { - while (string.toString().length < length) { - string = fill + string; - } - return string; -} - -// print logo -function printLogo() { - console.log(' _ _ _ __ __ '); - console.log(' | |__ __ _ __| |__ _ ___ _ _ /_\\ | \\/ |'); - console.log(' | \'_ \\\/ _` \/ _` \/ _` / -_) \'_\/ _ \\| |\\/| |'); - console.log(' |_.__\/\\__,_\\__,_\\__, \\___|_|\/_\/ \\_\\_| |_|'); - console.log(' |___/ '); -} - -// shutdown -function exit(err) { - if (err) { - console.error(err); - process.exit(1); - } - const diff = process.hrtime(start); - console.log('exiting after ' + ((diff[0] + (diff[1] / 1000000)) / 1000).toFixed(2) + ' seconds'); - process.exit(0); -} \ No newline at end of file diff --git a/lib/audio.js b/lib/audio.js new file mode 100644 index 0000000..25166b8 --- /dev/null +++ b/lib/audio.js @@ -0,0 +1,14 @@ +// requirements +const metadata = require('musicmetadata'); + +// extract metadata for further processing +exports.extractMetadata = function extractMetadata(sourceFile, callback) { + const stream = fs.createReadStream(sourceFile); + metadata(stream, function (err, metadata) { + if (err) { + return callback(err); + } + stream.close(); + callback(null, sourceFile, metadata) + }); +}; \ No newline at end of file diff --git a/lib/util.js b/lib/util.js new file mode 100644 index 0000000..382ab84 --- /dev/null +++ b/lib/util.js @@ -0,0 +1,100 @@ +// requirements +const progress = require('progress'); + +// print logo +exports.printLogo = function printLogo() { + console.log(' _ _ _ __ __ '); + console.log(' | |__ __ _ __| |__ _ ___ _ _ /_\\ | \\/ |'); + console.log(' | \'_ \\\/ _` \/ _` \/ _` / -_) \'_\/ _ \\| |\\/| |'); + console.log(' |_.__\/\\__,_\\__,_\\__, \\___|_|\/_\/ \\_\\_| |_|'); + console.log(' |___/ '); +}; + +// create path for target file +exports.pathFromMetadata = function pathFromMetadata(file, output, metadata, callback) { + // define directory + let filePath = path.normalize(output); + if (metadata.albumartist && metadata.albumartist.length > 0) { + let tmp; + const artistCount = metadata.albumartist.length; + for (let counter = 0; counter < artistCount; counter++) { + if (counter > 0) { + tmp += ' - ' + metadata.albumartist[counter]; + } else { + tmp = metadata.albumartist[counter]; + } + } + filePath = path.join(filePath, tmp); + } else { + filePath = path.join(filePath, metadata.artist[0]); + } + if (metadata.album) { + filePath = path.join(filePath, metadata.album); + } + // define filename + let fileName = ''; + if (metadata.disk.no) { + fileName += frontFill(metadata.disk.no, '0', 2); + } + if (metadata.track.no) { + if (fileName) { + fileName += '-' + frontFill(metadata.track.no, '0', 2) + ' '; + } else { + fileName += frontFill(metadata.track.no, '0', 2) + ' '; + } + } + if (metadata.artist) { + fileName += metadata.artist[0] + ' - '; + } + if (metadata.title) { + fileName += metadata.title; + } + // append extension + fileName += path.extname(file); + // join directory and name + callback(null, path.join(filePath, fileName)); +}; + +// create target directory and move the file +exports.moveFile = function moveFile(source, target, callback) { + async.series([ + function (asyncCallback) { + fse.mkdirs(path.dirname(target), asyncCallback); + }, + function (asyncCallback) { + fse.move(source, target, asyncCallback); + } + ], function (err, results) { + if (err) { + return callback(err); + } + callback(); + }); +} + +// fill a string beginning from the front +exports.frontfill = function frontFill(string, fill, length) { + while (string.toString().length < length) { + string = fill + string; + } + return string; +}; + +// create a ascii progressbar +exports.createProgressBar = function createProgressBar(total) { + return new progress(':bar | progress: :current/:total (:percent) | elapsed: :elapseds | eta: :etas', { + total: total, + width: 32 + }); +}; + +// shutdown +exports.exit = function exit(err, start) { + if (err) { + console.error(err); + process.exit(1); + } + const diff = process.hrtime(start); + console.log('exiting after ' + ((diff[0] + (diff[1] / 1000000)) / 1000).toFixed(2) + ' seconds'); + process.exit(0); +}; \ No newline at end of file diff --git a/package.json b/package.json index b221b98..ec59421 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "badger-am", - "version": "0.2.0", + "version": "0.2.1", "license": "MIT", "description": "audio manager", "author": "Daniel Sommer ",