This commit is contained in:
Evert Prants 2018-10-07 19:03:04 +03:00
parent 97ab22a5f4
commit 52e4810a1f
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 21 additions and 16 deletions

View File

@ -1,4 +1,5 @@
import {exec} from 'child_process'
import path from 'path'
import fs from 'fs-extra'
function filewalker (dir, done) {
@ -34,6 +35,19 @@ function filewalker (dir, done) {
})
}
async function insertDB (db, track) {
let ensure = await db.get('SELECT * FROM Track WHERE title=? AND artist=?', track.title, track.artist)
if (ensure) {
return null
}
await db.run('INSERT INTO Track VALUES (NULL,?,?,?,?,?,?,?,?)',
[track.title, track.artist, track.file, track.album || null, track.genre || null, track.track || null,
track.year || null, Math.floor(track.duration)])
return track
}
function getFiles (dir) {
return new Promise((resolve, reject) => {
filewalker(dir, (err, files) => {
@ -58,6 +72,4 @@ function askAsync (rl, q) {
})
}
let a = {getFiles, promiseExec, askAsync}
export default a
export default {getFiles, promiseExec, askAsync, insertDB}

View File

@ -70,21 +70,17 @@ async function run () {
return
}
let ensure = await db.get('SELECT * FROM Track WHERE title=? AND artist=?', trackinf.title, trackinf.artist)
if (ensure) {
let ins = await asn.insertDB(db, trackinf)
if (!ins) {
console.error('A track of this description already exists in the database.')
return
}
await db.run('INSERT INTO Track VALUES (NULL,?,?,?,?,?,?,?,?)',
[trackinf.title, trackinf.artist, trackinf.file, trackinf.album || null, trackinf.genre || null, trackinf.track || null,
trackinf.year || null, Math.floor(trackinf.duration)])
console.log('=> Done.')
return
}
let files = await getFiles(musicdir)
let files = await asn.getFiles(musicdir)
let cleanTrackData = []
let skips = 0
@ -107,11 +103,8 @@ async function run () {
let track = cleanTrackData[i]
process.stdout.write(`\rPopulating database.. (Track ${parseInt(i) + 1} of ${cleanTrackData.length})`)
try {
let ensure = await db.get('SELECT * FROM Track WHERE title=? AND artist=?', track.title, track.artist)
if (ensure) continue
await db.run('INSERT INTO Track VALUES (NULL,?,?,?,?,?,?,?,?)',
[track.title, track.artist, track.file, track.album || null, track.genre || null, track.track || null,
track.year || null, Math.floor(track.duration)])
let ins = await asn.insertDB(db, track)
if (!ins) continue
entries++
} catch (e) {
console.warn(e.message)
@ -119,7 +112,7 @@ async function run () {
}
}
console.log(`=> \n${entries} tracks were successfully added to the cache!`)
console.log(`\n=> ${entries} tracks were successfully added to the cache!`)
}
run()