test24 / api /validate.py
Niansuh's picture
Update api/validate.py
44a6b31 verified
raw
history blame
2.07 kB
# validate.py
import requests
import re
import time
BASE_URL = "https://www.blackbox.ai"
HEADERS = {
"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"
}
# Cache variables
cached_hid = None
cache_time = 0
CACHE_DURATION = 36000 # Cache duration in seconds (10 hours)
def getHid(force_refresh=False):
global cached_hid, cache_time
current_time = time.time()
# Check if we should use the cached HID
if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION:
print("Using cached HID:", cached_hid)
return cached_hid
try:
# Get initial HTML content
response = requests.get(BASE_URL, headers=HEADERS)
response.raise_for_status()
content = response.text
# Find the specific static/chunks path
pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
match = re.search(pattern, content)
if match:
# Construct the full URL of the JS file
js_path = match.group()
full_url = f"{BASE_URL}/_next/{js_path}"
# Request the JS file content
js_response = requests.get(full_url, headers=HEADERS)
js_response.raise_for_status()
# Search for the h-value in the JS content
h_pattern = r'h="([0-9a-f-]+)"'
h_match = re.search(h_pattern, js_response.text)
if h_match:
h_value = h_match.group(1)
print("Found h-value:", h_value)
# Update the cache
cached_hid = h_value
cache_time = current_time
return h_value
else:
print("h-value not found in JS content")
return None
else:
print("Specified JS file path not found in HTML content")
return None
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None