Update api/validate.py
Browse files- api/validate.py +27 -32
api/validate.py
CHANGED
@@ -2,66 +2,61 @@ import requests
|
|
2 |
import re
|
3 |
import time
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
"User-Agent": (
|
8 |
-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
9 |
-
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
10 |
-
"Chrome/91.0.4472.124 Safari/537.36"
|
11 |
-
)
|
12 |
}
|
13 |
|
14 |
# Cache variables
|
15 |
-
|
16 |
-
|
17 |
-
CACHE_DURATION = 36000 # Cache duration in seconds (10
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
global _cached_hid, _cache_time
|
22 |
current_time = time.time()
|
23 |
|
24 |
-
#
|
25 |
-
if not force_refresh and
|
26 |
-
print("
|
27 |
-
return
|
28 |
|
29 |
try:
|
30 |
-
#
|
31 |
-
response = requests.get(
|
32 |
response.raise_for_status()
|
33 |
content = response.text
|
34 |
|
35 |
-
#
|
36 |
pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
|
37 |
match = re.search(pattern, content)
|
38 |
|
39 |
if match:
|
40 |
-
#
|
41 |
js_path = match.group()
|
42 |
-
full_url = f"{
|
43 |
|
44 |
-
#
|
45 |
-
js_response = requests.get(full_url, headers=
|
46 |
js_response.raise_for_status()
|
47 |
|
48 |
-
#
|
49 |
h_pattern = r'h="([0-9a-f-]+)"'
|
50 |
h_match = re.search(h_pattern, js_response.text)
|
51 |
|
52 |
if h_match:
|
53 |
h_value = h_match.group(1)
|
54 |
-
print("
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
return h_value
|
59 |
else:
|
60 |
-
print("h-value
|
61 |
return None
|
62 |
else:
|
63 |
-
print("
|
64 |
return None
|
65 |
except requests.exceptions.RequestException as e:
|
66 |
-
print(f"
|
67 |
return None
|
|
|
2 |
import re
|
3 |
import time
|
4 |
|
5 |
+
base_url = "https://www.blackbox.ai"
|
6 |
+
headers = {
|
7 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
|
|
|
|
|
|
|
|
8 |
}
|
9 |
|
10 |
# Cache variables
|
11 |
+
cached_hid = None
|
12 |
+
cache_time = 0
|
13 |
+
CACHE_DURATION = 36000 # Cache duration in seconds (10 hour)
|
14 |
|
15 |
+
def getHid(force_refresh=False):
|
16 |
+
global cached_hid, cache_time
|
|
|
17 |
current_time = time.time()
|
18 |
|
19 |
+
# 检查是否强制刷新或缓存值是否仍然有效
|
20 |
+
if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION:
|
21 |
+
print("using cached_hid:", cached_hid)
|
22 |
+
return cached_hid
|
23 |
|
24 |
try:
|
25 |
+
# 获取初始 HTML 内容
|
26 |
+
response = requests.get(base_url, headers=headers)
|
27 |
response.raise_for_status()
|
28 |
content = response.text
|
29 |
|
30 |
+
# 使用正则表达式查找特定的 static/chunks 路径
|
31 |
pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
|
32 |
match = re.search(pattern, content)
|
33 |
|
34 |
if match:
|
35 |
+
# 构造 JS 文件的完整 URL
|
36 |
js_path = match.group()
|
37 |
+
full_url = f"{base_url}/_next/{js_path}"
|
38 |
|
39 |
+
# 请求 JS 文件内容
|
40 |
+
js_response = requests.get(full_url, headers=headers)
|
41 |
js_response.raise_for_status()
|
42 |
|
43 |
+
# 在 JS 内容中使用正则表达式搜索 h-value
|
44 |
h_pattern = r'h="([0-9a-f-]+)"'
|
45 |
h_match = re.search(h_pattern, js_response.text)
|
46 |
|
47 |
if h_match:
|
48 |
h_value = h_match.group(1)
|
49 |
+
print("找到 h-value:", h_value)
|
50 |
+
# 更新缓存
|
51 |
+
cached_hid = h_value
|
52 |
+
cache_time = current_time
|
53 |
return h_value
|
54 |
else:
|
55 |
+
print("在 JS 内容中未找到 h-value")
|
56 |
return None
|
57 |
else:
|
58 |
+
print("在 HTML 内容中未找到指定的 JS 文件路径")
|
59 |
return None
|
60 |
except requests.exceptions.RequestException as e:
|
61 |
+
print(f"发生错误: {e}")
|
62 |
return None
|