Commit c13911a1 authored by Guillaume Lebigot's avatar Guillaume Lebigot

Ajout option --generate pour uniquement générer la BDD, retourne 1 ou 0 en RC

parent a9ac7aa3
......@@ -16,6 +16,8 @@ import {
updateSeriesAltNames
} from '../_common/db/generation';
let error = false;
async function emptyDatabase(db) {
await db.run('DELETE FROM kara_tag;');
await db.run('DELETE FROM kara_serie;');
......@@ -31,10 +33,10 @@ async function emptyDatabase(db) {
async function backupDir(directory) {
const backupDir = directory + '_backup';
if (await asyncExists(backupDir)) {
logger.debug('[Gen] [Gen] Removing backup dir ' + backupDir);
logger.debug('[Gen] Removing backup dir ' + backupDir);
await asyncRemove(backupDir);
}
logger.debug('[Gen] [Gen] Creating backup dir ' + backupDir);
logger.debug('[Gen] Creating backup dir ' + backupDir);
await asyncMkdirp(backupDir);
await asyncCopy(
directory,
......@@ -95,7 +97,11 @@ async function getAllKaras(karafiles) {
async function readAndCompleteKarafile(karafile) {
const karaData = await getKara(karafile);
await writeKara(karafile, karaData);
if (karaData.error) {
error = true;
} else {
await writeKara(karafile, karaData);
}
return karaData;
}
......@@ -141,7 +147,8 @@ function getSeries(kara) {
}
// Karas not LIVE nor MV must have a series.
if (isEmpty(series)) {
throw 'Karaoke series cannot be detected! ('+JSON.stringify(kara);
error = true;
logger.warn('[Gen] Karaoke series cannot be detected! ('+JSON.stringify(kara));
}
}
......@@ -219,7 +226,7 @@ async function prepareAltSeriesInsertData(altSeriesFile) {
$serie_altnamesnorm: deburr(altNames),
$serie_name: serie
});
logger.debug('[Gen] Added alt. names "' + altNames + '" to ' + serie);
logger.debug('[Gen] Added alt. name "' + altNames + '" to ' + serie);
}
});
} else {
......@@ -299,7 +306,8 @@ function getTypes(kara, allTags) {
});
if (result.size === 0) {
throw 'Karaoke type cannot be detected: ' + kara.type + ' in kara : ' + JSON.stringify(kara);
logger.warn('[Gen] Karaoke type cannot be detected : ' + kara.type + ' in kara : ' + JSON.stringify(kara));
error = true;
}
return result;
......@@ -388,9 +396,7 @@ export async function run(config) {
logger.info('[Gen] Starting database generation');
logger.info('[Gen] GENERATING DATABASE CAN TAKE A WHILE, PLEASE WAIT.');
const db = await open(karas_dbfile, {verbose: true, Promise});
logger.info('[Gen] Karaoke databases created');
const db = await open(karas_dbfile, {verbose: true, Promise});
await emptyDatabase(db);
await backupKaraDirs(conf);
......@@ -432,8 +438,10 @@ export async function run(config) {
await checkUserdbIntegrity(null, conf);
await deleteBackupDirs(conf);
return error;
} catch (err) {
logger.error(err);
return error;
}
}
......@@ -484,7 +492,7 @@ export async function checkUserdbIntegrity(uuid, config) {
await userdb.run('BEGIN TRANSACTION');
await userdb.run('PRAGMA foreign_keys = OFF;');
// Listing existing KIDs
// Listing existing KIDs
const karaKIDs = allKaras.map(k => '\'' + k.kid + '\'').join(',');
// Deleting records which aren't in our KID list
......@@ -496,10 +504,8 @@ export async function checkUserdbIntegrity(uuid, config) {
userdb.run(`DELETE FROM viewcount WHERE kid NOT IN (${karaKIDs});`),
userdb.run(`DELETE FROM playlist_content WHERE kid NOT IN (${karaKIDs});`)
]);
const karaIdByKid = new Map();
allKaras.forEach(k => karaIdByKid.set(k.kid, k.id_kara));
let sql = '';
whitelistKaras.forEach(wlk => {
......@@ -532,7 +538,7 @@ export async function checkUserdbIntegrity(uuid, config) {
sql += `UPDATE playlist_content SET fk_id_kara = ${karaIdByKid.get(plck.kid)} WHERE kid = '${plck.kid}';`;
}
});
blcTags.forEach(function (blcTag) {
let tagFound = false;
allTags.forEach(function (tag) {
......@@ -553,7 +559,7 @@ export async function checkUserdbIntegrity(uuid, config) {
});
if (sql) {
logger.debug('[Gen] [Gen] Tags UPDATE SQL : ' + sql);
logger.debug('[Gen] UPDATE SQL : ' + sql);
await userdb.run(sql);
}
......@@ -567,5 +573,5 @@ export async function checkUserdbIntegrity(uuid, config) {
await userdb.run('PRAGMA foreign_keys = ON;');
await userdb.run('COMMIT');
logger.info('[Gen] Integrity checks complete!');
logger.info('[Gen] Integrity checks complete! Please wait a little bit more...');
}
......@@ -26,23 +26,30 @@ export function getVideoGain(videofile, config) {
proc.stderr.on('data',(data) => {
output += data.toString();
});
const res = {};
proc.on('close', (code) => {
if (code !== 0) {
logger.error('Video ' + videofile + ' gain calculation error : ' + code);
resolve(0);
logger.warn('Video ' + videofile + ' gain calculation error : ' + code);
res.videogain = 0;
res.error = true;
resolve(res);
} else {
const outputArray = output.split(' ');
const index = outputArray.indexOf('track_gain');
if (index > -1) {
let audioGain = parseFloat(outputArray[index + 2]);
if (typeof audioGain === 'number') {
resolve(audioGain.toString());
res.data = audioGain.toString();
resolve(res);
} else {
resolve(0);
res.data = 0;
res.error = true;
resolve(res);
}
} else {
resolve(0);
res.data = 0;
res.error = true;
resolve(res);
}
}
});
......@@ -52,14 +59,17 @@ export function getVideoGain(videofile, config) {
export function getVideoDuration(videofile, config) {
const conf = config || getConfig();
const res = {};
return new Promise((resolve) => {
probe(conf.BinffprobePath, videofile, function(err, videodata) {
if (err) {
logger.error('Video ' + videofile + ' probe error : ' + err);
resolve(0);
logger.warn('Video ' + videofile + ' probe error : ' + err);
res.error = true;
res.data = 0;
resolve(res);
} else {
resolve(Math.floor(videodata.format.duration));
res.data = Math.floor(videodata.format.duration);
resolve(res);
}
});
});
......
......@@ -14,12 +14,14 @@ import {asyncReadFile, asyncStat, asyncWriteFile, resolveFileInDirs} from './fil
import {resolvedPathSubs, resolvedPathTemp, resolvedPathVideos} from './config';
import {extractSubtitles, getVideoDuration, getVideoGain} from './ffmpeg';
let error = false;
export async function getKara(karafile) {
const karaData = await parseKara(karafile);
karaData.isKaraModified = false;
verifyRequiredInfos(karaData);
verifyRequiredInfos(karaData,karafile);
if (!karaData.KID) {
karaData.isKaraModified = true;
......@@ -42,6 +44,7 @@ export async function getKara(karafile) {
videoFile = await resolveFileInDirs(karaData.videofile, resolvedPathVideos());
} catch (err) {
logger.warn('[Kara] Video file not found : ' + karaData.videofile);
error = true;
karaData.videogain = 0;
karaData.videosize = 0;
karaData.videoduration = 0;
......@@ -64,8 +67,10 @@ export async function getKara(karafile) {
const [videogain, videoduration] = await Promise.all([getVideoGain(videoFile), getVideoDuration(videoFile)]);
karaData.videogain = videogain;
karaData.videoduration = videoduration;
if (videoduration.error || videogain.error) error = true;
karaData.videogain = videogain.data;
karaData.videoduration = videoduration.data;
}
}
......@@ -73,6 +78,8 @@ export async function getKara(karafile) {
karaData.viewcount = 0;
karaData.checksum = checksum(stringify(karaData));
if (error) karaData.error = true;
return karaData;
}
......@@ -83,7 +90,7 @@ export async function writeKara(karafile, karaData) {
return;
}
verifyRequiredInfos(karaData);
verifyRequiredInfos(karaData,karafile);
timestamp.round = true;
const infosToWrite = {
......@@ -117,12 +124,14 @@ export async function parseKara(karaFile) {
return parseini(data);
}
export function verifyRequiredInfos(karaData) {
export function verifyRequiredInfos(karaData,karafile) {
if (!karaData.videofile || karaData.videofile.trim() === '') {
throw 'Karaoke video file empty!';
error = true;
logger.warn('[Kara] Karaoke video file empty for kara : '+karafile);
}
if (!karaData.subfile || karaData.subfile.trim() === '') {
throw 'Karaoke sub file empty!';
error = true;
logger.warn('[Kara] Karaoke sub file empty for kara : '+karafile);
}
}
......@@ -135,7 +144,8 @@ async function findSubFile(videoFile, kara) {
return await extractSubtitles(videoFile, extractFile);
} catch (err) {
// Not blocking.
logger.debug('[Kara] Could not extract subtitles from video file ' + videoFile);
logger.warn('[Kara] Could not extract subtitles from video file ' + videoFile);
error = true;
}
}
} else {
......@@ -143,6 +153,7 @@ async function findSubFile(videoFile, kara) {
return await resolveFileInDirs(kara.subfile, resolvedPathSubs());
} catch (err) {
logger.warn(`[Kara] Could not find subfile '${kara.subfile}'.`);
error = true;
}
}
// Non-blocking case if file isn't found
......
......@@ -65,6 +65,13 @@ module.exports = {
.then(() => {
var karasDB_Test = new Promise(function(resolve,reject){
var doGenerate = false;
if(module.exports.SETTINGS.optGenerateDB) {
// Manual generation triggered.
// Delete any existing karas.sqlite3 file
if(fs.existsSync(db_file)) {
fs.removeSync(db_file);
}
}
if(!fs.existsSync(db_file)) {
logger.warn('[DBI] Karaokes database not found');
doGenerate = true;
......@@ -83,9 +90,19 @@ module.exports = {
generator.onLog = function(type,message) {
logger.info('[DBI] [Gen]',message);
};
generator.run().then(function(){
generator.run().then(function(err){
logger.info('[DBI] Karaokes database created');
resolve();
if (module.exports.SETTINGS.optGenerateDB) {
if (err) {
logger.info('[DBI] Database generation completed with errors!');
process.exit(1);
} else {
logger.info('[DBI] Database generation completed successfully!');
process.exit(0);
}
} else {
resolve();
}
}).catch(function(error){
// error.
reject(error);
......
......@@ -72,7 +72,10 @@ async function main() {
console.log('Karaoke Mugen '+ config.VersionNo + ' - ' + config.VersionName);
process.exit(0);
}
if (argv.generate) {
logger.info('[Launcher] Database generation manually triggered');
config.optGenerateDB = true;
}
logger.info('[Launcher] Loaded configuration file');
logger.debug('[Launcher] Loaded configuration : ' + JSON.stringify(config, null, '\n'));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment