« get me outta code hell

Restore downloaders.js? - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLiam <towerofnix@gmail.com>2017-07-14 15:07:05 +0000
committerLiam <towerofnix@gmail.com>2017-07-14 15:07:05 +0000
commitcd8155c336321017d13a6fa5776db650cb040d90 (patch)
tree9d985a35f0a040c0d67fa0d6cbd5b6108a7ee416
parentd25c15797741e92e35ad3f43b8b827811e6d4ca7 (diff)
Restore downloaders.js?
-rw-r--r--src/downloaders.js86
1 files changed, 86 insertions, 0 deletions
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
+    }
+  }
+}