|
|
|
|
|
import re |
|
import time |
|
import asyncio |
|
import httpx |
|
from typing import Optional |
|
|
|
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" |
|
} |
|
|
|
|
|
cached_hid = None |
|
cache_time = 0 |
|
CACHE_DURATION = 36000 |
|
|
|
async def getHid(force_refresh: bool = False) -> Optional[str]: |
|
global cached_hid, cache_time |
|
current_time = time.time() |
|
|
|
|
|
if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION: |
|
print("Using cached_hid:", cached_hid) |
|
return cached_hid |
|
|
|
try: |
|
async with httpx.AsyncClient() as client: |
|
|
|
response = await client.get(base_url, headers=headers) |
|
response.raise_for_status() |
|
content = response.text |
|
|
|
|
|
pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js" |
|
match = re.search(pattern, content) |
|
|
|
if match: |
|
|
|
js_path = match.group() |
|
full_url = f"{base_url}/_next/{js_path}" |
|
|
|
|
|
js_response = await client.get(full_url, headers=headers) |
|
js_response.raise_for_status() |
|
|
|
|
|
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 the h-value:", h_value) |
|
|
|
cached_hid = h_value |
|
cache_time = current_time |
|
return h_value |
|
else: |
|
print("The h-value was not found in the JS content.") |
|
return None |
|
else: |
|
print("The specified JS file path was not found in the HTML content.") |
|
return None |
|
except httpx.RequestError as e: |
|
print(f"An error occurred during the request: {e}") |
|
return None |
|
except httpx.HTTPStatusError as e: |
|
print(f"HTTP error occurred: {e}") |
|
return None |
|
|