« get me outta code hell

Ugh!! - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorliam4 <towerofnix@gmail.com>2017-06-21 19:04:48 +0000
committerliam4 <towerofnix@gmail.com>2017-06-21 19:04:55 +0000
commit8107e3f49ef959061d7fe0e04ef2f6eda01df354 (patch)
treed62a0e5eedcf7243641309418d4de7f4810e9911 /src
parent0f6be3c9f221aeed36970fc488c812ee1dadd97f (diff)
Ugh!!
Diffstat (limited to 'src')
-rw-r--r--src/download-playlist.js95
-rw-r--r--src/downloaders.js22
-rw-r--r--src/playlist-utils.js8
3 files changed, 123 insertions, 2 deletions
diff --git a/src/download-playlist.js b/src/download-playlist.js
new file mode 100644
index 0000000..eb6375a
--- /dev/null
+++ b/src/download-playlist.js
@@ -0,0 +1,95 @@
+'use strict'
+
+const fs = require('fs')
+const downloaders = require('./downloaders')
+const path = require('path')
+const sanitize = require('sanitize-filename')
+
+const {
+  isGroup, isTrack
+} = require('./playlist-utils')
+
+const { promisify } = require('util')
+
+const access = promisify(fs.access)
+const mkdir = promisify(fs.mkdir)
+const readFile = promisify(fs.readFile)
+const readdir = promisify(fs.readdir)
+const rename = promisify(fs.rename)
+const stat = promisify(fs.stat)
+const writeFile = promisify(fs.writeFile)
+
+async function downloadCrawl(playlist, downloader, outPath = './out/') {
+  let doesExist = true
+  try {
+    doesExist = (await stat(outPath)).isDirectory()
+  } catch(err) {
+    doesExist = false
+  }
+
+  if (!doesExist) {
+    await mkdir(outPath)
+  }
+
+  return Promise.all(playlist.map(async (item) => {
+    if (isGroup(item)) {
+      // TODO: Not sure if this is the best way to pick the next out dir.
+      const out = outPath + sanitize(item[0]) + '/'
+
+      return [item[0], await downloadCrawl(item[1], downloader, out)]
+    } else if (isTrack(item)) {
+      console.log(`\x1b[2m${item[0]} - ${item[1]}\x1b[0m`)
+
+      // TODO: How to deal with songs that don't have an extension?
+      const ext = path.extname(item[1])
+      const base = path.basename(item[1], ext)
+
+      const items = await readdir(outPath)
+      const match = items.find(x => path.basename(x, path.extname(x)) === base)
+      if (match) {
+        return [item[0], outPath + match]
+      }
+
+      const downloadFile = await downloader(item[1])
+      // const base = path.basename(downloadFile)
+      // const out = outPath + base
+
+      // console.log(`\x1b[1m${downloadFile}\x1b[0m`)
+
+      try {
+        await rename(downloadFile, path.resolve(out))
+        console.log(`\x1b[1m${out}\x1b[0m`)
+        return [item[0], out]
+      } catch(err) {
+        console.error(`\x1b[31mFAILED: ${out}\x1b[0m`)
+        console.error(err)
+        return false
+      }
+    }
+  })).then(p => p.filter(Boolean))
+}
+
+async function main() {
+  // TODO: Implement command line stuff here
+
+  if (process.argv.length === 2) {
+    console.error('Usage: download-playlist <playlistFile> [opts]')
+    process.exit(1)
+    return
+  }
+
+  const playlist = JSON.parse(await readFile(process.argv[2]))
+
+  const dl = downloaders.makePowerfulDownloader(
+    downloaders.makeHTTPDownloader()
+  )
+
+  const outPlaylist = await downloadCrawl(playlist, dl)
+
+  writeFile('out/playlist.json', JSON.stringify(outPlaylist, null, 2))
+
+  console.log('Done - saved playlist to out/playlist.json.')
+}
+
+main()
+  .catch(err => console.error(err))
diff --git a/src/downloaders.js b/src/downloaders.js
index fa1f337..2b193eb 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,26 @@ 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
 }
diff --git a/src/playlist-utils.js b/src/playlist-utils.js
index ff19ea9..13c6003 100644
--- a/src/playlist-utils.js
+++ b/src/playlist-utils.js
@@ -130,6 +130,7 @@ function parsePathString(pathString) {
   return pathParts
 }
 
+// TODO: Are these two functions actually useful??
 function getGroupTitle(group) {
   return group[0]
 }
@@ -142,11 +143,16 @@ function isGroup(array) {
   return Array.isArray(array[1])
 }
 
+function isTrack(array) {
+  return typeof array[1] === 'string'
+}
+
 module.exports = {
   flattenPlaylist,
   filterPlaylistByPathString, filterPlaylistByPath,
   removeGroupByPathString, removeGroupByPath,
   getPlaylistTreeString,
   parsePathString,
-  getGroupTitle, getGroupContents
+  getGroupTitle, getGroupContents,
+  isGroup, isTrack
 }