mirror of
https://github.com/chase-manning/pokemon-js.git
synced 2025-08-11 21:29:13 +00:00
✨ add script to get evolutions
This commit is contained in:
parent
22f2a0c151
commit
bbb6aca967
1 changed files with 60 additions and 0 deletions
60
scripts/get-evolutions.js
Normal file
60
scripts/get-evolutions.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
const pokemon = require("./pokemon.json");
|
||||
const fs = require("fs");
|
||||
|
||||
const BASE_URL = "https://pokeapi.co/api/v2/evolution-chain/";
|
||||
|
||||
function getPokemonId(name) {
|
||||
for (let i = 0; i < pokemon.length; i++) {
|
||||
if (pokemon[i].name === name) return i + 1;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const downloadData = async () => {
|
||||
// Read data
|
||||
const output = JSON.parse(fs.readFileSync("data.json"));
|
||||
|
||||
for (let i = 1; i < 150; i++) {
|
||||
console.log("Downloading", i);
|
||||
const response = await fetch(`${BASE_URL}${i}`);
|
||||
const data = await response.json();
|
||||
const pokemonName = data.chain.species.name;
|
||||
console.log(pokemonName);
|
||||
|
||||
const evolvesTo = data.chain.evolves_to.find(
|
||||
(e) => e.evolution_details[0].trigger.name === "level-up"
|
||||
);
|
||||
if (!evolvesTo) continue;
|
||||
const evolution = {
|
||||
pokemon: getPokemonId(evolvesTo.species.name),
|
||||
level: evolvesTo.evolution_details[0].min_level,
|
||||
};
|
||||
const pokemonId = getPokemonId(pokemonName);
|
||||
if (pokemonId && evolution.pokemon) {
|
||||
output[pokemonId].evolution = evolution;
|
||||
}
|
||||
const evolvesTo2 = evolvesTo.evolves_to.find(
|
||||
(e) => e.evolution_details[0].trigger.name === "level-up"
|
||||
);
|
||||
console.log(evolvesTo2);
|
||||
if (!evolvesTo2) continue;
|
||||
console.log(evolvesTo2);
|
||||
const evolution2 = {
|
||||
pokemon: getPokemonId(evolvesTo2.species.name),
|
||||
level: evolvesTo2.evolution_details[0].min_level,
|
||||
};
|
||||
console.log(evolution2);
|
||||
|
||||
if (evolution.pokemon && evolution2.pokemon) {
|
||||
output[evolution.pokemon].evolution = evolution2;
|
||||
}
|
||||
|
||||
console.log(`Downloaded ${i}`);
|
||||
// wait one second
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
|
||||
fs.writeFileSync("data.json", JSON.stringify(output));
|
||||
};
|
||||
|
||||
downloadData();
|
Loading…
Add table
Add a link
Reference in a new issue