btrtracks/src/dbpopulate.js

123 lines
3.0 KiB
JavaScript
Raw Normal View History

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