2018-10-05 10:36:57 +00:00
|
|
|
#!/usr/bin/env babel-node
|
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'
|
|
|
|
import sqlite from 'sqlite'
|
|
|
|
import Promise from 'bluebird'
|
2018-10-06 12:30:02 +00:00
|
|
|
import readline from 'readline'
|
|
|
|
|
|
|
|
import asn from './common/async'
|
|
|
|
import dl from './common/download'
|
2018-10-05 10:36:57 +00:00
|
|
|
|
|
|
|
const values = require(path.join(__dirname, 'values.json'))
|
|
|
|
const musicdir = path.resolve(values.directory)
|
|
|
|
|
|
|
|
const dbPromise = Promise.resolve()
|
|
|
|
.then(() => sqlite.open(path.join(__dirname, values.database), { Promise, cache: true }))
|
|
|
|
.then(db => db.migrate())
|
|
|
|
|
|
|
|
// ffprobe -i <file> -show_entries format=duration -v quiet -of csv="p=0"
|
|
|
|
|
|
|
|
async function interactive (fpath, db) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
async function run () {
|
|
|
|
let db = await dbPromise
|
|
|
|
|
|
|
|
if (process.argv[2] != null) {
|
|
|
|
let filePath = path.resolve(process.argv[2])
|
|
|
|
let trackinf
|
|
|
|
|
|
|
|
try {
|
2018-10-06 12:30:02 +00:00
|
|
|
trackinf = await dl.getInfos(filePath)
|
2018-10-05 10:36:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
trackinf = await interactive(filePath, db)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!trackinf) {
|
|
|
|
console.error('Nothing to do.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-07 16:03:04 +00:00
|
|
|
let ins = await asn.insertDB(db, trackinf)
|
|
|
|
if (!ins) {
|
2018-10-06 12:30:02 +00:00
|
|
|
console.error('A track of this description already exists in the database.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-10-06 12:30:02 +00:00
|
|
|
let fd = await dl.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()
|