Added duplicate weapon stories checks

This commit is contained in:
DrMint 2023-02-13 02:02:47 +01:00
parent 72d642ed13
commit c00531c7d2
6 changed files with 81 additions and 0 deletions

View File

@ -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=

View File

@ -15,4 +15,10 @@ Usage:
```sh
npm run import:weapon-stories
```
Related checks:
```sh
npm run check:weapon-stories-duplicates
```

View File

@ -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);
}
}
}

View File

@ -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

3
package-lock.json generated
View File

@ -11,6 +11,9 @@
"dependencies": {
"dotenv": "^16.0.3",
"envsafe": "^2.0.3"
},
"engines": {
"node": ">=18.13.0"
}
},
"node_modules/dotenv": {

View File

@ -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": {