diff options
Diffstat (limited to 'src/downloaders.js')
-rw-r--r-- | src/downloaders.js | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/src/downloaders.js b/src/downloaders.js index fa1f337..8fa830c 100644 --- a/src/downloaders.js +++ b/src/downloaders.js @@ -1,3 +1,5 @@ +'use strict' + const fs = require('fs') const fetch = require('node-fetch') const promisifyProcess = require('./promisify-process') @@ -47,8 +49,38 @@ function makeLocalDownloader() { } } +function makePowerfulDownloader(downloader, maxAttempts = 5) { + // This should totally be named better.. + + return async function recursive(arg, attempts = 0) { + try { + return await downloader(arg) + } catch(err) { + if (attempts < maxAttempts) { + console.warn('Failed - attempting again:', arg) + return await recursive(arg, attempts + 1) + } else { + throw err + } + } + } +} + module.exports = { makeHTTPDownloader, makeYouTubeDownloader, - makeLocalDownloader + makeLocalDownloader, + makePowerfulDownloader, + + getDownloader: downloaderType => { + if (downloaderType === 'http') { + return makeHTTPDownloader() + } else if (downloaderType === 'youtube') { + return makeYouTubeDownloader() + } else if (downloaderType === 'local') { + return makeLocalDownloader() + } else { + return null + } + } } |