diff options
author | noptuno <repollo.marrero@gmail.com> | 2023-04-28 02:40:47 +0200 |
---|---|---|
committer | noptuno <repollo.marrero@gmail.com> | 2023-04-28 02:40:47 +0200 |
commit | 6f6a73987201c9c303047c61389b82ad98b15597 (patch) | |
tree | bf67eb590d49979d6740bc1e94b4018df48bce98 /openai_rev/quora | |
parent | Resolved merge conflicts and merged pr_218 into STREAMLIT_CHAT_IMPLEMENTATION (diff) | |
parent | Merging PR_218 openai_rev package with new streamlit chat app (diff) | |
download | gpt4free-6f6a73987201c9c303047c61389b82ad98b15597.tar gpt4free-6f6a73987201c9c303047c61389b82ad98b15597.tar.gz gpt4free-6f6a73987201c9c303047c61389b82ad98b15597.tar.bz2 gpt4free-6f6a73987201c9c303047c61389b82ad98b15597.tar.lz gpt4free-6f6a73987201c9c303047c61389b82ad98b15597.tar.xz gpt4free-6f6a73987201c9c303047c61389b82ad98b15597.tar.zst gpt4free-6f6a73987201c9c303047c61389b82ad98b15597.zip |
Diffstat (limited to '')
-rw-r--r-- | openai_rev/quora/README.md | 68 | ||||
-rw-r--r-- | openai_rev/quora/__init__.py | 472 | ||||
-rw-r--r-- | openai_rev/quora/api.py | 545 | ||||
-rw-r--r-- | openai_rev/quora/cookies.txt (renamed from quora/cookies.txt) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/AddHumanMessageMutation.graphql (renamed from quora/graphql/AddHumanMessageMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/AddMessageBreakMutation.graphql (renamed from quora/graphql/AddMessageBreakMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/AutoSubscriptionMutation.graphql (renamed from quora/graphql/AutoSubscriptionMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/BioFragment.graphql (renamed from quora/graphql/BioFragment.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/ChatAddedSubscription.graphql (renamed from quora/graphql/ChatAddedSubscription.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/ChatFragment.graphql (renamed from quora/graphql/ChatFragment.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/ChatListPaginationQuery.graphql (renamed from quora/graphql/ChatListPaginationQuery.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/ChatPaginationQuery.graphql (renamed from quora/graphql/ChatPaginationQuery.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/ChatViewQuery.graphql (renamed from quora/graphql/ChatViewQuery.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/DeleteHumanMessagesMutation.graphql (renamed from quora/graphql/DeleteHumanMessagesMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/DeleteMessageMutation.graphql (renamed from quora/graphql/DeleteMessageMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/HandleFragment.graphql (renamed from quora/graphql/HandleFragment.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/LoginWithVerificationCodeMutation.graphql (renamed from quora/graphql/LoginWithVerificationCodeMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/MessageAddedSubscription.graphql (renamed from quora/graphql/MessageAddedSubscription.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/MessageDeletedSubscription.graphql (renamed from quora/graphql/MessageDeletedSubscription.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/MessageFragment.graphql (renamed from quora/graphql/MessageFragment.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/MessageRemoveVoteMutation.graphql (renamed from quora/graphql/MessageRemoveVoteMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/MessageSetVoteMutation.graphql (renamed from quora/graphql/MessageSetVoteMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/PoeBotCreateMutation.graphql (renamed from quora/graphql/PoeBotCreateMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/PoeBotEditMutation.graphql (renamed from quora/graphql/PoeBotEditMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/SendMessageMutation.graphql (renamed from quora/graphql/SendMessageMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/SendVerificationCodeForLoginMutation.graphql (renamed from quora/graphql/SendVerificationCodeForLoginMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/ShareMessagesMutation.graphql (renamed from quora/graphql/ShareMessagesMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/SignupWithVerificationCodeMutation.graphql (renamed from quora/graphql/SignupWithVerificationCodeMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/StaleChatUpdateMutation.graphql (renamed from quora/graphql/StaleChatUpdateMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/SubscriptionsMutation.graphql (renamed from quora/graphql/SubscriptionsMutation.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/SummarizePlainPostQuery.graphql (renamed from quora/graphql/SummarizePlainPostQuery.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/SummarizeQuotePostQuery.graphql (renamed from quora/graphql/SummarizeQuotePostQuery.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/SummarizeSharePostQuery.graphql (renamed from quora/graphql/SummarizeSharePostQuery.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/UserSnippetFragment.graphql (renamed from quora/graphql/UserSnippetFragment.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/ViewerInfoQuery.graphql (renamed from quora/graphql/ViewerInfoQuery.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/ViewerStateFragment.graphql (renamed from quora/graphql/ViewerStateFragment.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/ViewerStateUpdatedSubscription.graphql (renamed from quora/graphql/ViewerStateUpdatedSubscription.graphql) | 0 | ||||
-rw-r--r-- | openai_rev/quora/graphql/__init__.py (renamed from quora/graphql/__init__.py) | 0 | ||||
-rw-r--r-- | openai_rev/quora/mail.py | 80 |
39 files changed, 1165 insertions, 0 deletions
diff --git a/openai_rev/quora/README.md b/openai_rev/quora/README.md new file mode 100644 index 00000000..dc2bb32d --- /dev/null +++ b/openai_rev/quora/README.md @@ -0,0 +1,68 @@ +#### warning !!! +poe.com added security and can detect if you are making automated requests. You may get your account banned if you are using this api. +The normal non-driver api is also currently not very stable + + +### Example: `quora (poe)` (use like openai pypi package) - GPT-4 <a name="example-poe"></a> + +```python +# quora model names: (use left key as argument) +models = { + 'sage' : 'capybara', + 'gpt-4' : 'beaver', + 'claude-v1.2' : 'a2_2', + 'claude-instant-v1.0' : 'a2', + 'gpt-3.5-turbo' : 'chinchilla' +} +``` + +#### !! new: bot creation + +```python +# import quora (poe) package +from openai_rev import quora + +# create account +# make sure to set enable_bot_creation to True +token = quora.Account.create(logging=True, enable_bot_creation=True) + +model = quora.Model.create( + token=token, + model='gpt-3.5-turbo', # or claude-instant-v1.0 + system_prompt='you are ChatGPT a large language model ...' +) + +print(model.name) # gptx.... + +# streaming response +for response in quora.StreamingCompletion.create( + custom_model=model.name, + prompt='hello world', + token=token): + print(response.text) +``` + +#### Normal Response: +```python +import quora + +response = quora.Completion.create(model = 'gpt-4', + prompt = 'hello world', + token = 'token') + +print(response.text) +``` + +#### Update Use This For Poe +```python +from quora import Poe + +# available models: ['Sage', 'GPT-4', 'Claude+', 'Claude-instant', 'ChatGPT', 'Dragonfly', 'NeevaAI'] + +poe = Poe(model='ChatGPT', driver='firefox', cookie_path='cookie.json', driver_path='path_of_driver') +poe.chat('who won the football world cup most?') + +# new bot creation +poe.create_bot('new_bot_name', prompt='You are new test bot', base_model='gpt-3.5-turbo') + +``` diff --git a/openai_rev/quora/__init__.py b/openai_rev/quora/__init__.py new file mode 100644 index 00000000..5303f206 --- /dev/null +++ b/openai_rev/quora/__init__.py @@ -0,0 +1,472 @@ +import json +from datetime import datetime +from hashlib import md5 +from json import dumps +from pathlib import Path +from random import choice, choices, randint +from re import search, findall +from string import ascii_letters, digits +from typing import Optional, Union, List, Any, Generator +from urllib.parse import unquote + +import selenium.webdriver.support.expected_conditions as EC +from fake_useragent import UserAgent +from pydantic import BaseModel +from pypasser import reCaptchaV3 +from requests import Session +from selenium.webdriver import Firefox, Chrome, FirefoxOptions, ChromeOptions +from selenium.webdriver.common.by import By +from selenium.webdriver.support.wait import WebDriverWait +from tls_client import Session as TLS + +from .api import Client as PoeClient +from .mail import Emailnator + +SELENIUM_WEB_DRIVER_ERROR_MSG = 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.''' + +# from twocaptcha import TwoCaptcha +# solver = TwoCaptcha('72747bf24a9d89b4dcc1b24875efd358') + +MODELS = { + 'Sage': 'capybara', + 'GPT-4': 'beaver', + 'Claude+': 'a2_2', + 'Claude-instant': 'a2', + 'ChatGPT': 'chinchilla', + 'Dragonfly': 'nutria', + 'NeevaAI': 'hutia', +} + + +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 Choice(BaseModel): + text: str + index: int + logprobs: Any + finish_reason: str + + +class Usage(BaseModel): + prompt_tokens: int + completion_tokens: int + total_tokens: int + + +class PoeResponse(BaseModel): + id: int + object: str + created: int + model: str + choices: List[Choice] + usage: Usage + text: str + + +class ModelResponse: + def __init__(self, json_response: dict) -> None: + self.id = json_response['data']['poeBotCreate']['bot']['id'] + self.name = json_response['data']['poeBotCreate']['bot']['displayName'] + self.limit = json_response['data']['poeBotCreate']['bot']['messageLimit']['dailyLimit'] + self.deleted = json_response['data']['poeBotCreate']['bot']['deletionState'] + + +class Model: + @staticmethod + def create( + token: str, + model: str = 'gpt-3.5-turbo', # claude-instant + system_prompt: str = 'You are ChatGPT a large language model developed by Openai. Answer as consisely as possible', + description: str = 'gpt-3.5 language model from openai, skidded by poe.com', + handle: str = None, + ) -> ModelResponse: + + if not handle: + handle = f'gptx{randint(1111111, 9999999)}' + + 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 = { + 'host': 'poe.com', + 'origin': 'https://poe.com', + 'referer': 'https://poe.com/', + 'poe-formkey': formkey, + 'poe-tchannel': settings['tchannelData']['channel'], + 'user-agent': UserAgent().random, + 'connection': 'keep-alive', + 'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': '"macOS"', + 'content-type': 'application/json', + 'sec-fetch-site': 'same-origin', + 'sec-fetch-mode': 'cors', + 'sec-fetch-dest': 'empty', + 'accept': '*/*', + 'accept-encoding': 'gzip, deflate, br', + 'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8', + } + + payload = dumps( + separators=(',', ':'), + obj={ + 'queryName': 'CreateBotMain_poeBotCreate_Mutation', + 'variables': { + 'model': MODELS[model], + 'handle': handle, + 'prompt': system_prompt, + 'isPromptPublic': True, + 'introduction': '', + 'description': description, + 'profilePictureUrl': 'https://qph.fs.quoracdn.net/main-qimg-24e0b480dcd946e1cc6728802c5128b6', + 'apiUrl': None, + 'apiKey': ''.join(choices(ascii_letters + digits, k=32)), + 'isApiBot': False, + 'hasLinkification': False, + 'hasMarkdownRendering': False, + 'hasSuggestedReplies': False, + 'isPrivateBot': False, + }, + '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 'success' not in response.text: + raise Exception( + ''' + Bot creation Failed + !! Important !! + Bot creation was not enabled on this account + please use: quora.Account.create with enable_bot_creation set to True + ''' + ) + + return ModelResponse(response.json()) + + +class Account: + @staticmethod + def create( + proxy: Optional[str] = None, + logging: bool = False, + enable_bot_creation: bool = False, + ): + client = TLS(client_identifier='chrome110') + client.proxies = {'http': f'http://{proxy}', 'https': f'http://{proxy}'} if proxy else None + + mail_client = Emailnator() + mail_address = mail_client.get_mail() + + if logging: + print('email', mail_address) + + client.headers = { + 'authority': 'poe.com', + 'accept': '*/*', + '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', + 'content-type': 'application/json', + 'origin': 'https://poe.com', + 'poe-tag-id': 'null', + 'referer': 'https://poe.com/login', + 'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': '"macOS"', + 'sec-fetch-dest': 'empty', + 'sec-fetch-mode': 'cors', + 'sec-fetch-site': 'same-origin', + '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', + 'poe-formkey': extract_formkey(client.get('https://poe.com/login').text), + 'poe-tchannel': client.get('https://poe.com/api/settings').json()['tchannelData']['channel'], + } + + token = reCaptchaV3( + 'https://www.recaptcha.net/recaptcha/enterprise/anchor?ar=1&k=6LflhEElAAAAAI_ewVwRWI9hsyV4mbZnYAslSvlG&co=aHR0cHM6Ly9wb2UuY29tOjQ0Mw..&hl=en&v=4PnKmGB9wRHh1i04o7YUICeI&size=invisible&cb=bi6ivxoskyal' + ) + # token = solver.recaptcha(sitekey='6LflhEElAAAAAI_ewVwRWI9hsyV4mbZnYAslSvlG', + # url = 'https://poe.com/login?redirect_url=%2F', + # version = 'v3', + # enterprise = 1, + # invisible = 1, + # action = 'login',)['code'] + + payload = dumps( + separators=(',', ':'), + obj={ + 'queryName': 'MainSignupLoginSection_sendVerificationCodeMutation_Mutation', + 'variables': { + 'emailAddress': mail_address, + 'phoneNumber': None, + 'recaptchaToken': token, + }, + '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', + }, + ) + + base_string = payload + client.headers['poe-formkey'] + 'WpuLMiXEKKE98j56k' + client.headers['poe-tag-id'] = md5(base_string.encode()).hexdigest() + + print(dumps(client.headers, indent=4)) + + response = client.post('https://poe.com/api/gql_POST', data=payload) + + if 'automated_request_detected' in response.text: + print('please try using a proxy / wait for fix') + + if 'Bad Request' in response.text: + if logging: + print('bad request, retrying...', response.json()) + quit() + + if logging: + print('send_code', response.json()) + + mail_content = mail_client.get_message() + mail_token = findall(r';">(\d{6,7})</div>', mail_content)[0] + + if logging: + print('code', mail_token) + + payload = dumps( + separators=(',', ':'), + obj={ + 'queryName': 'SignupOrLoginWithCodeSection_signupWithVerificationCodeMutation_Mutation', + 'variables': { + 'verificationCode': str(mail_token), + 'emailAddress': mail_address, + 'phoneNumber': None, + }, + '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() + + response = client.post('https://poe.com/api/gql_POST', data=payload) + if logging: + print('verify_code', response.json()) + + def get(self): + cookies = open(Path(__file__).resolve().parent / 'cookies.txt', 'r').read().splitlines() + return choice(cookies) + + +class StreamingCompletion: + @staticmethod + def create( + model: str = 'gpt-4', + custom_model: bool = None, + prompt: str = 'hello world', + token: str = '', + ) -> Generator[PoeResponse, None, None]: + _model = MODELS[model] if not custom_model else custom_model + + client = PoeClient(token) + + for chunk in client.send_message(_model, prompt): + yield PoeResponse( + **{ + 'id': chunk['messageId'], + 'object': 'text_completion', + 'created': chunk['creationTime'], + 'model': _model, + 'text': chunk['text_new'], + 'choices': [ + { + 'text': chunk['text_new'], + 'index': 0, + 'logprobs': None, + 'finish_reason': 'stop', + } + ], + 'usage': { + 'prompt_tokens': len(prompt), + 'completion_tokens': len(chunk['text_new']), + 'total_tokens': len(prompt) + len(chunk['text_new']), + }, + } + ) + + +class Completion: + @staticmethod + def create( + model: str = 'gpt-4', + custom_model: str = None, + prompt: str = 'hello world', + token: str = '', + ) -> PoeResponse: + _model = MODELS[model] if not custom_model else custom_model + + client = PoeClient(token) + + chunk = None + for response in client.send_message(_model, prompt): + chunk = response + + return PoeResponse( + **{ + 'id': chunk['messageId'], + 'object': 'text_completion', + 'created': chunk['creationTime'], + 'model': _model, + 'text': chunk['text'], + 'choices': [ + { + 'text': chunk['text'], + 'index': 0, + 'logprobs': None, + 'finish_reason': 'stop', + } + ], + 'usage': { + 'prompt_tokens': len(prompt), + 'completion_tokens': len(chunk['text']), + 'total_tokens': len(prompt) + len(chunk['text']), + }, + } + ) + + +class Poe: + def __init__( + self, + model: str = 'ChatGPT', + driver: str = 'firefox', + download_driver: bool = False, + driver_path: Optional[str] = None, + cookie_path: str = './quora/cookie.json', + ): + # validating the model + if model and model not in MODELS: + raise RuntimeError('Sorry, the model you provided does not exist. Please check and try again.') + self.model = MODELS[model] + self.cookie_path = cookie_path + self.cookie = self.__load_cookie(driver, download_driver, driver_path=driver_path) + self.client = PoeClient(self.cookie) + + def __load_cookie(self, driver: str, download_driver: bool, driver_path: Optional[str] = None) -> str: + if (cookie_file := Path(self.cookie_path)).exists(): + with cookie_file.open() as fp: + cookie = json.load(fp) + if datetime.fromtimestamp(cookie['expiry']) < datetime.now(): + cookie = self.__register_and_get_cookie(driver, driver_path=driver_path) + else: + print('Loading the cookie from file') + else: + cookie = self.__register_and_get_cookie(driver, driver_path=driver_path) + + return unquote(cookie['value']) + + def __register_and_get_cookie(self, driver: str, driver_path: Optional[str] = None) -> dict: + mail_client = Emailnator() + mail_address = mail_client.get_mail() + + driver = self.__resolve_driver(driver, driver_path=driver_path) + driver.get("https://www.poe.com") + + # clicking use email button + driver.find_element(By.XPATH, '//button[contains(text(), "Use email")]').click() + + email = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//input[@type="email"]'))) + email.send_keys(mail_address) + driver.find_element(By.XPATH, '//button[text()="Go"]').click() + + code = findall(r';">(\d{6,7})</div>', mail_client.get_message())[0] + print(code) + + verification_code = WebDriverWait(driver, 30).until( + EC.presence_of_element_located((By.XPATH, '//input[@placeholder="Code"]')) + ) + verification_code.send_keys(code) + verify_button = EC.presence_of_element_located((By.XPATH, '//button[text()="Verify"]')) + login_button = EC.presence_of_element_located((By.XPATH, '//button[text()="Log In"]')) + + WebDriverWait(driver, 30).until(EC.any_of(verify_button, login_button)).click() + + cookie = driver.get_cookie('p-b') + + with open(self.cookie_path, 'w') as fw: + json.dump(cookie, fw) + + driver.close() + return cookie + + @classmethod + def __resolve_driver(cls, driver: str, driver_path: Optional[str] = None) -> Union[Firefox, Chrome]: + options = FirefoxOptions() if driver == 'firefox' else ChromeOptions() + options.add_argument('-headless') + + if driver_path: + options.binary_location = driver_path + try: + return Firefox(options=options) if driver == 'firefox' else Chrome(options=options) + except Exception: + raise Exception(SELENIUM_WEB_DRIVER_ERROR_MSG) + + def chat(self, message: str, model: Optional[str] = None) -> str: + if model and model not in MODELS: + raise RuntimeError('Sorry, the model you provided does not exist. Please check and try again.') + model = MODELS[model] if model else self.model + response = None + for chunk in self.client.send_message(model, message): + response = chunk['text'] + return response + + def create_bot( + self, + name: str, + /, + prompt: str = '', + base_model: str = 'ChatGPT', + description: str = '', + ) -> None: + if base_model not in MODELS: + raise RuntimeError('Sorry, the base_model you provided does not exist. Please check and try again.') + + response = self.client.create_bot( + handle=name, + prompt=prompt, + base_model=MODELS[base_model], + description=description, + ) + print(f'Successfully created bot with name: {response["bot"]["displayName"]}') + + def list_bots(self) -> list: + return list(self.client.bot_names.values()) diff --git a/openai_rev/quora/api.py b/openai_rev/quora/api.py new file mode 100644 index 00000000..42814f2c --- /dev/null +++ b/openai_rev/quora/api.py @@ -0,0 +1,545 @@ +# This file was taken from the repository poe-api https://github.com/ading2210/poe-api and is unmodified +# This file is licensed under the GNU GPL v3 and written by @ading2210 + +# license: +# ading2210/poe-api: a reverse engineered Python API wrapepr for Quora's Poe +# Copyright (C) 2023 ading2210 + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +import hashlib +import json +import logging +import queue +import random +import re +import threading +import time +import traceback +from pathlib import Path +from urllib.parse import urlparse + +import requests +import requests.adapters +import websocket + +parent_path = Path(__file__).resolve().parent +queries_path = parent_path / "graphql" +queries = {} + +logging.basicConfig() +logger = logging.getLogger() + +user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0" + + +def load_queries(): + for path in queries_path.iterdir(): + if path.suffix != ".graphql": + continue + with open(path) as f: + queries[path.stem] = f.read() + + +def generate_payload(query_name, variables): + return {"query": queries[query_name], "variables": variables} + + +def request_with_retries(method, *args, **kwargs): + attempts = kwargs.get("attempts") or 10 + url = args[0] + for i in range(attempts): + r = method(*args, **kwargs) + if r.status_code == 200: + return r + logger.warn( + f"Server returned a status code of {r.status_code} while downloading {url}. Retrying ({i + 1}/{attempts})..." + ) + + raise RuntimeError(f"Failed to download {url} too many times.") + + +class Client: + gql_url = "https://poe.com/api/gql_POST" + gql_recv_url = "https://poe.com/api/receive_POST" + home_url = "https://poe.com" + settings_url = "https://poe.com/api/settings" + + def __init__(self, token, proxy=None): + self.proxy = proxy + self.session = requests.Session() + self.adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100) + self.session.mount("http://", self.adapter) + self.session.mount("https://", self.adapter) + + if proxy: + self.session.proxies = {"http": self.proxy, "https": self.proxy} + logger.info(f"Proxy enabled: {self.proxy}") + + self.active_messages = {} + self.message_queues = {} + + self.session.cookies.set("p-b", token, domain="poe.com") + self.headers = { + "User-Agent": user_agent, + "Referrer": "https://poe.com/", + "Origin": "https://poe.com", + } + self.session.headers.update(self.headers) + + self.setup_connection() + self.connect_ws() + + def setup_connection(self): + self.ws_domain = f"tch{random.randint(1, 1e6)}" + self.next_data = self.get_next_data(overwrite_vars=True) + self.channel = self.get_channel_data() + self.bots = self.get_bots(download_next_data=False) + self.bot_names = self.get_bot_names() + + self.gql_headers = { + "poe-formkey": self.formkey, + "poe-tchannel": self.channel["channel"], + } + self.gql_headers = {**self.gql_headers, **self.headers} + self.subscribe() + + def extract_formkey(self, html): + script_regex = r"<script>if\(.+\)throw new Error;(.+)</script>" + script_text = re.search(script_regex, html).group(1) + key_regex = r'var .="([0-9a-f]+)",' + key_text = re.search(key_regex, script_text).group(1) + cipher_regex = r".\[(\d+)\]=.\[(\d+)\]" + cipher_pairs = re.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 + + def get_next_data(self, overwrite_vars=False): + logger.info("Downloading next_data...") + + r = request_with_retries(self.session.get, self.home_url) + json_regex = r'<script id="__NEXT_DATA__" type="application\/json">(.+?)</script>' + json_text = re.search(json_regex, r.text).group(1) + next_data = json.loads(json_text) + + if overwrite_vars: + self.formkey = self.extract_formkey(r.text) + self.viewer = next_data["props"]["pageProps"]["payload"]["viewer"] + self.next_data = next_data + + return next_data + + def get_bot(self, display_name): + url = f'https://poe.com/_next/data/{self.next_data["buildId"]}/{display_name}.json' + + r = request_with_retries(self.session.get, url) + + chat_data = r.json()["pageProps"]["payload"]["chatOfBotDisplayName"] + return chat_data + + def get_bots(self, download_next_data=True): + logger.info("Downloading all bots...") + if download_next_data: + next_data = self.get_next_data(overwrite_vars=True) + else: + next_data = self.next_data + + if not "availableBots" in self.viewer: + raise RuntimeError("Invalid token or no bots are available.") + bot_list = self.viewer["availableBots"] + + threads = [] + bots = {} + + def get_bot_thread(bot): + chat_data = self.get_bot(bot["displayName"]) + bots[chat_data["defaultBotObject"]["nickname"]] = chat_data + + for bot in bot_list: + thread = threading.Thread(target=get_bot_thread, args=(bot,), daemon=True) + threads.append(thread) + + for thread in threads: + thread.start() + for thread in threads: + thread.join() + + self.bots = bots + self.bot_names = self.get_bot_names() + return bots + + def get_bot_names(self): + bot_names = {} + for bot_nickname in self.bots: + bot_obj = self.bots[bot_nickname]["defaultBotObject"] + bot_names[bot_nickname] = bot_obj["displayName"] + return bot_names + + def get_remaining_messages(self, chatbot): + chat_data = self.get_bot(self.bot_names[chatbot]) + return chat_data["defaultBotObject"]["messageLimit"]["numMessagesRemaining"] + + def get_channel_data(self, channel=None): + logger.info("Downloading channel data...") + r = request_with_retries(self.session.get, self.settings_url) + data = r.json() + + return data["tchannelData"] + + def get_websocket_url(self, channel=None): + if channel is None: + channel = self.channel + query = f'?min_seq={channel["minSeq"]}&channel={channel["channel"]}&hash={channel["channelHash"]}' + return f'wss://{self.ws_domain}.tch.{channel["baseHost"]}/up/{channel["boxName"]}/updates' + query + + def send_query(self, query_name, variables): + for i in range(20): + json_data = generate_payload(query_name, variables) + payload = json.dumps(json_data, separators=(",", ":")) + + base_string = payload + self.gql_headers["poe-formkey"] + "WpuLMiXEKKE98j56k" + + headers = { + "content-type": "application/json", + "poe-tag-id": hashlib.md5(base_string.encode()).hexdigest(), + } + headers = {**self.gql_headers, **headers} + + r = request_with_retries(self.session.post, self.gql_url, data=payload, headers=headers) + + data = r.json() + if data["data"] == None: + logger.warn(f'{query_name} returned an error: {data["errors"][0]["message"]} | Retrying ({i + 1}/20)') + time.sleep(2) + continue + + return r.json() + + raise RuntimeError(f"{query_name} failed too many times.") + + def subscribe(self): + logger.info("Subscribing to mutations") + result = self.send_query( + "SubscriptionsMutation", + { + "subscriptions": [ + { + "subscriptionName": "messageAdded", + "query": queries["MessageAddedSubscription"], + }, + { + "subscriptionName": "viewerStateUpdated", + "query": queries["ViewerStateUpdatedSubscription"], + }, + ] + }, + ) + + def ws_run_thread(self): + kwargs = {} + if self.proxy: + proxy_parsed = urlparse(self.proxy) + kwargs = { + "proxy_type": proxy_parsed.scheme, + "http_proxy_host": proxy_parsed.hostname, + "http_proxy_port": proxy_parsed.port, + } + + self.ws.run_forever(**kwargs) + + def connect_ws(self): + self.ws_connected = False + self.ws = websocket.WebSocketApp( + self.get_websocket_url(), + header={"User-Agent": user_agent}, + on_message=self.on_message, + on_open=self.on_ws_connect, + on_error=self.on_ws_error, + on_close=self.on_ws_close, + ) + t = threading.Thread(target=self.ws_run_thread, daemon=True) + t.start() + while not self.ws_connected: + time.sleep(0.01) + + def disconnect_ws(self): + if self.ws: + self.ws.close() + self.ws_connected = False + + def on_ws_connect(self, ws): + self.ws_connected = True + + def on_ws_close(self, ws, close_status_code, close_message): + self.ws_connected = False + logger.warn(f"Websocket closed with status {close_status_code}: {close_message}") + + def on_ws_error(self, ws, error): + self.disconnect_ws() + self.connect_ws() + + def on_message(self, ws, msg): + try: + data = json.loads(msg) + + if not "messages" in data: + return + + for message_str in data["messages"]: + message_data = json.loads(message_str) + if message_data["message_type"] != "subscriptionUpdate": + continue + message = message_data["payload"]["data"]["messageAdded"] + + copied_dict = self.active_messages.copy() + for key, value in copied_dict.items(): + # add the message to the appropriate queue + if value == message["messageId"] and key in self.message_queues: + self.message_queues[key].put(message) + return + + # indicate that the response id is tied to the human message id + elif key != "pending" and value == None and message["state"] != "complete": + self.active_messages[key] = message["messageId"] + self.message_queues[key].put(message) + return + + except Exception: + logger.error(traceback.format_exc()) + self.disconnect_ws() + self.connect_ws() + + def send_message(self, chatbot, message, with_chat_break=False, timeout=20): + # if there is another active message, wait until it has finished sending + while None in self.active_messages.values(): + time.sleep(0.01) + + # None indicates that a message is still in progress + self.active_messages["pending"] = None + + logger.info(f"Sending message to {chatbot}: {message}") + + # reconnect websocket + if not self.ws_connected: + self.disconnect_ws() + self.setup_connection() + self.connect_ws() + + message_data = self.send_query( + "SendMessageMutation", + { + "bot": chatbot, + "query": message, + "chatId": self.bots[chatbot]["chatId"], + "source": None, + "withChatBreak": with_chat_break, + }, + ) + del self.active_messages["pending"] + + if not message_data["data"]["messageEdgeCreate"]["message"]: + raise RuntimeError(f"Daily limit reached for {chatbot}.") + try: + human_message = message_data["data"]["messageEdgeCreate"]["message"] + human_message_id = human_message["node"]["messageId"] + except TypeError: + raise RuntimeError(f"An unknown error occurred. Raw response data: {message_data}") + + # indicate that the current message is waiting for a response + self.active_messages[human_message_id] = None + self.message_queues[human_message_id] = queue.Queue() + + last_text = "" + message_id = None + while True: + try: + message = self.message_queues[human_message_id].get(timeout=timeout) + except queue.Empty: + del self.active_messages[human_message_id] + del self.message_queues[human_message_id] + raise RuntimeError("Response timed out.") + + # only break when the message is marked as complete + if message["state"] == "complete": + if last_text and message["messageId"] == message_id: + break + else: + continue + + # update info about response + message["text_new"] = message["text"][len(last_text) :] + last_text = message["text"] + message_id = message["messageId"] + + yield message + + del self.active_messages[human_message_id] + del self.message_queues[human_message_id] + + def send_chat_break(self, chatbot): + logger.info(f"Sending chat break to {chatbot}") + result = self.send_query("AddMessageBreakMutation", {"chatId": self.bots[chatbot]["chatId"]}) + return result["data"]["messageBreakCreate"]["message"] + + def get_message_history(self, chatbot, count=25, cursor=None): + logger.info(f"Downloading {count} messages from {chatbot}") + + messages = [] + if cursor == None: + chat_data = self.get_bot(self.bot_names[chatbot]) + if not chat_data["messagesConnection"]["edges"]: + return [] + messages = chat_data["messagesConnection"]["edges"][:count] + cursor = chat_data["messagesConnection"]["pageInfo"]["startCursor"] + count -= len(messages) + + cursor = str(cursor) + if count > 50: + messages = self.get_message_history(chatbot, count=50, cursor=cursor) + messages + while count > 0: + count -= 50 + new_cursor = messages[0]["cursor"] + new_messages = self.get_message_history(chatbot, min(50, count), cursor=new_cursor) + messages = new_messages + messages + return messages + elif count <= 0: + return messages + + result = self.send_query( + "ChatListPaginationQuery", + {"count": count, "cursor": cursor, "id": self.bots[chatbot]["id"]}, + ) + query_messages = result["data"]["node"]["messagesConnection"]["edges"] + messages = query_messages + messages + return messages + + def delete_message(self, message_ids): + logger.info(f"Deleting messages: {message_ids}") + if not type(message_ids) is list: + message_ids = [int(message_ids)] + + result = self.send_query("DeleteMessageMutation", {"messageIds": message_ids}) + + def purge_conversation(self, chatbot, count=-1): + logger.info(f"Purging messages from {chatbot}") + last_messages = self.get_message_history(chatbot, count=50)[::-1] + while last_messages: + message_ids = [] + for message in last_messages: + if count == 0: + break + count -= 1 + message_ids.append(message["node"]["messageId"]) + + self.delete_message(message_ids) + + if count == 0: + return + last_messages = self.get_message_history(chatbot, count=50)[::-1] + logger.info(f"No more messages left to delete.") + + def create_bot( + self, + handle, + prompt="", + base_model="chinchilla", + description="", + intro_message="", + api_key=None, + api_bot=False, + api_url=None, + prompt_public=True, + pfp_url=None, + linkification=False, + markdown_rendering=True, + suggested_replies=False, + private=False, + ): + result = self.send_query( + "PoeBotCreateMutation", + { + "model": base_model, + "handle": handle, + "prompt": prompt, + "isPromptPublic": prompt_public, + "introduction": intro_message, + "description": description, + "profilePictureUrl": pfp_url, + "apiUrl": api_url, + "apiKey": api_key, + "isApiBot": api_bot, + "hasLinkification": linkification, + "hasMarkdownRendering": markdown_rendering, + "hasSuggestedReplies": suggested_replies, + "isPrivateBot": private, + }, + ) + + data = result["data"]["poeBotCreate"] + if data["status"] != "success": + raise RuntimeError(f"Poe returned an error while trying to create a bot: {data['status']}") + self.get_bots() + return data + + def edit_bot( + self, + bot_id, + handle, + prompt="", + base_model="chinchilla", + description="", + intro_message="", + api_key=None, + api_url=None, + private=False, + prompt_public=True, + pfp_url=None, + linkification=False, + markdown_rendering=True, + suggested_replies=False, + ): + result = self.send_query( + "PoeBotEditMutation", + { + "baseBot": base_model, + "botId": bot_id, + "handle": handle, + "prompt": prompt, + "isPromptPublic": prompt_public, + "introduction": intro_message, + "description": description, + "profilePictureUrl": pfp_url, + "apiUrl": api_url, + "apiKey": api_key, + "hasLinkification": linkification, + "hasMarkdownRendering": markdown_rendering, + "hasSuggestedReplies": suggested_replies, + "isPrivateBot": private, + }, + ) + + data = result["data"]["poeBotEdit"] + if data["status"] != "success": + raise RuntimeError(f"Poe returned an error while trying to edit a bot: {data['status']}") + self.get_bots() + return data + + +load_queries() diff --git a/quora/cookies.txt b/openai_rev/quora/cookies.txt index 9cccf6ba..9cccf6ba 100644 --- a/quora/cookies.txt +++ b/openai_rev/quora/cookies.txt diff --git a/quora/graphql/AddHumanMessageMutation.graphql b/openai_rev/quora/graphql/AddHumanMessageMutation.graphql index 01e6bc8c..01e6bc8c 100644 --- a/quora/graphql/AddHumanMessageMutation.graphql +++ b/openai_rev/quora/graphql/AddHumanMessageMutation.graphql diff --git a/quora/graphql/AddMessageBreakMutation.graphql b/openai_rev/quora/graphql/AddMessageBreakMutation.graphql index b28d9903..b28d9903 100644 --- a/quora/graphql/AddMessageBreakMutation.graphql +++ b/openai_rev/quora/graphql/AddMessageBreakMutation.graphql diff --git a/quora/graphql/AutoSubscriptionMutation.graphql b/openai_rev/quora/graphql/AutoSubscriptionMutation.graphql index 6cf7bf74..6cf7bf74 100644 --- a/quora/graphql/AutoSubscriptionMutation.graphql +++ b/openai_rev/quora/graphql/AutoSubscriptionMutation.graphql diff --git a/quora/graphql/BioFragment.graphql b/openai_rev/quora/graphql/BioFragment.graphql index c4218030..c4218030 100644 --- a/quora/graphql/BioFragment.graphql +++ b/openai_rev/quora/graphql/BioFragment.graphql diff --git a/quora/graphql/ChatAddedSubscription.graphql b/openai_rev/quora/graphql/ChatAddedSubscription.graphql index 664b107f..664b107f 100644 --- a/quora/graphql/ChatAddedSubscription.graphql +++ b/openai_rev/quora/graphql/ChatAddedSubscription.graphql diff --git a/quora/graphql/ChatFragment.graphql b/openai_rev/quora/graphql/ChatFragment.graphql index 605645ff..605645ff 100644 --- a/quora/graphql/ChatFragment.graphql +++ b/openai_rev/quora/graphql/ChatFragment.graphql diff --git a/quora/graphql/ChatListPaginationQuery.graphql b/openai_rev/quora/graphql/ChatListPaginationQuery.graphql index 6d9ae884..6d9ae884 100644 --- a/quora/graphql/ChatListPaginationQuery.graphql +++ b/openai_rev/quora/graphql/ChatListPaginationQuery.graphql diff --git a/quora/graphql/ChatPaginationQuery.graphql b/openai_rev/quora/graphql/ChatPaginationQuery.graphql index f2452cd6..f2452cd6 100644 --- a/quora/graphql/ChatPaginationQuery.graphql +++ b/openai_rev/quora/graphql/ChatPaginationQuery.graphql diff --git a/quora/graphql/ChatViewQuery.graphql b/openai_rev/quora/graphql/ChatViewQuery.graphql index c330107d..c330107d 100644 --- a/quora/graphql/ChatViewQuery.graphql +++ b/openai_rev/quora/graphql/ChatViewQuery.graphql diff --git a/quora/graphql/DeleteHumanMessagesMutation.graphql b/openai_rev/quora/graphql/DeleteHumanMessagesMutation.graphql index 42692c6e..42692c6e 100644 --- a/quora/graphql/DeleteHumanMessagesMutation.graphql +++ b/openai_rev/quora/graphql/DeleteHumanMessagesMutation.graphql diff --git a/quora/graphql/DeleteMessageMutation.graphql b/openai_rev/quora/graphql/DeleteMessageMutation.graphql index 7b9e36d4..7b9e36d4 100644 --- a/quora/graphql/DeleteMessageMutation.graphql +++ b/openai_rev/quora/graphql/DeleteMessageMutation.graphql diff --git a/quora/graphql/HandleFragment.graphql b/openai_rev/quora/graphql/HandleFragment.graphql index f53c484b..f53c484b 100644 --- a/quora/graphql/HandleFragment.graphql +++ b/openai_rev/quora/graphql/HandleFragment.graphql diff --git a/quora/graphql/LoginWithVerificationCodeMutation.graphql b/openai_rev/quora/graphql/LoginWithVerificationCodeMutation.graphql index 723b1f44..723b1f44 100644 --- a/quora/graphql/LoginWithVerificationCodeMutation.graphql +++ b/openai_rev/quora/graphql/LoginWithVerificationCodeMutation.graphql diff --git a/quora/graphql/MessageAddedSubscription.graphql b/openai_rev/quora/graphql/MessageAddedSubscription.graphql index 8dc9499c..8dc9499c 100644 --- a/quora/graphql/MessageAddedSubscription.graphql +++ b/openai_rev/quora/graphql/MessageAddedSubscription.graphql diff --git a/quora/graphql/MessageDeletedSubscription.graphql b/openai_rev/quora/graphql/MessageDeletedSubscription.graphql index 54c1c164..54c1c164 100644 --- a/quora/graphql/MessageDeletedSubscription.graphql +++ b/openai_rev/quora/graphql/MessageDeletedSubscription.graphql diff --git a/quora/graphql/MessageFragment.graphql b/openai_rev/quora/graphql/MessageFragment.graphql index cc860811..cc860811 100644 --- a/quora/graphql/MessageFragment.graphql +++ b/openai_rev/quora/graphql/MessageFragment.graphql diff --git a/quora/graphql/MessageRemoveVoteMutation.graphql b/openai_rev/quora/graphql/MessageRemoveVoteMutation.graphql index d5e6e610..d5e6e610 100644 --- a/quora/graphql/MessageRemoveVoteMutation.graphql +++ b/openai_rev/quora/graphql/MessageRemoveVoteMutation.graphql diff --git a/quora/graphql/MessageSetVoteMutation.graphql b/openai_rev/quora/graphql/MessageSetVoteMutation.graphql index 76000df0..76000df0 100644 --- a/quora/graphql/MessageSetVoteMutation.graphql +++ b/openai_rev/quora/graphql/MessageSetVoteMutation.graphql diff --git a/quora/graphql/PoeBotCreateMutation.graphql b/openai_rev/quora/graphql/PoeBotCreateMutation.graphql index 971b4248..971b4248 100644 --- a/quora/graphql/PoeBotCreateMutation.graphql +++ b/openai_rev/quora/graphql/PoeBotCreateMutation.graphql diff --git a/quora/graphql/PoeBotEditMutation.graphql b/openai_rev/quora/graphql/PoeBotEditMutation.graphql index fdd309ef..fdd309ef 100644 --- a/quora/graphql/PoeBotEditMutation.graphql +++ b/openai_rev/quora/graphql/PoeBotEditMutation.graphql diff --git a/quora/graphql/SendMessageMutation.graphql b/openai_rev/quora/graphql/SendMessageMutation.graphql index 4b0a4383..4b0a4383 100644 --- a/quora/graphql/SendMessageMutation.graphql +++ b/openai_rev/quora/graphql/SendMessageMutation.graphql diff --git a/quora/graphql/SendVerificationCodeForLoginMutation.graphql b/openai_rev/quora/graphql/SendVerificationCodeForLoginMutation.graphql index 45af4799..45af4799 100644 --- a/quora/graphql/SendVerificationCodeForLoginMutation.graphql +++ b/openai_rev/quora/graphql/SendVerificationCodeForLoginMutation.graphql diff --git a/quora/graphql/ShareMessagesMutation.graphql b/openai_rev/quora/graphql/ShareMessagesMutation.graphql index 92e80db5..92e80db5 100644 --- a/quora/graphql/ShareMessagesMutation.graphql +++ b/openai_rev/quora/graphql/ShareMessagesMutation.graphql diff --git a/quora/graphql/SignupWithVerificationCodeMutation.graphql b/openai_rev/quora/graphql/SignupWithVerificationCodeMutation.graphql index 06b2826f..06b2826f 100644 --- a/quora/graphql/SignupWithVerificationCodeMutation.graphql +++ b/openai_rev/quora/graphql/SignupWithVerificationCodeMutation.graphql diff --git a/quora/graphql/StaleChatUpdateMutation.graphql b/openai_rev/quora/graphql/StaleChatUpdateMutation.graphql index de203d47..de203d47 100644 --- a/quora/graphql/StaleChatUpdateMutation.graphql +++ b/openai_rev/quora/graphql/StaleChatUpdateMutation.graphql diff --git a/quora/graphql/SubscriptionsMutation.graphql b/openai_rev/quora/graphql/SubscriptionsMutation.graphql index b864bd60..b864bd60 100644 --- a/quora/graphql/SubscriptionsMutation.graphql +++ b/openai_rev/quora/graphql/SubscriptionsMutation.graphql diff --git a/quora/graphql/SummarizePlainPostQuery.graphql b/openai_rev/quora/graphql/SummarizePlainPostQuery.graphql index afa2a84c..afa2a84c 100644 --- a/quora/graphql/SummarizePlainPostQuery.graphql +++ b/openai_rev/quora/graphql/SummarizePlainPostQuery.graphql diff --git a/quora/graphql/SummarizeQuotePostQuery.graphql b/openai_rev/quora/graphql/SummarizeQuotePostQuery.graphql index 5147c3c5..5147c3c5 100644 --- a/quora/graphql/SummarizeQuotePostQuery.graphql +++ b/openai_rev/quora/graphql/SummarizeQuotePostQuery.graphql diff --git a/quora/graphql/SummarizeSharePostQuery.graphql b/openai_rev/quora/graphql/SummarizeSharePostQuery.graphql index cb4a623c..cb4a623c 100644 --- a/quora/graphql/SummarizeSharePostQuery.graphql +++ b/openai_rev/quora/graphql/SummarizeSharePostQuery.graphql diff --git a/quora/graphql/UserSnippetFragment.graphql b/openai_rev/quora/graphql/UserSnippetFragment.graphql index 17fc8426..17fc8426 100644 --- a/quora/graphql/UserSnippetFragment.graphql +++ b/openai_rev/quora/graphql/UserSnippetFragment.graphql diff --git a/quora/graphql/ViewerInfoQuery.graphql b/openai_rev/quora/graphql/ViewerInfoQuery.graphql index 1ecaf9e8..1ecaf9e8 100644 --- a/quora/graphql/ViewerInfoQuery.graphql +++ b/openai_rev/quora/graphql/ViewerInfoQuery.graphql diff --git a/quora/graphql/ViewerStateFragment.graphql b/openai_rev/quora/graphql/ViewerStateFragment.graphql index 3cd83e9c..3cd83e9c 100644 --- a/quora/graphql/ViewerStateFragment.graphql +++ b/openai_rev/quora/graphql/ViewerStateFragment.graphql diff --git a/quora/graphql/ViewerStateUpdatedSubscription.graphql b/openai_rev/quora/graphql/ViewerStateUpdatedSubscription.graphql index 03fc73d1..03fc73d1 100644 --- a/quora/graphql/ViewerStateUpdatedSubscription.graphql +++ b/openai_rev/quora/graphql/ViewerStateUpdatedSubscription.graphql diff --git a/quora/graphql/__init__.py b/openai_rev/quora/graphql/__init__.py index e69de29b..e69de29b 100644 --- a/quora/graphql/__init__.py +++ b/openai_rev/quora/graphql/__init__.py diff --git a/openai_rev/quora/mail.py b/openai_rev/quora/mail.py new file mode 100644 index 00000000..864d9568 --- /dev/null +++ b/openai_rev/quora/mail.py @@ -0,0 +1,80 @@ +from json import loads +from re import findall +from time import sleep + +from fake_useragent import UserAgent +from requests import Session + + +class Emailnator: + def __init__(self) -> None: + self.client = Session() + self.client.get("https://www.emailnator.com/", timeout=6) + self.cookies = self.client.cookies.get_dict() + + self.client.headers = { + "authority": "www.emailnator.com", + "origin": "https://www.emailnator.com", + "referer": "https://www.emailnator.com/", + "user-agent": UserAgent().random, + "x-xsrf-token": self.client.cookies.get("XSRF-TOKEN")[:-3] + "=", + } + + self.email = None + + def get_mail(self): + response = self.client.post( + "https://www.emailnator.com/generate-email", + json={ + "email": [ + "domain", + "plusGmail", + "dotGmail", + ] + }, + ) + + self.email = loads(response.text)["email"][0] + return self.email + + def get_message(self): + print("Waiting for message...") + + while True: + sleep(2) + mail_token = self.client.post("https://www.emailnator.com/message-list", json={"email": self.email}) + + mail_token = loads(mail_token.text)["messageData"] + + if len(mail_token) == 2: + print("Message received!") + print(mail_token[1]["messageID"]) + break + + mail_context = self.client.post( + "https://www.emailnator.com/message-list", + json={ + "email": self.email, + "messageID": mail_token[1]["messageID"], + }, + ) + + return mail_context.text + + def get_verification_code(self): + message = self.get_message() + code = findall(r';">(\d{6,7})</div>', message)[0] + print(f"Verification code: {code}") + return code + + def clear_inbox(self): + print("Clearing inbox...") + self.client.post( + "https://www.emailnator.com/delete-all", + json={"email": self.email}, + ) + print("Inbox cleared!") + + def __del__(self): + if self.email: + self.clear_inbox() |