« get me outta code hell

Skip ALL already-downloaded tracks before downloading any in download-playlist - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/download-playlist.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-08-05 00:29:17 -0300
committerFlorrie <towerofnix@gmail.com>2017-08-05 00:29:17 -0300
commit9d89feb5ab50091214daf05b92100ff83067fd2d (patch)
tree10002b0d6eadc042e3d8cec8d592991ebff5bedb /src/download-playlist.js
parent8173c0727882be456555b05424bc7a0bb31eb1cb (diff)
Skip ALL already-downloaded tracks before downloading any in download-playlist
Diffstat (limited to 'src/download-playlist.js')
-rwxr-xr-xsrc/download-playlist.js34
1 files changed, 26 insertions, 8 deletions
diff --git a/src/download-playlist.js b/src/download-playlist.js
index dcaa7b7..b41c240 100755
--- a/src/download-playlist.js
+++ b/src/download-playlist.js
@@ -24,7 +24,7 @@ async function downloadCrawl(playlist, topOut = './out/') {
   const flat = flattenGrouplike(playlist)
   let doneCount = 0
 
-  const status = function() {
+  const showStatus = function() {
     const total = flat.items.length
     const percent = Math.trunc(doneCount / total * 10000) / 100
     console.log(
@@ -32,6 +32,13 @@ async function downloadCrawl(playlist, topOut = './out/') {
       `(${doneCount}/${total} tracks)\x1b[0m`)
   }
 
+  // First off, we go through all tracks and see which are already downloaded.
+  // We store the ones that *aren't* downloaded in an 'itemsToDownload' array,
+  // which we use later.
+  const itemsToDownload = []
+
+  const targetFileSymbol = Symbol('Target file')
+
   for (let item of flat.items) {
     const parentGroups = getItemPath(item).slice(0, -1)
 
@@ -39,11 +46,15 @@ async function downloadCrawl(playlist, topOut = './out/') {
       return a + '/' + sanitize(b.name)
     }, topOut) + '/'
 
-    await mkdirp(dir)
-
     const base = path.basename(item.name, path.extname(item.name))
     const targetFile = dir + sanitize(base) + '.mp3'
 
+    // We'll be using the target file later when we download all tracks, so
+    // we save that right on the playlist item.
+    item[targetFileSymbol] = targetFile
+
+    await mkdirp(dir)
+
     // If we've already downloaded a file at some point in previous time,
     // there's no need to download it again!
     //
@@ -61,9 +72,16 @@ async function downloadCrawl(playlist, topOut = './out/') {
     if (match) {
       console.log(`\x1b[32;2mAlready downloaded: ${targetFile}\x1b[0m`)
       doneCount++
-      status()
-      continue
+      showStatus()
+    } else {
+      itemsToDownload.push(item)
     }
+  }
+
+  // Now that we've decided on which items we need to download, we go through
+  // and download all of them.
+  for (let item of itemsToDownload) {
+    const targetFile = item[targetFileSymbol]
 
     console.log(
       `\x1b[2mDownloading: ${item.name} - ${item.downloaderArg}` +
@@ -89,6 +107,8 @@ async function downloadCrawl(playlist, topOut = './out/') {
       }
 
       try {
+        console.log(targetFile)
+
         await promisifyProcess(spawn('ffmpeg', [
           '-i', outputtedFile,
 
@@ -106,13 +126,11 @@ async function downloadCrawl(playlist, topOut = './out/') {
 
         break downloadProcess
       }
-
-      console.log('Added:', item.name)
     }
 
     doneCount++
 
-    status()
+    showStatus()
   }
 }