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
b8a3fe0d
Commit
b8a3fe0d
authored
Dec 14, 2022
by
Joel
Committed by
GitHub
Dec 14, 2022
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/improve-robustness' into feature/improve-robustness
parents
16d144ad
7ddac692
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
142 additions
and
25 deletions
+142
-25
demos/demo.ts
demos/demo.ts
+1
-1
src/chatgpt-api-browser.ts
src/chatgpt-api-browser.ts
+88
-18
src/openai-auth.ts
src/openai-auth.ts
+6
-6
src/utils.ts
src/utils.ts
+47
-0
No files found.
demos/demo.ts
View file @
b8a3fe0d
...
...
@@ -16,7 +16,7 @@ async function main() {
const
email
=
process
.
env
.
OPENAI_EMAIL
const
password
=
process
.
env
.
OPENAI_PASSWORD
const
api
=
new
ChatGPTAPIBrowser
({
email
,
password
})
const
api
=
new
ChatGPTAPIBrowser
({
email
,
password
,
debug
:
true
})
const
res
=
await
api
.
init
()
console
.
log
(
'
init result
'
,
res
)
...
...
src/chatgpt-api-browser.ts
View file @
b8a3fe0d
import
delay
from
'
delay
'
import
html2md
from
'
html-to-md
'
import
{
type
Browser
,
type
HTTPResponse
,
type
Page
}
from
'
puppeteer
'
import
type
{
Browser
,
HTTPRequest
,
HTTPResponse
,
Page
}
from
'
puppeteer
'
import
*
as
types
from
'
./types
'
import
{
getBrowser
,
getOpenAIAuth
}
from
'
./openai-auth
'
import
{
isRelevantRequest
,
minimizePage
}
from
'
./utils
'
export
class
ChatGPTAPIBrowser
{
protected
_markdown
:
boolean
...
...
@@ -102,25 +103,93 @@ export class ChatGPTAPIBrowser {
return
false
}
// this._page.on('response', this._onResponse.bind(this))
// await minimizePage(this._page)
this
.
_page
.
on
(
'
request
'
,
this
.
_onRequest
.
bind
(
this
))
this
.
_page
.
on
(
'
response
'
,
this
.
_onResponse
.
bind
(
this
))
return
true
}
// _onResponse = (response: HTTPResponse) => {
// const request = response.request()
// console.log('response', {
// url: response.url(),
// ok: response.ok(),
// status: response.status(),
// statusText: response.statusText(),
// headers: response.headers(),
// request: {
// method: request.method(),
// headers: request.headers()
// }
// })
// }
_onRequest
=
(
request
:
HTTPRequest
)
=>
{
if
(
!
this
.
_debug
)
return
const
url
=
request
.
url
()
if
(
!
isRelevantRequest
(
url
))
{
return
}
const
method
=
request
.
method
()
let
body
:
any
if
(
method
===
'
POST
'
)
{
body
=
request
.
postData
()
try
{
body
=
JSON
.
parse
(
body
)
}
catch
(
_
)
{}
// if (url.endsWith('/conversation') && typeof body === 'object') {
// const conversationBody: types.ConversationJSONBody = body
// const conversationId = conversationBody.conversation_id
// const parentMessageId = conversationBody.parent_message_id
// const messageId = conversationBody.messages?.[0]?.id
// const prompt = conversationBody.messages?.[0]?.content?.parts?.[0]
// // TODO: store this info for the current sendMessage request
// }
}
console
.
log
(
'
\n
request
'
,
{
url
,
method
,
headers
:
request
.
headers
(),
body
})
}
_onResponse
=
async
(
response
:
HTTPResponse
)
=>
{
if
(
!
this
.
_debug
)
return
const
request
=
response
.
request
()
const
url
=
response
.
url
()
if
(
!
isRelevantRequest
(
url
))
{
return
}
let
body
:
any
try
{
body
=
await
response
.
json
()
}
catch
(
_
)
{}
if
(
url
.
endsWith
(
'
/conversation
'
))
{
// const parser = createParser((event) => {
// if (event.type === 'event') {
// onMessage(event.data)
// }
// })
// await response.buffer()
// for await (const chunk of streamAsyncIterable(response.body)) {
// const str = new TextDecoder().decode(chunk)
// parser.feed(str)
// }
}
console
.
log
(
'
\n
response
'
,
{
url
,
ok
:
response
.
ok
(),
status
:
response
.
status
(),
statusText
:
response
.
statusText
(),
headers
:
response
.
headers
(),
body
,
request
:
{
method
:
request
.
method
(),
headers
:
request
.
headers
(),
body
:
request
.
postData
()
}
})
}
async
getIsAuthenticated
()
{
try
{
...
...
@@ -198,7 +267,7 @@ export class ChatGPTAPIBrowser {
const
lastMessage
=
await
this
.
getLastMessage
()
await
inputBox
.
click
()
await
inputBox
.
focus
()
const
paragraphs
=
message
.
split
(
'
\n
'
)
for
(
let
i
=
0
;
i
<
paragraphs
.
length
;
i
++
)
{
await
inputBox
.
type
(
paragraphs
[
i
],
{
delay
:
0
})
...
...
@@ -220,6 +289,7 @@ export class ChatGPTAPIBrowser {
newLastMessage
&&
lastMessage
?.
toLowerCase
()
!==
newLastMessage
?.
toLowerCase
()
)
{
await
delay
(
5000
)
return
newLastMessage
}
}
while
(
true
)
...
...
src/openai-auth.ts
View file @
b8a3fe0d
...
...
@@ -2,12 +2,12 @@ import * as fs from 'node:fs'
import
*
as
os
from
'
node:os
'
import
delay
from
'
delay
'
import
{
type
Browser
,
type
ElementHandle
,
type
Page
,
type
Protocol
,
type
PuppeteerLaunchOptions
import
type
{
Browser
,
ElementHandle
,
Page
,
Protocol
,
PuppeteerLaunchOptions
}
from
'
puppeteer
'
import
puppeteer
from
'
puppeteer-extra
'
import
RecaptchaPlugin
from
'
puppeteer-extra-plugin-recaptcha
'
...
...
src/utils.ts
View file @
b8a3fe0d
import
type
{
Page
}
from
'
puppeteer
'
import
{
remark
}
from
'
remark
'
import
stripMarkdown
from
'
strip-markdown
'
...
...
@@ -7,3 +8,49 @@ export function markdownToText(markdown?: string): string {
.
processSync
(
markdown
??
''
)
.
toString
()
}
export
async
function
minimizePage
(
page
:
Page
)
{
const
session
=
await
page
.
target
().
createCDPSession
()
const
goods
=
await
session
.
send
(
'
Browser.getWindowForTarget
'
)
const
{
windowId
}
=
goods
await
session
.
send
(
'
Browser.setWindowBounds
'
,
{
windowId
,
bounds
:
{
windowState
:
'
minimized
'
}
})
}
export
async
function
maximizePage
(
page
:
Page
)
{
const
session
=
await
page
.
target
().
createCDPSession
()
const
goods
=
await
session
.
send
(
'
Browser.getWindowForTarget
'
)
const
{
windowId
}
=
goods
await
session
.
send
(
'
Browser.setWindowBounds
'
,
{
windowId
,
bounds
:
{
windowState
:
'
normal
'
}
})
}
export
function
isRelevantRequest
(
url
:
string
):
boolean
{
let
pathname
try
{
const
parsedUrl
=
new
URL
(
url
)
pathname
=
parsedUrl
.
pathname
url
=
parsedUrl
.
toString
()
}
catch
(
_
)
{
return
false
}
if
(
!
url
.
startsWith
(
'
https://chat.openai.com
'
))
{
return
false
}
if
(
pathname
.
startsWith
(
'
/_next
'
))
{
return
false
}
if
(
pathname
.
endsWith
(
'
backend-api/moderations
'
))
{
return
false
}
return
true
}
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