summaryrefslogtreecommitdiff
path: root/lib/videos.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/videos.js')
-rw-r--r--lib/videos.js71
1 files changed, 32 insertions, 39 deletions
diff --git a/lib/videos.js b/lib/videos.js
index b15d16e..56ebbaf 100644
--- a/lib/videos.js
+++ b/lib/videos.js
@@ -139,37 +139,40 @@ var handleList = function handleList(list, tags) {
},
- getVideos = function getVideos(item) {
+ getVideos = function getVideos(item, nocc) {
if (handout) { return getHandouts(item); }
- var dl = youtubedl.download(item, downloadPath, opt), bar;
+ var dl = youtubedl(item, nocc || opt, { cwd: downloadPath }), size = 0, pos = 0, stash = {}, bar;
- dl.on('download', function download(data) {
- if (co) { downloadList.push({id: item, name: path.basename(data.filename)}); }
- console.log('[' + 'i'.magenta + '] Downloading: ' + data.filename.cyan + ' > ' + item);
- bar = new ProgressBar('[' + '>'.green + '] ' + data.size + ' [:bar] :percent :etas', { complete: '=', incomplete: ' ', width: 20, total: 100.0 });
+ dl.on('info', function(info) {
+ size = info.size;
+ stash = info;
+ if (co) { downloadList.push({id: item, name: path.basename(info.filename)}); }
+ console.log('[' + 'i'.magenta + '] Downloading: ' + info.filename.cyan + ' > ' + item);
+ bar = new ProgressBar('[' + '>'.green + '] ' + filesize(size) + ' [:bar] :percent :etas', { complete: '=', incomplete: ' ', width: 20, total: 100.0 });
+ console.time('[' + 'i'.magenta + '] Done in');
+ dl.pipe(fs.createWriteStream(downloadPath + info.filename));
});
- dl.on('progress', function progress(data) {
- if (!bar.complete) { bar.tick(parseInt(data.percent, 10)); }
+ dl.on('data', function(data) {
+ pos += data.length;
+ var percent = (pos / size * 100).toFixed(2);
+ if (!bar.complete) { bar.tick(parseInt(percent, 10)); }
});
dl.on('error', function error(err) {
- return console.log(err.stack);
+ if (err.message.indexOf('video doesn\'t have subtitles') !== -1) {
+ console.log('[' + 'i'.magenta + '] No Closed Captions Available For: ' + stash.filename.cyan + ' > ' + item);
+ return getVideos(item, _.without(opt, '--write-sub', '--srt-lang=en'));
+ }
+ console.log(err.stack);
});
- dl.on('end', function end(data) {
+ dl.on('end', function end() {
var left = (currentList.length)? ' ' + currentList.length + ' left ...' : '';
- if (data.filename) {
- if (data.filename.indexOf('has already been downloaded') !== -1) {
- console.log('[' + '>'.magenta + '] ' + data.filename + '.' + left);
- } else {
- console.log('[' + 'i'.green + '] Done in ' + data.timeTakenms + 'ms.' + left);
- }
- } else {
- console.log('[' + 'i'.red + '] Download Issues');
- }
+ console.log('[' + '>'.magenta + '] ' + stash.filename + '.' + left);
+ console.timeEnd('[' + 'i'.magenta + '] Done in');
handleList(currentList, tags);
});
};
@@ -284,34 +287,24 @@ module.exports = {
'use strict';
- var args = [opt.url, '--get-id', '--get-title'];
- args = (!argv.ncc) ? args : args.concat(['--no-check-certificate']);
-
- var file = path.join(__dirname, '..', 'node_modules/youtube-dl/bin', 'youtube-dl'),
- options = [file, args];
-
setOptions(argv);
- if (isWin) { options = [process.env.PYTHON, [file].concat(args)]; }
+ var options = (!ncc) ? [] : ['--no-check-certificate'], items = [], i, item;
- execFile(options[0], options[1], function(err, stdout, stderr) {
- if (err !== null) { return callback(err); }
- if (stderr) { return callback(new Error(stderr.slice(7))); }
+ youtubedl.getInfo(opt.url, options, function(err, info) {
- var data = stdout.trim().split(/\r?\n/);
- var out = [], total = data.length, count = 0, i;
-
- if (data.length) {
-
- for (i = 0; i < total / 2; i++) {
- count = count + 1;
- out.push({ name: data.shift(), value: 'https://www.youtube.com/watch?v=' + data.shift(), id: count});
+ if (info.length) {
+ for (i = 0; i < info.length; i++) {
+ item = 'https://www.youtube.com/watch?v=' + info[i].id;
+ items.push((!err)?{name: info[i].title + ' - ' + info[i].resolution, value: item, id: i}:{name: 'No info: ' + item, value: item, id: i});
}
- out.unshift({name: 'All', value: 'all', checked: true}, {name: 'Cancel', value: 'cancel'});
+ items.unshift({name: 'All', value: 'all', checked: true}, {name: 'Cancel', value: 'cancel'});
}
- callback(null, out, true);
+ callback(null, items, true);
+
});
+
}
};