From cd8155c336321017d13a6fa5776db650cb040d90 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 14 Jul 2017 15:07:05 +0000 Subject: Restore downloaders.js? --- src/downloaders.js | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/downloaders.js (limited to 'src') diff --git a/src/downloaders.js b/src/downloaders.js new file mode 100644 index 0000000..8fa830c --- /dev/null +++ b/src/downloaders.js @@ -0,0 +1,86 @@ +'use strict' + +const fs = require('fs') +const fetch = require('node-fetch') +const promisifyProcess = require('./promisify-process') +const tempy = require('tempy') +const path = require('path') +const sanitize = require('sanitize-filename') + +const { spawn } = require('child_process') +const { promisify } = require('util') + +const writeFile = promisify(fs.writeFile) + +function makeHTTPDownloader() { + return function(arg) { + const dir = tempy.directory() + const out = dir + '/' + sanitize(decodeURIComponent(path.basename(arg))) + + return fetch(arg) + .then(response => response.buffer()) + .then(buffer => writeFile(out, buffer)) + .then(() => out) + } +} + +function makeYouTubeDownloader() { + return function(arg) { + const tempDir = tempy.directory() + + const opts = [ + '--quiet', + '--extract-audio', + '--audio-format', 'wav', + '--output', tempDir + '/dl.%(ext)s', + arg + ] + + return promisifyProcess(spawn('youtube-dl', opts)) + .then(() => tempDir + '/dl.wav') + } +} + +function makeLocalDownloader() { + return function(arg) { + // Since we're grabbing the file from the local file system, there's no + // need to download or copy it! + return arg + } +} + +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, + makePowerfulDownloader, + + getDownloader: downloaderType => { + if (downloaderType === 'http') { + return makeHTTPDownloader() + } else if (downloaderType === 'youtube') { + return makeYouTubeDownloader() + } else if (downloaderType === 'local') { + return makeLocalDownloader() + } else { + return null + } + } +} -- cgit 1.3.0-6-gf8a5