Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Remove redundant functions
Browse files Browse the repository at this point in the history
  • Loading branch information
terry3041 committed Dec 12, 2022
1 parent 6ae287b commit 3caf610
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ api3 = ChatGPT(session_token, proxy='http://proxy.example.com:8080') # specify
resp = api.send_message('Hello, world!')
print(resp['message'])

api.refresh_cookies() # refresh the cookies (if cloudflare bypass is not working
api.refresh_auth() # refresh the authorization token
api.refresh_auth() # refresh the authorization token & cf cookies
api.reset_conversation() # reset the conversation
```

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pyChatGPT"
version = "0.2.0"
version = "0.2.1"
authors = [
{ name="terry3041", email="[email protected]" },
]
Expand Down
50 changes: 24 additions & 26 deletions src/pyChatGPT/pyChatGPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,39 @@ def __init__(
self.session.proxies = self.proxies

self.session_token = session_token
self.refresh_cookies()

def refresh_cookies(self) -> None:
'''
Refresh the session cookies
'''
options = uc.ChromeOptions()

if self.proxy:
options.add_argument(f'--proxy-server={self.proxy}')
self.driver = uc.Chrome(options=options)
self.refresh_auth(init=True)

self.driver.get('https://chat.openai.com/')
self.headers['user-agent'] = self.driver.execute_script(
'return navigator.userAgent'
)
self.driver.add_cookie(
{'name': '__Secure-next-auth.session-token', 'value': self.session_token}
)
WebDriverWait(self.driver, 10).until(
EC.text_to_be_present_in_element((By.TAG_NAME, 'h1'), 'ChatGPT')
)
for cookie in self.driver.get_cookies():
self.session.cookies.set(cookie['name'], cookie['value'])
self.driver.close()
self.driver.quit()

def refresh_auth(self) -> None:
def refresh_auth(self, init: bool = False) -> None:
'''
Refresh the session's authorization
Parameters:
- init: (optional) Whether to initialize the session
'''
resp = self.session.get('https://chat.openai.com/api/auth/session')
if resp.status_code != 200:
raise ValueError(f'Status code {resp.status_code}: {resp.text}')
if init:
self.driver.get('https://chat.openai.com/api/auth/session')
self.headers['user-agent'] = self.driver.execute_script(
'return navigator.userAgent'
)
self.driver.add_cookie(
{
'name': '__Secure-next-auth.session-token',
'value': self.session_token,
}
)

data = resp.json()
self.driver.get('https://chat.openai.com/api/auth/session')
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, 'pre'))
)
for cookie in self.driver.get_cookies():
self.session.cookies.set(cookie['name'], cookie['value'])
resp = self.driver.find_element(By.TAG_NAME, 'pre').text
data = json.loads(resp)
if not data:
raise ValueError('Invalid session token')
access_token = data['accessToken']
Expand Down

0 comments on commit 3caf610

Please sign in to comment.