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] 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 = /