Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Last active September 7, 2020 04:30
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 Ratstail91/d1a661cd5203327ad0cd5e18e561c3c0 to your computer and use it in GitHub Desktop.
Save Ratstail91/d1a661cd5203327ad0cd5e18e561c3c0 to your computer and use it in GitHub Desktop.
const fetch = require('node-fetch');
const fs = require('fs');
const nameExceptions = ['ho-oh', 'porygon-z', 'jangmo-o', 'hakamo-o', 'kommo-o', 'mr-mime', 'mime-jr', 'type-null', 'tapu-lele', 'tapu-fini', 'tapu-koko', 'tapu-bulu'];
const collection = {};
(async () => {
const result = fetch('https://pokeapi.co/api/v2/pokemon?limit=100000')
.then(res => res.text())
.then(blob => JSON.parse(blob).results)
.then(async array => {
return array.map(header => {
return fetch(header.url)
.then(res => res.text())
.then(blob => JSON.parse(blob))
.then(body => {
// console.log('processing', body.name);
let poke = {
name: body.name,
height: body.height,
weight: body.weight,
base_stats: {
hp: body.stats.length > 0 ? body.stats.filter(s => s.stat.name == "hp")[0].base_stat : null,
attack: body.stats.length > 0 ? body.stats.filter(s => s.stat.name == "attack")[0].base_stat : null,
defense: body.stats.length > 0 ? body.stats.filter(s => s.stat.name == "defense")[0].base_stat : null,
specialAttack: body.stats.length > 0 ? body.stats.filter(s => s.stat.name == "special-attack")[0].base_stat : null,
specialDefense: body.stats.length > 0 ? body.stats.filter(s => s.stat.name == "special-defense")[0].base_stat : null,
speed: body.stats.length > 0 ? body.stats.filter(s => s.stat.name == "speed")[0].base_stat : null,
},
forms: [] //for later
};
return poke;
})
.then(poke => {
if (poke.name.indexOf('-') == -1 || nameExceptions.includes(poke.name.toLowerCase())) {
if (collection[poke.name]) {
poke.forms = poke.forms.concat(collection[poke.name].forms);
}
collection[poke.name] = poke;
return;
}
let name;
if (poke.name.indexOf(' ') != -1) {
name = poke.name.split(' ');
} else {
name = poke.name.split('-');
}
const baseName = name.shift();
const formName = name.length > 1 ? name.join('-') : name[0];
// console.log('\t\t', poke.name);
try {
collection[baseName] = collection[baseName] || { name: baseName, forms: [] };
collection[baseName].forms.push(poke);
}catch (err) {
console.log('damn', err);
}
})
.catch(err => console.log(err))
;
});
})
.catch(err => console.error(err))
;
//finally
result.then((res) => {
Promise.all(res).then(() => {
fs.writeFileSync('./pokemon.json', JSON.stringify(Object.values(collection)));
});
});
})();
/**
* Simple object check.
* @param item
* @returns {boolean}
*/
function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
/**
* Deep merge two objects.
* @param target
* @param ...sources
*/
function mergeDeep(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return mergeDeep(target, ...sources);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment