fix: not signed问题

This commit is contained in:
葛胤池 2022-12-19 12:34:54 +08:00
parent 4df0addffb
commit 89dd029d0e

View file

@ -4,6 +4,7 @@ import StealthPlugin from 'puppeteer-extra-plugin-stealth'
import { getOpenAIAuth } from './openai-auth.js' import { getOpenAIAuth } from './openai-auth.js'
import delay from 'delay' import delay from 'delay'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
const chatUrl = 'https://chat.openai.com/chat' const chatUrl = 'https://chat.openai.com/chat'
let puppeteer = {} let puppeteer = {}
@ -126,9 +127,9 @@ export class ChatGPTPuppeteer extends Puppeteer {
} }
async init () { async init () {
if (this.inited) { // if (this.inited) {
return true // return true
} // }
try { try {
// this.browser = await getBrowser({ // this.browser = await getBrowser({
// captchaToken: this._captchaToken, // captchaToken: this._captchaToken,
@ -148,13 +149,22 @@ export class ChatGPTPuppeteer extends Puppeteer {
await this._page.goto(chatUrl, { await this._page.goto(chatUrl, {
waitUntil: 'networkidle2' waitUntil: 'networkidle2'
}) })
while ((await this._page.title()).toLowerCase().indexOf('moment') > -1) { try {
// if meet captcha while ((await this._page.title()).toLowerCase().indexOf('moment') > -1) {
// await this._page.solveRecaptchas() // if meet captcha
await delay(300) // await this._page.solveRecaptchas()
await delay(300)
}
} catch (e) {
// navigation后获取title会报错报错说明已经在navigation了正合我意。
}
try {
await this._page.waitForNavigation({ timeout: 3000 })
} catch (e) {
logger.error(e)
} }
await delay(300)
if (!await this.getIsAuthenticated()) { if (!await this.getIsAuthenticated()) {
await redis.del('CHATGPT:RAW_COOKIES')
logger.info('需要登录,准备进行自动化登录') logger.info('需要登录,准备进行自动化登录')
await getOpenAIAuth({ await getOpenAIAuth({
email: this._email, email: this._email,
@ -167,7 +177,6 @@ export class ChatGPTPuppeteer extends Puppeteer {
} else { } else {
logger.info('无需登录') logger.info('无需登录')
} }
this.inited = true
} catch (err) { } catch (err) {
if (this.browser) { if (this.browser) {
await this.browser.close() await this.browser.close()
@ -929,6 +938,7 @@ export async function browserPostEventStream (
} }
} }
} }
function hasBom (buffer) { function hasBom (buffer) {
return BOM.every( return BOM.every(
(charCode, index) => buffer.charCodeAt(index) === charCode (charCode, index) => buffer.charCodeAt(index) === charCode
@ -973,7 +983,7 @@ export async function browserPostEventStream (
const cancelablePromise = new Promise((resolve, reject) => { const cancelablePromise = new Promise((resolve, reject) => {
if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) { if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) {
throw new TypeError( throw new TypeError(
`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\`` `Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``
) )
} }
@ -1007,11 +1017,11 @@ export async function browserPostEventStream (
} }
const errorMessage = const errorMessage =
typeof message === 'string' typeof message === 'string'
? message ? message
: `Promise timed out after ${milliseconds} milliseconds` : `Promise timed out after ${milliseconds} milliseconds`
const timeoutError = const timeoutError =
message instanceof Error ? message : new TimeoutError(errorMessage) message instanceof Error ? message : new TimeoutError(errorMessage)
if (typeof promise.cancel === 'function') { if (typeof promise.cancel === 'function') {
promise.cancel() promise.cancel()