Update api/validate.py
Browse files- 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":
|
10 |
-
|
|
|
|
|
|
|
11 |
}
|
12 |
|
13 |
# Cache variables
|
14 |
-
|
15 |
-
|
16 |
CACHE_DURATION = 36000 # Cache duration in seconds (10 hours)
|
17 |
|
18 |
-
|
19 |
-
|
|
|
20 |
current_time = time.time()
|
21 |
|
22 |
-
#
|
23 |
-
if not force_refresh and
|
24 |
-
print("Using cached
|
25 |
-
return
|
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
|
34 |
pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
|
35 |
match = re.search(pattern, content)
|
36 |
|
37 |
if match:
|
38 |
-
# Construct
|
39 |
js_path = match.group()
|
40 |
full_url = f"{BASE_URL}/_next/{js_path}"
|
41 |
|
42 |
-
# Request
|
43 |
js_response = requests.get(full_url, headers=HEADERS)
|
44 |
js_response.raise_for_status()
|
45 |
|
46 |
-
# Search for
|
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
|
54 |
-
|
55 |
-
|
56 |
return h_value
|
57 |
else:
|
58 |
print("h-value not found in JS content")
|
59 |
return None
|
60 |
else:
|
61 |
-
print("
|
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}")
|