From 8107e3f49ef959061d7fe0e04ef2f6eda01df354 Mon Sep 17 00:00:00 2001 From: liam4 Date: Wed, 21 Jun 2017 19:04:48 +0000 Subject: Ugh!! --- src/download-playlist.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/download-playlist.js (limited to 'src/download-playlist.js') 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 [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)) -- cgit 1.3.0-6-gf8a5