File size: 2,232 Bytes
00a7e34 e2968e5 00a7e34 381d345 00a7e34 e2968e5 00a7e34 e2968e5 00a7e34 e2968e5 aaae064 e2968e5 00a7e34 e2968e5 00a7e34 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import re
import time
import asyncio
import aiohttp
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/131.0.0.0 Safari/537.36',
}
# Cache variables
cached_hid = None
cache_time = 0
CACHE_DURATION = 36000 # Cache duration in seconds (10 hours)
async def getHid(force_refresh: bool = False) -> Optional[str]:
global cached_hid, cache_time
current_time = time.time()
# Check if a forced refresh is needed or if the cached values are still valid.
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 aiohttp.ClientSession(headers=headers) as session:
async with session.get(base_url) as response:
if response.status != 200:
print("Failed to load the page.")
return None
page_content = await response.text()
js_files = re.findall(r'static/chunks/\d{4}-[a-fA-F0-9]+\.js', page_content)
key_pattern = re.compile(r'w="([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"')
for js_file in js_files:
js_url = f"{base_url}/_next/{js_file}"
async with session.get(js_url) as js_response:
if js_response.status == 200:
js_content = await js_response.text()
match = key_pattern.search(js_content)
if match:
h_value = match.group(1)
print("Found the h-value:", h_value)
# Update the cache
cached_hid = h_value
cache_time = current_time
return h_value
print("The h-value was not found in any JS content.")
return None
except Exception as e:
print(f"An error occurred during the request: {e}")
return None
|