Import: Added band and character import

Use 'npm run import' to import from assets/import/{bands,characters}.
Existing entries are not overwritten!
This commit is contained in:
2022-08-29 00:33:37 +02:00
parent de326e3cd5
commit 202ab08d6c
4 changed files with 113 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
{
"id": "1",
"name": "BAND-MAID",
"description": "Band-Maid (stylized as BAND-MAID) is an all girl rock band from Tokyo that formed in July 2013. The band combines a rock sound with a maid image modeled on Japanese maid cafés.",
"imageURL": "https://cdn.discordapp.com/attachments/851543504831119380/1009467684490063892/unknown.png",
"enabled": 0
}

View File

@@ -0,0 +1,18 @@
[
{
"id": "1",
"bandId": "1",
"name": "Miku Kobato",
"description": "Miku Kobato is a Japanese singer, songwriter and guitarist. She is the initial founding member and main lyricist for BAND-MAID.",
"imageIdentifier": "bandmaid/miku.png",
"enabled": 0
},
{
"id": "2",
"bandId": "1",
"name": "Akane Hirose",
"description": "Akane Hirose is a Japanese drummer and founding member of BAND-MAID.",
"imageIdentifier": "bandmaid/akane.png",
"enabled": 0
}
]

86
import.js Normal file
View File

@@ -0,0 +1,86 @@
require("dotenv").config();
const { Console } = require("console");
const fs = require("fs");
const dbUtil = require("./util/db")
const { Band, Character } = require("./models");
const logger = new Console({
stdout: process.stdout,
stderr: process.stderr
});
//TODO: Fix ./data folders permission so wen can move out dataset in there
async function importBands() {
const bandFiles = fs.readdirSync("./assets/import/bands").filter(file => file.endsWith(".json"));
//read json file and parse it into a javascript object
for (const file of bandFiles) {
let band = fs.readFileSync(`./assets/import/bands/${file}`);
band = JSON.parse(band);
logger.log(`Importing band: ${band.name}`);
//check if band exists in database
let existingBand = await db.Band.findOne({
where: {
name: band.name
}
})
if (existingBand) {
logger.log(`Band ${band.name} already exists in database`);
continue;
} else {
//create band in database
await db.Band.create({
id: band.id,
name: band.name,
description: band.description,
imageURL: band.imageURL,
enabled: band.enabled
});
logger.log(`Created band ${band.name} in database`);
}
}
}
async function importCharacters() {
const characterFiles = fs.readdirSync("./assets/import/characters").filter(file => file.endsWith(".json"));
//read json file and parse it into a javascript object
for (const file of characterFiles) {
let characters = fs.readFileSync(`./assets/import/characters/${file}`);
characters = JSON.parse(characters);
for (character of characters) {
logger.log(`Importing character: ${character.name}`);
//check if character exists in database
let existingCharacter = await db.Character.findOne({
where: {
id: character.id
}
})
if (existingCharacter) {
logger.log(`Character ${character.name} already exists in database`);
continue;
} else {
//create band in database
await db.Character.create({
id: character.id,
bandId: character.bandId,
name: character.name,
description: character.description,
imageIdentifier: character.imageIdentifier,
enabled: character.enabled
});
logger.log(`Created character ${character.name} in database`);
}
}
}
}
logger.log("Importing...");
dbUtil.syncDb();
db = dbUtil.getDb();
importBands();
importCharacters();
logger.log("Import complete");

View File

@@ -5,7 +5,8 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon --inspect=0.0.0.0:9229 index.js" "dev": "nodemon --inspect=0.0.0.0:9229 index.js",
"import": "node import.js"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",