Spaces:
Runtime error
Runtime error
:gem: [Feature] Request cookies to bypass challenge
Browse files
conversations/conversation_creator.py
CHANGED
@@ -1,17 +1,22 @@
|
|
1 |
import requests
|
2 |
from pprint import pprint
|
|
|
3 |
from utils.enver import enver
|
4 |
from networks import ConversationCreateHeadersConstructor
|
|
|
5 |
|
6 |
|
7 |
class ConversationCreator:
|
8 |
conversation_create_url = "https://www.bing.com/turing/conversation/create"
|
9 |
|
10 |
def __init__(self, cookies: dict = {}):
|
11 |
-
self.
|
12 |
|
13 |
def construct_cookies(self):
|
14 |
-
|
|
|
|
|
|
|
15 |
|
16 |
def construct_headers(self):
|
17 |
# New Bing 封锁原理探讨 #78
|
@@ -26,6 +31,7 @@ class ConversationCreator:
|
|
26 |
self.conversation_create_url,
|
27 |
headers=self.request_headers,
|
28 |
proxies=enver.requests_proxies,
|
|
|
29 |
)
|
30 |
try:
|
31 |
self.response_data = self.response.json()
|
|
|
1 |
import requests
|
2 |
from pprint import pprint
|
3 |
+
from utils.logger import logger
|
4 |
from utils.enver import enver
|
5 |
from networks import ConversationCreateHeadersConstructor
|
6 |
+
from networks import CookiesConstructor
|
7 |
|
8 |
|
9 |
class ConversationCreator:
|
10 |
conversation_create_url = "https://www.bing.com/turing/conversation/create"
|
11 |
|
12 |
def __init__(self, cookies: dict = {}):
|
13 |
+
self.request_cookies = cookies
|
14 |
|
15 |
def construct_cookies(self):
|
16 |
+
cookies_constructor = CookiesConstructor()
|
17 |
+
if not self.request_cookies:
|
18 |
+
cookies_constructor.construct()
|
19 |
+
self.request_cookies = cookies_constructor.cookies
|
20 |
|
21 |
def construct_headers(self):
|
22 |
# New Bing 封锁原理探讨 #78
|
|
|
31 |
self.conversation_create_url,
|
32 |
headers=self.request_headers,
|
33 |
proxies=enver.requests_proxies,
|
34 |
+
cookies=self.request_cookies,
|
35 |
)
|
36 |
try:
|
37 |
self.response_data = self.response.json()
|
networks/__init__.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
-
from .chathub_request_payload_constructor import ChathubRequestPayloadConstructor
|
2 |
from .cookies_constructor import CookiesConstructor
|
|
|
3 |
from .conversation_request_headers_constructor import (
|
4 |
ConversationRequestHeadersConstructor,
|
5 |
)
|
|
|
|
|
1 |
from .cookies_constructor import CookiesConstructor
|
2 |
+
from .chathub_request_payload_constructor import ChathubRequestPayloadConstructor
|
3 |
from .conversation_request_headers_constructor import (
|
4 |
ConversationRequestHeadersConstructor,
|
5 |
)
|
networks/cookies_constructor.py
CHANGED
@@ -1,8 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
class CookiesConstructor:
|
2 |
-
|
3 |
-
self.cookies_list = [] # Dumped from Cookie-Editor extension in Browser
|
4 |
|
5 |
-
def
|
6 |
self.cookies = {}
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
from datetime import datetime
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
from utils.logger import logger
|
7 |
+
|
8 |
+
|
9 |
class CookiesConstructor:
|
10 |
+
bypass_url = "https://zklcdc-go-bingai-pass.hf.space"
|
|
|
11 |
|
12 |
+
def __init__(self):
|
13 |
self.cookies = {}
|
14 |
+
self.secrets_path = Path(__file__).parents[1] / "secrets.json"
|
15 |
+
self.created_datetime_format = "%Y-%m-%d %H:%M:%S"
|
16 |
+
|
17 |
+
def create_secrets_json(self):
|
18 |
+
if not self.secrets_path.exists():
|
19 |
+
self.secrets_path.parent.mkdir(parents=True, exist_ok=True)
|
20 |
+
with open(self.secrets_path, "w") as wf:
|
21 |
+
json.dump({}, wf)
|
22 |
+
|
23 |
+
def is_local_cookies_valid(self):
|
24 |
+
self.create_secrets_json()
|
25 |
+
if self.secrets_path.exists():
|
26 |
+
with open(self.secrets_path, "r") as f:
|
27 |
+
secrets = json.load(f)
|
28 |
+
if secrets.get("cookies"):
|
29 |
+
cookies = secrets["cookies"]
|
30 |
+
cookies_str = cookies.get("cookies_str")
|
31 |
+
cookies_created_datetime = datetime.strptime(
|
32 |
+
cookies.get("created_time"), self.created_datetime_format
|
33 |
+
)
|
34 |
+
datetime_now = datetime.now()
|
35 |
+
# if cookies created more than 12 hours, then it's invalid
|
36 |
+
self.cookies_created_seconds = (
|
37 |
+
datetime_now - cookies_created_datetime
|
38 |
+
).seconds
|
39 |
+
if self.cookies_created_seconds < 12 * 60 * 60:
|
40 |
+
self.cookies_str = cookies_str
|
41 |
+
self.cookies_created_datetime = cookies_created_datetime
|
42 |
+
return True
|
43 |
+
else:
|
44 |
+
return False
|
45 |
+
return False
|
46 |
+
|
47 |
+
def requests_cookies(self):
|
48 |
+
if self.is_local_cookies_valid():
|
49 |
+
logger.success(
|
50 |
+
f"Local Cookies Used: {self.cookies_created_datetime} "
|
51 |
+
f"({round(self.cookies_created_seconds/60/60,2)} hours ago)"
|
52 |
+
)
|
53 |
+
return
|
54 |
+
|
55 |
+
requests_body = {"cookies": ""}
|
56 |
+
try:
|
57 |
+
res = requests.post(
|
58 |
+
self.bypass_url,
|
59 |
+
json=requests_body,
|
60 |
+
timeout=15,
|
61 |
+
)
|
62 |
+
data = res.json()
|
63 |
+
cookies_str = data["result"]["cookies"]
|
64 |
+
cookies_snapshot = data["result"]["snapshot"]
|
65 |
+
logger.note(f"Get Cookies: {cookies_str}")
|
66 |
+
if cookies_str:
|
67 |
+
with open(self.secrets_path, "r") as rf:
|
68 |
+
secrets = json.load(rf)
|
69 |
+
secrets["cookies"] = {
|
70 |
+
"cookies_str": cookies_str,
|
71 |
+
"created_time": datetime.now().strftime(
|
72 |
+
self.created_datetime_format
|
73 |
+
),
|
74 |
+
"snapshot": self.bypass_url + cookies_snapshot,
|
75 |
+
}
|
76 |
+
with open(self.secrets_path, "w") as wf:
|
77 |
+
json.dump(secrets, wf)
|
78 |
+
except Exception as e:
|
79 |
+
cookies_str = ""
|
80 |
+
logger.err(e)
|
81 |
+
|
82 |
+
self.cookies_str = cookies_str
|
83 |
+
|
84 |
+
def cookies_str_to_dict(self):
|
85 |
+
cookie_items = self.cookies_str.split(";")
|
86 |
+
for cookie_item in cookie_items:
|
87 |
+
if not cookie_item:
|
88 |
+
continue
|
89 |
+
cookie_key, cookie_value = cookie_item.split("=", 1)
|
90 |
+
self.cookies[cookie_key.strip()] = cookie_value.strip()
|
91 |
+
logger.success(f"Cookies: {self.cookies}")
|
92 |
+
|
93 |
+
def construct(self):
|
94 |
+
self.requests_cookies()
|
95 |
+
self.cookies_str_to_dict()
|
96 |
+
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
cookies_constructor = CookiesConstructor()
|
100 |
+
cookies_constructor.construct()
|