btrtracks/src/download.js

64 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-10-05 10:36:57 +00:00
'use strict'
2018-10-06 12:30:02 +00:00
import fs from 'fs-extra'
import readline from 'readline'
import path from 'path'
2018-10-05 10:36:57 +00:00
2018-10-06 12:30:02 +00:00
import asn from './common/async'
import dl from './common/download'
2018-10-18 15:34:47 +00:00
const values = require(path.join(process.cwd(), 'values.json'))
2018-10-06 12:30:02 +00:00
const musicdir = path.resolve(values.directory)
2018-10-05 10:36:57 +00:00
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
2018-10-06 12:30:02 +00:00
async function download (furl) {
console.log('=> Getting information..')
let data = await dl.getVideoInfo(furl)
2018-10-05 10:36:57 +00:00
2018-10-06 12:30:02 +00:00
console.log('=> Downloading file..')
let file = await dl.fetchVideo(data)
2018-10-05 10:36:57 +00:00
2018-10-06 12:30:02 +00:00
console.log('=> Cleaning up..')
let clean = dl.parseTitle(file)
let filename = clean.artist + ' - ' + clean.title + '.mp3'
2018-10-05 10:36:57 +00:00
2018-10-06 12:30:02 +00:00
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)
2018-10-05 10:36:57 +00:00
2018-10-06 12:30:02 +00:00
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}] ? `)
2018-10-05 10:36:57 +00:00
2018-10-06 12:30:02 +00:00
if (titleAnsw && titleAnsw.trim() !== '') {
clean.title = titleAnsw
}
2018-10-05 10:36:57 +00:00
2018-10-06 12:30:02 +00:00
if (artistAnsw && artistAnsw.trim() !== '') {
clean.artist = artistAnsw
}
2018-10-05 10:36:57 +00:00
2018-10-06 12:30:02 +00:00
if (fileAnsw && fileAnsw.trim() !== '') {
filename = fileAnsw
2018-10-05 10:36:57 +00:00
}
2018-10-06 12:30:02 +00:00
let fn = path.join(musicdir, filename)
2018-10-18 15:15:33 +00:00
await asn.copyAsync(file.source, fn)
await fs.unlink(file.source)
2018-10-05 10:36:57 +00:00
2018-10-06 12:30:02 +00:00
let id3 = await asn.promiseExec(`id3 -a "${clean.artist}" -t "${clean.title}" "${fn}"`)
2018-10-05 10:36:57 +00:00
2018-10-06 12:30:02 +00:00
console.log('=> Done.')
rl.close()
}
2018-10-05 10:36:57 +00:00
2018-10-06 12:30:02 +00:00
download(process.argv[2]).catch((e) => {
console.error(e.message)
rl.close()
2018-10-05 10:36:57 +00:00
})