btrtracks/src/dbpopulate.js

124 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2018-10-05 10:36:57 +00:00
import path from 'path'
2018-10-06 12:30:02 +00:00
import readline from 'readline'
2020-10-04 08:01:16 +00:00
import * as asn from './common/async'
import * as 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) {
const rl = readline.createInterface({
2018-10-05 10:36:57 +00:00
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
const pt = path.parse(fpath)
const track = {
2018-10-05 10:36:57 +00:00
file: fpath,
title: pt.name
}
const 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
const newTitle = await asn.askAsync(rl, `Title [${clean.title}] ? `)
const newArtist = await asn.askAsync(rl, `Artist [${clean.artist}] ? `)
2018-10-05 10:36:57 +00:00
if (newTitle.trim() !== '') {
2018-10-05 10:36:57 +00:00
track.title = newTitle
}
2018-10-05 10:36:57 +00:00
if (newArtist.trim() !== '') {
2018-10-05 10:36:57 +00:00
track.artist = newArtist
}
2018-10-05 10:36:57 +00:00
const 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) {
const filePath = path.resolve(fpath)
2018-11-07 16:28:08 +00:00
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
const ins = await asn.insertDB(db, trackinf)
2018-11-07 16:28:08 +00:00
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 () {
const 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++) {
const f = process.argv[i]
2018-11-07 16:28:08 +00:00
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
}
const files = await asn.getFiles(musicdir)
const cleanTrackData = []
2018-10-05 10:36:57 +00:00
let skips = 0
for (const i in files) {
const file = files[i]
2018-10-05 10:36:57 +00:00
// 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)
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 (const i in cleanTrackData) {
const track = cleanTrackData[i]
2018-10-05 10:36:57 +00:00
process.stdout.write(`\rPopulating database.. (Track ${parseInt(i) + 1} of ${cleanTrackData.length})`)
try {
const ins = await asn.insertDB(db, track)
2018-10-07 16:03:04 +00:00
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()