Upload 15 files
Browse files- api/config.py +2 -1
- api/utils.py +11 -2
- api/validate.py +62 -0
- requirements.txt +1 -0
api/config.py
CHANGED
|
@@ -7,9 +7,10 @@ BASE_URL = "https://www.blackbox.ai"
|
|
| 7 |
headers = {
|
| 8 |
'accept': '*/*',
|
| 9 |
'accept-language': 'zh-CN,zh;q=0.9',
|
|
|
|
| 10 |
'origin': 'https://www.blackbox.ai',
|
| 11 |
'priority': 'u=1, i',
|
| 12 |
-
'sec-ch-ua': '"
|
| 13 |
'sec-ch-ua-mobile': '?0',
|
| 14 |
'sec-ch-ua-platform': '"Windows"',
|
| 15 |
'sec-fetch-dest': 'empty',
|
|
|
|
| 7 |
headers = {
|
| 8 |
'accept': '*/*',
|
| 9 |
'accept-language': 'zh-CN,zh;q=0.9',
|
| 10 |
+
'content-type': 'application/json',
|
| 11 |
'origin': 'https://www.blackbox.ai',
|
| 12 |
'priority': 'u=1, i',
|
| 13 |
+
'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
|
| 14 |
'sec-ch-ua-mobile': '?0',
|
| 15 |
'sec-ch-ua-platform': '"Windows"',
|
| 16 |
'sec-fetch-dest': 'empty',
|
api/utils.py
CHANGED
|
@@ -5,6 +5,7 @@ from typing import Any, Dict, Optional
|
|
| 5 |
import uuid
|
| 6 |
|
| 7 |
import httpx
|
|
|
|
| 8 |
from api.config import MODEL_MAPPING, headers
|
| 9 |
from fastapi import Depends, security
|
| 10 |
from fastapi.security import HTTPAuthorizationCredentials
|
|
@@ -80,7 +81,7 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 80 |
"visitFromDelta": False,
|
| 81 |
"mobileClient": False,
|
| 82 |
"userSelectedModel": MODEL_MAPPING.get(request.model),
|
| 83 |
-
"validated":
|
| 84 |
}
|
| 85 |
|
| 86 |
async with httpx.AsyncClient() as client:
|
|
@@ -97,6 +98,11 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 97 |
timestamp = int(datetime.now().timestamp())
|
| 98 |
if line:
|
| 99 |
content = line + "\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
| 101 |
yield f"data: {json.dumps(create_chat_completion_data(content[21:], request.model, timestamp))}\n\n"
|
| 102 |
else:
|
|
@@ -133,7 +139,7 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
| 133 |
"visitFromDelta": False,
|
| 134 |
"mobileClient": False,
|
| 135 |
"userSelectedModel": MODEL_MAPPING.get(request.model),
|
| 136 |
-
"validated":
|
| 137 |
}
|
| 138 |
full_response = ""
|
| 139 |
async with httpx.AsyncClient() as client:
|
|
@@ -142,6 +148,9 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
| 142 |
) as response:
|
| 143 |
async for chunk in response.aiter_text():
|
| 144 |
full_response += chunk
|
|
|
|
|
|
|
|
|
|
| 145 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
| 146 |
full_response = full_response[21:]
|
| 147 |
return {
|
|
|
|
| 5 |
import uuid
|
| 6 |
|
| 7 |
import httpx
|
| 8 |
+
from api import validate
|
| 9 |
from api.config import MODEL_MAPPING, headers
|
| 10 |
from fastapi import Depends, security
|
| 11 |
from fastapi.security import HTTPAuthorizationCredentials
|
|
|
|
| 81 |
"visitFromDelta": False,
|
| 82 |
"mobileClient": False,
|
| 83 |
"userSelectedModel": MODEL_MAPPING.get(request.model),
|
| 84 |
+
"validated": validate.getHid()
|
| 85 |
}
|
| 86 |
|
| 87 |
async with httpx.AsyncClient() as client:
|
|
|
|
| 98 |
timestamp = int(datetime.now().timestamp())
|
| 99 |
if line:
|
| 100 |
content = line + "\n"
|
| 101 |
+
if "https://www.blackbox.ai" in content:
|
| 102 |
+
validate.getHid(True)
|
| 103 |
+
content = "hid已刷新,重新对话即可\n"
|
| 104 |
+
yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
|
| 105 |
+
break
|
| 106 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
| 107 |
yield f"data: {json.dumps(create_chat_completion_data(content[21:], request.model, timestamp))}\n\n"
|
| 108 |
else:
|
|
|
|
| 139 |
"visitFromDelta": False,
|
| 140 |
"mobileClient": False,
|
| 141 |
"userSelectedModel": MODEL_MAPPING.get(request.model),
|
| 142 |
+
"validated": validate.getHid()
|
| 143 |
}
|
| 144 |
full_response = ""
|
| 145 |
async with httpx.AsyncClient() as client:
|
|
|
|
| 148 |
) as response:
|
| 149 |
async for chunk in response.aiter_text():
|
| 150 |
full_response += chunk
|
| 151 |
+
if "https://www.blackbox.ai" in full_response:
|
| 152 |
+
validate.getHid(True)
|
| 153 |
+
full_response = "hid已刷新,重新对话即可"
|
| 154 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
| 155 |
full_response = full_response[21:]
|
| 156 |
return {
|
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 hour)
|
| 14 |
+
|
| 15 |
+
def getHid(force_refresh=False):
|
| 16 |
+
global cached_hid, cache_time
|
| 17 |
+
current_time = time.time()
|
| 18 |
+
|
| 19 |
+
# 检查是否强制刷新或缓存值是否仍然有效
|
| 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 |
+
# 获取初始 HTML 内容
|
| 26 |
+
response = requests.get(base_url, headers=headers)
|
| 27 |
+
response.raise_for_status()
|
| 28 |
+
content = response.text
|
| 29 |
+
|
| 30 |
+
# 使用正则表达式查找特定的 static/chunks 路径
|
| 31 |
+
pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
|
| 32 |
+
match = re.search(pattern, content)
|
| 33 |
+
|
| 34 |
+
if match:
|
| 35 |
+
# 构造 JS 文件的完整 URL
|
| 36 |
+
js_path = match.group()
|
| 37 |
+
full_url = f"{base_url}/_next/{js_path}"
|
| 38 |
+
|
| 39 |
+
# 请求 JS 文件内容
|
| 40 |
+
js_response = requests.get(full_url, headers=headers)
|
| 41 |
+
js_response.raise_for_status()
|
| 42 |
+
|
| 43 |
+
# 在 JS 内容中使用正则表达式搜索 h-value
|
| 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("找到 h-value:", h_value)
|
| 50 |
+
# 更新缓存
|
| 51 |
+
cached_hid = h_value
|
| 52 |
+
cache_time = current_time
|
| 53 |
+
return h_value
|
| 54 |
+
else:
|
| 55 |
+
print("在 JS 内容中未找到 h-value")
|
| 56 |
+
return None
|
| 57 |
+
else:
|
| 58 |
+
print("在 HTML 内容中未找到指定的 JS 文件路径")
|
| 59 |
+
return None
|
| 60 |
+
except requests.exceptions.RequestException as e:
|
| 61 |
+
print(f"发生错误: {e}")
|
| 62 |
+
return None
|
requirements.txt
CHANGED
|
@@ -3,5 +3,6 @@ httpx
|
|
| 3 |
pydantic
|
| 4 |
pyinstaller
|
| 5 |
python-dotenv
|
|
|
|
| 6 |
starlette
|
| 7 |
uvicorn
|
|
|
|
| 3 |
pydantic
|
| 4 |
pyinstaller
|
| 5 |
python-dotenv
|
| 6 |
+
Requests
|
| 7 |
starlette
|
| 8 |
uvicorn
|