From 084d17dc191b478c5b3262a30b66d2231b8129af Mon Sep 17 00:00:00 2001 From: RossAscends <124905043+RossAscends@users.noreply.github.com> Date: Thu, 29 Jun 2023 12:13:28 +0900 Subject: [PATCH 1/9] revert poe changes. data>>payload --- src/poe-client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/poe-client.js b/src/poe-client.js index 95b1b1297..b060db966 100644 --- a/src/poe-client.js +++ b/src/poe-client.js @@ -369,13 +369,13 @@ class Client { const nextData = JSON.parse(jsonText); this.formkey = extractFormKey(r.data); - this.viewer = nextData.props.pageProps.data.viewer; + this.viewer = nextData.props.pageProps.payload.viewer; return nextData; } async get_bots() { - const viewer = this.next_data.props.pageProps.data.viewer; + const viewer = this.next_data.props.pageProps.payload.viewer; if (!viewer.availableBotsConnection) { throw new Error('Invalid token.'); } @@ -398,7 +398,7 @@ class Client { cached_bots[url] = r; } - const chatData = r.data.pageProps.data.chatOfBotDisplayName; + const chatData = r.data.pageProps.payload.chatOfBotDisplayName; bots[chatData.defaultBotObject.nickname] = chatData; resolve(); From 29e0a8335b699b26244e9151ab69b7624d679b39 Mon Sep 17 00:00:00 2001 From: RossAscends <124905043+RossAscends@users.noreply.github.com> Date: Thu, 29 Jun 2023 14:10:30 +0900 Subject: [PATCH 2/9] smart detection of data vs payload on poe api --- src/poe-client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/poe-client.js b/src/poe-client.js index b060db966..c633445f2 100644 --- a/src/poe-client.js +++ b/src/poe-client.js @@ -369,13 +369,13 @@ class Client { const nextData = JSON.parse(jsonText); this.formkey = extractFormKey(r.data); - this.viewer = nextData.props.pageProps.payload.viewer; + this.viewer = nextData.props.pageProps.payload?.viewer || nextData.props.pageProps.data?.viewer; return nextData; } async get_bots() { - const viewer = this.next_data.props.pageProps.payload.viewer; + const viewer = this.next_data.props.pageProps.payload?.viewer || this.next_data.props.pageProps.data?.viewer; if (!viewer.availableBotsConnection) { throw new Error('Invalid token.'); } @@ -398,7 +398,7 @@ class Client { cached_bots[url] = r; } - const chatData = r.data.pageProps.payload.chatOfBotDisplayName; + const chatData = r.data.pageProps.payload?.chatOfBotDisplayName || r.data.pageProps.data?.chatOfBotDisplayName; bots[chatData.defaultBotObject.nickname] = chatData; resolve(); From 0c55d36a2b159b7fd145f592e003cebe06b50d7b Mon Sep 17 00:00:00 2001 From: RossAscends <124905043+RossAscends@users.noreply.github.com> Date: Thu, 29 Jun 2023 17:06:17 +0900 Subject: [PATCH 3/9] sync dev poe fix with main --- src/poe-client.js | 73 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/src/poe-client.js b/src/poe-client.js index c633445f2..f67f6bd64 100644 --- a/src/poe-client.js +++ b/src/poe-client.js @@ -24,6 +24,7 @@ const fs = require('fs'); const path = require('path'); const http = require('http'); const https = require('https'); +const _ = require('lodash'); const directory = __dirname; @@ -281,6 +282,33 @@ async function request_with_retries(method, attempts = 10) { throw new Error(`Failed to download ${url} too many times.`); } +function findKey(obj, key, path = []) { + if (obj && typeof obj === 'object') { + if (key in obj) { + return [...path, key]; + } + for (const k in obj) { + const result = findKey(obj[k], key, [...path, k]); + if (result) { + return result; + } + } + } + return false; +} + +function logObjectStructure(obj, indent = 0, depth = Infinity) { + const keys = Object.keys(obj); + keys.forEach((key) => { + console.log(`${' '.repeat(indent)}${key}`); + if (typeof obj[key] === 'object' && obj[key] !== null && indent < depth) { + logObjectStructure(obj[key], indent + 1, depth); + } + }); +} + + + class Client { gql_url = "https://poe.com/api/gql_POST"; gql_recv_url = "https://poe.com/api/receive_POST"; @@ -363,19 +391,52 @@ class Client { async get_next_data() { logger.info('Downloading next_data...'); + //these keys are used as of June 29, 2023 + //if API changes in the future, just change these to find the new path + const viewerKeyName = 'viewer' + const botNameKeyName = 'chatOfBotDisplayName' + const defaultBotKeyName = 'defaultBotNickname' + const r = await request_with_retries(() => this.session.get(this.home_url)); const jsonRegex = /