File size: 3,971 Bytes
70c1e23
 
b2e11d6
 
 
70c1e23
 
 
 
 
44c5e78
70c1e23
44c5e78
70c1e23
44c5e78
70c1e23
 
 
b2e11d6
 
 
 
70c1e23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2e11d6
70c1e23
 
 
 
 
 
 
b2e11d6
 
 
70c1e23
 
 
 
 
 
 
 
 
b2e11d6
70c1e23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import requests
import random
import string

from datetime import datetime
from pathlib import Path
from utils.logger import logger


class CookiesConstructor:
    bypass_url = "https://zklcdc-go-bingai-pass.hf.space"

    def __init__(self):
        self.cookies = {}
        self.secrets_path = Path(__file__).parents[1] / "secrets.json"
        self.created_datetime_format = "%Y-%m-%d %H:%M:%S"

    def generate_kiev_rps_sec_auth(self):
        kiev = "".join(random.choices(string.ascii_uppercase + string.digits, k=32))
        return kiev

    def create_secrets_json(self):
        if not self.secrets_path.exists():
            self.secrets_path.parent.mkdir(parents=True, exist_ok=True)
            with open(self.secrets_path, "w") as wf:
                json.dump({}, wf)

    def is_local_cookies_valid(self):
        self.create_secrets_json()
        if self.secrets_path.exists():
            with open(self.secrets_path, "r") as f:
                secrets = json.load(f)
            if secrets.get("cookies"):
                cookies = secrets["cookies"]
                cookies_str = cookies.get("cookies_str")
                cookies_created_datetime = datetime.strptime(
                    cookies.get("created_time"), self.created_datetime_format
                )
                datetime_now = datetime.now()
                # if cookies created more than 12 hours, then it's invalid
                self.cookies_created_seconds = (
                    datetime_now - cookies_created_datetime
                ).seconds
                if self.cookies_created_seconds < 12 * 60 * 60:
                    self.cookies_str = cookies_str
                    self.cookies_created_datetime = cookies_created_datetime
                    return True
                else:
                    return False
        return False

    def requests_cookies(self):
        if self.is_local_cookies_valid():
            logger.success(
                f"Local Cookies Used: {self.cookies_created_datetime} "
                f"({round(self.cookies_created_seconds/60/60,2)} hours ago)"
            )
            return

        requests_body = {"cookies": ""}
        try:
            logger.note(f"Requesting Cookies from: {self.bypass_url}")
            res = requests.post(
                self.bypass_url,
                json=requests_body,
                timeout=15,
            )
            data = res.json()
            cookies_str = data["result"]["cookies"]
            cookies_screenshot = data["result"]["screenshot"]
            kiev = self.generate_kiev_rps_sec_auth()
            cookies_str = f"KievRPSSecAuth={kiev}; {cookies_str}"
            logger.note(f"Get Cookies: {cookies_str}")
            if cookies_str:
                with open(self.secrets_path, "r") as rf:
                    secrets = json.load(rf)
                secrets["cookies"] = {
                    "cookies_str": cookies_str,
                    "created_time": datetime.now().strftime(
                        self.created_datetime_format
                    ),
                    "screenshot": self.bypass_url + cookies_screenshot,
                }
                with open(self.secrets_path, "w") as wf:
                    json.dump(secrets, wf)
        except Exception as e:
            cookies_str = ""
            logger.err(e)

        self.cookies_str = cookies_str

    def cookies_str_to_dict(self):
        cookie_items = self.cookies_str.split(";")
        for cookie_item in cookie_items:
            if not cookie_item:
                continue
            cookie_key, cookie_value = cookie_item.split("=", 1)
            self.cookies[cookie_key.strip()] = cookie_value.strip()
        logger.success(f"Cookies: {self.cookies}")

    def construct(self):
        self.requests_cookies()
        self.cookies_str_to_dict()


if __name__ == "__main__":
    cookies_constructor = CookiesConstructor()
    cookies_constructor.construct()