Raju Komati
commited on
Commit
•
a37920b
1
Parent(s):
1be1e44
updated quora module, added selenium to get cookie
Browse files- .gitignore +16 -0
- quora/__init__.py +350 -238
- quora/api.py +152 -106
- quora/mail.py +42 -42
- requirements.txt +3 -1
.gitignore
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Default ignored files
|
2 |
+
/shelf/
|
3 |
+
/workspace.xml
|
4 |
+
# Editor-based HTTP Client requests
|
5 |
+
/httpRequests/
|
6 |
+
# Datasource local storage ignored files
|
7 |
+
/dataSources/
|
8 |
+
/dataSources.local.xml
|
9 |
+
|
10 |
+
.idea/
|
11 |
+
|
12 |
+
*/__pycache__/
|
13 |
+
|
14 |
+
*.log
|
15 |
+
|
16 |
+
cookie.json
|
quora/__init__.py
CHANGED
@@ -1,28 +1,42 @@
|
|
1 |
-
|
2 |
-
from
|
3 |
-
from
|
4 |
-
from
|
5 |
-
from
|
6 |
-
from
|
7 |
-
from
|
8 |
-
from
|
9 |
-
from
|
10 |
-
|
11 |
-
|
12 |
-
from
|
13 |
-
from
|
14 |
-
from
|
15 |
-
from
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# from twocaptcha import TwoCaptcha
|
18 |
# solver = TwoCaptcha('72747bf24a9d89b4dcc1b24875efd358')
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def extract_formkey(html):
|
21 |
-
script_regex = r
|
22 |
-
script_text
|
23 |
-
key_regex
|
24 |
-
key_text
|
25 |
-
cipher_regex = r
|
26 |
cipher_pairs = findall(cipher_regex, script_text)
|
27 |
|
28 |
formkey_list = [""] * len(cipher_pairs)
|
@@ -33,42 +47,40 @@ def extract_formkey(html):
|
|
33 |
|
34 |
return formkey
|
35 |
|
|
|
36 |
class PoeResponse:
|
37 |
-
|
38 |
class Completion:
|
39 |
-
|
40 |
class Choices:
|
41 |
def __init__(self, choice: dict) -> None:
|
42 |
-
self.text
|
43 |
-
self.content
|
44 |
-
self.index
|
45 |
-
self.logprobs
|
46 |
-
self.finish_reason
|
47 |
-
|
48 |
def __repr__(self) -> str:
|
49 |
-
return f
|
50 |
|
51 |
def __init__(self, choices: dict) -> None:
|
52 |
self.choices = [self.Choices(choice) for choice in choices]
|
53 |
|
54 |
class Usage:
|
55 |
def __init__(self, usage_dict: dict) -> None:
|
56 |
-
self.prompt_tokens
|
57 |
-
self.completion_tokens
|
58 |
-
self.total_tokens
|
59 |
|
60 |
def __repr__(self):
|
61 |
-
return f
|
62 |
-
|
63 |
def __init__(self, response_dict: dict) -> None:
|
64 |
-
|
65 |
-
self.
|
66 |
-
self.
|
67 |
-
self.
|
68 |
-
self.
|
69 |
-
self.
|
70 |
-
self.
|
71 |
-
self.usage = self.Usage(response_dict['usage'])
|
72 |
|
73 |
def json(self) -> dict:
|
74 |
return self.response_dict
|
@@ -76,127 +88,140 @@ class PoeResponse:
|
|
76 |
|
77 |
class ModelResponse:
|
78 |
def __init__(self, json_response: dict) -> None:
|
79 |
-
self.id
|
80 |
-
self.name
|
81 |
-
self.limit
|
82 |
-
|
|
|
|
|
|
|
83 |
|
84 |
class Model:
|
85 |
def create(
|
86 |
token: str,
|
87 |
-
model: str =
|
88 |
-
system_prompt: str =
|
89 |
-
description: str =
|
90 |
-
handle: str = None
|
91 |
-
|
92 |
models = {
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
}
|
97 |
-
|
98 |
if not handle:
|
99 |
-
handle = f
|
100 |
-
|
101 |
client = Session()
|
102 |
-
client.cookies[
|
103 |
-
|
104 |
-
formkey
|
105 |
-
settings = client.get(
|
106 |
|
107 |
client.headers = {
|
108 |
-
"host"
|
109 |
-
"origin"
|
110 |
-
"referer"
|
111 |
-
"
|
112 |
-
"poe-
|
113 |
-
"
|
114 |
-
"
|
115 |
-
"
|
116 |
-
"sec-ch-ua"
|
117 |
-
"sec-ch-ua-
|
118 |
-
"
|
119 |
-
"
|
120 |
-
"sec-fetch-
|
121 |
-
"sec-fetch-
|
122 |
-
"
|
123 |
-
"accept"
|
124 |
-
"accept-
|
125 |
-
"accept-language" : "en-GB,en-US;q=0.9,en;q=0.8",
|
126 |
}
|
127 |
-
|
128 |
-
payload = dumps(
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
145 |
},
|
146 |
-
|
147 |
-
|
|
|
|
|
148 |
|
149 |
-
|
150 |
-
client.headers["poe-tag-id"] = md5(base_string.encode()).hexdigest()
|
151 |
-
|
152 |
-
response = client.post("https://poe.com/api/gql_POST", data = payload)
|
153 |
|
154 |
-
if not
|
155 |
-
raise Exception(
|
|
|
156 |
Bot creation Failed
|
157 |
!! Important !!
|
158 |
Bot creation was not enabled on this account
|
159 |
please use: quora.Account.create with enable_bot_creation set to True
|
160 |
-
|
161 |
-
|
|
|
162 |
return ModelResponse(response.json())
|
163 |
|
164 |
|
165 |
class Account:
|
166 |
-
def create(
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
|
|
|
|
|
|
|
|
171 |
|
172 |
-
mail_client
|
173 |
-
mail_address
|
174 |
|
175 |
-
if logging:
|
|
|
176 |
|
177 |
client.headers = {
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
|
|
194 |
}
|
195 |
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
token = reCaptchaV3('https://www.recaptcha.net/recaptcha/enterprise/anchor?ar=1&k=6LflhEElAAAAAI_ewVwRWI9hsyV4mbZnYAslSvlG&co=aHR0cHM6Ly9wb2UuY29tOjQ0Mw..&hl=en&v=4PnKmGB9wRHh1i04o7YUICeI&size=invisible&cb=bi6ivxoskyal')
|
200 |
# token = solver.recaptcha(sitekey='6LflhEElAAAAAI_ewVwRWI9hsyV4mbZnYAslSvlG',
|
201 |
# url = 'https://poe.com/login?redirect_url=%2F',
|
202 |
# version = 'v3',
|
@@ -204,132 +229,219 @@ class Account:
|
|
204 |
# invisible = 1,
|
205 |
# action = 'login',)['code']
|
206 |
|
207 |
-
payload = dumps(
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
|
|
|
|
|
|
|
|
213 |
},
|
214 |
-
|
215 |
-
|
|
|
|
|
216 |
|
217 |
-
base_string = payload + client.headers["poe-formkey"] + 'WpuLMiXEKKE98j56k'
|
218 |
-
client.headers["poe-tag-id"] = md5(base_string.encode()).hexdigest()
|
219 |
-
|
220 |
print(dumps(client.headers, indent=4))
|
221 |
-
|
222 |
-
response = client.post('https://poe.com/api/gql_POST', data=payload)
|
223 |
-
|
224 |
-
if 'automated_request_detected' in response.text:
|
225 |
-
print('please try using a proxy / wait for fix')
|
226 |
-
|
227 |
-
if 'Bad Request' in response.text:
|
228 |
-
if logging: print('bad request, retrying...' , response.json())
|
229 |
-
quit()
|
230 |
|
231 |
-
|
232 |
-
|
233 |
-
mail_content = mail_client.get_message()
|
234 |
-
mail_token = findall(r';">(\d{6,7})</div>', mail_content)[0]
|
235 |
|
236 |
-
if
|
|
|
237 |
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
},
|
245 |
-
|
246 |
-
|
|
|
|
|
247 |
|
248 |
-
|
249 |
-
|
|
|
250 |
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
|
|
256 |
return choice(cookies)
|
257 |
|
|
|
258 |
class StreamingCompletion:
|
259 |
def create(
|
260 |
-
model
|
261 |
-
custom_model
|
262 |
-
prompt: str =
|
263 |
-
token
|
|
|
|
|
264 |
|
265 |
-
models = {
|
266 |
-
'sage' : 'capybara',
|
267 |
-
'gpt-4' : 'beaver',
|
268 |
-
'claude-v1.2' : 'a2_2',
|
269 |
-
'claude-instant-v1.0' : 'a2',
|
270 |
-
'gpt-3.5-turbo' : 'chinchilla'
|
271 |
-
}
|
272 |
-
|
273 |
-
_model = models[model] if not custom_model else custom_model
|
274 |
-
|
275 |
client = PoeClient(token)
|
276 |
-
|
277 |
for chunk in client.send_message(_model, prompt):
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
|
|
|
|
|
|
294 |
}
|
295 |
-
|
|
|
296 |
|
297 |
class Completion:
|
298 |
def create(
|
299 |
-
model
|
300 |
-
custom_model
|
301 |
-
prompt: str =
|
302 |
-
token
|
303 |
-
|
304 |
models = {
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
}
|
311 |
-
|
312 |
_model = models[model] if not custom_model else custom_model
|
313 |
-
|
314 |
client = PoeClient(token)
|
315 |
-
|
316 |
for chunk in client.send_message(_model, prompt):
|
317 |
pass
|
318 |
-
|
319 |
-
return PoeResponse(
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from datetime import datetime
|
3 |
+
from hashlib import md5
|
4 |
+
from json import dumps
|
5 |
+
from pathlib import Path
|
6 |
+
from random import choice, choices, randint
|
7 |
+
from re import search, findall
|
8 |
+
from string import ascii_letters, digits
|
9 |
+
from urllib.parse import unquote
|
10 |
+
|
11 |
+
import selenium.webdriver.support.expected_conditions as EC
|
12 |
+
from pypasser import reCaptchaV3
|
13 |
+
from requests import Session
|
14 |
+
from selenium import webdriver
|
15 |
+
from selenium.webdriver.common.by import By
|
16 |
+
from selenium.webdriver.support.wait import WebDriverWait
|
17 |
+
from tls_client import Session as TLS
|
18 |
+
|
19 |
+
from quora.api import Client as PoeClient
|
20 |
+
from quora.mail import Emailnator
|
21 |
|
22 |
# from twocaptcha import TwoCaptcha
|
23 |
# solver = TwoCaptcha('72747bf24a9d89b4dcc1b24875efd358')
|
24 |
|
25 |
+
MODELS = {
|
26 |
+
"sage": "capybara",
|
27 |
+
"gpt-4": "beaver",
|
28 |
+
"claude-v1.2": "a2_2",
|
29 |
+
"claude-instant-v1.0": "a2",
|
30 |
+
"gpt-3.5-turbo": "chinchilla",
|
31 |
+
}
|
32 |
+
|
33 |
+
|
34 |
def extract_formkey(html):
|
35 |
+
script_regex = r"<script>if\(.+\)throw new Error;(.+)</script>"
|
36 |
+
script_text = search(script_regex, html).group(1)
|
37 |
+
key_regex = r'var .="([0-9a-f]+)",'
|
38 |
+
key_text = search(key_regex, script_text).group(1)
|
39 |
+
cipher_regex = r".\[(\d+)\]=.\[(\d+)\]"
|
40 |
cipher_pairs = findall(cipher_regex, script_text)
|
41 |
|
42 |
formkey_list = [""] * len(cipher_pairs)
|
|
|
47 |
|
48 |
return formkey
|
49 |
|
50 |
+
|
51 |
class PoeResponse:
|
|
|
52 |
class Completion:
|
|
|
53 |
class Choices:
|
54 |
def __init__(self, choice: dict) -> None:
|
55 |
+
self.text = choice["text"]
|
56 |
+
self.content = self.text.encode()
|
57 |
+
self.index = choice["index"]
|
58 |
+
self.logprobs = choice["logprobs"]
|
59 |
+
self.finish_reason = choice["finish_reason"]
|
60 |
+
|
61 |
def __repr__(self) -> str:
|
62 |
+
return f"""<__main__.APIResponse.Completion.Choices(\n text = {self.text.encode()},\n index = {self.index},\n logprobs = {self.logprobs},\n finish_reason = {self.finish_reason})object at 0x1337>"""
|
63 |
|
64 |
def __init__(self, choices: dict) -> None:
|
65 |
self.choices = [self.Choices(choice) for choice in choices]
|
66 |
|
67 |
class Usage:
|
68 |
def __init__(self, usage_dict: dict) -> None:
|
69 |
+
self.prompt_tokens = usage_dict["prompt_tokens"]
|
70 |
+
self.completion_tokens = usage_dict["completion_tokens"]
|
71 |
+
self.total_tokens = usage_dict["total_tokens"]
|
72 |
|
73 |
def __repr__(self):
|
74 |
+
return f"""<__main__.APIResponse.Usage(\n prompt_tokens = {self.prompt_tokens},\n completion_tokens = {self.completion_tokens},\n total_tokens = {self.total_tokens})object at 0x1337>"""
|
75 |
+
|
76 |
def __init__(self, response_dict: dict) -> None:
|
77 |
+
self.response_dict = response_dict
|
78 |
+
self.id = response_dict["id"]
|
79 |
+
self.object = response_dict["object"]
|
80 |
+
self.created = response_dict["created"]
|
81 |
+
self.model = response_dict["model"]
|
82 |
+
self.completion = self.Completion(response_dict["choices"])
|
83 |
+
self.usage = self.Usage(response_dict["usage"])
|
|
|
84 |
|
85 |
def json(self) -> dict:
|
86 |
return self.response_dict
|
|
|
88 |
|
89 |
class ModelResponse:
|
90 |
def __init__(self, json_response: dict) -> None:
|
91 |
+
self.id = json_response["data"]["poeBotCreate"]["bot"]["id"]
|
92 |
+
self.name = json_response["data"]["poeBotCreate"]["bot"]["displayName"]
|
93 |
+
self.limit = json_response["data"]["poeBotCreate"]["bot"]["messageLimit"][
|
94 |
+
"dailyLimit"
|
95 |
+
]
|
96 |
+
self.deleted = json_response["data"]["poeBotCreate"]["bot"]["deletionState"]
|
97 |
+
|
98 |
|
99 |
class Model:
|
100 |
def create(
|
101 |
token: str,
|
102 |
+
model: str = "gpt-3.5-turbo", # claude-instant
|
103 |
+
system_prompt: str = "You are ChatGPT a large language model developed by Openai. Answer as consisely as possible",
|
104 |
+
description: str = "gpt-3.5 language model from openai, skidded by poe.com",
|
105 |
+
handle: str = None,
|
106 |
+
) -> ModelResponse:
|
107 |
models = {
|
108 |
+
"gpt-3.5-turbo": "chinchilla",
|
109 |
+
"claude-instant-v1.0": "a2",
|
110 |
+
"gpt-4": "beaver",
|
111 |
}
|
112 |
+
|
113 |
if not handle:
|
114 |
+
handle = f"gptx{randint(1111111, 9999999)}"
|
115 |
+
|
116 |
client = Session()
|
117 |
+
client.cookies["p-b"] = token
|
118 |
+
|
119 |
+
formkey = extract_formkey(client.get("https://poe.com").text)
|
120 |
+
settings = client.get("https://poe.com/api/settings").json()
|
121 |
|
122 |
client.headers = {
|
123 |
+
"host": "poe.com",
|
124 |
+
"origin": "https://poe.com",
|
125 |
+
"referer": "https://poe.com/",
|
126 |
+
"poe-formkey": formkey,
|
127 |
+
"poe-tchannel": settings["tchannelData"]["channel"],
|
128 |
+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
|
129 |
+
"connection": "keep-alive",
|
130 |
+
"sec-ch-ua": '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
|
131 |
+
"sec-ch-ua-mobile": "?0",
|
132 |
+
"sec-ch-ua-platform": '"macOS"',
|
133 |
+
"content-type": "application/json",
|
134 |
+
"sec-fetch-site": "same-origin",
|
135 |
+
"sec-fetch-mode": "cors",
|
136 |
+
"sec-fetch-dest": "empty",
|
137 |
+
"accept": "*/*",
|
138 |
+
"accept-encoding": "gzip, deflate, br",
|
139 |
+
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
|
|
|
140 |
}
|
141 |
+
|
142 |
+
payload = dumps(
|
143 |
+
separators=(",", ":"),
|
144 |
+
obj={
|
145 |
+
"queryName": "CreateBotMain_poeBotCreate_Mutation",
|
146 |
+
"variables": {
|
147 |
+
"model": models[model],
|
148 |
+
"handle": handle,
|
149 |
+
"prompt": system_prompt,
|
150 |
+
"isPromptPublic": True,
|
151 |
+
"introduction": "",
|
152 |
+
"description": description,
|
153 |
+
"profilePictureUrl": "https://qph.fs.quoracdn.net/main-qimg-24e0b480dcd946e1cc6728802c5128b6",
|
154 |
+
"apiUrl": None,
|
155 |
+
"apiKey": "".join(choices(ascii_letters + digits, k=32)),
|
156 |
+
"isApiBot": False,
|
157 |
+
"hasLinkification": False,
|
158 |
+
"hasMarkdownRendering": False,
|
159 |
+
"hasSuggestedReplies": False,
|
160 |
+
"isPrivateBot": False,
|
161 |
+
},
|
162 |
+
"query": "mutation CreateBotMain_poeBotCreate_Mutation(\n $model: String!\n $handle: String!\n $prompt: String!\n $isPromptPublic: Boolean!\n $introduction: String!\n $description: String!\n $profilePictureUrl: String\n $apiUrl: String\n $apiKey: String\n $isApiBot: Boolean\n $hasLinkification: Boolean\n $hasMarkdownRendering: Boolean\n $hasSuggestedReplies: Boolean\n $isPrivateBot: Boolean\n) {\n poeBotCreate(model: $model, handle: $handle, promptPlaintext: $prompt, isPromptPublic: $isPromptPublic, introduction: $introduction, description: $description, profilePicture: $profilePictureUrl, apiUrl: $apiUrl, apiKey: $apiKey, isApiBot: $isApiBot, hasLinkification: $hasLinkification, hasMarkdownRendering: $hasMarkdownRendering, hasSuggestedReplies: $hasSuggestedReplies, isPrivateBot: $isPrivateBot) {\n status\n bot {\n id\n ...BotHeader_bot\n }\n }\n}\n\nfragment BotHeader_bot on Bot {\n displayName\n messageLimit {\n dailyLimit\n }\n ...BotImage_bot\n ...BotLink_bot\n ...IdAnnotation_node\n ...botHelpers_useViewerCanAccessPrivateBot\n ...botHelpers_useDeletion_bot\n}\n\nfragment BotImage_bot on Bot {\n displayName\n ...botHelpers_useDeletion_bot\n ...BotImage_useProfileImage_bot\n}\n\nfragment BotImage_useProfileImage_bot on Bot {\n image {\n __typename\n ... on LocalBotImage {\n localName\n }\n ... on UrlBotImage {\n url\n }\n }\n ...botHelpers_useDeletion_bot\n}\n\nfragment BotLink_bot on Bot {\n displayName\n}\n\nfragment IdAnnotation_node on Node {\n __isNode: __typename\n id\n}\n\nfragment botHelpers_useDeletion_bot on Bot {\n deletionState\n}\n\nfragment botHelpers_useViewerCanAccessPrivateBot on Bot {\n isPrivateBot\n viewerIsCreator\n}\n",
|
163 |
},
|
164 |
+
)
|
165 |
+
|
166 |
+
base_string = payload + client.headers["poe-formkey"] + "WpuLMiXEKKE98j56k"
|
167 |
+
client.headers["poe-tag-id"] = md5(base_string.encode()).hexdigest()
|
168 |
|
169 |
+
response = client.post("https://poe.com/api/gql_POST", data=payload)
|
|
|
|
|
|
|
170 |
|
171 |
+
if "success" not in response.text:
|
172 |
+
raise Exception(
|
173 |
+
"""
|
174 |
Bot creation Failed
|
175 |
!! Important !!
|
176 |
Bot creation was not enabled on this account
|
177 |
please use: quora.Account.create with enable_bot_creation set to True
|
178 |
+
"""
|
179 |
+
)
|
180 |
+
|
181 |
return ModelResponse(response.json())
|
182 |
|
183 |
|
184 |
class Account:
|
185 |
+
def create(
|
186 |
+
proxy: str | None = None,
|
187 |
+
logging: bool = False,
|
188 |
+
enable_bot_creation: bool = False,
|
189 |
+
):
|
190 |
+
client = TLS(client_identifier="chrome110")
|
191 |
+
client.proxies = (
|
192 |
+
{"http": f"http://{proxy}", "https": f"http://{proxy}"} if proxy else None
|
193 |
+
)
|
194 |
|
195 |
+
mail_client = Emailnator()
|
196 |
+
mail_address = mail_client.get_mail()
|
197 |
|
198 |
+
if logging:
|
199 |
+
print("email", mail_address)
|
200 |
|
201 |
client.headers = {
|
202 |
+
"authority": "poe.com",
|
203 |
+
"accept": "*/*",
|
204 |
+
"accept-language": "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
|
205 |
+
"content-type": "application/json",
|
206 |
+
"origin": "https://poe.com",
|
207 |
+
"poe-tag-id": "null",
|
208 |
+
"referer": "https://poe.com/login",
|
209 |
+
"sec-ch-ua": '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
|
210 |
+
"sec-ch-ua-mobile": "?0",
|
211 |
+
"sec-ch-ua-platform": '"macOS"',
|
212 |
+
"sec-fetch-dest": "empty",
|
213 |
+
"sec-fetch-mode": "cors",
|
214 |
+
"sec-fetch-site": "same-origin",
|
215 |
+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
|
216 |
+
"poe-formkey": extract_formkey(client.get("https://poe.com/login").text),
|
217 |
+
"poe-tchannel": client.get("https://poe.com/api/settings").json()[
|
218 |
+
"tchannelData"
|
219 |
+
]["channel"],
|
220 |
}
|
221 |
|
222 |
+
token = reCaptchaV3(
|
223 |
+
"https://www.recaptcha.net/recaptcha/enterprise/anchor?ar=1&k=6LflhEElAAAAAI_ewVwRWI9hsyV4mbZnYAslSvlG&co=aHR0cHM6Ly9wb2UuY29tOjQ0Mw..&hl=en&v=4PnKmGB9wRHh1i04o7YUICeI&size=invisible&cb=bi6ivxoskyal"
|
224 |
+
)
|
|
|
225 |
# token = solver.recaptcha(sitekey='6LflhEElAAAAAI_ewVwRWI9hsyV4mbZnYAslSvlG',
|
226 |
# url = 'https://poe.com/login?redirect_url=%2F',
|
227 |
# version = 'v3',
|
|
|
229 |
# invisible = 1,
|
230 |
# action = 'login',)['code']
|
231 |
|
232 |
+
payload = dumps(
|
233 |
+
separators=(",", ":"),
|
234 |
+
obj={
|
235 |
+
"queryName": "MainSignupLoginSection_sendVerificationCodeMutation_Mutation",
|
236 |
+
"variables": {
|
237 |
+
"emailAddress": mail_address,
|
238 |
+
"phoneNumber": None,
|
239 |
+
"recaptchaToken": token,
|
240 |
+
},
|
241 |
+
"query": "mutation MainSignupLoginSection_sendVerificationCodeMutation_Mutation(\n $emailAddress: String\n $phoneNumber: String\n $recaptchaToken: String\n) {\n sendVerificationCode(verificationReason: login, emailAddress: $emailAddress, phoneNumber: $phoneNumber, recaptchaToken: $recaptchaToken) {\n status\n errorMessage\n }\n}\n",
|
242 |
},
|
243 |
+
)
|
244 |
+
|
245 |
+
base_string = payload + client.headers["poe-formkey"] + "WpuLMiXEKKE98j56k"
|
246 |
+
client.headers["poe-tag-id"] = md5(base_string.encode()).hexdigest()
|
247 |
|
|
|
|
|
|
|
248 |
print(dumps(client.headers, indent=4))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
|
250 |
+
response = client.post("https://poe.com/api/gql_POST", data=payload)
|
|
|
|
|
|
|
251 |
|
252 |
+
if "automated_request_detected" in response.text:
|
253 |
+
print("please try using a proxy / wait for fix")
|
254 |
|
255 |
+
if "Bad Request" in response.text:
|
256 |
+
if logging:
|
257 |
+
print("bad request, retrying...", response.json())
|
258 |
+
quit()
|
259 |
+
|
260 |
+
if logging:
|
261 |
+
print("send_code", response.json())
|
262 |
+
|
263 |
+
mail_content = mail_client.get_message()
|
264 |
+
mail_token = findall(r';">(\d{6,7})</div>', mail_content)[0]
|
265 |
+
|
266 |
+
if logging:
|
267 |
+
print("code", mail_token)
|
268 |
+
|
269 |
+
payload = dumps(
|
270 |
+
separators=(",", ":"),
|
271 |
+
obj={
|
272 |
+
"queryName": "SignupOrLoginWithCodeSection_signupWithVerificationCodeMutation_Mutation",
|
273 |
+
"variables": {
|
274 |
+
"verificationCode": str(mail_token),
|
275 |
+
"emailAddress": mail_address,
|
276 |
+
"phoneNumber": None,
|
277 |
+
},
|
278 |
+
"query": "mutation SignupOrLoginWithCodeSection_signupWithVerificationCodeMutation_Mutation(\n $verificationCode: String!\n $emailAddress: String\n $phoneNumber: String\n) {\n signupWithVerificationCode(verificationCode: $verificationCode, emailAddress: $emailAddress, phoneNumber: $phoneNumber) {\n status\n errorMessage\n }\n}\n",
|
279 |
},
|
280 |
+
)
|
281 |
+
|
282 |
+
base_string = payload + client.headers["poe-formkey"] + "WpuLMiXEKKE98j56k"
|
283 |
+
client.headers["poe-tag-id"] = md5(base_string.encode()).hexdigest()
|
284 |
|
285 |
+
response = client.post("https://poe.com/api/gql_POST", data=payload)
|
286 |
+
if logging:
|
287 |
+
print("verify_code", response.json())
|
288 |
|
289 |
+
def get(self):
|
290 |
+
cookies = (
|
291 |
+
open(Path(__file__).resolve().parent / "cookies.txt", "r")
|
292 |
+
.read()
|
293 |
+
.splitlines()
|
294 |
+
)
|
295 |
return choice(cookies)
|
296 |
|
297 |
+
|
298 |
class StreamingCompletion:
|
299 |
def create(
|
300 |
+
model: str = "gpt-4",
|
301 |
+
custom_model: bool = None,
|
302 |
+
prompt: str = "hello world",
|
303 |
+
token: str = "",
|
304 |
+
):
|
305 |
+
_model = MODELS[model] if not custom_model else custom_model
|
306 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
client = PoeClient(token)
|
308 |
+
|
309 |
for chunk in client.send_message(_model, prompt):
|
310 |
+
yield PoeResponse(
|
311 |
+
{
|
312 |
+
"id": chunk["messageId"],
|
313 |
+
"object": "text_completion",
|
314 |
+
"created": chunk["creationTime"],
|
315 |
+
"model": _model,
|
316 |
+
"choices": [
|
317 |
+
{
|
318 |
+
"text": chunk["text_new"],
|
319 |
+
"index": 0,
|
320 |
+
"logprobs": None,
|
321 |
+
"finish_reason": "stop",
|
322 |
+
}
|
323 |
+
],
|
324 |
+
"usage": {
|
325 |
+
"prompt_tokens": len(prompt),
|
326 |
+
"completion_tokens": len(chunk["text_new"]),
|
327 |
+
"total_tokens": len(prompt) + len(chunk["text_new"]),
|
328 |
+
},
|
329 |
}
|
330 |
+
)
|
331 |
+
|
332 |
|
333 |
class Completion:
|
334 |
def create(
|
335 |
+
model: str = "gpt-4",
|
336 |
+
custom_model: str = None,
|
337 |
+
prompt: str = "hello world",
|
338 |
+
token: str = "",
|
339 |
+
):
|
340 |
models = {
|
341 |
+
"sage": "capybara",
|
342 |
+
"gpt-4": "beaver",
|
343 |
+
"claude-v1.2": "a2_2",
|
344 |
+
"claude-instant-v1.0": "a2",
|
345 |
+
"gpt-3.5-turbo": "chinchilla",
|
346 |
}
|
347 |
+
|
348 |
_model = models[model] if not custom_model else custom_model
|
349 |
+
|
350 |
client = PoeClient(token)
|
351 |
+
|
352 |
for chunk in client.send_message(_model, prompt):
|
353 |
pass
|
354 |
+
|
355 |
+
return PoeResponse(
|
356 |
+
{
|
357 |
+
"id": chunk["messageId"],
|
358 |
+
"object": "text_completion",
|
359 |
+
"created": chunk["creationTime"],
|
360 |
+
"model": _model,
|
361 |
+
"choices": [
|
362 |
+
{
|
363 |
+
"text": chunk["text"],
|
364 |
+
"index": 0,
|
365 |
+
"logprobs": None,
|
366 |
+
"finish_reason": "stop",
|
367 |
+
}
|
368 |
+
],
|
369 |
+
"usage": {
|
370 |
+
"prompt_tokens": len(prompt),
|
371 |
+
"completion_tokens": len(chunk["text"]),
|
372 |
+
"total_tokens": len(prompt) + len(chunk["text"]),
|
373 |
+
},
|
374 |
+
}
|
375 |
+
)
|
376 |
+
|
377 |
+
|
378 |
+
class Poe:
|
379 |
+
def __init__(self, model: str = "sage"):
|
380 |
+
self.cookie = self.__load_cookie()
|
381 |
+
self.model = MODELS[model]
|
382 |
+
self.client = PoeClient(self.cookie)
|
383 |
+
|
384 |
+
def __load_cookie(self) -> str:
|
385 |
+
if (cookie_file := Path("cookie.json")).exists():
|
386 |
+
with cookie_file.open() as fp:
|
387 |
+
cookie = json.load(fp)
|
388 |
+
if datetime.fromtimestamp(cookie["expiry"]) < datetime.now():
|
389 |
+
cookie = self.__register_and_get_cookie()
|
390 |
+
else:
|
391 |
+
print("Loading the cookie from file")
|
392 |
+
else:
|
393 |
+
cookie = self.__register_and_get_cookie()
|
394 |
+
|
395 |
+
return unquote(cookie["value"])
|
396 |
+
|
397 |
+
@classmethod
|
398 |
+
def __register_and_get_cookie(cls) -> dict:
|
399 |
+
mail_client = Emailnator()
|
400 |
+
mail_address = mail_client.get_mail()
|
401 |
+
|
402 |
+
print(mail_address)
|
403 |
+
options = webdriver.FirefoxOptions()
|
404 |
+
options.add_argument("-headless")
|
405 |
+
driver = webdriver.Firefox(options=options)
|
406 |
+
|
407 |
+
driver.get("https://www.poe.com")
|
408 |
+
|
409 |
+
# clicking use email button
|
410 |
+
driver.find_element(By.XPATH, '//button[contains(text(), "Use email")]').click()
|
411 |
+
|
412 |
+
email = WebDriverWait(driver, 30).until(
|
413 |
+
EC.presence_of_element_located((By.XPATH, '//input[@type="email"]'))
|
414 |
+
)
|
415 |
+
email.send_keys(mail_address)
|
416 |
+
driver.find_element(By.XPATH, '//button[text()="Go"]').click()
|
417 |
+
|
418 |
+
code = findall(r';">(\d{6,7})</div>', mail_client.get_message())[0]
|
419 |
+
print(code)
|
420 |
+
|
421 |
+
verification_code = WebDriverWait(driver, 30).until(
|
422 |
+
EC.presence_of_element_located((By.XPATH, '//input[@placeholder="Code"]'))
|
423 |
+
)
|
424 |
+
verification_code.send_keys(code)
|
425 |
+
verify_button = EC.presence_of_element_located(
|
426 |
+
(By.XPATH, '//button[text()="Verify"]')
|
427 |
+
)
|
428 |
+
login_button = EC.presence_of_element_located(
|
429 |
+
(By.XPATH, '//button[text()="Log In"]')
|
430 |
+
)
|
431 |
+
|
432 |
+
WebDriverWait(driver, 30).until(EC.any_of(verify_button, login_button)).click()
|
433 |
+
|
434 |
+
cookie = driver.get_cookie("p-b")
|
435 |
+
|
436 |
+
with open("cookie.json", "w") as fw:
|
437 |
+
json.dump(cookie, fw)
|
438 |
+
|
439 |
+
driver.close()
|
440 |
+
return cookie
|
441 |
+
|
442 |
+
def chat(self, message: str):
|
443 |
+
response = None
|
444 |
+
for chunk in self.client.send_message(self.model, message):
|
445 |
+
response = chunk["text"]
|
446 |
+
|
447 |
+
return response
|
quora/api.py
CHANGED
@@ -55,10 +55,7 @@ def load_queries():
|
|
55 |
|
56 |
|
57 |
def generate_payload(query_name, variables):
|
58 |
-
return {
|
59 |
-
"query": queries[query_name],
|
60 |
-
"variables": variables
|
61 |
-
}
|
62 |
|
63 |
|
64 |
def request_with_retries(method, *args, **kwargs):
|
@@ -69,7 +66,8 @@ def request_with_retries(method, *args, **kwargs):
|
|
69 |
if r.status_code == 200:
|
70 |
return r
|
71 |
logger.warn(
|
72 |
-
f"Server returned a status code of {r.status_code} while downloading {url}. Retrying ({i+1}/{attempts})..."
|
|
|
73 |
|
74 |
raise RuntimeError(f"Failed to download {url} too many times.")
|
75 |
|
@@ -84,15 +82,13 @@ class Client:
|
|
84 |
self.proxy = proxy
|
85 |
self.session = requests.Session()
|
86 |
self.adapter = requests.adapters.HTTPAdapter(
|
87 |
-
pool_connections=100, pool_maxsize=100
|
|
|
88 |
self.session.mount("http://", self.adapter)
|
89 |
self.session.mount("https://", self.adapter)
|
90 |
|
91 |
if proxy:
|
92 |
-
self.session.proxies = {
|
93 |
-
"http": self.proxy,
|
94 |
-
"https": self.proxy
|
95 |
-
}
|
96 |
logger.info(f"Proxy enabled: {self.proxy}")
|
97 |
|
98 |
self.active_messages = {}
|
@@ -124,11 +120,11 @@ class Client:
|
|
124 |
self.subscribe()
|
125 |
|
126 |
def extract_formkey(self, html):
|
127 |
-
script_regex = r
|
128 |
script_text = re.search(script_regex, html).group(1)
|
129 |
key_regex = r'var .="([0-9a-f]+)",'
|
130 |
key_text = re.search(key_regex, script_text).group(1)
|
131 |
-
cipher_regex = r
|
132 |
cipher_pairs = re.findall(cipher_regex, script_text)
|
133 |
|
134 |
formkey_list = [""] * len(cipher_pairs)
|
@@ -143,7 +139,9 @@ class Client:
|
|
143 |
logger.info("Downloading next_data...")
|
144 |
|
145 |
r = request_with_retries(self.session.get, self.home_url)
|
146 |
-
json_regex =
|
|
|
|
|
147 |
json_text = re.search(json_regex, r.text).group(1)
|
148 |
next_data = json.loads(json_text)
|
149 |
|
@@ -181,8 +179,7 @@ class Client:
|
|
181 |
bots[chat_data["defaultBotObject"]["nickname"]] = chat_data
|
182 |
|
183 |
for bot in bot_list:
|
184 |
-
thread = threading.Thread(
|
185 |
-
target=get_bot_thread, args=(bot,), daemon=True)
|
186 |
threads.append(thread)
|
187 |
|
188 |
for thread in threads:
|
@@ -216,50 +213,59 @@ class Client:
|
|
216 |
if channel is None:
|
217 |
channel = self.channel
|
218 |
query = f'?min_seq={channel["minSeq"]}&channel={channel["channel"]}&hash={channel["channelHash"]}'
|
219 |
-
return
|
|
|
|
|
|
|
220 |
|
221 |
def send_query(self, query_name, variables):
|
222 |
for i in range(20):
|
223 |
json_data = generate_payload(query_name, variables)
|
224 |
payload = json.dumps(json_data, separators=(",", ":"))
|
225 |
|
226 |
-
base_string =
|
227 |
-
self.gql_headers["poe-formkey"] + "WpuLMiXEKKE98j56k"
|
|
|
228 |
|
229 |
headers = {
|
230 |
"content-type": "application/json",
|
231 |
-
"poe-tag-id": hashlib.md5(base_string.encode()).hexdigest()
|
232 |
}
|
233 |
headers = {**self.gql_headers, **headers}
|
234 |
|
235 |
r = request_with_retries(
|
236 |
-
self.session.post, self.gql_url, data=payload, headers=headers
|
|
|
237 |
|
238 |
data = r.json()
|
239 |
if data["data"] == None:
|
240 |
logger.warn(
|
241 |
-
f'{query_name} returned an error: {data["errors"][0]["message"]} | Retrying ({i+1}/20)'
|
|
|
242 |
time.sleep(2)
|
243 |
continue
|
244 |
|
245 |
return r.json()
|
246 |
|
247 |
-
raise RuntimeError(f
|
248 |
|
249 |
def subscribe(self):
|
250 |
logger.info("Subscribing to mutations")
|
251 |
-
result = self.send_query(
|
252 |
-
"
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
|
|
|
|
|
|
263 |
|
264 |
def ws_run_thread(self):
|
265 |
kwargs = {}
|
@@ -268,7 +274,7 @@ class Client:
|
|
268 |
kwargs = {
|
269 |
"proxy_type": proxy_parsed.scheme,
|
270 |
"http_proxy_host": proxy_parsed.hostname,
|
271 |
-
"http_proxy_port": proxy_parsed.port
|
272 |
}
|
273 |
|
274 |
self.ws.run_forever(**kwargs)
|
@@ -281,7 +287,7 @@ class Client:
|
|
281 |
on_message=self.on_message,
|
282 |
on_open=self.on_ws_connect,
|
283 |
on_error=self.on_ws_error,
|
284 |
-
on_close=self.on_ws_close
|
285 |
)
|
286 |
t = threading.Thread(target=self.ws_run_thread, daemon=True)
|
287 |
t.start()
|
@@ -299,7 +305,8 @@ class Client:
|
|
299 |
def on_ws_close(self, ws, close_status_code, close_message):
|
300 |
self.ws_connected = False
|
301 |
logger.warn(
|
302 |
-
f"Websocket closed with status {close_status_code}: {close_message}"
|
|
|
303 |
|
304 |
def on_ws_error(self, ws, error):
|
305 |
self.disconnect_ws()
|
@@ -326,7 +333,11 @@ class Client:
|
|
326 |
return
|
327 |
|
328 |
# indicate that the response id is tied to the human message id
|
329 |
-
elif
|
|
|
|
|
|
|
|
|
330 |
self.active_messages[key] = message["messageId"]
|
331 |
self.message_queues[key].put(message)
|
332 |
return
|
@@ -352,13 +363,16 @@ class Client:
|
|
352 |
self.setup_connection()
|
353 |
self.connect_ws()
|
354 |
|
355 |
-
message_data = self.send_query(
|
356 |
-
"
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
|
|
|
|
|
|
362 |
del self.active_messages["pending"]
|
363 |
|
364 |
if not message_data["data"]["messageEdgeCreate"]["message"]:
|
@@ -368,7 +382,8 @@ class Client:
|
|
368 |
human_message_id = human_message["node"]["messageId"]
|
369 |
except TypeError:
|
370 |
raise RuntimeError(
|
371 |
-
f"An unknown error occurred. Raw response data: {message_data}"
|
|
|
372 |
|
373 |
# indicate that the current message is waiting for a response
|
374 |
self.active_messages[human_message_id] = None
|
@@ -378,8 +393,7 @@ class Client:
|
|
378 |
message_id = None
|
379 |
while True:
|
380 |
try:
|
381 |
-
message = self.message_queues[human_message_id].get(
|
382 |
-
timeout=timeout)
|
383 |
except queue.Empty:
|
384 |
del self.active_messages[human_message_id]
|
385 |
del self.message_queues[human_message_id]
|
@@ -393,7 +407,7 @@ class Client:
|
|
393 |
continue
|
394 |
|
395 |
# update info about response
|
396 |
-
message["text_new"] = message["text"][len(last_text):]
|
397 |
last_text = message["text"]
|
398 |
message_id = message["messageId"]
|
399 |
|
@@ -404,9 +418,9 @@ class Client:
|
|
404 |
|
405 |
def send_chat_break(self, chatbot):
|
406 |
logger.info(f"Sending chat break to {chatbot}")
|
407 |
-
result = self.send_query(
|
408 |
-
"chatId": self.bots[chatbot]["chatId"]
|
409 |
-
|
410 |
return result["data"]["messageBreakCreate"]["message"]
|
411 |
|
412 |
def get_message_history(self, chatbot, count=25, cursor=None):
|
@@ -423,23 +437,24 @@ class Client:
|
|
423 |
|
424 |
cursor = str(cursor)
|
425 |
if count > 50:
|
426 |
-
messages =
|
427 |
-
chatbot, count=50, cursor=cursor) + messages
|
|
|
428 |
while count > 0:
|
429 |
count -= 50
|
430 |
new_cursor = messages[0]["cursor"]
|
431 |
new_messages = self.get_message_history(
|
432 |
-
chatbot, min(50, count), cursor=new_cursor
|
|
|
433 |
messages = new_messages + messages
|
434 |
return messages
|
435 |
elif count <= 0:
|
436 |
return messages
|
437 |
|
438 |
-
result = self.send_query(
|
439 |
-
"
|
440 |
-
"cursor": cursor,
|
441 |
-
|
442 |
-
})
|
443 |
query_messages = result["data"]["node"]["messagesConnection"]["edges"]
|
444 |
messages = query_messages + messages
|
445 |
return messages
|
@@ -449,9 +464,7 @@ class Client:
|
|
449 |
if not type(message_ids) is list:
|
450 |
message_ids = [int(message_ids)]
|
451 |
|
452 |
-
result = self.send_query("DeleteMessageMutation", {
|
453 |
-
"messageIds": message_ids
|
454 |
-
})
|
455 |
|
456 |
def purge_conversation(self, chatbot, count=-1):
|
457 |
logger.info(f"Purging messages from {chatbot}")
|
@@ -471,60 +484,93 @@ class Client:
|
|
471 |
last_messages = self.get_message_history(chatbot, count=50)[::-1]
|
472 |
logger.info(f"No more messages left to delete.")
|
473 |
|
474 |
-
def create_bot(
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
"
|
493 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
494 |
|
495 |
data = result["data"]["poeBotCreate"]
|
496 |
if data["status"] != "success":
|
497 |
raise RuntimeError(
|
498 |
-
f"Poe returned an error while trying to create a bot: {data['status']}"
|
|
|
499 |
self.get_bots()
|
500 |
return data
|
501 |
|
502 |
-
def edit_bot(
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
"
|
521 |
-
|
522 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
523 |
|
524 |
data = result["data"]["poeBotEdit"]
|
525 |
if data["status"] != "success":
|
526 |
raise RuntimeError(
|
527 |
-
f"Poe returned an error while trying to edit a bot: {data['status']}"
|
|
|
528 |
self.get_bots()
|
529 |
return data
|
530 |
|
|
|
55 |
|
56 |
|
57 |
def generate_payload(query_name, variables):
|
58 |
+
return {"query": queries[query_name], "variables": variables}
|
|
|
|
|
|
|
59 |
|
60 |
|
61 |
def request_with_retries(method, *args, **kwargs):
|
|
|
66 |
if r.status_code == 200:
|
67 |
return r
|
68 |
logger.warn(
|
69 |
+
f"Server returned a status code of {r.status_code} while downloading {url}. Retrying ({i+1}/{attempts})..."
|
70 |
+
)
|
71 |
|
72 |
raise RuntimeError(f"Failed to download {url} too many times.")
|
73 |
|
|
|
82 |
self.proxy = proxy
|
83 |
self.session = requests.Session()
|
84 |
self.adapter = requests.adapters.HTTPAdapter(
|
85 |
+
pool_connections=100, pool_maxsize=100
|
86 |
+
)
|
87 |
self.session.mount("http://", self.adapter)
|
88 |
self.session.mount("https://", self.adapter)
|
89 |
|
90 |
if proxy:
|
91 |
+
self.session.proxies = {"http": self.proxy, "https": self.proxy}
|
|
|
|
|
|
|
92 |
logger.info(f"Proxy enabled: {self.proxy}")
|
93 |
|
94 |
self.active_messages = {}
|
|
|
120 |
self.subscribe()
|
121 |
|
122 |
def extract_formkey(self, html):
|
123 |
+
script_regex = r"<script>if\(.+\)throw new Error;(.+)</script>"
|
124 |
script_text = re.search(script_regex, html).group(1)
|
125 |
key_regex = r'var .="([0-9a-f]+)",'
|
126 |
key_text = re.search(key_regex, script_text).group(1)
|
127 |
+
cipher_regex = r".\[(\d+)\]=.\[(\d+)\]"
|
128 |
cipher_pairs = re.findall(cipher_regex, script_text)
|
129 |
|
130 |
formkey_list = [""] * len(cipher_pairs)
|
|
|
139 |
logger.info("Downloading next_data...")
|
140 |
|
141 |
r = request_with_retries(self.session.get, self.home_url)
|
142 |
+
json_regex = (
|
143 |
+
r'<script id="__NEXT_DATA__" type="application\/json">(.+?)</script>'
|
144 |
+
)
|
145 |
json_text = re.search(json_regex, r.text).group(1)
|
146 |
next_data = json.loads(json_text)
|
147 |
|
|
|
179 |
bots[chat_data["defaultBotObject"]["nickname"]] = chat_data
|
180 |
|
181 |
for bot in bot_list:
|
182 |
+
thread = threading.Thread(target=get_bot_thread, args=(bot,), daemon=True)
|
|
|
183 |
threads.append(thread)
|
184 |
|
185 |
for thread in threads:
|
|
|
213 |
if channel is None:
|
214 |
channel = self.channel
|
215 |
query = f'?min_seq={channel["minSeq"]}&channel={channel["channel"]}&hash={channel["channelHash"]}'
|
216 |
+
return (
|
217 |
+
f'wss://{self.ws_domain}.tch.{channel["baseHost"]}/up/{channel["boxName"]}/updates'
|
218 |
+
+ query
|
219 |
+
)
|
220 |
|
221 |
def send_query(self, query_name, variables):
|
222 |
for i in range(20):
|
223 |
json_data = generate_payload(query_name, variables)
|
224 |
payload = json.dumps(json_data, separators=(",", ":"))
|
225 |
|
226 |
+
base_string = (
|
227 |
+
payload + self.gql_headers["poe-formkey"] + "WpuLMiXEKKE98j56k"
|
228 |
+
)
|
229 |
|
230 |
headers = {
|
231 |
"content-type": "application/json",
|
232 |
+
"poe-tag-id": hashlib.md5(base_string.encode()).hexdigest(),
|
233 |
}
|
234 |
headers = {**self.gql_headers, **headers}
|
235 |
|
236 |
r = request_with_retries(
|
237 |
+
self.session.post, self.gql_url, data=payload, headers=headers
|
238 |
+
)
|
239 |
|
240 |
data = r.json()
|
241 |
if data["data"] == None:
|
242 |
logger.warn(
|
243 |
+
f'{query_name} returned an error: {data["errors"][0]["message"]} | Retrying ({i+1}/20)'
|
244 |
+
)
|
245 |
time.sleep(2)
|
246 |
continue
|
247 |
|
248 |
return r.json()
|
249 |
|
250 |
+
raise RuntimeError(f"{query_name} failed too many times.")
|
251 |
|
252 |
def subscribe(self):
|
253 |
logger.info("Subscribing to mutations")
|
254 |
+
result = self.send_query(
|
255 |
+
"SubscriptionsMutation",
|
256 |
+
{
|
257 |
+
"subscriptions": [
|
258 |
+
{
|
259 |
+
"subscriptionName": "messageAdded",
|
260 |
+
"query": queries["MessageAddedSubscription"],
|
261 |
+
},
|
262 |
+
{
|
263 |
+
"subscriptionName": "viewerStateUpdated",
|
264 |
+
"query": queries["ViewerStateUpdatedSubscription"],
|
265 |
+
},
|
266 |
+
]
|
267 |
+
},
|
268 |
+
)
|
269 |
|
270 |
def ws_run_thread(self):
|
271 |
kwargs = {}
|
|
|
274 |
kwargs = {
|
275 |
"proxy_type": proxy_parsed.scheme,
|
276 |
"http_proxy_host": proxy_parsed.hostname,
|
277 |
+
"http_proxy_port": proxy_parsed.port,
|
278 |
}
|
279 |
|
280 |
self.ws.run_forever(**kwargs)
|
|
|
287 |
on_message=self.on_message,
|
288 |
on_open=self.on_ws_connect,
|
289 |
on_error=self.on_ws_error,
|
290 |
+
on_close=self.on_ws_close,
|
291 |
)
|
292 |
t = threading.Thread(target=self.ws_run_thread, daemon=True)
|
293 |
t.start()
|
|
|
305 |
def on_ws_close(self, ws, close_status_code, close_message):
|
306 |
self.ws_connected = False
|
307 |
logger.warn(
|
308 |
+
f"Websocket closed with status {close_status_code}: {close_message}"
|
309 |
+
)
|
310 |
|
311 |
def on_ws_error(self, ws, error):
|
312 |
self.disconnect_ws()
|
|
|
333 |
return
|
334 |
|
335 |
# indicate that the response id is tied to the human message id
|
336 |
+
elif (
|
337 |
+
key != "pending"
|
338 |
+
and value == None
|
339 |
+
and message["state"] != "complete"
|
340 |
+
):
|
341 |
self.active_messages[key] = message["messageId"]
|
342 |
self.message_queues[key].put(message)
|
343 |
return
|
|
|
363 |
self.setup_connection()
|
364 |
self.connect_ws()
|
365 |
|
366 |
+
message_data = self.send_query(
|
367 |
+
"SendMessageMutation",
|
368 |
+
{
|
369 |
+
"bot": chatbot,
|
370 |
+
"query": message,
|
371 |
+
"chatId": self.bots[chatbot]["chatId"],
|
372 |
+
"source": None,
|
373 |
+
"withChatBreak": with_chat_break,
|
374 |
+
},
|
375 |
+
)
|
376 |
del self.active_messages["pending"]
|
377 |
|
378 |
if not message_data["data"]["messageEdgeCreate"]["message"]:
|
|
|
382 |
human_message_id = human_message["node"]["messageId"]
|
383 |
except TypeError:
|
384 |
raise RuntimeError(
|
385 |
+
f"An unknown error occurred. Raw response data: {message_data}"
|
386 |
+
)
|
387 |
|
388 |
# indicate that the current message is waiting for a response
|
389 |
self.active_messages[human_message_id] = None
|
|
|
393 |
message_id = None
|
394 |
while True:
|
395 |
try:
|
396 |
+
message = self.message_queues[human_message_id].get(timeout=timeout)
|
|
|
397 |
except queue.Empty:
|
398 |
del self.active_messages[human_message_id]
|
399 |
del self.message_queues[human_message_id]
|
|
|
407 |
continue
|
408 |
|
409 |
# update info about response
|
410 |
+
message["text_new"] = message["text"][len(last_text) :]
|
411 |
last_text = message["text"]
|
412 |
message_id = message["messageId"]
|
413 |
|
|
|
418 |
|
419 |
def send_chat_break(self, chatbot):
|
420 |
logger.info(f"Sending chat break to {chatbot}")
|
421 |
+
result = self.send_query(
|
422 |
+
"AddMessageBreakMutation", {"chatId": self.bots[chatbot]["chatId"]}
|
423 |
+
)
|
424 |
return result["data"]["messageBreakCreate"]["message"]
|
425 |
|
426 |
def get_message_history(self, chatbot, count=25, cursor=None):
|
|
|
437 |
|
438 |
cursor = str(cursor)
|
439 |
if count > 50:
|
440 |
+
messages = (
|
441 |
+
self.get_message_history(chatbot, count=50, cursor=cursor) + messages
|
442 |
+
)
|
443 |
while count > 0:
|
444 |
count -= 50
|
445 |
new_cursor = messages[0]["cursor"]
|
446 |
new_messages = self.get_message_history(
|
447 |
+
chatbot, min(50, count), cursor=new_cursor
|
448 |
+
)
|
449 |
messages = new_messages + messages
|
450 |
return messages
|
451 |
elif count <= 0:
|
452 |
return messages
|
453 |
|
454 |
+
result = self.send_query(
|
455 |
+
"ChatListPaginationQuery",
|
456 |
+
{"count": count, "cursor": cursor, "id": self.bots[chatbot]["id"]},
|
457 |
+
)
|
|
|
458 |
query_messages = result["data"]["node"]["messagesConnection"]["edges"]
|
459 |
messages = query_messages + messages
|
460 |
return messages
|
|
|
464 |
if not type(message_ids) is list:
|
465 |
message_ids = [int(message_ids)]
|
466 |
|
467 |
+
result = self.send_query("DeleteMessageMutation", {"messageIds": message_ids})
|
|
|
|
|
468 |
|
469 |
def purge_conversation(self, chatbot, count=-1):
|
470 |
logger.info(f"Purging messages from {chatbot}")
|
|
|
484 |
last_messages = self.get_message_history(chatbot, count=50)[::-1]
|
485 |
logger.info(f"No more messages left to delete.")
|
486 |
|
487 |
+
def create_bot(
|
488 |
+
self,
|
489 |
+
handle,
|
490 |
+
prompt="",
|
491 |
+
base_model="chinchilla",
|
492 |
+
description="",
|
493 |
+
intro_message="",
|
494 |
+
api_key=None,
|
495 |
+
api_bot=False,
|
496 |
+
api_url=None,
|
497 |
+
prompt_public=True,
|
498 |
+
pfp_url=None,
|
499 |
+
linkification=False,
|
500 |
+
markdown_rendering=True,
|
501 |
+
suggested_replies=False,
|
502 |
+
private=False,
|
503 |
+
):
|
504 |
+
result = self.send_query(
|
505 |
+
"PoeBotCreateMutation",
|
506 |
+
{
|
507 |
+
"model": base_model,
|
508 |
+
"handle": handle,
|
509 |
+
"prompt": prompt,
|
510 |
+
"isPromptPublic": prompt_public,
|
511 |
+
"introduction": intro_message,
|
512 |
+
"description": description,
|
513 |
+
"profilePictureUrl": pfp_url,
|
514 |
+
"apiUrl": api_url,
|
515 |
+
"apiKey": api_key,
|
516 |
+
"isApiBot": api_bot,
|
517 |
+
"hasLinkification": linkification,
|
518 |
+
"hasMarkdownRendering": markdown_rendering,
|
519 |
+
"hasSuggestedReplies": suggested_replies,
|
520 |
+
"isPrivateBot": private,
|
521 |
+
},
|
522 |
+
)
|
523 |
|
524 |
data = result["data"]["poeBotCreate"]
|
525 |
if data["status"] != "success":
|
526 |
raise RuntimeError(
|
527 |
+
f"Poe returned an error while trying to create a bot: {data['status']}"
|
528 |
+
)
|
529 |
self.get_bots()
|
530 |
return data
|
531 |
|
532 |
+
def edit_bot(
|
533 |
+
self,
|
534 |
+
bot_id,
|
535 |
+
handle,
|
536 |
+
prompt="",
|
537 |
+
base_model="chinchilla",
|
538 |
+
description="",
|
539 |
+
intro_message="",
|
540 |
+
api_key=None,
|
541 |
+
api_url=None,
|
542 |
+
private=False,
|
543 |
+
prompt_public=True,
|
544 |
+
pfp_url=None,
|
545 |
+
linkification=False,
|
546 |
+
markdown_rendering=True,
|
547 |
+
suggested_replies=False,
|
548 |
+
):
|
549 |
+
result = self.send_query(
|
550 |
+
"PoeBotEditMutation",
|
551 |
+
{
|
552 |
+
"baseBot": base_model,
|
553 |
+
"botId": bot_id,
|
554 |
+
"handle": handle,
|
555 |
+
"prompt": prompt,
|
556 |
+
"isPromptPublic": prompt_public,
|
557 |
+
"introduction": intro_message,
|
558 |
+
"description": description,
|
559 |
+
"profilePictureUrl": pfp_url,
|
560 |
+
"apiUrl": api_url,
|
561 |
+
"apiKey": api_key,
|
562 |
+
"hasLinkification": linkification,
|
563 |
+
"hasMarkdownRendering": markdown_rendering,
|
564 |
+
"hasSuggestedReplies": suggested_replies,
|
565 |
+
"isPrivateBot": private,
|
566 |
+
},
|
567 |
+
)
|
568 |
|
569 |
data = result["data"]["poeBotEdit"]
|
570 |
if data["status"] != "success":
|
571 |
raise RuntimeError(
|
572 |
+
f"Poe returned an error while trying to edit a bot: {data['status']}"
|
573 |
+
)
|
574 |
self.get_bots()
|
575 |
return data
|
576 |
|
quora/mail.py
CHANGED
@@ -1,66 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from requests import Session
|
2 |
-
|
3 |
-
from re import search, findall
|
4 |
-
from json import loads
|
5 |
|
6 |
class Emailnator:
|
7 |
def __init__(self) -> None:
|
8 |
self.client = Session()
|
9 |
-
self.client.get(
|
10 |
self.cookies = self.client.cookies.get_dict()
|
11 |
|
12 |
self.client.headers = {
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
}
|
19 |
-
|
20 |
self.email = None
|
21 |
-
|
22 |
def get_mail(self):
|
23 |
-
response = self.client.post(
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
31 |
self.email = loads(response.text)["email"][0]
|
32 |
return self.email
|
33 |
-
|
34 |
def get_message(self):
|
35 |
print("waiting for code...")
|
36 |
-
|
37 |
while True:
|
38 |
sleep(2)
|
39 |
-
mail_token = self.client.post(
|
40 |
-
json
|
41 |
-
|
|
|
42 |
mail_token = loads(mail_token.text)["messageData"]
|
43 |
-
|
44 |
if len(mail_token) == 2:
|
45 |
print(mail_token[1]["messageID"])
|
46 |
break
|
47 |
-
|
48 |
-
mail_context = self.client.post('https://www.emailnator.com/message-list', json = {
|
49 |
-
'email' : self.email,
|
50 |
-
'messageID': mail_token[1]["messageID"],
|
51 |
-
})
|
52 |
-
|
53 |
-
return mail_context.text
|
54 |
-
|
55 |
-
# mail_client = Emailnator()
|
56 |
-
# mail_adress = mail_client.get_mail()
|
57 |
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
# print(mail_content)
|
63 |
-
|
64 |
-
# code = findall(r';">(\d{6,7})</div>', mail_content)[0]
|
65 |
-
# print(code)
|
66 |
|
|
|
|
|
|
1 |
+
from json import loads
|
2 |
+
from re import findall
|
3 |
+
from time import sleep
|
4 |
+
|
5 |
+
from fake_useragent import UserAgent
|
6 |
from requests import Session
|
7 |
+
|
|
|
|
|
8 |
|
9 |
class Emailnator:
|
10 |
def __init__(self) -> None:
|
11 |
self.client = Session()
|
12 |
+
self.client.get("https://www.emailnator.com/", timeout=6)
|
13 |
self.cookies = self.client.cookies.get_dict()
|
14 |
|
15 |
self.client.headers = {
|
16 |
+
"authority": "www.emailnator.com",
|
17 |
+
"origin": "https://www.emailnator.com",
|
18 |
+
"referer": "https://www.emailnator.com/",
|
19 |
+
"user-agent": UserAgent().random,
|
20 |
+
"x-xsrf-token": self.client.cookies.get("XSRF-TOKEN")[:-3] + "=",
|
21 |
}
|
22 |
+
|
23 |
self.email = None
|
24 |
+
|
25 |
def get_mail(self):
|
26 |
+
response = self.client.post(
|
27 |
+
"https://www.emailnator.com/generate-email",
|
28 |
+
json={
|
29 |
+
"email": [
|
30 |
+
"domain",
|
31 |
+
"plusGmail",
|
32 |
+
"dotGmail",
|
33 |
+
]
|
34 |
+
},
|
35 |
+
)
|
36 |
+
|
37 |
self.email = loads(response.text)["email"][0]
|
38 |
return self.email
|
39 |
+
|
40 |
def get_message(self):
|
41 |
print("waiting for code...")
|
42 |
+
|
43 |
while True:
|
44 |
sleep(2)
|
45 |
+
mail_token = self.client.post(
|
46 |
+
"https://www.emailnator.com/message-list", json={"email": self.email}
|
47 |
+
)
|
48 |
+
|
49 |
mail_token = loads(mail_token.text)["messageData"]
|
50 |
+
|
51 |
if len(mail_token) == 2:
|
52 |
print(mail_token[1]["messageID"])
|
53 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
mail_context = self.client.post(
|
56 |
+
"https://www.emailnator.com/message-list",
|
57 |
+
json={
|
58 |
+
"email": self.email,
|
59 |
+
"messageID": mail_token[1]["messageID"],
|
60 |
+
},
|
61 |
+
)
|
62 |
|
63 |
+
return mail_context.text
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
def get_verification_code(self):
|
66 |
+
return findall(r';">(\d{6,7})</div>', self.get_message())[0]
|
requirements.txt
CHANGED
@@ -4,4 +4,6 @@ tls-client
|
|
4 |
pypasser
|
5 |
names
|
6 |
colorama
|
7 |
-
curl_cffi
|
|
|
|
|
|
4 |
pypasser
|
5 |
names
|
6 |
colorama
|
7 |
+
curl_cffi
|
8 |
+
selenium
|
9 |
+
fake-useragent
|