-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import axios from 'axios'; | ||
import AdmZip from 'adm-zip'; | ||
import xml2js from 'xml2js'; | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
|
||
export default async (req, res) => { | ||
try { | ||
// Step 1: Download switchtdb.zip | ||
const response = await axios.get('https://www.gametdb.com/switchtdb.zip', { | ||
headers: { | ||
'User-Agent': 'RiiTag', | ||
}, | ||
responseType: 'arraybuffer', | ||
}); | ||
|
||
// Step 2: Unzip switchtdb.zip and save switchtdb.xml | ||
const zip = new AdmZip(response.data); | ||
zip.extractEntryTo('switchtdb.xml', path.resolve(DATA.IDS, 'switchtdb.xml')); | ||
|
||
// Step 3: Read and modify the XML | ||
const xmlData = await fs.readFile(path.resolve(DATA.IDS, 'switchtdb.xml')); | ||
const jsonData = await xml2js.parseStringPromise(xmlData, { explicitArray: false }); | ||
|
||
// Scrubbing logic (modify jsonData as needed) | ||
|
||
// Step 4: Save the modified XML | ||
const builder = new xml2js.Builder(); | ||
const modifiedXml = builder.buildObject(jsonData); | ||
await fs.writeFile(path.resolve(DATA.IDS, 'switchtdb.xml'), modifiedXml); | ||
|
||
// Step 5: Create switchtdb.json | ||
const gameData = jsonData.datafile.game; | ||
const gameMap = {}; | ||
|
||
gameData.forEach((game) => { | ||
const name = game.name.replace(/ \(EN\)$/, ''); | ||
const id = game.id; | ||
if (!gameMap[name]) { | ||
gameMap[name] = []; | ||
} | ||
gameMap[name].push(id); | ||
}); | ||
|
||
await fs.writeFile( | ||
path.resolve(DATA.IDS, 'switchtdb.json'), | ||
JSON.stringify(gameMap, null, 2) | ||
); | ||
|
||
// Step 6: Sort the keys alphabetically | ||
const sortedGameMap = {}; | ||
Object.keys(gameMap) | ||
.sort() | ||
.forEach((key) => { | ||
sortedGameMap[key] = gameMap[key]; | ||
}); | ||
|
||
// Step 7: Respond with the sorted JSON data | ||
res.status(200).json(sortedGameMap); | ||
} catch (error) { | ||
console.error('Error:', error); | ||
res.status(500).json({ error: 'An error occurred' }); | ||
} | ||
}; |