Add an episode filter.

This commit is contained in:
Roel van Uden 2015-01-24 12:49:09 +01:00
parent 66f62d3aa1
commit 4b0c993969
3 changed files with 13 additions and 7 deletions

View File

@ -3,7 +3,7 @@
*CrunchyRoll.js* is capable of saving *anime* episodes from the popular *CrunchyRoll*
streaming service. An episode is stored in the original video format (often
H.264 in a MP4 container) and the configured subtitle format (ASS or SRT). The
two output files are optionally muxed into a single MKV file.
two output files are then merged into a single MKV file.
## Motivation

View File

@ -67,6 +67,8 @@ function _parse(args) {
// Disables
.option('-c, --cache', 'Disables the cache.')
.option('-m, --merge', 'Disables merging subtitles and videos.')
// Filters
.option('-e, --episode <i>', 'The episode filter.')
// Settings
.option('-f, --format <s>', 'The subtitle format. (Default: ass)')
.option('-o, --output <s>', 'The output path.')

View File

@ -22,9 +22,7 @@ module.exports = function (config, address, done) {
var i = 0;
(function next() {
if (i >= page.episodes.length) return done();
var episode = page.episodes[i];
var episodeAddress = url.resolve(address, episode.address);
_download(cache, config, episodeAddress, function(err) {
_download(cache, config, address, page.episodes[i], function(err) {
if (err) return done(err);
var newCache = JSON.stringify(cache, null, ' ');
fs.writeFile(persistentPath, newCache, function(err) {
@ -42,10 +40,16 @@ module.exports = function (config, address, done) {
* Downloads the episode.
* @param {Object.<string, string>} cache
* @param {Object} config
* @param {string} address
* @param {string} baseAddress
* @param {Object} data
* @param {function(Error)} done
*/
function _download(cache, config, address, done) {
function _download(cache, config, baseAddress, data, done) {
if (typeof config.episode === 'number') {
if (config.episode > 0 && data.number < config.episode) return done();
if (config.episode < 0 && -config.episode < data.number) return done();
}
var address = url.resolve(baseAddress, data.address);
if (cache[address]) return done();
episode(config, address, function(err) {
if (err) return done(err);
@ -73,7 +77,7 @@ function _page(address, done) {
var title = ($(el).children('.series-title').text() || '').trim();
var match = /([0-9]+)$/.exec(title);
if (!address || !match) return;
episodes.push({address: address, episode: parseInt(match[0], 10)});
episodes.push({address: address, number: parseInt(match[0], 10)});
});
done(undefined, {episodes: episodes.reverse(), series: title});
});