返回提交历史
Modified
quora/README.md
+1
-1
Modified
quora/__init__.py
+233
-222
Modified
quora/api.py
+22
-55
XFEstudio/gpt4free
refactored the code and updated Poe class to accept keyword arguments to specify driver type
90e7797a
代码差异
3 个文件
+256
-278
@@ -59,7 +59,7 @@ from quora import Poe
59
59
60
60
# available models: ['Sage', 'GPT-4', 'Claude+', 'Claude-instant', 'ChatGPT', 'Dragonfly', 'NeevaAI']
61
61
62
poe = Poe(model='ChatGPT')
62
poe = Poe(model='ChatGPT', driver='firefox', cookie_path='cookie.json')
63
63
poe.chat('who won the football world cup most?')
64
64
65
65
# new bot creation
@@ -6,13 +6,14 @@ from pathlib import Path
6
6
from random import choice, choices, randint
7
7
from re import search, findall
8
8
from string import ascii_letters, digits
9
from typing import Optional
9
from typing import Optional, Union
10
10
from urllib.parse import unquote
11
11
12
12
import selenium.webdriver.support.expected_conditions as EC
13
from fake_useragent import UserAgent
13
14
from pypasser import reCaptchaV3
14
15
from requests import Session
15
from selenium import webdriver
16
from selenium.webdriver import Firefox, Chrome, FirefoxOptions, ChromeOptions
16
17
from selenium.webdriver.common.by import By
17
18
from selenium.webdriver.support.wait import WebDriverWait
18
19
from tls_client import Session as TLS
@@ -20,33 +21,51 @@ from tls_client import Session as TLS
20
21
from quora.api import Client as PoeClient
21
22
from quora.mail import Emailnator
22
23
24
CHROME_DRIVER_URL = 'https://chromedriver.storage.googleapis.com'
25
FIREFOX_DRIVER_URL = 'https://api.github.com/repos/mozilla/geckodriver/releases'
26
27
SELENIUM_WEB_DRIVER_ERROR_MSG = b'''The error message you are receiving is due to the `geckodriver` executable not
28
being found in your system\'s PATH. To resolve this issue, you need to download the geckodriver and add its location
29
to your system\'s PATH.\n\nHere are the steps to resolve the issue:\n\n1. Download the geckodriver for your platform
30
(Windows, macOS, or Linux) from the following link: https://github.com/mozilla/geckodriver/releases\n\n2. Extract the
31
downloaded archive and locate the geckodriver executable.\n\n3. Add the geckodriver executable to your system\'s
32
PATH.\n\nFor macOS and Linux:\n\n- Open a terminal window.\n- Move the geckodriver executable to a directory that is
33
already in your PATH, or create a new directory and add it to your PATH:\n\n```bash\n# Example: Move geckodriver to
34
/usr/local/bin\nmv /path/to/your/geckodriver /usr/local/bin\n```\n\n- If you created a new directory, add it to your
35
PATH:\n\n```bash\n# Example: Add a new directory to PATH\nexport PATH=$PATH:/path/to/your/directory\n```\n\nFor
36
Windows:\n\n- Right-click on "My Computer" or "This PC" and select "Properties".\n- Click on "Advanced system
37
settings".\n- Click on the "Environment Variables" button.\n- In the "System variables" section, find the "Path"
38
variable, select it, and click "Edit".\n- Click "New" and add the path to the directory containing the geckodriver
39
executable.\n\nAfter adding the geckodriver to your PATH, restart your terminal or command prompt and try running
40
your script again. The error should be resolved.'''
41
23
42
# from twocaptcha import TwoCaptcha
24
43
# solver = TwoCaptcha('72747bf24a9d89b4dcc1b24875efd358')
25
44
26
45
MODELS = {
27
"Sage": "capybara",
28
"GPT-4": "beaver",
29
"Claude+": "a2_2",
30
"Claude-instant": "a2",
31
"ChatGPT": "chinchilla",
32
"Dragonfly": "nutria",
33
"NeevaAI": "hutia",
46
'Sage': 'capybara',
47
'GPT-4': 'beaver',
48
'Claude+': 'a2_2',
49
'Claude-instant': 'a2',
50
'ChatGPT': 'chinchilla',
51
'Dragonfly': 'nutria',
52
'NeevaAI': 'hutia',
34
53
}
35
54
36
55
37
56
def extract_formkey(html):
38
script_regex = r"<script>if\(.+\)throw new Error;(.+)</script>"
57
script_regex = r'<script>if\(.+\)throw new Error;(.+)</script>'
39
58
script_text = search(script_regex, html).group(1)
40
59
key_regex = r'var .="([0-9a-f]+)",'
41
60
key_text = search(key_regex, script_text).group(1)
42
cipher_regex = r".\[(\d+)\]=.\[(\d+)\]"
61
cipher_regex = r'.\[(\d+)\]=.\[(\d+)\]'
43
62
cipher_pairs = findall(cipher_regex, script_text)
44
63
45
formkey_list = [""] * len(cipher_pairs)
64
formkey_list = [''] * len(cipher_pairs)
46
65
for pair in cipher_pairs:
47
66
formkey_index, key_index = map(int, pair)
48
67
formkey_list[formkey_index] = key_text[key_index]
49
formkey = "".join(formkey_list)
68
formkey = ''.join(formkey_list)
50
69
51
70
return formkey
52
71
@@ -55,35 +74,35 @@ class PoeResponse:
55
74
class Completion:
56
75
class Choices:
57
76
def __init__(self, choice: dict) -> None:
58
self.text = choice["text"]
77
self.text = choice['text']
59
78
self.content = self.text.encode()
60
self.index = choice["index"]
61
self.logprobs = choice["logprobs"]
62
self.finish_reason = choice["finish_reason"]
79
self.index = choice['index']
80
self.logprobs = choice['logprobs']
81
self.finish_reason = choice['finish_reason']
63
82
64
83
def __repr__(self) -> str:
65
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>"""
84
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>'''
66
85
67
86
def __init__(self, choices: dict) -> None:
68
87
self.choices = [self.Choices(choice) for choice in choices]
69
88
70
89
class Usage:
71
90
def __init__(self, usage_dict: dict) -> None:
72
self.prompt_tokens = usage_dict["prompt_tokens"]
73
self.completion_tokens = usage_dict["completion_tokens"]
74
self.total_tokens = usage_dict["total_tokens"]
91
self.prompt_tokens = usage_dict['prompt_tokens']
92
self.completion_tokens = usage_dict['completion_tokens']
93
self.total_tokens = usage_dict['total_tokens']
75
94
76
95
def __repr__(self):
77
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>"""
96
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>'''
78
97
79
98
def __init__(self, response_dict: dict) -> None:
80
99
self.response_dict = response_dict
81
self.id = response_dict["id"]
82
self.object = response_dict["object"]
83
self.created = response_dict["created"]
84
self.model = response_dict["model"]
85
self.completion = self.Completion(response_dict["choices"])
86
self.usage = self.Usage(response_dict["usage"])
100
self.id = response_dict['id']
101
self.object = response_dict['object']
102
self.created = response_dict['created']
103
self.model = response_dict['model']
104
self.completion = self.Completion(response_dict['choices'])
105
self.usage = self.Usage(response_dict['usage'])
87
106
88
107
def json(self) -> dict:
89
108
return self.response_dict
@@ -91,139 +110,135 @@ class PoeResponse:
91
110
92
111
class ModelResponse:
93
112
def __init__(self, json_response: dict) -> None:
94
self.id = json_response["data"]["poeBotCreate"]["bot"]["id"]
95
self.name = json_response["data"]["poeBotCreate"]["bot"]["displayName"]
96
self.limit = json_response["data"]["poeBotCreate"]["bot"]["messageLimit"][
97
"dailyLimit"
98
]
99
self.deleted = json_response["data"]["poeBotCreate"]["bot"]["deletionState"]
113
self.id = json_response['data']['poeBotCreate']['bot']['id']
114
self.name = json_response['data']['poeBotCreate']['bot']['displayName']
115
self.limit = json_response['data']['poeBotCreate']['bot']['messageLimit']['dailyLimit']
116
self.deleted = json_response['data']['poeBotCreate']['bot']['deletionState']
100
117
101
118
102
119
class Model:
120
@staticmethod
103
121
def create(
104
122
token: str,
105
model: str = "gpt-3.5-turbo", # claude-instant
106
system_prompt: str = "You are ChatGPT a large language model developed by Openai. Answer as consisely as possible",
107
description: str = "gpt-3.5 language model from openai, skidded by poe.com",
123
model: str = 'gpt-3.5-turbo', # claude-instant
124
system_prompt: str = 'You are ChatGPT a large language model developed by Openai. Answer as consisely as possible',
125
description: str = 'gpt-3.5 language model from openai, skidded by poe.com',
108
126
handle: str = None,
109
127
) -> ModelResponse:
110
128
models = {
111
"gpt-3.5-turbo": "chinchilla",
112
"claude-instant-v1.0": "a2",
113
"gpt-4": "beaver",
129
'gpt-3.5-turbo': 'chinchilla',
130
'claude-instant-v1.0': 'a2',
131
'gpt-4': 'beaver',
114
132
}
115
133
116
134
if not handle:
117
handle = f"gptx{randint(1111111, 9999999)}"
135
handle = f'gptx{randint(1111111, 9999999)}'
118
136
119
137
client = Session()
120
client.cookies["p-b"] = token
138
client.cookies['p-b'] = token
121
139
122
formkey = extract_formkey(client.get("https://poe.com").text)
123
settings = client.get("https://poe.com/api/settings").json()
140
formkey = extract_formkey(client.get('https://poe.com').text)
141
settings = client.get('https://poe.com/api/settings').json()
124
142
125
143
client.headers = {
126
"host": "poe.com",
127
"origin": "https://poe.com",
128
"referer": "https://poe.com/",
129
"poe-formkey": formkey,
130
"poe-tchannel": settings["tchannelData"]["channel"],
131
"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",
132
"connection": "keep-alive",
133
"sec-ch-ua": '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
134
"sec-ch-ua-mobile": "?0",
135
"sec-ch-ua-platform": '"macOS"',
136
"content-type": "application/json",
137
"sec-fetch-site": "same-origin",
138
"sec-fetch-mode": "cors",
139
"sec-fetch-dest": "empty",
140
"accept": "*/*",
141
"accept-encoding": "gzip, deflate, br",
142
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
144
'host': 'poe.com',
145
'origin': 'https://poe.com',
146
'referer': 'https://poe.com/',
147
'poe-formkey': formkey,
148
'poe-tchannel': settings['tchannelData']['channel'],
149
'user-agent': UserAgent().random,
150
'connection': 'keep-alive',
151
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
152
'sec-ch-ua-mobile': '?0',
153
'sec-ch-ua-platform': '"macOS"',
154
'content-type': 'application/json',
155
'sec-fetch-site': 'same-origin',
156
'sec-fetch-mode': 'cors',
157
'sec-fetch-dest': 'empty',
158
'accept': '*/*',
159
'accept-encoding': 'gzip, deflate, br',
160
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
143
161
}
144
162
145
163
payload = dumps(
146
separators=(",", ":"),
164
separators=(',', ':'),
147
165
obj={
148
"queryName": "CreateBotMain_poeBotCreate_Mutation",
149
"variables": {
150
"model": models[model],
151
"handle": handle,
152
"prompt": system_prompt,
153
"isPromptPublic": True,
154
"introduction": "",
155
"description": description,
156
"profilePictureUrl": "https://qph.fs.quoracdn.net/main-qimg-24e0b480dcd946e1cc6728802c5128b6",
157
"apiUrl": None,
158
"apiKey": "".join(choices(ascii_letters + digits, k=32)),
159
"isApiBot": False,
160
"hasLinkification": False,
161
"hasMarkdownRendering": False,
162
"hasSuggestedReplies": False,
163
"isPrivateBot": False,
166
'queryName': 'CreateBotMain_poeBotCreate_Mutation',
167
'variables': {
168
'model': models[model],
169
'handle': handle,
170
'prompt': system_prompt,
171
'isPromptPublic': True,
172
'introduction': '',
173
'description': description,
174
'profilePictureUrl': 'https://qph.fs.quoracdn.net/main-qimg-24e0b480dcd946e1cc6728802c5128b6',
175
'apiUrl': None,
176
'apiKey': ''.join(choices(ascii_letters + digits, k=32)),
177
'isApiBot': False,
178
'hasLinkification': False,
179
'hasMarkdownRendering': False,
180
'hasSuggestedReplies': False,
181
'isPrivateBot': False,
164
182
},
165
"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",
183
'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',
166
184
},
167
185
)
168
186
169
base_string = payload + client.headers["poe-formkey"] + "WpuLMiXEKKE98j56k"
170
client.headers["poe-tag-id"] = md5(base_string.encode()).hexdigest()
187
base_string = payload + client.headers['poe-formkey'] + 'WpuLMiXEKKE98j56k'
188
client.headers['poe-tag-id'] = md5(base_string.encode()).hexdigest()
171
189
172
response = client.post("https://poe.com/api/gql_POST", data=payload)
190
response = client.post('https://poe.com/api/gql_POST', data=payload)
173
191
174
if "success" not in response.text:
192
if 'success' not in response.text:
175
193
raise Exception(
176
"""
194
'''
177
195
Bot creation Failed
178
196
!! Important !!
179
197
Bot creation was not enabled on this account
180
198
please use: quora.Account.create with enable_bot_creation set to True
181
"""
199
'''
182
200
)
183
201
184
202
return ModelResponse(response.json())
185
203
186
204
187
205
class Account:
206
@staticmethod
188
207
def create(
189
208
proxy: Optional[str] = None,
190
209
logging: bool = False,
191
210
enable_bot_creation: bool = False,
192
211
):
193
client = TLS(client_identifier="chrome110")
194
client.proxies = (
195
{"http": f"http://{proxy}", "https": f"http://{proxy}"} if proxy else None
196
)
212
client = TLS(client_identifier='chrome110')
213
client.proxies = {'http': f'http://{proxy}', 'https': f'http://{proxy}'} if proxy else None
197
214
198
215
mail_client = Emailnator()
199
216
mail_address = mail_client.get_mail()
200
217
201
218
if logging:
202
print("email", mail_address)
219
print('email', mail_address)
203
220
204
221
client.headers = {
205
"authority": "poe.com",
206
"accept": "*/*",
207
"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",
208
"content-type": "application/json",
209
"origin": "https://poe.com",
210
"poe-tag-id": "null",
211
"referer": "https://poe.com/login",
212
"sec-ch-ua": '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
213
"sec-ch-ua-mobile": "?0",
214
"sec-ch-ua-platform": '"macOS"',
215
"sec-fetch-dest": "empty",
216
"sec-fetch-mode": "cors",
217
"sec-fetch-site": "same-origin",
218
"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",
219
"poe-formkey": extract_formkey(client.get("https://poe.com/login").text),
220
"poe-tchannel": client.get("https://poe.com/api/settings").json()[
221
"tchannelData"
222
]["channel"],
222
'authority': 'poe.com',
223
'accept': '*/*',
224
'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',
225
'content-type': 'application/json',
226
'origin': 'https://poe.com',
227
'poe-tag-id': 'null',
228
'referer': 'https://poe.com/login',
229
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
230
'sec-ch-ua-mobile': '?0',
231
'sec-ch-ua-platform': '"macOS"',
232
'sec-fetch-dest': 'empty',
233
'sec-fetch-mode': 'cors',
234
'sec-fetch-site': 'same-origin',
235
'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',
236
'poe-formkey': extract_formkey(client.get('https://poe.com/login').text),
237
'poe-tchannel': client.get('https://poe.com/api/settings').json()['tchannelData']['channel'],
223
238
}
224
239
225
240
token = reCaptchaV3(
226
"https://www.recaptcha.net/recaptcha/enterprise/anchor?ar=1&k=6LflhEElAAAAAI_ewVwRWI9hsyV4mbZnYAslSvlG&co=aHR0cHM6Ly9wb2UuY29tOjQ0Mw..&hl=en&v=4PnKmGB9wRHh1i04o7YUICeI&size=invisible&cb=bi6ivxoskyal"
241
'https://www.recaptcha.net/recaptcha/enterprise/anchor?ar=1&k=6LflhEElAAAAAI_ewVwRWI9hsyV4mbZnYAslSvlG&co=aHR0cHM6Ly9wb2UuY29tOjQ0Mw..&hl=en&v=4PnKmGB9wRHh1i04o7YUICeI&size=invisible&cb=bi6ivxoskyal'
227
242
)
228
243
# token = solver.recaptcha(sitekey='6LflhEElAAAAAI_ewVwRWI9hsyV4mbZnYAslSvlG',
229
244
# url = 'https://poe.com/login?redirect_url=%2F',
@@ -233,77 +248,74 @@ class Account:
233
248
# action = 'login',)['code']
234
249
235
250
payload = dumps(
236
separators=(",", ":"),
251
separators=(',', ':'),
237
252
obj={
238
"queryName": "MainSignupLoginSection_sendVerificationCodeMutation_Mutation",
239
"variables": {
240
"emailAddress": mail_address,
241
"phoneNumber": None,
242
"recaptchaToken": token,
253
'queryName': 'MainSignupLoginSection_sendVerificationCodeMutation_Mutation',
254
'variables': {
255
'emailAddress': mail_address,
256
'phoneNumber': None,
257
'recaptchaToken': token,
243
258
},
244
"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",
259
'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',
245
260
},
246
261
)
247
262
248
base_string = payload + client.headers["poe-formkey"] + "WpuLMiXEKKE98j56k"
249
client.headers["poe-tag-id"] = md5(base_string.encode()).hexdigest()
263
base_string = payload + client.headers['poe-formkey'] + 'WpuLMiXEKKE98j56k'
264
client.headers['poe-tag-id'] = md5(base_string.encode()).hexdigest()
250
265
251
266
print(dumps(client.headers, indent=4))
252
267
253
response = client.post("https://poe.com/api/gql_POST", data=payload)
268
response = client.post('https://poe.com/api/gql_POST', data=payload)
254
269
255
if "automated_request_detected" in response.text:
256
print("please try using a proxy / wait for fix")
270
if 'automated_request_detected' in response.text:
271
print('please try using a proxy / wait for fix')
257
272
258
if "Bad Request" in response.text:
273
if 'Bad Request' in response.text:
259
274
if logging:
260
print("bad request, retrying...", response.json())
275
print('bad request, retrying...', response.json())
261
276
quit()
262
277
263
278
if logging:
264
print("send_code", response.json())
279
print('send_code', response.json())
265
280
266
281
mail_content = mail_client.get_message()
267
282
mail_token = findall(r';">(\d{6,7})</div>', mail_content)[0]
268
283
269
284
if logging:
270
print("code", mail_token)
285
print('code', mail_token)
271
286
272
287
payload = dumps(
273
separators=(",", ":"),
288
separators=(',', ':'),
274
289
obj={
275
"queryName": "SignupOrLoginWithCodeSection_signupWithVerificationCodeMutation_Mutation",
276
"variables": {
277
"verificationCode": str(mail_token),
278
"emailAddress": mail_address,
279
"phoneNumber": None,
290
'queryName': 'SignupOrLoginWithCodeSection_signupWithVerificationCodeMutation_Mutation',
291
'variables': {
292
'verificationCode': str(mail_token),
293
'emailAddress': mail_address,
294
'phoneNumber': None,
280
295
},
281
"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",
296
'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',
282
297
},
283
298
)
284
299
285
base_string = payload + client.headers["poe-formkey"] + "WpuLMiXEKKE98j56k"
286
client.headers["poe-tag-id"] = md5(base_string.encode()).hexdigest()
300
base_string = payload + client.headers['poe-formkey'] + 'WpuLMiXEKKE98j56k'
301
client.headers['poe-tag-id'] = md5(base_string.encode()).hexdigest()
287
302
288
response = client.post("https://poe.com/api/gql_POST", data=payload)
303
response = client.post('https://poe.com/api/gql_POST', data=payload)
289
304
if logging:
290
print("verify_code", response.json())
305
print('verify_code', response.json())
291
306
292
307
def get(self):
293
cookies = (
294
open(Path(__file__).resolve().parent / "cookies.txt", "r")
295
.read()
296
.splitlines()
297
)
308
cookies = open(Path(__file__).resolve().parent / 'cookies.txt', 'r').read().splitlines()
298
309
return choice(cookies)
299
310
300
311
301
312
class StreamingCompletion:
313
@staticmethod
302
314
def create(
303
model: str = "gpt-4",
315
model: str = 'gpt-4',
304
316
custom_model: bool = None,
305
prompt: str = "hello world",
306
token: str = "",
317
prompt: str = 'hello world',
318
token: str = '',
307
319
):
308
320
_model = MODELS[model] if not custom_model else custom_model
309
321
@@ -312,22 +324,22 @@ class StreamingCompletion:
312
324
for chunk in client.send_message(_model, prompt):
313
325
yield PoeResponse(
314
326
{
315
"id": chunk["messageId"],
316
"object": "text_completion",
317
"created": chunk["creationTime"],
318
"model": _model,
319
"choices": [
327
'id': chunk['messageId'],
328
'object': 'text_completion',
329
'created': chunk['creationTime'],
330
'model': _model,
331
'choices': [
320
332
{
321
"text": chunk["text_new"],
322
"index": 0,
323
"logprobs": None,
324
"finish_reason": "stop",
333
'text': chunk['text_new'],
334
'index': 0,
335
'logprobs': None,
336
'finish_reason': 'stop',
325
337
}
326
338
],
327
"usage": {
328
"prompt_tokens": len(prompt),
329
"completion_tokens": len(chunk["text_new"]),
330
"total_tokens": len(prompt) + len(chunk["text_new"]),
339
'usage': {
340
'prompt_tokens': len(prompt),
341
'completion_tokens': len(chunk['text_new']),
342
'total_tokens': len(prompt) + len(chunk['text_new']),
331
343
},
332
344
}
333
345
)
@@ -335,17 +347,17 @@ class StreamingCompletion:
335
347
336
348
class Completion:
337
349
def create(
338
model: str = "gpt-4",
350
model: str = 'gpt-4',
339
351
custom_model: str = None,
340
prompt: str = "hello world",
341
token: str = "",
352
prompt: str = 'hello world',
353
token: str = '',
342
354
):
343
355
models = {
344
"sage": "capybara",
345
"gpt-4": "beaver",
346
"claude-v1.2": "a2_2",
347
"claude-instant-v1.0": "a2",
348
"gpt-3.5-turbo": "chinchilla",
356
'sage': 'capybara',
357
'gpt-4': 'beaver',
358
'claude-v1.2': 'a2_2',
359
'claude-instant-v1.0': 'a2',
360
'gpt-3.5-turbo': 'chinchilla',
349
361
}
350
362
351
363
_model = models[model] if not custom_model else custom_model
@@ -357,73 +369,68 @@ class Completion:
357
369
358
370
return PoeResponse(
359
371
{
360
"id": chunk["messageId"],
361
"object": "text_completion",
362
"created": chunk["creationTime"],
363
"model": _model,
364
"choices": [
372
'id': chunk['messageId'],
373
'object': 'text_completion',
374
'created': chunk['creationTime'],
375
'model': _model,
376
'choices': [
365
377
{
366
"text": chunk["text"],
367
"index": 0,
368
"logprobs": None,
369
"finish_reason": "stop",
378
'text': chunk['text'],
379
'index': 0,
380
'logprobs': None,
381
'finish_reason': 'stop',
370
382
}
371
383
],
372
"usage": {
373
"prompt_tokens": len(prompt),
374
"completion_tokens": len(chunk["text"]),
375
"total_tokens": len(prompt) + len(chunk["text"]),
384
'usage': {
385
'prompt_tokens': len(prompt),
386
'completion_tokens': len(chunk['text']),
387
'total_tokens': len(prompt) + len(chunk['text']),
376
388
},
377
389
}
378
390
)
379
391
380
392
381
393
class Poe:
382
def __init__(self, model: str = "ChatGPT"):
394
def __init__(
395
self,
396
model: str = 'ChatGPT',
397
driver: str = 'firefox',
398
download_driver: bool = False,
399
driver_path: Optional[str] = None,
400
cookie_path: str = './quora/cookie.json',
401
):
383
402
# validating the model
384
403
if model and model not in MODELS:
385
raise RuntimeError(
386
"Sorry, the model you provided does not exist. Please check and try again."
387
)
404
raise RuntimeError('Sorry, the model you provided does not exist. Please check and try again.')
388
405
self.model = MODELS[model]
389
self.cookie = self.__load_cookie()
406
self.cookie_path = cookie_path
407
self.cookie = self.__load_cookie(driver, download_driver, driver_path=driver_path)
390
408
self.client = PoeClient(self.cookie)
391
409
392
def __load_cookie(self) -> str:
393
if (cookie_file := Path("./quora/cookie.json")).exists():
410
def __load_cookie(self, driver: str, download_driver: bool, driver_path: Optional[str] = None) -> str:
411
if (cookie_file := Path(self.cookie_path)).exists():
394
412
with cookie_file.open() as fp:
395
413
cookie = json.load(fp)
396
if datetime.fromtimestamp(cookie["expiry"]) < datetime.now():
397
cookie = self.__register_and_get_cookie()
414
if datetime.fromtimestamp(cookie['expiry']) < datetime.now():
415
cookie = self.__register_and_get_cookie(driver, driver_path=driver_path)
398
416
else:
399
print("Loading the cookie from file")
417
print('Loading the cookie from file')
400
418
else:
401
cookie = self.__register_and_get_cookie()
419
cookie = self.__register_and_get_cookie(driver, driver_path=driver_path)
402
420
403
return unquote(cookie["value"])
421
return unquote(cookie['value'])
404
422
405
@classmethod
406
def __register_and_get_cookie(cls) -> dict:
423
def __register_and_get_cookie(self, driver: str, driver_path: Optional[str] = None) -> dict:
407
424
mail_client = Emailnator()
408
425
mail_address = mail_client.get_mail()
409
426
410
print(mail_address)
411
options = webdriver.FirefoxOptions()
412
# options.add_argument("-headless")
413
try:
414
driver = webdriver.Firefox(options=options)
415
416
except Exception:
417
raise Exception(b'The error message you are receiving is due to the `geckodriver` executable not being found in your system\'s PATH. To resolve this issue, you need to download the geckodriver and add its location to your system\'s PATH.\n\nHere are the steps to resolve the issue:\n\n1. Download the geckodriver for your platform (Windows, macOS, or Linux) from the following link: https://github.com/mozilla/geckodriver/releases\n\n2. Extract the downloaded archive and locate the geckodriver executable.\n\n3. Add the geckodriver executable to your system\'s PATH.\n\nFor macOS and Linux:\n\n- Open a terminal window.\n- Move the geckodriver executable to a directory that is already in your PATH, or create a new directory and add it to your PATH:\n\n```bash\n# Example: Move geckodriver to /usr/local/bin\nmv /path/to/your/geckodriver /usr/local/bin\n```\n\n- If you created a new directory, add it to your PATH:\n\n```bash\n# Example: Add a new directory to PATH\nexport PATH=$PATH:/path/to/your/directory\n```\n\nFor Windows:\n\n- Right-click on "My Computer" or "This PC" and select "Properties".\n- Click on "Advanced system settings".\n- Click on the "Environment Variables" button.\n- In the "System variables" section, find the "Path" variable, select it, and click "Edit".\n- Click "New" and add the path to the directory containing the geckodriver executable.\n\nAfter adding the geckodriver to your PATH, restart your terminal or command prompt and try running your script again. The error should be resolved.')
418
427
driver = self.__resolve_driver(driver, driver_path=driver_path)
419
428
driver.get("https://www.poe.com")
420
429
421
430
# clicking use email button
422
431
driver.find_element(By.XPATH, '//button[contains(text(), "Use email")]').click()
423
432
424
email = WebDriverWait(driver, 30).until(
425
EC.presence_of_element_located((By.XPATH, '//input[@type="email"]'))
426
)
433
email = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//input[@type="email"]')))
427
434
email.send_keys(mail_address)
428
435
driver.find_element(By.XPATH, '//button[text()="Go"]').click()
429
436
@@ -434,46 +441,50 @@ class Poe:
434
441
EC.presence_of_element_located((By.XPATH, '//input[@placeholder="Code"]'))
435
442
)
436
443
verification_code.send_keys(code)
437
verify_button = EC.presence_of_element_located(
438
(By.XPATH, '//button[text()="Verify"]')
439
)
440
login_button = EC.presence_of_element_located(
441
(By.XPATH, '//button[text()="Log In"]')
442
)
444
verify_button = EC.presence_of_element_located((By.XPATH, '//button[text()="Verify"]'))
445
login_button = EC.presence_of_element_located((By.XPATH, '//button[text()="Log In"]'))
443
446
444
447
WebDriverWait(driver, 30).until(EC.any_of(verify_button, login_button)).click()
445
448
446
cookie = driver.get_cookie("p-b")
449
cookie = driver.get_cookie('p-b')
447
450
448
with open("./quora/cookie.json", "w") as fw:
451
with open(self.cookie_path, 'w') as fw:
449
452
json.dump(cookie, fw)
450
453
451
454
driver.close()
452
455
return cookie
453
456
457
@classmethod
458
def __resolve_driver(cls, driver: str, driver_path: Optional[str] = None) -> Union[Firefox, Chrome]:
459
options = FirefoxOptions() if driver == 'firefox' else ChromeOptions()
460
options.add_argument('-headless')
461
462
if driver_path:
463
options.binary_location = driver_path
464
try:
465
return Firefox(options=options) if driver == 'firefox' else Chrome(options=options)
466
except Exception:
467
raise Exception(SELENIUM_WEB_DRIVER_ERROR_MSG)
468
454
469
def chat(self, message: str, model: Optional[str] = None) -> str:
455
470
if model and model not in MODELS:
456
raise RuntimeError(
457
"Sorry, the model you provided does not exist. Please check and try again."
458
)
471
raise RuntimeError('Sorry, the model you provided does not exist. Please check and try again.')
459
472
model = MODELS[model] if model else self.model
460
473
response = None
461
474
for chunk in self.client.send_message(model, message):
462
response = chunk["text"]
475
response = chunk['text']
463
476
return response
464
477
465
478
def create_bot(
466
479
self,
467
480
name: str,
468
481
/,
469
prompt: str = "",
470
base_model: str = "ChatGPT",
471
description: str = "",
482
prompt: str = '',
483
base_model: str = 'ChatGPT',
484
description: str = '',
472
485
) -> None:
473
486
if base_model not in MODELS:
474
raise RuntimeError(
475
"Sorry, the base_model you provided does not exist. Please check and try again."
476
)
487
raise RuntimeError('Sorry, the base_model you provided does not exist. Please check and try again.')
477
488
478
489
response = self.client.create_bot(
479
490
handle=name,
@@ -18,23 +18,21 @@
18
18
# You should have received a copy of the GNU General Public License
19
19
# along with this program. If not, see <https://www.gnu.org/licenses/>.
20
20
21
import requests
22
import re
21
import hashlib
23
22
import json
24
import random
25
23
import logging
26
import time
27
24
import queue
25
import random
26
import re
28
27
import threading
28
import time
29
29
import traceback
30
import hashlib
31
import string
32
import random
33
import requests.adapters
34
import websocket
35
30
from pathlib import Path
36
31
from urllib.parse import urlparse
37
32
33
import requests
34
import requests.adapters
35
import websocket
38
36
39
37
parent_path = Path(__file__).resolve().parent
40
38
queries_path = parent_path / "graphql"
@@ -66,7 +64,7 @@ def request_with_retries(method, *args, **kwargs):
66
64
if r.status_code == 200:
67
65
return r
68
66
logger.warn(
69
f"Server returned a status code of {r.status_code} while downloading {url}. Retrying ({i+1}/{attempts})..."
67
f"Server returned a status code of {r.status_code} while downloading {url}. Retrying ({i + 1}/{attempts})..."
70
68
)
71
69
72
70
raise RuntimeError(f"Failed to download {url} too many times.")
@@ -81,9 +79,7 @@ class Client:
81
79
def __init__(self, token, proxy=None):
82
80
self.proxy = proxy
83
81
self.session = requests.Session()
84
self.adapter = requests.adapters.HTTPAdapter(
85
pool_connections=100, pool_maxsize=100
86
)
82
self.adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100)
87
83
self.session.mount("http://", self.adapter)
88
84
self.session.mount("https://", self.adapter)
89
85
@@ -139,9 +135,7 @@ class Client:
139
135
logger.info("Downloading next_data...")
140
136
141
137
r = request_with_retries(self.session.get, self.home_url)
142
json_regex = (
143
r'<script id="__NEXT_DATA__" type="application\/json">(.+?)</script>'
144
)
138
json_regex = r'<script id="__NEXT_DATA__" type="application\/json">(.+?)</script>'
145
139
json_text = re.search(json_regex, r.text).group(1)
146
140
next_data = json.loads(json_text)
147
141
@@ -213,19 +207,14 @@ class Client:
213
207
if channel is None:
214
208
channel = self.channel
215
209
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
)
210
return f'wss://{self.ws_domain}.tch.{channel["baseHost"]}/up/{channel["boxName"]}/updates' + query
220
211
221
212
def send_query(self, query_name, variables):
222
213
for i in range(20):
223
214
json_data = generate_payload(query_name, variables)
224
215
payload = json.dumps(json_data, separators=(",", ":"))
225
216
226
base_string = (
227
payload + self.gql_headers["poe-formkey"] + "WpuLMiXEKKE98j56k"
228
)
217
base_string = payload + self.gql_headers["poe-formkey"] + "WpuLMiXEKKE98j56k"
229
218
230
219
headers = {
231
220
"content-type": "application/json",
@@ -233,15 +222,11 @@ class Client:
233
222
}
234
223
headers = {**self.gql_headers, **headers}
235
224
236
r = request_with_retries(
237
self.session.post, self.gql_url, data=payload, headers=headers
238
)
225
r = request_with_retries(self.session.post, self.gql_url, data=payload, headers=headers)
239
226
240
227
data = r.json()
241
228
if data["data"] == None:
242
logger.warn(
243
f'{query_name} returned an error: {data["errors"][0]["message"]} | Retrying ({i+1}/20)'
244
)
229
logger.warn(f'{query_name} returned an error: {data["errors"][0]["message"]} | Retrying ({i + 1}/20)')
245
230
time.sleep(2)
246
231
continue
247
232
@@ -304,9 +289,7 @@ class Client:
304
289
305
290
def on_ws_close(self, ws, close_status_code, close_message):
306
291
self.ws_connected = False
307
logger.warn(
308
f"Websocket closed with status {close_status_code}: {close_message}"
309
)
292
logger.warn(f"Websocket closed with status {close_status_code}: {close_message}")
310
293
311
294
def on_ws_error(self, ws, error):
312
295
self.disconnect_ws()
@@ -333,11 +316,7 @@ class Client:
333
316
return
334
317
335
318
# 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
):
319
elif key != "pending" and value == None and message["state"] != "complete":
341
320
self.active_messages[key] = message["messageId"]
342
321
self.message_queues[key].put(message)
343
322
return
@@ -381,9 +360,7 @@ class Client:
381
360
human_message = message_data["data"]["messageEdgeCreate"]["message"]
382
361
human_message_id = human_message["node"]["messageId"]
383
362
except TypeError:
384
raise RuntimeError(
385
f"An unknown error occurred. Raw response data: {message_data}"
386
)
363
raise RuntimeError(f"An unknown error occurred. Raw response data: {message_data}")
387
364
388
365
# indicate that the current message is waiting for a response
389
366
self.active_messages[human_message_id] = None
@@ -418,9 +395,7 @@ class Client:
418
395
419
396
def send_chat_break(self, chatbot):
420
397
logger.info(f"Sending chat break to {chatbot}")
421
result = self.send_query(
422
"AddMessageBreakMutation", {"chatId": self.bots[chatbot]["chatId"]}
423
)
398
result = self.send_query("AddMessageBreakMutation", {"chatId": self.bots[chatbot]["chatId"]})
424
399
return result["data"]["messageBreakCreate"]["message"]
425
400
426
401
def get_message_history(self, chatbot, count=25, cursor=None):
@@ -437,15 +412,11 @@ class Client:
437
412
438
413
cursor = str(cursor)
439
414
if count > 50:
440
messages = (
441
self.get_message_history(chatbot, count=50, cursor=cursor) + messages
442
)
415
messages = self.get_message_history(chatbot, count=50, cursor=cursor) + messages
443
416
while count > 0:
444
417
count -= 50
445
418
new_cursor = messages[0]["cursor"]
446
new_messages = self.get_message_history(
447
chatbot, min(50, count), cursor=new_cursor
448
)
419
new_messages = self.get_message_history(chatbot, min(50, count), cursor=new_cursor)
449
420
messages = new_messages + messages
450
421
return messages
451
422
elif count <= 0:
@@ -523,9 +494,7 @@ class Client:
523
494
524
495
data = result["data"]["poeBotCreate"]
525
496
if data["status"] != "success":
526
raise RuntimeError(
527
f"Poe returned an error while trying to create a bot: {data['status']}"
528
)
497
raise RuntimeError(f"Poe returned an error while trying to create a bot: {data['status']}")
529
498
self.get_bots()
530
499
return data
531
500
@@ -568,9 +537,7 @@ class Client:
568
537
569
538
data = result["data"]["poeBotEdit"]
570
539
if data["status"] != "success":
571
raise RuntimeError(
572
f"Poe returned an error while trying to edit a bot: {data['status']}"
573
)
540
raise RuntimeError(f"Poe returned an error while trying to edit a bot: {data['status']}")
574
541
self.get_bots()
575
542
return data
576
543