Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
Chatgpt Puppeteer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
Chatgpt Puppeteer
Commits
fdb60f63
Commit
fdb60f63
authored
Dec 15, 2022
by
Travis Fischer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: minor cleanups
parent
1f1fc664
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
77 additions
and
66 deletions
+77
-66
src/openai-auth.ts
src/openai-auth.ts
+77
-66
No files found.
src/openai-auth.ts
View file @
fdb60f63
...
...
@@ -2,13 +2,7 @@ import * as fs from 'node:fs'
import
*
as
os
from
'
node:os
'
import
delay
from
'
delay
'
import
type
{
Browser
,
ElementHandle
,
Page
,
Protocol
,
PuppeteerLaunchOptions
}
from
'
puppeteer
'
import
type
{
Browser
,
Page
,
Protocol
,
PuppeteerLaunchOptions
}
from
'
puppeteer
'
import
puppeteer
from
'
puppeteer-extra
'
import
RecaptchaPlugin
from
'
puppeteer-extra-plugin-recaptcha
'
import
StealthPlugin
from
'
puppeteer-extra-plugin-stealth
'
...
...
@@ -51,8 +45,6 @@ export async function getOpenAIAuth({
browser
,
page
,
timeoutMs
=
2
*
60
*
1000
,
// TODO: temporary for testing...
// timeoutMs = 60 * 60 * 1000,
isGoogleLogin
=
false
,
captchaToken
=
process
.
env
.
CAPTCHA_TOKEN
}:
{
...
...
@@ -93,15 +85,18 @@ export async function getOpenAIAuth({
// login as well (optional)
if
(
email
&&
password
)
{
await
page
.
waitForSelector
(
'
#__next .btn-primary
'
,
{
timeout
:
timeoutMs
})
await
waitForConditionOrAtCapacity
(
page
,
()
=>
page
.
waitForSelector
(
'
#__next .btn-primary
'
,
{
timeout
:
timeoutMs
})
)
await
delay
(
500
)
// click login button and wait for navigation to finish
await
Promise
.
all
([
// click login button
page
.
click
(
'
#__next .btn-primary
'
),
page
.
waitForNavigation
({
waitUntil
:
'
networkidle0
'
})
}),
page
.
click
(
'
#__next .btn-primary
'
)
])
await
checkForChatGPTAtCapacity
(
page
)
...
...
@@ -125,9 +120,9 @@ export async function getOpenAIAuth({
await
delay
(
100
)
if
(
hasRecaptchaPlugin
)
{
console
.
log
(
'
solveRecaptchas()
'
)
//
console.log('solveRecaptchas()')
const
res
=
await
page
.
solveRecaptchas
()
console
.
log
(
'
solveRecaptchas result
'
,
res
)
//
console.log('solveRecaptchas result', res)
}
await
page
.
click
(
'
button[type="submit"]
'
)
...
...
@@ -137,50 +132,16 @@ export async function getOpenAIAuth({
}
await
Promise
.
all
([
new
Promise
<
void
>
((
resolve
,
reject
)
=>
{
let
resolved
=
false
async
function
waitForCapacityText
()
{
if
(
resolved
)
{
return
}
try
{
await
checkForChatGPTAtCapacity
(
page
)
if
(
!
resolved
)
{
setTimeout
(
waitForCapacityText
,
500
)
}
}
catch
(
err
)
{
if
(
!
resolved
)
{
resolved
=
true
return
reject
(
err
)
}
}
}
page
.
waitForNavigation
({
waitUntil
:
'
networkidle0
'
})
.
then
(()
=>
{
if
(
!
resolved
)
{
resolved
=
true
resolve
()
}
})
.
catch
((
err
)
=>
{
if
(
!
resolved
)
{
resolved
=
true
reject
(
err
)
}
})
setTimeout
(
waitForCapacityText
,
500
)
}),
waitForConditionOrAtCapacity
(
page
,
()
=>
page
.
waitForNavigation
({
waitUntil
:
'
networkidle0
'
})
),
submitP
()
])
}
else
{
await
delay
(
2000
)
await
checkForChatGPTAtCapacity
(
page
)
}
const
pageCookies
=
await
page
.
cookies
()
...
...
@@ -227,7 +188,7 @@ export async function getBrowser(
if
(
captchaToken
&&
!
hasRecaptchaPlugin
)
{
hasRecaptchaPlugin
=
true
console
.
log
(
'
use captcha
'
,
captchaToken
)
//
console.log('use captcha', captchaToken)
puppeteer
.
use
(
RecaptchaPlugin
({
...
...
@@ -275,17 +236,18 @@ export const defaultChromeExecutablePath = (): string => {
}
async
function
checkForChatGPTAtCapacity
(
page
:
Page
)
{
let
res
:
ElementHandle
<
Node
>
[]
// console.log('checkForChatGPTAtCapacity', page.url())
let
res
:
any
[]
try
{
// res = await page.$('[role="alert"]')
res
=
await
page
.
$x
(
"
//div[contains(., 'ChatGPT is at capacity')]
"
)
console
.
log
(
'
capacity
'
,
res
)
if
(
!
res
?.
length
)
{
res
=
await
page
.
$x
(
"
//div[contains(., 'at capacity right now')]
"
)
console
.
log
(
'
capacity2
'
,
res
)
}
// console.log('capacity1', els)
// if (els?.length) {
// res = await Promise.all(
// els.map((a) => a.evaluate((el) => el.textContent))
// )
// console.log('capacity2', res)
// }
}
catch
(
err
)
{
// ignore errors likely due to navigation
}
...
...
@@ -296,3 +258,52 @@ async function checkForChatGPTAtCapacity(page: Page) {
throw
error
}
}
async
function
waitForConditionOrAtCapacity
(
page
:
Page
,
condition
:
()
=>
Promise
<
any
>
,
opts
:
{
pollingIntervalMs
?:
number
}
=
{}
)
{
const
{
pollingIntervalMs
=
500
}
=
opts
return
new
Promise
<
void
>
((
resolve
,
reject
)
=>
{
let
resolved
=
false
async
function
waitForCapacityText
()
{
if
(
resolved
)
{
return
}
try
{
await
checkForChatGPTAtCapacity
(
page
)
if
(
!
resolved
)
{
setTimeout
(
waitForCapacityText
,
pollingIntervalMs
)
}
}
catch
(
err
)
{
if
(
!
resolved
)
{
resolved
=
true
return
reject
(
err
)
}
}
}
condition
()
.
then
(()
=>
{
if
(
!
resolved
)
{
resolved
=
true
resolve
()
}
})
.
catch
((
err
)
=>
{
if
(
!
resolved
)
{
resolved
=
true
reject
(
err
)
}
})
setTimeout
(
waitForCapacityText
,
pollingIntervalMs
)
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment