Niansuh commited on
Commit
e143070
·
verified ·
1 Parent(s): 99ff4b8

Update api/validate.py

Browse files
Files changed (1) hide show
  1. api/validate.py +27 -32
api/validate.py CHANGED
@@ -2,66 +2,61 @@ import requests
2
  import re
3
  import time
4
 
5
- BASE_URL = "https://www.blackbox.ai"
6
- HEADERS = {
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
- _cached_hid = None
16
- _cache_time = 0
17
- CACHE_DURATION = 36000 # Cache duration in seconds (10 hours)
18
 
19
-
20
- def get_hid(force_refresh=False):
21
- global _cached_hid, _cache_time
22
  current_time = time.time()
23
 
24
- # Use cached value if not expired
25
- if not force_refresh and _cached_hid and (current_time - _cache_time) < CACHE_DURATION:
26
- print("Using cached hid:", _cached_hid)
27
- return _cached_hid
28
 
29
  try:
30
- # Get initial HTML content
31
- response = requests.get(BASE_URL, headers=HEADERS)
32
  response.raise_for_status()
33
  content = response.text
34
 
35
- # Find the specific JS file path
36
  pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
37
  match = re.search(pattern, content)
38
 
39
  if match:
40
- # Construct full JS file URL
41
  js_path = match.group()
42
- full_url = f"{BASE_URL}/_next/{js_path}"
43
 
44
- # Request JS file content
45
- js_response = requests.get(full_url, headers=HEADERS)
46
  js_response.raise_for_status()
47
 
48
- # Search for h-value in JS content
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("Found h-value:", h_value)
55
- # Update cache
56
- _cached_hid = h_value
57
- _cache_time = current_time
58
  return h_value
59
  else:
60
- print("h-value not found in JS content")
61
  return None
62
  else:
63
- print("JS file path not found in HTML content")
64
  return None
65
  except requests.exceptions.RequestException as e:
66
- print(f"An error occurred: {e}")
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