Skip to content

Instantly share code, notes, and snippets.

@anurag-roy
Last active October 1, 2021 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anurag-roy/6b39fff1cfe89fcf7132e95b6ac66de1 to your computer and use it in GitHub Desktop.
Save anurag-roy/6b39fff1cfe89fcf7132e95b6ac66de1 to your computer and use it in GitHub Desktop.
Script to create custom Pokemon data from the PokeAPI data at https://github.com/PokeAPI/api-data
// .../api-data/data/createPokeData.js
import { readdirSync, readFileSync, writeFileSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
const baseDir = dirname(fileURLToPath(import.meta.url));
const pokemonDir = join(baseDir, 'api', 'v2', 'pokemon');
const outDir = join(baseDir, 'out');
// Filter function which will be used to get the `en` versions of all names and texts.
const findEn = (obj) => obj.language.name === 'en';
// Remove escape characters from a string
const unescape = (escapedStr) => escapedStr.replace(/[\u0000-\u001F\u007F-\u009F]/g, ' ').trim();
// Utility function to read the JSON file for the corresponding URL.
// URLs are of the format `/api/v2/{resource}/{index}/`
const readAsJson = (fileUrl) => {
const fileContents = readFileSync(join(baseDir, fileUrl, 'index.json'), { encoding: 'utf-8' });
return JSON.parse(fileContents);
};
const allPokemonDirs = readdirSync(pokemonDir, { withFileTypes: true });
let allPokemon = [];
for (const dir of allPokemonDirs) {
if (dir.isDirectory()) {
const pokemon = readAsJson(`api/v2/pokemon/${dir.name}`);
// Basic species details
const species = readAsJson(pokemon.species.url);
const { name } = species.names.find(findEn);
const { genus } = species.genera.find(findEn);
const { flavor_text } = species.flavor_text_entries.find(findEn);
const description = unescape(flavor_text);
// Image URL
const imageUrl = `https://raw.githubusercontent.com/anurag-roy/poke-api/main/assets/images/${pokemon.id}.webp`;
// Types - Preserve slot order
const types = pokemon.types
.sort((t1, t2) => t1.slot - t2.slot)
.map((t) => {
const type = readAsJson(t.type.url);
const { name } = type.names.find(findEn);
return name;
});
// Abilities
const abilities = pokemon.abilities.map((a) => {
const ability = readAsJson(a.ability.url);
const { name } = ability.names.find(findEn);
let { effect } = ability.effect_entries.find(findEn) || {};
effect = effect ? unescape(effect) : '';
const { flavor_text } = ability.flavor_text_entries.find(findEn);
const description = unescape(flavor_text);
return {
name,
effect,
description,
};
});
// Stats
const stats = pokemon.stats.reduce((statsObj, currentStat) => {
const stat = readAsJson(currentStat.stat.url);
const { name } = stat.names.find(findEn);
statsObj[name] = currentStat.base_stat;
return statsObj;
}, {});
// Encounters and locations
const encounters = readAsJson(pokemon.location_area_encounters);
const locations = encounters.map((e) => {
const locationArea = readAsJson(e.location_area.url);
const location = readAsJson(locationArea.location.url);
const { name } = location.names.find(findEn);
return name;
});
const uniqueLocations = Array.from(new Set(locations));
// Construct final object
const outputPokemon = {
id: pokemon.id,
name,
genus,
description,
imageUrl,
types,
};
allPokemon.push(outputPokemon);
// Slightly more detailed object
const pokemonDetail = {
...outputPokemon,
abilities,
stats,
locations: uniqueLocations,
};
writeFileSync(`${outDir}/${dir.name}.json`, JSON.stringify(pokemonDetail), {
encoding: 'utf-8',
});
}
}
// Sort to order pokemon as 1,2,3,.. instead of 1,10,100,...
allPokemon = allPokemon.sort((p1, p2) => p1.id - p2.id);
writeFileSync(`${outDir}/pokemon.json`, JSON.stringify(allPokemon), {
encoding: 'utf-8',
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment