Niansuh commited on
Commit
0a7708d
·
verified ·
1 Parent(s): 4f37972

Update api/validate.py

Browse files
Files changed (1) hide show
  1. api/validate.py +11 -11
api/validate.py CHANGED
@@ -16,47 +16,47 @@ 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
 
16
  global cached_hid, cache_time
17
  current_time = time.time()
18
 
19
+ # Check if a forced refresh is needed or if the cached values are still valid.
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
+ # Retrieve the initial HTML content.
26
  response = requests.get(base_url, headers=headers)
27
  response.raise_for_status()
28
  content = response.text
29
 
30
+ # Use a regular expression to find specific `static/chunks` paths.
31
  pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
32
  match = re.search(pattern, content)
33
 
34
  if match:
35
+ # Construct the full URL of the JS file.
36
  js_path = match.group()
37
  full_url = f"{base_url}/_next/{js_path}"
38
 
39
+ # Request the content of the JS file.
40
  js_response = requests.get(full_url, headers=headers)
41
  js_response.raise_for_status()
42
 
43
+ # In JS content, use a regular expression to search for 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("Found the h-value.", h_value)
50
+ # Update the cache.
51
  cached_hid = h_value
52
  cache_time = current_time
53
  return h_value
54
  else:
55
+ print("The h-value was not found in the JS content.")
56
  return None
57
  else:
58
+ print("The specified JS file path was not found in the HTML content.")
59
  return None
60
  except requests.exceptions.RequestException as e:
61
+ print(f"An error occurred. {e}")
62
  return None