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 {exec} from 'child_process'
import path from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
function filewalker (dir, done) { 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) { function getFiles (dir) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
filewalker(dir, (err, files) => { filewalker(dir, (err, files) => {
@ -58,6 +72,4 @@ function askAsync (rl, q) {
}) })
} }
let a = {getFiles, promiseExec, askAsync} export default {getFiles, promiseExec, askAsync, insertDB}
export default a

View File

@ -70,21 +70,17 @@ async function run () {
return return
} }
let ensure = await db.get('SELECT * FROM Track WHERE title=? AND artist=?', trackinf.title, trackinf.artist) let ins = await asn.insertDB(db, trackinf)
if (ensure) { if (!ins) {
console.error('A track of this description already exists in the database.') console.error('A track of this description already exists in the database.')
return 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.') console.log('=> Done.')
return return
} }
let files = await getFiles(musicdir) let files = await asn.getFiles(musicdir)
let cleanTrackData = [] let cleanTrackData = []
let skips = 0 let skips = 0
@ -107,11 +103,8 @@ async function run () {
let track = cleanTrackData[i] let track = cleanTrackData[i]
process.stdout.write(`\rPopulating database.. (Track ${parseInt(i) + 1} of ${cleanTrackData.length})`) process.stdout.write(`\rPopulating database.. (Track ${parseInt(i) + 1} of ${cleanTrackData.length})`)
try { try {
let ensure = await db.get('SELECT * FROM Track WHERE title=? AND artist=?', track.title, track.artist) let ins = await asn.insertDB(db, track)
if (ensure) continue if (!ins) 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)])
entries++ entries++
} catch (e) { } catch (e) {
console.warn(e.message) 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() run()