diff options
Diffstat (limited to '')
-rw-r--r-- | quora/__init__.py | 81 |
1 files changed, 50 insertions, 31 deletions
diff --git a/quora/__init__.py b/quora/__init__.py index 79de68b1..0a11bb23 100644 --- a/quora/__init__.py +++ b/quora/__init__.py @@ -8,9 +8,25 @@ from pathlib import Path from random import choice, choices, randint from string import ascii_letters, digits from urllib import parse -from os import urandom -from hashlib import md5 -from json import dumps +from os import urandom +from hashlib import md5 +from json import dumps + +def extract_formkey(html): + script_regex = r'<script>if\(.+\)throw new Error;(.+)</script>' + script_text = search(script_regex, html).group(1) + key_regex = r'var .="([0-9a-f]+)",' + key_text = search(key_regex, script_text).group(1) + cipher_regex = r'.\[(\d+)\]=.\[(\d+)\]' + cipher_pairs = findall(cipher_regex, script_text) + + formkey_list = [""] * len(cipher_pairs) + for pair in cipher_pairs: + formkey_index, key_index = map(int, pair) + formkey_list[formkey_index] = key_text[key_index] + formkey = "".join(formkey_list) + + return formkey class PoeResponse: @@ -79,7 +95,8 @@ class Model: client = Session() client.cookies['p-b'] = token - + + formkey = extract_formkey(client.get('https://poe.com').text) settings = client.get('https://poe.com/api/settings').json() client.headers = { @@ -87,7 +104,7 @@ class Model: "origin" : "https://poe.com", "referer" : "https://poe.com/", "content-type" : "application/json", - "poe-formkey" : settings['formkey'], + "poe-formkey" : formkey, "poe-tchannel" : settings['tchannelData']['channel'], "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", "connection" : "keep-alive", @@ -102,8 +119,8 @@ class Model: "accept-encoding" : "gzip, deflate, br", "accept-language" : "en-GB,en-US;q=0.9,en;q=0.8", } - - response = client.post("https://poe.com/api/gql_POST", json = { + + payload = dumps(separators=(',', ':'), obj = { 'queryName': 'CreateBotMain_poeBotCreate_Mutation', 'variables': { 'model' : models[model], @@ -124,6 +141,11 @@ class Model: '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', }) + base_string = payload + client.headers["poe-formkey"] + 'WpuLMiXEKKE98j56k' + client.headers["poe-tag-id"] = md5(base_string.encode()).hexdigest() + + response = client.post("https://poe.com/api/gql_POST", data = payload) + if not 'success' in response.text: raise Exception(''' Bot creation Failed @@ -136,15 +158,14 @@ class Model: class Account: def create(proxy: None or str = None, logging: bool = False, enable_bot_creation: bool = False): - client = Session() client.proxies = { 'http': f'http://{proxy}', 'https': f'http://{proxy}'} if proxy else None - + mail = Mail(client.proxies) mail_token = None - mail_address = mail.get_mail() + _, mail_address = mail.get_mail() if logging: print('email', mail_address) @@ -167,12 +188,9 @@ class Account: "upgrade-insecure-requests": "1", } - init = client.get('https://poe.com/login') - next_data = loads(search(r'json">(.+?)</script>', init.text).group(1)) + client.headers["poe-formkey"] = extract_formkey(client.get('https://poe.com/login').text) + client.headers["poe-tchannel"] = client.get('https://poe.com/api/settings').json()['tchannelData']['channel'] - client.headers["poe-formkey"] = next_data['props']['formkey'] - client.headers["poe-tchannel"] = client.get('https://poe.com/api/settings').json()['tchannelData']['channel'] - payload = dumps(separators = (',', ':'), obj = { 'queryName': 'MainSignupLoginSection_sendVerificationCodeMutation_Mutation', 'variables': { @@ -189,18 +207,18 @@ class Account: response = client.post('https://poe.com/api/gql_POST', data=payload) if 'Bad Request' in response.text: if logging: print('bad request, retrying...' , response.json()) - Account.create(proxy = proxy, logging = logging) + quit() if logging: print('send_code' ,response.json()) while True: sleep(1) - inbox = mail.fetch_inbox() - - for _ in inbox: - content = mail.get_message(_["id"]) - mail_token = findall(r';">(\d{6,7})</div>', content['html'][0])[0] - + messages = mail.fetch_inbox() + + if len(messages["messages"]) > 0: + email_content = mail.get_message_content(messages["messages"][0]["_id"]) + mail_token = findall(r';">(\d{6,7})</div>', email_content)[0] + if mail_token: break @@ -215,7 +233,7 @@ class Account: }, "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" }) - + base_string = payload + client.headers["poe-formkey"] + 'WpuLMiXEKKE98j56k' client.headers["poe-tag-id"] = md5(base_string.encode()).hexdigest() @@ -223,23 +241,24 @@ class Account: if logging: print('verify_code', response.json()) token = parse.unquote(client.cookies.get_dict()['p-b']) - + with open(Path(__file__).resolve().parent / 'cookies.txt', 'a') as f: f.write(f'{token}\n') if enable_bot_creation: - - payload = { + + payload = dumps(separators = (',', ':'), obj={ "queryName": "UserProfileConfigurePreviewModal_markMultiplayerNuxCompleted_Mutation", "variables": {}, "query": "mutation UserProfileConfigurePreviewModal_markMultiplayerNuxCompleted_Mutation {\n markMultiplayerNuxCompleted {\n viewer {\n hasCompletedMultiplayerNux\n id\n }\n }\n}\n" - } + }) - base_string = dumps(payload, separators = (',', ':')) + client.headers["poe-formkey"] + 'WpuLMiXEKKE98j56k' + base_string = payload + client.headers["poe-formkey"] + 'WpuLMiXEKKE98j56k' client.headers["poe-tag-id"] = md5(base_string.encode()).hexdigest() - - client.post("https://poe.com/api/gql_POST", json = payload) - + + resp = client.post("https://poe.com/api/gql_POST", data = payload) + if logging: print(resp.json()) + return token def get(): |