feat: 接入黄豆短剧独立清单与下载
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
import json
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from server.services.http import http
|
||||
from server.services.huangdou.crypto import (
|
||||
APP_VERSION,
|
||||
DEVICE_TYPE,
|
||||
LANG,
|
||||
USER_AGENT,
|
||||
decrypt_body,
|
||||
encrypt_body,
|
||||
new_uuid,
|
||||
session_id_of,
|
||||
sign_header,
|
||||
)
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
STATE_DIR = ROOT / "cache" / "huangdou"
|
||||
SESSION_PATH = STATE_DIR / "session.json"
|
||||
|
||||
HOSTS = (
|
||||
"https://pmihlvee.cc",
|
||||
"https://sxqirtho.top",
|
||||
"https://qicuknlj.top",
|
||||
"https://ferncider.cc",
|
||||
"https://hvthtcpa.top",
|
||||
"https://onyxripple.cc",
|
||||
"https://vivifable.top",
|
||||
"https://kucvcxrv.cc",
|
||||
"https://larkgarden.top",
|
||||
"https://niniharbor.top",
|
||||
"https://larksummit.icu",
|
||||
)
|
||||
|
||||
LINE_CODE = "china_1"
|
||||
|
||||
|
||||
class TokenExpired(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class ApiError(RuntimeError):
|
||||
def __init__(self, message: str, code=None) -> None:
|
||||
super().__init__(message)
|
||||
self.code = code
|
||||
|
||||
|
||||
class HuangdouClient:
|
||||
def __init__(self) -> None:
|
||||
self.device_id = new_uuid()
|
||||
self.session_id = session_id_of("")
|
||||
self.token = ""
|
||||
self.host = HOSTS[0]
|
||||
self.profile: dict = {}
|
||||
self._load()
|
||||
|
||||
def _load(self) -> None:
|
||||
if not SESSION_PATH.is_file():
|
||||
return
|
||||
try:
|
||||
data = json.loads(SESSION_PATH.read_text(encoding="utf-8"))
|
||||
except (OSError, json.JSONDecodeError):
|
||||
return
|
||||
if not isinstance(data, dict):
|
||||
return
|
||||
self.device_id = str(data.get("device_id") or self.device_id)
|
||||
self.session_id = session_id_of(str(data.get("session_id") or self.session_id))
|
||||
self.token = str(data.get("token") or "")
|
||||
host = str(data.get("host") or "")
|
||||
if host.startswith("https://"):
|
||||
self.host = host.rstrip("/")
|
||||
profile = data.get("profile")
|
||||
if isinstance(profile, dict):
|
||||
self.profile = profile
|
||||
|
||||
def save(self) -> None:
|
||||
STATE_DIR.mkdir(parents=True, exist_ok=True)
|
||||
payload = {
|
||||
"device_id": self.device_id,
|
||||
"session_id": self.session_id,
|
||||
"token": self.token,
|
||||
"host": self.host,
|
||||
"profile": self.profile,
|
||||
}
|
||||
temporary = SESSION_PATH.with_suffix(".json.tmp")
|
||||
temporary.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
|
||||
temporary.replace(SESSION_PATH)
|
||||
|
||||
def clear_token(self) -> None:
|
||||
self.token = ""
|
||||
self.profile = {}
|
||||
self.save()
|
||||
|
||||
def request(self, path: str, data: dict | None = None, *, auth: bool = True) -> dict | list | None:
|
||||
last_error: Exception | None = None
|
||||
hosts = [self.host, *[item for item in HOSTS if item != self.host]]
|
||||
for host in hosts:
|
||||
try:
|
||||
payload = self._call(host, path, data or {}, auth=auth)
|
||||
self.host = host
|
||||
return payload
|
||||
except (TokenExpired, ApiError):
|
||||
raise
|
||||
except RuntimeError as exc:
|
||||
last_error = exc
|
||||
raise last_error or RuntimeError("网络请求异常")
|
||||
|
||||
def _call(self, host: str, path: str, data: dict, *, auth: bool) -> dict | list | None:
|
||||
url = host.rstrip("/") + "/api" + path
|
||||
request_id = new_uuid()
|
||||
now = str(int(time.time()))
|
||||
body = {
|
||||
"token": self.token if auth else "",
|
||||
"deviceId": self.device_id,
|
||||
"data": data,
|
||||
}
|
||||
headers = {
|
||||
"Accept": "*/*",
|
||||
"Accept-Language": LANG,
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Origin": host,
|
||||
"Referer": host + "/",
|
||||
"User-Agent": USER_AGENT,
|
||||
"version": APP_VERSION,
|
||||
"deviceType": DEVICE_TYPE,
|
||||
"time": now,
|
||||
"sign": sign_header(self.session_id, request_id, now, url),
|
||||
"requestId": request_id,
|
||||
"sessionId": self.session_id,
|
||||
"deviceBrand": "",
|
||||
"deviceModel": "",
|
||||
"systemName": "",
|
||||
"systemVersion": "",
|
||||
"X-Lang": LANG,
|
||||
}
|
||||
try:
|
||||
response = http.send("POST", url, headers=headers, content=encrypt_body(body, request_id), timeout=30.0)
|
||||
except Exception as exc:
|
||||
raise RuntimeError(f"请求失败: {exc}") from exc
|
||||
if response.status_code != 200:
|
||||
raise RuntimeError(f"网络请求异常 ({response.status_code})")
|
||||
parsed = decrypt_body(response.content, request_id)
|
||||
if parsed.get("status") == "n":
|
||||
code = parsed.get("errorCode")
|
||||
if str(code) == "2002":
|
||||
raise TokenExpired("登录已失效")
|
||||
raise ApiError(str(parsed.get("error") or "网络请求异常"), code)
|
||||
return parsed.get("data")
|
||||
|
||||
|
||||
client = HuangdouClient()
|
||||
Reference in New Issue
Block a user