From 6f85d4d542f7d6d9f6e807faa89ca26cc2124a5d Mon Sep 17 00:00:00 2001 From: przemyslawpluta Date: Fri, 8 Jan 2016 20:47:47 +0000 Subject: retry option if connection / download failed --- README.md | 1 + lib/initialize.js | 2 +- lib/videos.js | 26 ++++++++++++++++++++------ package.json | 1 + 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c803a7d..5a051b1 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Options: --py pass switch to point to Python --ncc no check certificate for py3.x < py3.4.x --verbose print debug info + --retry retry time in seconds if connection / download fails Videos: --cw switch from wiki video lists (default) to courseware diff --git a/lib/initialize.js b/lib/initialize.js index 195423a..9552c41 100644 --- a/lib/initialize.js +++ b/lib/initialize.js @@ -38,7 +38,7 @@ module.exports = function run(profile, argv) { mdbvideos.init(answers, argv, function get(err, data, profile) { - if (err !== null) { throw err; } + if (err !== null) { return console.log('i'.red + ' ' + err.stack + '.\n'); } if (profile.preset.video[1] === videoPreference || argv.h) { return processList(data, profile); } diff --git a/lib/videos.js b/lib/videos.js index 1dde7e1..7c2e56d 100644 --- a/lib/videos.js +++ b/lib/videos.js @@ -17,10 +17,11 @@ var path = require('path'), extract = require('extract-zip'), rimraf = require('rimraf'), mv = require('mv'), - _ = require('lodash'); + _ = require('lodash'), + moment = require('moment'); var isDebug = /[debug]/, downloadPath = '', proxy = '', downloadList = [], hash = {}, - co = false, ncc = false, handout = false, cc = false, uz = false, hq = false, verbose = false; + co = false, ncc = false, handout = false, cc = false, uz = false, hq = false, verbose = false, retry = 0, maxRetry; function setOptions(argv) { @@ -34,6 +35,7 @@ function setOptions(argv) { if (argv.uz) { uz = true; } if (argv.hq) { hq = true; } if (argv.co) { co = true; } + if (argv.retry) { retry = argv.retry; } if (argv.verbose) { verbose = true; } } @@ -160,7 +162,7 @@ var handleList = function handleList(list, tags) { }); }, - getVideos = function getVideos(item, nocc, notAvailable) { + getVideos = function getVideos(item, notAvailable) { if (handout) { return getHandouts(item); } @@ -170,7 +172,7 @@ var handleList = function handleList(list, tags) { downloaded = fs.statSync(downloadPath + hash[item]).size; } - var dl = youtubedl(item, nocc || opt, {start: downloaded, cwd: downloadPath}); + var dl = youtubedl(item, opt, {start: downloaded, cwd: downloadPath}); dl.on('info', function(info) { size = info.size + downloaded; @@ -198,9 +200,16 @@ var handleList = function handleList(list, tags) { if (err.message.indexOf('requested format not available') !== -1) { _.pull(opt, '--format=22'); opt.push('--format=18'); - return getVideos(item, nocc, true); + return getVideos(item, true); } - console.log(err.stack); + + if (err.message.match(/Command failed: python|ENETDOWN|ENOTFOUND|EAI_AGAIN/) && retry !== 0) { + if (!maxRetry) { maxRetry = moment().add(retry, 'seconds'); } + if (maxRetry && maxRetry.diff(moment()) > 0) { return delay(item); } + } + + console.log('i'.red + ' Download Failed: '.red + err.stack); + }); dl.on('close', function close() { @@ -208,10 +217,15 @@ var handleList = function handleList(list, tags) { }); dl.on('end', function end() { + maxRetry = null; var left = (currentList.length) ? currentList.length + ' left ...' : ''; console.timeEnd('i'.magenta + ' ' + stash._filename + '. Done in'); getSubtitles(item, left, currentList, tags); }); + }, + + delay = function delay(target) { + setTimeout(function timeout() { getVideos(target); }, 5000); }; if (currentList.length) { diff --git a/package.json b/package.json index 4d47d62..9788430 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "inquirer": "~0.11.1", "lodash": "~3.10.1", "mkdirp": "~0.5.1", + "moment": "^2.11.0", "mv": "~2.1.1", "path-extra": "~3.0.0", "pretty-error": "~2.0.0", -- cgit v1.2.3