Ariel commited on
Commit
bf93dd7
·
unverified ·
1 Parent(s): 0db0b7b

Add: specify cookie file by parameter (#51)

Browse files
Files changed (2) hide show
  1. README.md +5 -1
  2. src/EdgeGPT.py +8 -6
README.md CHANGED
@@ -109,7 +109,11 @@ options:
109
  -----
110
 
111
  ### Developer demo
112
- Remember to set cookie file path: `export COOKIE_FILE=/path/to/cookies.json`
 
 
 
 
113
 
114
  Use Async for the best experience
115
 
 
109
  -----
110
 
111
  ### Developer demo
112
+ Remember to set cookie file path: `export COOKIE_FILE=/path/to/cookies.json`. You can also specify the path to `cookies.json` in the argument `cookiePath` like this:
113
+
114
+ ```python
115
+ bot = Chatbot(cookiePath='./cookie.json')
116
+ ```
117
 
118
  Use Async for the best experience
119
 
src/EdgeGPT.py CHANGED
@@ -103,7 +103,7 @@ class Conversation:
103
  Conversation API
104
  """
105
 
106
- def __init__(self) -> None:
107
  self.struct: dict = {
108
  "conversationId": None,
109
  "clientId": None,
@@ -111,9 +111,11 @@ class Conversation:
111
  "result": {"value": "Success", "message": None},
112
  }
113
  self.session = tls_client.Session(client_identifier="chrome_108")
114
- cookie_file = json.loads(
115
- open(os.environ.get("COOKIE_FILE"), encoding="utf-8").read(),
116
- )
 
 
117
  for cookie in cookie_file:
118
  self.session.cookies.set(cookie["name"], cookie["value"])
119
  url = "https://www.bing.com/turing/conversation/create"
@@ -211,8 +213,8 @@ class Chatbot:
211
  Combines everything to make it seamless
212
  """
213
 
214
- def __init__(self) -> None:
215
- self.chat_hub: ChatHub = ChatHub(Conversation())
216
 
217
  async def ask(self, prompt: str) -> dict:
218
  """
 
103
  Conversation API
104
  """
105
 
106
+ def __init__(self, cookiePath: str = '') -> None:
107
  self.struct: dict = {
108
  "conversationId": None,
109
  "clientId": None,
 
111
  "result": {"value": "Success", "message": None},
112
  }
113
  self.session = tls_client.Session(client_identifier="chrome_108")
114
+ if cookiePath == '':
115
+ f = open(os.environ.get("COOKIE_FILE"), encoding="utf-8").read()
116
+ else:
117
+ f = open(cookiePath, encoding='utf8').read()
118
+ cookie_file = json.loads(f)
119
  for cookie in cookie_file:
120
  self.session.cookies.set(cookie["name"], cookie["value"])
121
  url = "https://www.bing.com/turing/conversation/create"
 
213
  Combines everything to make it seamless
214
  """
215
 
216
+ def __init__(self, cookiePath: str = '') -> None:
217
+ self.chat_hub: ChatHub = ChatHub(Conversation(cookiePath))
218
 
219
  async def ask(self, prompt: str) -> dict:
220
  """