Spaces:
Paused
Paused
| 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 hour) | |
| def getHid(force_refresh=False): | |
| 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: | |
| # 获取初始 HTML 内容 | |
| response = requests.get(base_url, headers=headers) | |
| response.raise_for_status() | |
| content = response.text | |
| # 使用正则表达式查找特定的 static/chunks 路径 | |
| pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js" | |
| match = re.search(pattern, content) | |
| if match: | |
| # 构造 JS 文件的完整 URL | |
| js_path = match.group() | |
| full_url = f"{base_url}/_next/{js_path}" | |
| # 请求 JS 文件内容 | |
| js_response = requests.get(full_url, headers=headers) | |
| js_response.raise_for_status() | |
| # 在 JS 内容中使用正则表达式搜索 h-value | |
| 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("找到 h-value:", h_value) | |
| # 更新缓存 | |
| cached_hid = h_value | |
| cache_time = current_time | |
| return h_value | |
| else: | |
| print("在 JS 内容中未找到 h-value") | |
| return None | |
| else: | |
| print("在 HTML 内容中未找到指定的 JS 文件路径") | |
| return None | |
| except requests.exceptions.RequestException as e: | |
| print(f"发生错误: {e}") | |
| return None | |