Added batch mode and CLI interface.

This commit is contained in:
Roel van Uden
2015-01-24 12:29:12 +01:00
parent 7d0c8f2ecd
commit 66f62d3aa1
7 changed files with 118 additions and 55 deletions

77
src/batch.js Normal file
View File

@@ -0,0 +1,77 @@
'use strict';
var Command = require('commander').Command;
var fs = require('fs');
var path = require('path');
var series = require('./series');
/**
* Streams the batch of series to disk.
* @param {Array.<string>} args
* @param {function(Error)} done
*/
module.exports = function(args, done) {
var config = _parse(args);
var batchPath = path.join(config.output || process.cwd(), 'CrunchyRoll.txt');
_tasks(config, batchPath, function(err, tasks) {
if (err) return done(err);
var i = 0;
(function next() {
if (i >= tasks.length) return done();
series(tasks[i].config, tasks[i].address, function(err) {
if (err) return done(err);
i += 1;
next();
});
})();
});
};
/**
* Parses the configuration or reads the batch-mode file for tasks.
* @private
* @param {Object} config
* @param {string} batchPath
* @param {function(Error, Object=} done
*/
function _tasks(config, batchPath, done) {
if (config.args.length) {
return done(undefined, config.args.map(function(address) {
return {address: address, config: config};
}));
}
fs.exists(batchPath, function(exists) {
if (!exists) return done(undefined, []);
fs.readFile(batchPath, 'utf8', function(err, data) {
if (err) return done(err);
var map = [];
data.split(/\r?\n/).forEach(function(line) {
var lineConfig = _parse(process.argv.concat(line.split(' ')));
lineConfig.args.forEach(function(address) {
if (!address) return;
map.push({address: address, config: lineConfig});
});
});
done(undefined, map);
});
});
}
/**
* Parses the arguments and returns a configuration.
* @private
* @param {Array.<string>} args
* @returns {Object}
*/
function _parse(args) {
return new Command().version(require('../package').version)
// Disables
.option('-c, --cache', 'Disables the cache.')
.option('-m, --merge', 'Disables merging subtitles and videos.')
// Settings
.option('-f, --format <s>', 'The subtitle format. (Default: ass)')
.option('-o, --output <s>', 'The output path.')
.option('-s, --series <s>', 'The series override.')
.option('-t, --tag <s>', 'The subgroup. (Default: CrunchyRoll)')
.option('-v, --volume <i>', 'The volume.')
.parse(args);
}

View File

@@ -24,21 +24,6 @@ module.exports = function (config, address, done) {
});
};
/**
* Affixes zero-padding to the value.
* @private
* @param {(number|string)} value
* @param {number} length
* @returns {string}
*/
function _affix(value, length) {
if (typeof value !== 'string') value = String(value);
var suffix = value.indexOf('.') !== -1;
var add = length - (suffix ? value.indexOf('.') : value.length);
while ((add -= 1) >= 0) value = '0' + value;
return value;
}
/**
* Completes a download and writes the message with an elapsed time.
* @param {string} message
@@ -47,9 +32,9 @@ function _affix(value, length) {
*/
function _complete(message, begin, done) {
var timeInMs = Date.now() - begin;
var seconds = _affix(Math.floor(timeInMs / 1000) % 60, 2);
var minutes = _affix(Math.floor(timeInMs / 1000 / 60) % 60, 2);
var hours = _affix(Math.floor(timeInMs / 1000 / 60 / 60), 2);
var seconds = _prefix(Math.floor(timeInMs / 1000) % 60, 2);
var minutes = _prefix(Math.floor(timeInMs / 1000 / 60) % 60, 2);
var hours = _prefix(Math.floor(timeInMs / 1000 / 60 / 60), 2);
console.log(message + ' (' + hours + ':' + minutes + ':' + seconds + ')');
done();
}
@@ -86,17 +71,17 @@ function _download(config, page, player, done) {
}
/**
* Names the file based on the config, page, series and tag.
* @param {Object} config
* @param {Object} page
* @param {string} series
* @param {string} tag
* @returns {string}
*/
* Names the file based on the config, page, series and tag.
* @param {Object} config
* @param {Object} page
* @param {string} series
* @param {string} tag
* @returns {string}
*/
function _name(config, page, series, tag) {
var v = config.volume ? (config.volume < 10 ? '0' : '') + config.volume : '';
var e = (page.episode < 10 ? '0' : '') + page.episode;
return series + ' - ' + (v ? v + 'x' : '') + e + ' [' + tag + ']';
return series + ' ' + (v ? v + 'x' : '') + e + ' [' + tag + ']';
}
/**
@@ -123,6 +108,19 @@ function _page(address, done) {
});
}
/**
* Prefixes a value.
* @private
* @param {(number|string)} value
* @param {number} length
* @returns {string}
*/
function _prefix(value, length) {
if (typeof value !== 'string') value = String(value);
while (value.length < length) value = '0' + value;
return value;
}
/**
* Requests the player data and scrapes the subtitle and video data.
* @private

5
src/index.js Normal file
View File

@@ -0,0 +1,5 @@
module.exports = {
batch: require('./batch'),
episode: require('./episode'),
series: require('./series')
};