2023-02-01 23:13:27 +00:00
import 'dotenv/config' ;
import { NIERREIN _GUIDE _API _URL , NIERREIN _GUIDE _CDN _URL , WEAPON _TYPES _RELATION _IDS } from '../../config.mjs' ;
import { env } from '../../env.mjs' ;
let currentIndex = 1 ;
let weapons = [ ] ;
2023-02-13 01:02:47 +00:00
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" ] ,
] ) ;
2023-02-01 23:13:27 +00:00
try {
console . log ( 'Fetching NieR Re[in]carnation weapons...' )
weapons = await fetch ( ` ${ NIERREIN _GUIDE _API _URL } /weapons ` )
. then ( ( response ) => response . json ( ) )
} catch ( error ) {
console . error ( error )
process . exit ( 1 ) ;
}
console . log ( ` ${ weapons . length } weapons fetched from " ${ NIERREIN _GUIDE _API _URL } /weapons" ` )
if ( weapons . length === 0 ) {
console . error ( ` Got 0 weapons from " ${ NIERREIN _GUIDE _API _URL } /weapons". Their database is probably in the process of being updated. Try again in 10 minutes. ` )
process . exit ( 1 ) ;
}
for ( const weapon of weapons ) {
console . log ( ` Uploading n° ${ currentIndex } / ${ weapons . length } weapons. ` ) ;
// Get weapon image blob for the thumbnail
const file = await fetch ( ` ${ NIERREIN _GUIDE _CDN _URL } ${ weapon . image _path } full.png ` ) . then (
( response ) => response . blob ( )
) ;
2023-02-13 01:02:47 +00:00
// 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 ) ;
}
2023-02-01 23:13:27 +00:00
const body = new FormData ( ) ;
// Create the weapon-stories entry
body . append (
"data" ,
JSON . stringify ( {
slug : weapon . slug ,
name : [
{
name : weapon . name ,
language : {
connect : [ 2 ] , // en
} ,
} ,
] ,
stories : [
{
categories : {
connect : [ 8 ] , // nier reincarnation
} ,
translations : [
{
language : {
connect : [ 2 ] , // en
} ,
status : "Done" ,
level _1 :
weapon . weapon _story _link [ 0 ] . weapon _story . story ? . replaceAll (
"\\n" ,
"<br>"
) ,
level _2 :
weapon . weapon _story _link [ 1 ] . weapon _story . story ? . replaceAll (
"\\n" ,
"<br>"
) ,
level _3 :
weapon . weapon _story _link [ 2 ] . weapon _story . story ? . replaceAll (
"\\n" ,
"<br>"
) ,
level _4 :
weapon . weapon _story _link [ 3 ] . weapon _story . story ? . replaceAll (
"\\n" ,
"<br>"
) ,
} ,
] ,
} ,
] ,
categories : {
connect : [ 8 ] , // nier reincarnation
} ,
type : {
connect : [ WEAPON _TYPES _RELATION _IDS [ weapon . weapon _type ] ] , // weapon type
} ,
} )
) ;
// Add the weapon image blob
body . append ( "files.thumbnail" , file , ` ${ weapon . slug } .png ` ) ;
const response = await fetch (
` ${ env . STRAPI _BASE _API _URL } /weapon-stories ` ,
{
method : "POST" ,
body ,
headers : {
Authorization :
` bearer ${ env . STRAPI _API _TOKEN } ` ,
} ,
}
)
. then ( ( res ) => res . json ( ) )
. catch ( ( err ) => err ? . json ( ) ) ;
if ( response ? . error ) {
if ( response . error . message === "This attribute must be unique" ) {
console . warn (
` [DUPLICATE] ${ weapon . name } ( ${ weapon . slug } ) already exists. `
) ;
continue
}
console . error ( ` [ERROR] ${ weapon . name } ( ${ weapon . slug } ): ` , response . error . message )
} else {
console . log ( ` [ADDED] " ${ weapon . name } " ( ${ weapon . slug } ) ` ) ;
}
currentIndex ++ ;
}