diff options
author | przemyslawpluta <przemekpluta@hotmail.com> | 2014-07-31 22:59:08 +0100 |
---|---|---|
committer | przemyslawpluta <przemekpluta@hotmail.com> | 2014-07-31 22:59:08 +0100 |
commit | 28a807a7c1da7d6bf38dd8d4c8aa5c568767b123 (patch) | |
tree | 4204819850c49e9235fcbf588f27ba0a0829f895 /lib | |
parent | c3af0ab36fd0aa379f106a9b27f7f1b0e3231672 (diff) | |
download | mongo-edu-28a807a7c1da7d6bf38dd8d4c8aa5c568767b123.zip |
update
upgrade to youtube-dl 1.5.8
cleanup
Diffstat (limited to 'lib')
-rw-r--r-- | lib/videos.js | 71 |
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); + }); + } }; |