diff --git a/nier-reincarnation/costumes/index.mjs b/nier-reincarnation/costumes/index.mjs new file mode 100644 index 0000000..634cfa7 --- /dev/null +++ b/nier-reincarnation/costumes/index.mjs @@ -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", + "
" + ), + }, + ], + 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})`); + } +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 5d1e827..fcf1f33 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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==" } } } diff --git a/package.json b/package.json index 9f2f6c6..8d55b2f 100644 --- a/package.json +++ b/package.json @@ -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" } }