Niansuh commited on
Commit
0e86a5a
·
verified ·
1 Parent(s): f48dc4d

Create validate.py

Browse files
Files changed (1) hide show
  1. api/validate.py +62 -0
api/validate.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
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 hours)
14
+
15
+ def getHid(force_refresh=False):
16
+ global cached_hid, cache_time
17
+ current_time = time.time()
18
+
19
+ # Check if we need to refresh the hid
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
+ # Get the initial HTML content
26
+ response = requests.get(base_url, headers=headers)
27
+ response.raise_for_status()
28
+ content = response.text
29
+
30
+ # Use regex to find the specific static/chunks path
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 JS file content
40
+ js_response = requests.get(full_url, headers=headers)
41
+ js_response.raise_for_status()
42
+
43
+ # Search for h-value in the JS content
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 h-value:", h_value)
50
+ # Update cache
51
+ cached_hid = h_value
52
+ cache_time = current_time
53
+ return h_value
54
+ else:
55
+ print("Did not find h-value in JS content")
56
+ return None
57
+ else:
58
+ print("Did not find specified JS file path in HTML content")
59
+ return None
60
+ except requests.exceptions.RequestException as e:
61
+ print(f"Error occurred: {e}")
62
+ return None