btrtracks/download.js

64 lines
1.6 KiB
JavaScript
Executable File

#!/usr/bin/env babel-node
'use strict'
import fs from 'fs-extra'
import readline from 'readline'
import path from 'path'
import asn from './common/async'
import dl from './common/download'
const values = require(path.join(__dirname, 'values.json'))
const musicdir = path.resolve(values.directory)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
async function download (furl) {
console.log('=> Getting information..')
let data = await dl.getVideoInfo(furl)
console.log('=> Downloading file..')
let file = await dl.fetchVideo(data)
console.log('=> Cleaning up..')
let clean = dl.parseTitle(file)
let filename = clean.artist + ' - ' + clean.title + '.mp3'
console.log('=> Original Title: ' + file.title + '\n')
console.log('== Determined Title: ' + clean.title)
console.log('== Determined Artist: ' + clean.artist)
console.log('== Determined File Name: ' + filename)
let titleAnsw = await asn.askAsync(rl, `Title [${clean.title}] ? `)
let artistAnsw = await asn.askAsync(rl, `Artist [${clean.artist}] ? `)
let fileAnsw = await asn.askAsync(rl, `File [${filename}] ? `)
if (titleAnsw && titleAnsw.trim() !== '') {
clean.title = titleAnsw
}
if (artistAnsw && artistAnsw.trim() !== '') {
clean.artist = artistAnsw
}
if (fileAnsw && fileAnsw.trim() !== '') {
filename = fileAnsw
}
let fn = path.join(musicdir, filename)
await fs.rename(file.source, fn)
let id3 = await asn.promiseExec(`id3 -a "${clean.artist}" -t "${clean.title}" "${fn}"`)
console.log('=> Done.')
rl.close()
}
download(process.argv[2]).catch((e) => {
console.error(e.message)
rl.close()
})