File size: 5,148 Bytes
f4b4235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import string
import time
import re
import json
import requests
import fake_useragent
import random
from password_generator import PasswordGenerator

from .utils import create_email, check_email


class Account:
    @staticmethod
    def create(logging: bool = False):
        is_custom_domain = input(
            "Do you want to use your custom domain name for temporary email? [Y/n]: "
        ).upper()

        if is_custom_domain == "Y":
            mail_address = create_email(custom_domain=True, logging=logging)
        elif is_custom_domain == "N":
            mail_address = create_email(custom_domain=False, logging=logging)
        else:
            print("Please, enter either Y or N")
            return

        name = string.ascii_lowercase + string.digits
        username = "".join(random.choice(name) for i in range(20))

        pwo = PasswordGenerator()
        pwo.minlen = 8
        password = pwo.generate()

        session = requests.Session()

        register_url = "https://ai.usesless.com/api/cms/auth/local/register"
        register_json = {
            "username": username,
            "password": password,
            "email": mail_address,
        }
        headers = {
            "authority": "ai.usesless.com",
            "accept": "application/json, text/plain, */*",
            "accept-language": "en-US,en;q=0.5",
            "cache-control": "no-cache",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": fake_useragent.UserAgent().random,
        }
        register = session.post(register_url, json=register_json, headers=headers)
        if logging:
            if register.status_code == 200:
                print("Registered successfully")
            else:
                print(register.status_code)
                print(register.json())
                print("There was a problem with account registration, try again")

        if register.status_code != 200:
            quit()

        while True:
            time.sleep(5)
            messages = check_email(mail=mail_address, logging=logging)

            # Check if method `message_list()` didn't return None or empty list.
            if not messages or len(messages) == 0:
                # If it returned None or empty list sleep for 5 seconds to wait for new message.
                continue

            message_text = messages[0]["content"]
            verification_url = re.findall(
                r"http:\/\/ai\.usesless\.com\/api\/cms\/auth\/email-confirmation\?confirmation=\w.+\w\w",
                message_text,
            )[0]
            if verification_url:
                break

        session.get(verification_url)
        login_json = {"identifier": mail_address, "password": password}
        login_request = session.post(
            url="https://ai.usesless.com/api/cms/auth/local", json=login_json
        )

        token = login_request.json()["jwt"]
        if logging and token:
            print(f"Token: {token}")

        with open("account.json", "w") as file:
            json.dump({"email": mail_address, "token": token}, file)
            if logging:
                print(
                    "\nNew account credentials has been successfully saved in 'account.json' file"
                )

        return token


class Completion:
    @staticmethod
    def create(
        token: str,
        systemMessage: str = "You are a helpful assistant",
        prompt: str = "",
        parentMessageId: str = "",
        presence_penalty: float = 1,
        temperature: float = 1,
        model: str = "gpt-3.5-turbo",
    ):
        headers = {
            "authority": "ai.usesless.com",
            "accept": "application/json, text/plain, */*",
            "accept-language": "en-US,en;q=0.5",
            "cache-control": "no-cache",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": fake_useragent.UserAgent().random,
            "Authorization": f"Bearer {token}",
        }

        json_data = {
            "openaiKey": "",
            "prompt": prompt,
            "options": {
                "parentMessageId": parentMessageId,
                "systemMessage": systemMessage,
                "completionParams": {
                    "presence_penalty": presence_penalty,
                    "temperature": temperature,
                    "model": model,
                },
            },
        }

        url = "https://ai.usesless.com/api/chat-process"
        request = requests.post(url, headers=headers, json=json_data)
        request.encoding = request.apparent_encoding
        content = request.content

        response = Completion.__response_to_json(content)
        return response


    @classmethod
    def __response_to_json(cls, text) -> str:
        text = str(text.decode("utf-8"))

        split_text = text.rsplit("\n", 1)
        if len(split_text) > 1:
            to_json = json.loads(split_text[1])
            return to_json
        else:
            return None