From 742bf51ddf8f799714ed1471b33c6fe14055a319 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Thu, 11 Feb 2021 14:25:20 -0500 Subject: [PATCH] Cache Pokemon Trainer Card IDs --- commands/edit-image/trainer-card.js | 11 +---------- structures/pokemon/Pokemon.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/commands/edit-image/trainer-card.js b/commands/edit-image/trainer-card.js index 0a98f1e3..62942cbd 100644 --- a/commands/edit-image/trainer-card.js +++ b/commands/edit-image/trainer-card.js @@ -91,7 +91,7 @@ module.exports = class TrainerCardCommand extends Command { try { const pokemonUsed = []; for (const pkmn of pokemon) { - const id = await this.fetchPokemonID(pkmn); + const id = await pokemon.fetchCardID(); pokemonUsed.push(id); } const card = await this.createCard(style, name, character, badgeChoice, pokemonUsed); @@ -114,13 +114,4 @@ module.exports = class TrainerCardCommand extends Command { .attach('_xfResponseType', 'json'); return Buffer.from(body.trainerCard, 'base64'); } - - async fetchPokemonID(pokemon) { - const { body } = await request - .post('https://pokecharms.com/trainer-card-maker/pokemon-panels') - .attach('number', pokemon.id) - .attach('_xfResponseType', 'json'); - const $ = cheerio.load(body.templateHtml); - return $('li[class="Panel"]').first().attr('data-id'); - } }; diff --git a/structures/pokemon/Pokemon.js b/structures/pokemon/Pokemon.js index c43b0b33..92a7b56c 100644 --- a/structures/pokemon/Pokemon.js +++ b/structures/pokemon/Pokemon.js @@ -1,4 +1,5 @@ const request = require('node-superfetch'); +const cheerio = require('cheerio'); const path = require('path'); const { removeDuplicates, firstUpperCase, delay } = require('../../util/Util'); const missingno = require('../../assets/json/missingno'); @@ -57,6 +58,7 @@ module.exports = class Pokemon { this.rawMoveSet = null; this.moveSet = data.missingno ? data.moveSet : []; this.moveSetVersion = data.missingno ? data.moveSetVersion : null; + this.trainerCardID = null; this.gameDataCached = data.missingno || false; this.gameDataFetching = data.missingno || false; this.missingno = data.missingno || false; @@ -168,6 +170,18 @@ module.exports = class Pokemon { return this.smogonTiers; } + async fetchCardID() { + if (this.trainerCardID) return this.trainerCardID; + const { body } = await request + .post('https://pokecharms.com/trainer-card-maker/pokemon-panels') + .attach('number', this.id) + .attach('_xfResponseType', 'json'); + const $ = cheerio.load(body.templateHtml); + const id = $('li[class="Panel"]').first().attr('data-id'); + this.trainerCardID = id; + return id; + } + async fetchGameData() { if (this.gameDataCached) return this; if (this.gameDataFetching) {