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 --- lib/videos.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'lib/videos.js') 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) { -- cgit v1.2.3