diff options
author | liam4 <towerofnix@gmail.com> | 2017-06-21 21:53:43 +0000 |
---|---|---|
committer | liam4 <towerofnix@gmail.com> | 2017-06-21 21:53:43 +0000 |
commit | 4074b66746ac87aa1a655dfaa914e5e02b8f8221 (patch) | |
tree | 7bb50dd4362feb9ce7ed8b3c131486114e1ffff5 /src/downloaders.js | |
parent | af4749b962a7c5c488f52ae2b6f2ff0fe3d920a0 (diff) | |
parent | 5cbda3bc6c678a9b212fc1b47d94a3b234bbf818 (diff) |
Merge branch 'workspace'
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 + } + } } |