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
0d09ca96
Commit
0d09ca96
authored
Dec 15, 2022
by
Travis Fischer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: handle session expiration gracefully
parent
6b6de584
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
36 deletions
+50
-36
src/chatgpt-api-browser.ts
src/chatgpt-api-browser.ts
+50
-36
No files found.
src/chatgpt-api-browser.ts
View file @
0d09ca96
...
@@ -2,9 +2,8 @@ import delay from 'delay'
...
@@ -2,9 +2,8 @@ import delay from 'delay'
import
html2md
from
'
html-to-md
'
import
html2md
from
'
html-to-md
'
import
type
{
Browser
,
HTTPRequest
,
HTTPResponse
,
Page
}
from
'
puppeteer
'
import
type
{
Browser
,
HTTPRequest
,
HTTPResponse
,
Page
}
from
'
puppeteer
'
import
*
as
types
from
'
./types
'
import
{
getBrowser
,
getOpenAIAuth
}
from
'
./openai-auth
'
import
{
getBrowser
,
getOpenAIAuth
}
from
'
./openai-auth
'
import
{
isRelevantRequest
,
minimizePage
}
from
'
./utils
'
import
{
isRelevantRequest
,
m
aximizePage
,
m
inimizePage
}
from
'
./utils
'
export
class
ChatGPTAPIBrowser
{
export
class
ChatGPTAPIBrowser
{
protected
_markdown
:
boolean
protected
_markdown
:
boolean
...
@@ -103,7 +102,7 @@ export class ChatGPTAPIBrowser {
...
@@ -103,7 +102,7 @@ export class ChatGPTAPIBrowser {
return
false
return
false
}
}
//
await minimizePage(this._page)
await
minimizePage
(
this
.
_page
)
this
.
_page
.
on
(
'
request
'
,
this
.
_onRequest
.
bind
(
this
))
this
.
_page
.
on
(
'
request
'
,
this
.
_onRequest
.
bind
(
this
))
this
.
_page
.
on
(
'
response
'
,
this
.
_onResponse
.
bind
(
this
))
this
.
_page
.
on
(
'
response
'
,
this
.
_onResponse
.
bind
(
this
))
...
@@ -112,8 +111,6 @@ export class ChatGPTAPIBrowser {
...
@@ -112,8 +111,6 @@ export class ChatGPTAPIBrowser {
}
}
_onRequest
=
(
request
:
HTTPRequest
)
=>
{
_onRequest
=
(
request
:
HTTPRequest
)
=>
{
if
(
!
this
.
_debug
)
return
const
url
=
request
.
url
()
const
url
=
request
.
url
()
if
(
!
isRelevantRequest
(
url
))
{
if
(
!
isRelevantRequest
(
url
))
{
return
return
...
@@ -140,17 +137,17 @@ export class ChatGPTAPIBrowser {
...
@@ -140,17 +137,17 @@ export class ChatGPTAPIBrowser {
// }
// }
}
}
console
.
log
(
'
\n
request
'
,
{
if
(
this
.
_debug
)
{
url
,
console
.
log
(
'
\n
request
'
,
{
method
,
url
,
headers
:
request
.
headers
(),
method
,
body
headers
:
request
.
headers
(),
})
body
})
}
}
}
_onResponse
=
async
(
response
:
HTTPResponse
)
=>
{
_onResponse
=
async
(
response
:
HTTPResponse
)
=>
{
if
(
!
this
.
_debug
)
return
const
request
=
response
.
request
()
const
request
=
response
.
request
()
const
url
=
response
.
url
()
const
url
=
response
.
url
()
...
@@ -158,37 +155,54 @@ export class ChatGPTAPIBrowser {
...
@@ -158,37 +155,54 @@ export class ChatGPTAPIBrowser {
return
return
}
}
const
status
=
response
.
status
()
let
body
:
any
let
body
:
any
try
{
try
{
body
=
await
response
.
json
()
body
=
await
response
.
json
()
}
catch
(
_
)
{}
}
catch
(
_
)
{}
if
(
url
.
endsWith
(
'
/conversation
'
))
{
if
(
this
.
_debug
)
{
// const parser = createParser((event) => {
console
.
log
(
'
\n
response
'
,
{
// if (event.type === 'event') {
url
,
// onMessage(event.data)
ok
:
response
.
ok
(),
// }
status
,
// })
statusText
:
response
.
statusText
(),
// await response.buffer()
headers
:
response
.
headers
(),
// for await (const chunk of streamAsyncIterable(response.body)) {
body
,
// const str = new TextDecoder().decode(chunk)
request
:
{
// parser.feed(str)
method
:
request
.
method
(),
// }
headers
:
request
.
headers
(),
body
:
request
.
postData
()
}
})
}
}
console
.
log
(
'
\n
response
'
,
{
if
(
url
.
endsWith
(
'
/conversation
'
))
{
url
,
if
(
status
===
403
)
{
ok
:
response
.
ok
(),
await
this
.
handle403Error
()
status
:
response
.
status
(),
statusText
:
response
.
statusText
(),
headers
:
response
.
headers
(),
body
,
request
:
{
method
:
request
.
method
(),
headers
:
request
.
headers
(),
body
:
request
.
postData
()
}
}
})
}
else
if
(
url
.
endsWith
(
'
api/auth/session
'
))
{
if
(
status
===
403
)
{
await
this
.
handle403Error
()
}
}
}
async
handle403Error
()
{
console
.
log
(
`ChatGPT "
${
this
.
_email
}
" session expired; refreshing...`
)
try
{
await
maximizePage
(
this
.
_page
)
await
this
.
_page
.
reload
({
waitUntil
:
'
networkidle0
'
})
await
minimizePage
(
this
.
_page
)
}
catch
(
err
)
{
console
.
error
(
`ChatGPT "
${
this
.
_email
}
" error refreshing session`
,
err
.
toString
()
)
}
}
}
async
getIsAuthenticated
()
{
async
getIsAuthenticated
()
{
...
...
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