feat(import): costumes

This commit is contained in:
Nemesis 2023-02-19 20:11:19 +01:00
parent c00531c7d2
commit 0b99a0d977
3 changed files with 133 additions and 3 deletions

View File

@ -0,0 +1,115 @@
import 'dotenv/config';
import { NIERREIN_GUIDE_API_URL, NIERREIN_GUIDE_CDN_URL } from '../../config.mjs';
import { env } from '../../env.mjs';
import slugg from 'slugg';
let currentIndex = 1;
const { data: emblems } = await fetch(
`${env.STRAPI_BASE_API_URL}/rein-emblems`,
{
headers: {
Authorization:
`bearer ${env.STRAPI_API_TOKEN}`,
},
}
)
.then((res) => res.json())
.catch((error) => {
console.error(error);
process.exit(1);
});
let costumes = [];
try {
console.log('Fetching NieR Re[in]carnation costumes...')
costumes = await fetch(`${NIERREIN_GUIDE_API_URL}/costumes`)
.then((response) => response.json())
} catch (error) {
console.error(error)
process.exit(1);
}
console.log(`${costumes.length} costumes fetched from "${NIERREIN_GUIDE_API_URL}/costumes"`)
if (costumes.length === 0) {
console.error(`Got 0 costume from "${NIERREIN_GUIDE_API_URL}/costumes". Their database is probably in the process of being updated. Try again in 10 minutes.`)
process.exit(1);
}
for (const costume of costumes) {
if (!currentIndex > 1) continue
console.log(`Uploading n°${currentIndex}/${costumes.length} costumes.`);
// Get costume image blob for the sprite
const file = await fetch(`${NIERREIN_GUIDE_CDN_URL}${costume.image_path_base}full.png`).then(
(response) => response.blob()
);
const body = new FormData();
let costumeEmblem = {}
if (costume.emblem_id) {
costumeEmblem = emblems.find((emblem) => emblem.attributes.slug === slugg(costume.emblem.name))
}
// Create the weapon-stories entry
body.append(
"data",
JSON.stringify({
slug: costume.slug,
translations: [
{
language: {
connect: [2], // en
},
name: costume.title,
description:
costume.description?.replaceAll(
"\\n",
"<br>"
),
},
],
emblem: {
connect: [costumeEmblem.id].filter(Boolean),
},
})
);
// Add the weapon image blob
body.append("files.sprite", file, `${costume.slug}.png`);
const response = await fetch(
`${env.STRAPI_BASE_API_URL}/rein-costumes`,
{
method: "POST",
body,
headers: {
Authorization:
`bearer ${env.STRAPI_API_TOKEN}`,
},
}
)
.then((res) => res.json())
.catch((err) => err?.json());
currentIndex++;
if (response?.error) {
if (response.error.message === "This attribute must be unique") {
console.warn(
`[DUPLICATE] ${costume.name} (${costume.slug}) already exists.`
);
continue
}
console.error(`[ERROR] ${costume.title} (${costume.slug}):`, response.error.message)
} else {
console.log(`[ADDED] "${costume.title}" (${costume.slug})`);
}
}

13
package-lock.json generated
View File

@ -10,7 +10,8 @@
"license": "MIT",
"dependencies": {
"dotenv": "^16.0.3",
"envsafe": "^2.0.3"
"envsafe": "^2.0.3",
"slugg": "^1.2.1"
},
"engines": {
"node": ">=18.13.0"
@ -31,6 +32,11 @@
"engines": {
"node": ">=10"
}
},
"node_modules/slugg": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/slugg/-/slugg-1.2.1.tgz",
"integrity": "sha512-yzUY7d53sdY3wp6rGCc54fn+cel0aNsdG5lvRVN8bvlAXYQI2fIo36AAOVjArOaMzlidHYyAYqriHzuos93h+Q=="
}
},
"dependencies": {
@ -43,6 +49,11 @@
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/envsafe/-/envsafe-2.0.3.tgz",
"integrity": "sha512-51lek8h5btjXhXUDW//Pno7HPydM1WQzltOb1/ONRKdwB2QkfAURnN4Qn0cJ5LzGvX2Km1ReR2O3K3Bqq9sBMg=="
},
"slugg": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/slugg/-/slugg-1.2.1.tgz",
"integrity": "sha512-yzUY7d53sdY3wp6rGCc54fn+cel0aNsdG5lvRVN8bvlAXYQI2fIo36AAOVjArOaMzlidHYyAYqriHzuos93h+Q=="
}
}
}

View File

@ -8,11 +8,15 @@
"description": "",
"license": "MIT",
"scripts": {
"import:all": "npm run import:emblems && npm run import:costumes && npm run import:weapon-stories",
"check:weapon-stories-duplicates": "node ./nier-reincarnation/weapon-stories/find-duplicates.mjs",
"import:weapon-stories": "node ./nier-reincarnation/weapon-stories/index.mjs"
"import:weapon-stories": "node ./nier-reincarnation/weapon-stories/index.mjs",
"import:emblems": "node ./nier-reincarnation/emblems/index.mjs",
"import:costumes": "node ./nier-reincarnation/costumes/index.mjs"
},
"dependencies": {
"dotenv": "^16.0.3",
"envsafe": "^2.0.3"
"envsafe": "^2.0.3",
"slugg": "^1.2.1"
}
}