« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/download-playlist.js34
-rw-r--r--todo.txt1
2 files changed, 27 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()
   }
 }
 
diff --git a/todo.txt b/todo.txt
index d3172ff..266e67c 100644
--- a/todo.txt
+++ b/todo.txt
@@ -198,6 +198,7 @@ TODO: In the playlist downloader, it would be nice if we skipped past existing
       which makes up 10%, and then the rest would still be downloaded, which
       take up 70%. It would be better if we went from 0%, skipped ALL complete
       tracks to get to 90%, then did the 10% for the downloaded tracks.
+      (Done!)
 
 TODO: Tracks should be able to contain more data than the title and downloader
       argument, by being stored as objects instead of arrays. This would also