Speedup poe generation by caching bot json

This commit is contained in:
SillyLossy
2023-04-13 16:50:10 +03:00
parent 14cc5ba937
commit 58ed03dfe0
2 changed files with 19 additions and 8 deletions

View File

@@ -29,6 +29,8 @@ const parent_path = path.resolve(__dirname);
const queries_path = path.join(parent_path, "poe_graphql"); const queries_path = path.join(parent_path, "poe_graphql");
let queries = {}; let queries = {};
const cached_bots = {};
const logger = console; const logger = console;
const user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0"; const user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0";
@@ -85,9 +87,11 @@ class Client {
ws = null; ws = null;
ws_connected = false; ws_connected = false;
auto_reconnect = false; auto_reconnect = false;
use_cached_bots = false;
constructor(auto_reconnect = false) { constructor(auto_reconnect = false, use_cached_bots = false) {
this.auto_reconnect = auto_reconnect; this.auto_reconnect = auto_reconnect;
this.use_cached_bots = use_cached_bots;
} }
async init(token, proxy = null) { async init(token, proxy = null) {
@@ -150,9 +154,16 @@ class Client {
const bots = {}; const bots = {};
for (const bot of botList.filter(x => x.deletionState == 'not_deleted')) { for (const bot of botList.filter(x => x.deletionState == 'not_deleted')) {
const url = `https://poe.com/_next/data/${this.next_data.buildId}/${bot.displayName}.json`; const url = `https://poe.com/_next/data/${this.next_data.buildId}/${bot.displayName}.json`;
logger.info(`Downloading ${url}`); let r;
const r = await request_with_retries(() => this.session.get(url)); if (this.use_cached_bots && cached_bots[url]) {
r = cached_bots[url];
}
else {
logger.info(`Downloading ${url}`);
r = await request_with_retries(() => this.session.get(url));
cached_bots[url] = r;
}
const chatData = r.data.pageProps.payload.chatOfBotDisplayName; const chatData = r.data.pageProps.payload.chatOfBotDisplayName;
bots[chatData.defaultBotObject.nickname] = chatData; bots[chatData.defaultBotObject.nickname] = chatData;

View File

@@ -1871,8 +1871,8 @@ app.post('/deletegroup', jsonParser, async (request, response) => {
const POE_DEFAULT_BOT = 'a2'; const POE_DEFAULT_BOT = 'a2';
async function getPoeClient(token) { async function getPoeClient(token, useCache=false) {
let client = new poe.Client(); let client = new poe.Client(false, useCache);
await client.init(token); await client.init(token);
return client; return client;
} }
@@ -1904,7 +1904,7 @@ app.post('/purge_poe', jsonParser, async (request, response) => {
const count = request.body.count ?? -1; const count = request.body.count ?? -1;
try { try {
const client = await getPoeClient(token); const client = await getPoeClient(token, true);
await client.purge_conversation(bot, count); await client.purge_conversation(bot, count);
client.disconnect_ws(); client.disconnect_ws();
@@ -1928,7 +1928,7 @@ app.post('/generate_poe', jsonParser, async (request, response) => {
let client; let client;
try { try {
client = await getPoeClient(token); client = await getPoeClient(token, true);
} }
catch (error) { catch (error) {
console.error(error); console.error(error);