diff --git a/.env.example b/.env.example index 35d8ec7..e53f66f 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ STRAPI_BASE_API_URL=https://strapi.accords-library.com|http://localhost:1337/api +STRAPI_GRAPHQL=https://strapi.accords-library.com/graphql|http://localhost:1337/graphql STRAPI_API_TOKEN= \ No newline at end of file diff --git a/README.md b/README.md index 5db20c8..8413811 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,10 @@ Usage: ```sh npm run import:weapon-stories +``` + +Related checks: + +```sh +npm run check:weapon-stories-duplicates ``` \ No newline at end of file diff --git a/nier-reincarnation/weapon-stories/find-duplicates.mjs b/nier-reincarnation/weapon-stories/find-duplicates.mjs new file mode 100644 index 0000000..7c06f77 --- /dev/null +++ b/nier-reincarnation/weapon-stories/find-duplicates.mjs @@ -0,0 +1,53 @@ +import "dotenv/config"; + +const response = await fetch(`${process.env.STRAPI_GRAPHQL}`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `bearer ${process.env.STRAPI_API_TOKEN}`, + }, + body: JSON.stringify({ + query: `{ + weaponStories(pagination: { limit: -1 }) { + data { + id + attributes { + slug + name { name } + } + } + } + }`, + }), +}); + +const normalizeName = (name) => { + return name + .toLowerCase() + .normalize("NFD") + .replace(/[\u0300-\u036f]/g, "") + .replace(/['’]/g, "'"); +}; + +const weapons = (await response.json()).data.weaponStories.data; + +const nameMap = new Map(); + +for (const weapon of weapons) { + for (const { name } of weapon.attributes.name) { + if (name === undefined || name === null) { + console.warn(name, "is nullable", weapon); + } + + const normalizedName = normalizeName(name); + + if (nameMap.has(normalizedName)) + console.warn(` +Duplicate names ${normalizedName} in: + 1. ${nameMap.get(normalizedName).attributes.slug} + 2. ${weapon.attributes.slug}`); + else { + nameMap.set(normalizedName, weapon); + } + } +} diff --git a/nier-reincarnation/weapon-stories/index.mjs b/nier-reincarnation/weapon-stories/index.mjs index 3e203cd..6e4b897 100644 --- a/nier-reincarnation/weapon-stories/index.mjs +++ b/nier-reincarnation/weapon-stories/index.mjs @@ -6,6 +6,18 @@ let currentIndex = 1; let weapons = []; +const slugSpecialCases = new Map([ + ["phoenix-lance", "phoenix-spear"], + ["iron-will", "hymir-finger"], + ["the-devil-queen", "widow-death"], + ["kains-sword", "kaines-sword"], + ["spear-of-the-usurper", "robber-king"], + ["ancient-overlord", "kingsblood"], + ["fang-of-the-twins", "twins-fang"], + ["dragoon-lance", "knight-vow"], + ["faith", "nobuyoshi"], +]); + try { console.log('Fetching NieR Re[in]carnation weapons...') weapons = await fetch(`${NIERREIN_GUIDE_API_URL}/weapons`) @@ -30,6 +42,11 @@ for (const weapon of weapons) { (response) => response.blob() ); + // Change slug if the weapon is known with a different name in accords-library + if (slugSpecialCases.has(weapon.slug)) { + weapon.slug = slugSpecialCases.get(weapon.slug); + } + const body = new FormData(); // Create the weapon-stories entry diff --git a/package-lock.json b/package-lock.json index fb14c24..5d1e827 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,9 @@ "dependencies": { "dotenv": "^16.0.3", "envsafe": "^2.0.3" + }, + "engines": { + "node": ">=18.13.0" } }, "node_modules/dotenv": { diff --git a/package.json b/package.json index b085631..9f2f6c6 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "description": "", "license": "MIT", "scripts": { + "check:weapon-stories-duplicates": "node ./nier-reincarnation/weapon-stories/find-duplicates.mjs", "import:weapon-stories": "node ./nier-reincarnation/weapon-stories/index.mjs" }, "dependencies": {