feat: 将工具更名为 VideosDownTools,并完善黄豆登录与清单
This commit is contained in:
@@ -19,21 +19,15 @@ 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",
|
||||
)
|
||||
DEFAULT_HOST = "https://pmihlvee.cc"
|
||||
|
||||
LINE_CODE = "china_1"
|
||||
DEFAULT_DEVICE_INFO = {
|
||||
"browserName": "Chrome",
|
||||
"language": LANG,
|
||||
"userAgent": USER_AGENT,
|
||||
"platform": "Win32",
|
||||
}
|
||||
|
||||
|
||||
class TokenExpired(RuntimeError):
|
||||
@@ -48,12 +42,20 @@ class ApiError(RuntimeError):
|
||||
|
||||
class HuangdouClient:
|
||||
def __init__(self) -> None:
|
||||
self.device_id = new_uuid()
|
||||
self.session_id = session_id_of("")
|
||||
self.device_id = ""
|
||||
self.session_id = ""
|
||||
self.device_info: dict = {}
|
||||
self.device_info_source = ""
|
||||
self.guest_device_id = ""
|
||||
self.guest_session_id = ""
|
||||
self.guest_device_info: dict = {}
|
||||
self.token = ""
|
||||
self.host = HOSTS[0]
|
||||
self.account_token = ""
|
||||
self.account_username = ""
|
||||
self.host = DEFAULT_HOST
|
||||
self.profile: dict = {}
|
||||
self._load()
|
||||
self._ensure_fingerprint()
|
||||
|
||||
def _load(self) -> None:
|
||||
if not SESSION_PATH.is_file():
|
||||
@@ -65,10 +67,21 @@ class HuangdouClient:
|
||||
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.session_id = str(data.get("session_id") or self.session_id)
|
||||
info = data.get("device_info")
|
||||
if isinstance(info, dict) and info:
|
||||
self.device_info = dict(info)
|
||||
self.device_info_source = str(data.get("device_info_source") or "")
|
||||
self.guest_device_id = str(data.get("guest_device_id") or "")
|
||||
self.guest_session_id = str(data.get("guest_session_id") or "")
|
||||
guest_info = data.get("guest_device_info")
|
||||
if isinstance(guest_info, dict) and guest_info:
|
||||
self.guest_device_info = dict(guest_info)
|
||||
self.token = str(data.get("token") or "")
|
||||
self.account_token = str(data.get("account_token") or "")
|
||||
self.account_username = str(data.get("account_username") or "")
|
||||
host = str(data.get("host") or "")
|
||||
if host.startswith("https://"):
|
||||
if host.startswith("http://") or host.startswith("https://"):
|
||||
self.host = host.rstrip("/")
|
||||
profile = data.get("profile")
|
||||
if isinstance(profile, dict):
|
||||
@@ -79,7 +92,14 @@ class HuangdouClient:
|
||||
payload = {
|
||||
"device_id": self.device_id,
|
||||
"session_id": self.session_id,
|
||||
"device_info": self.device_info,
|
||||
"device_info_source": self.device_info_source,
|
||||
"guest_device_id": self.guest_device_id,
|
||||
"guest_session_id": self.guest_session_id,
|
||||
"guest_device_info": self.guest_device_info,
|
||||
"token": self.token,
|
||||
"account_token": self.account_token,
|
||||
"account_username": self.account_username,
|
||||
"host": self.host,
|
||||
"profile": self.profile,
|
||||
}
|
||||
@@ -87,32 +107,123 @@ class HuangdouClient:
|
||||
temporary.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
|
||||
temporary.replace(SESSION_PATH)
|
||||
|
||||
def _ensure_fingerprint(self) -> None:
|
||||
changed = False
|
||||
if not self.device_id:
|
||||
self.device_id = new_uuid()
|
||||
changed = True
|
||||
if not self.session_id:
|
||||
self.session_id = session_id_of("")
|
||||
changed = True
|
||||
if not self.device_info:
|
||||
self.device_info = dict(DEFAULT_DEVICE_INFO)
|
||||
self.device_info_source = "default"
|
||||
changed = True
|
||||
if changed:
|
||||
self.save()
|
||||
|
||||
def _new_device_info(self) -> dict:
|
||||
return dict(self.device_info) if self.device_info else dict(DEFAULT_DEVICE_INFO)
|
||||
|
||||
def adopt_browser_fingerprint(self, info: dict) -> bool:
|
||||
self._ensure_fingerprint()
|
||||
if self.device_info_source in {"browser", "fixed"}:
|
||||
return False
|
||||
cleaned = {
|
||||
"browserName": str((info or {}).get("browserName") or "").strip() or "Chrome",
|
||||
"language": str((info or {}).get("language") or "").strip() or LANG,
|
||||
"userAgent": str((info or {}).get("userAgent") or "").strip(),
|
||||
"platform": str((info or {}).get("platform") or "").strip() or "Win32",
|
||||
}
|
||||
if not cleaned["userAgent"]:
|
||||
return False
|
||||
self.device_info = cleaned
|
||||
self.device_info_source = "browser"
|
||||
self.save()
|
||||
return True
|
||||
|
||||
def roll_guest_fingerprint(self) -> None:
|
||||
self.guest_device_id = new_uuid()
|
||||
self.guest_session_id = session_id_of("")
|
||||
self.guest_device_info = self._new_device_info()
|
||||
self.token = ""
|
||||
self.save()
|
||||
|
||||
def roll_account_fingerprint(self) -> None:
|
||||
self._ensure_fingerprint()
|
||||
info = dict(self.device_info) if self.device_info else dict(DEFAULT_DEVICE_INFO)
|
||||
self.device_id = new_uuid()
|
||||
self.session_id = session_id_of("")
|
||||
self.device_info = info
|
||||
self.device_info_source = "fixed"
|
||||
self.account_token = ""
|
||||
self.save()
|
||||
|
||||
def clear_token(self) -> None:
|
||||
self.token = ""
|
||||
self.guest_device_id = ""
|
||||
self.guest_session_id = ""
|
||||
self.guest_device_info = {}
|
||||
self.account_token = ""
|
||||
self.account_username = ""
|
||||
self.profile = {}
|
||||
self.save()
|
||||
|
||||
def request(self, path: str, data: dict | None = None, *, auth: bool = True) -> dict | list | None:
|
||||
def set_host(self, host: str) -> str:
|
||||
text = normalize_host(host)
|
||||
if text:
|
||||
self.host = text
|
||||
self.save()
|
||||
return self.host
|
||||
|
||||
def request(self, path: str, data: dict | None = None, *, auth: bool = True, identity: str = "guest") -> dict | list | None:
|
||||
token, device_id, session_id, info = self._identity(identity, auth)
|
||||
user_agent = str((info or {}).get("userAgent") or USER_AGENT)
|
||||
last_error: Exception | None = None
|
||||
hosts = [self.host, *[item for item in HOSTS if item != self.host]]
|
||||
for host in hosts:
|
||||
for _ in range(2):
|
||||
try:
|
||||
payload = self._call(host, path, data or {}, auth=auth)
|
||||
self.host = host
|
||||
return payload
|
||||
return self._call(
|
||||
self.host,
|
||||
path,
|
||||
data or {},
|
||||
token=token,
|
||||
device_id=device_id,
|
||||
session_id=session_id,
|
||||
user_agent=user_agent,
|
||||
)
|
||||
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:
|
||||
def _identity(self, identity: str, auth: bool) -> tuple[str, str, str, dict]:
|
||||
if identity == "account":
|
||||
self._ensure_fingerprint()
|
||||
token = self.account_token if auth else ""
|
||||
return token, self.device_id, self.session_id, self.device_info
|
||||
if not self.guest_device_id or not self.guest_session_id:
|
||||
self.roll_guest_fingerprint()
|
||||
token = self.token if auth else ""
|
||||
return token, self.guest_device_id, self.guest_session_id, self.guest_device_info
|
||||
|
||||
def _call(
|
||||
self,
|
||||
host: str,
|
||||
path: str,
|
||||
data: dict,
|
||||
*,
|
||||
token: str,
|
||||
device_id: str,
|
||||
session_id: str,
|
||||
user_agent: str,
|
||||
) -> 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,
|
||||
"token": token,
|
||||
"deviceId": device_id,
|
||||
"data": data,
|
||||
}
|
||||
headers = {
|
||||
@@ -121,13 +232,13 @@ class HuangdouClient:
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Origin": host,
|
||||
"Referer": host + "/",
|
||||
"User-Agent": USER_AGENT,
|
||||
"User-Agent": user_agent,
|
||||
"version": APP_VERSION,
|
||||
"deviceType": DEVICE_TYPE,
|
||||
"time": now,
|
||||
"sign": sign_header(self.session_id, request_id, now, url),
|
||||
"sign": sign_header(session_id, request_id, now, url),
|
||||
"requestId": request_id,
|
||||
"sessionId": self.session_id,
|
||||
"sessionId": session_id,
|
||||
"deviceBrand": "",
|
||||
"deviceModel": "",
|
||||
"systemName": "",
|
||||
@@ -144,9 +255,18 @@ class HuangdouClient:
|
||||
if parsed.get("status") == "n":
|
||||
code = parsed.get("errorCode")
|
||||
if str(code) == "2002":
|
||||
raise TokenExpired("登录已失效")
|
||||
raise TokenExpired(str(parsed.get("error") or "登录已失效"))
|
||||
raise ApiError(str(parsed.get("error") or "网络请求异常"), code)
|
||||
return parsed.get("data")
|
||||
|
||||
|
||||
def normalize_host(host: str) -> str:
|
||||
text = (host or "").strip().rstrip("/")
|
||||
if text and "://" not in text:
|
||||
text = "https://" + text
|
||||
if text and not (text.startswith("https://") or text.startswith("http://")):
|
||||
raise RuntimeError("域名需要以 http 或 https 开头")
|
||||
return text
|
||||
|
||||
|
||||
client = HuangdouClient()
|
||||
|
||||
Reference in New Issue
Block a user