2023-02-19 19:11:19 +00:00
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 ) ;
} ) ;
2023-02-19 19:21:29 +00:00
if ( ! emblems || emblems . length === 0 ) {
console . error ( ` Got 0 emblems from " ${ env . STRAPI _BASE _API _URL } /rein-emblems". You may want to run "npm run import:emblems". ` )
process . exit ( 1 ) ;
}
2023-02-19 19:11:19 +00:00
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 } ) ` ) ;
}
}