Niansuh commited on
Commit
ce5a8a3
·
verified ·
1 Parent(s): 9f04482

Update api/validate.py

Browse files
Files changed (1) hide show
  1. api/validate.py +22 -20
api/validate.py CHANGED
@@ -1,28 +1,30 @@
1
- # validate.py
2
-
3
  import requests
4
  import re
5
  import time
6
 
7
  BASE_URL = "https://www.blackbox.ai"
8
  HEADERS = {
9
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
10
- "(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
 
 
 
11
  }
12
 
13
  # Cache variables
14
- cached_hid = None
15
- cache_time = 0
16
  CACHE_DURATION = 36000 # Cache duration in seconds (10 hours)
17
 
18
- def getHid(force_refresh=False):
19
- global cached_hid, cache_time
 
20
  current_time = time.time()
21
 
22
- # Check if we should use the cached HID
23
- if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION:
24
- print("Using cached HID:", cached_hid)
25
- return cached_hid
26
 
27
  try:
28
  # Get initial HTML content
@@ -30,35 +32,35 @@ def getHid(force_refresh=False):
30
  response.raise_for_status()
31
  content = response.text
32
 
33
- # Find the specific static/chunks path
34
  pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
35
  match = re.search(pattern, content)
36
 
37
  if match:
38
- # Construct the full URL of the JS file
39
  js_path = match.group()
40
  full_url = f"{BASE_URL}/_next/{js_path}"
41
 
42
- # Request the JS file content
43
  js_response = requests.get(full_url, headers=HEADERS)
44
  js_response.raise_for_status()
45
 
46
- # Search for the h-value in the JS content
47
  h_pattern = r'h="([0-9a-f-]+)"'
48
  h_match = re.search(h_pattern, js_response.text)
49
 
50
  if h_match:
51
  h_value = h_match.group(1)
52
  print("Found h-value:", h_value)
53
- # Update the cache
54
- cached_hid = h_value
55
- cache_time = current_time
56
  return h_value
57
  else:
58
  print("h-value not found in JS content")
59
  return None
60
  else:
61
- print("Specified JS file path not found in HTML content")
62
  return None
63
  except requests.exceptions.RequestException as e:
64
  print(f"An error occurred: {e}")
 
 
 
1
  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
 
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}")