124 lines
3.0 KiB
JavaScript
124 lines
3.0 KiB
JavaScript
import path from 'path'
|
|
import readline from 'readline'
|
|
|
|
import asn from './common/async'
|
|
import dl from './common/download'
|
|
import { dbPromise } from './database'
|
|
|
|
const values = require(path.join(process.cwd(), 'values.json'))
|
|
const musicdir = path.resolve(values.directory)
|
|
|
|
// ffprobe -i <file> -show_entries format=duration -v quiet -of csv="p=0"
|
|
|
|
async function interactive (fpath) {
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
})
|
|
|
|
console.log('=> No metadata found for specified file! Interactive mode enabled.\n')
|
|
|
|
const pt = path.parse(fpath)
|
|
const track = {
|
|
file: fpath,
|
|
title: pt.name
|
|
}
|
|
const clean = dl.parseTitle(track)
|
|
|
|
console.log('== Determined Title: ' + clean.title)
|
|
console.log('== Determined Artist: ' + clean.artist)
|
|
|
|
const newTitle = await asn.askAsync(rl, `Title [${clean.title}] ? `)
|
|
const newArtist = await asn.askAsync(rl, `Artist [${clean.artist}] ? `)
|
|
|
|
if (newTitle.trim() !== '') {
|
|
track.title = newTitle
|
|
}
|
|
|
|
if (newArtist.trim() !== '') {
|
|
track.artist = newArtist
|
|
}
|
|
|
|
const len = await asn.promiseExec(`ffprobe -i "${track.file}" -show_entries format=duration -v quiet -of csv="p=0"`)
|
|
track.duration = parseFloat(len)
|
|
|
|
rl.close()
|
|
|
|
return track
|
|
}
|
|
|
|
async function handlePassed (db, fpath) {
|
|
const filePath = path.resolve(fpath)
|
|
let trackinf
|
|
|
|
try {
|
|
trackinf = await asn.getInfos(filePath)
|
|
} catch (e) {
|
|
trackinf = await interactive(filePath, db)
|
|
}
|
|
|
|
if (!trackinf) {
|
|
throw new Error('Nothing to do.')
|
|
}
|
|
|
|
const ins = await asn.insertDB(db, trackinf)
|
|
if (!ins) {
|
|
throw new Error('A track of this description already exists in the database.')
|
|
}
|
|
}
|
|
|
|
async function run () {
|
|
const db = await dbPromise
|
|
|
|
if (process.argv[2] != null) {
|
|
for (let i = 2; i < process.argv.length; i++) {
|
|
const f = process.argv[i]
|
|
console.log('=> Passing', f)
|
|
try {
|
|
await handlePassed(db, f)
|
|
} catch (e) {
|
|
console.error('==>', e.message)
|
|
}
|
|
}
|
|
|
|
console.log('=> Done.')
|
|
return
|
|
}
|
|
|
|
const files = await asn.getFiles(musicdir)
|
|
const cleanTrackData = []
|
|
let skips = 0
|
|
|
|
for (const i in files) {
|
|
const file = files[i]
|
|
// if (cleanTrackData.length > 10) break // debug purposes
|
|
process.stdout.write(`\rProcessing file ${parseInt(i) + 1} of ${files.length}.. (${skips} skipped)`)
|
|
try {
|
|
const fd = await asn.getInfos(file)
|
|
cleanTrackData.push(fd)
|
|
} catch (e) {
|
|
skips++
|
|
continue
|
|
}
|
|
}
|
|
process.stdout.write(`\r${cleanTrackData.length} files indexed, ${skips} files were skipped. \n`)
|
|
let entries = 0
|
|
|
|
for (const i in cleanTrackData) {
|
|
const track = cleanTrackData[i]
|
|
process.stdout.write(`\rPopulating database.. (Track ${parseInt(i) + 1} of ${cleanTrackData.length})`)
|
|
try {
|
|
const ins = await asn.insertDB(db, track)
|
|
if (!ins) continue
|
|
entries++
|
|
} catch (e) {
|
|
console.warn(e.message)
|
|
continue
|
|
}
|
|
}
|
|
|
|
console.log(`\n=> ${entries} tracks were successfully added to the cache!`)
|
|
}
|
|
|
|
run()
|