83cbfaf03f
- 修复Redis认证 (配置密码) - 启动Python管理后台 (端口9531, 15个功能开关) - 统一版本号 0.2.7 - 更新docker-compose.yml (镜像版本/Redis URL/Admin服务)
96 lines
2.7 KiB
Python
96 lines
2.7 KiB
Python
"""
|
|
CloudSearch Transfer — UC网盘凭证管理 v1.0.0
|
|
|
|
UC网盘使用 Cookie 直传(与夸克高度相似),无需 token 刷新机制。
|
|
验证方式:检查 Cookie 字符串长度是否 >= 50。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Dict
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class UcCredentialManager:
|
|
"""UC 网盘凭证管理器。
|
|
|
|
UC 网盘的转存 API 直接从 Cookie 中读取认证信息,
|
|
与夸克网盘机制完全一致,只是 API 域名不同(pc-api.uc.cn)。
|
|
|
|
Attributes:
|
|
cookie: 存储的 UC Cookie 字符串。
|
|
"""
|
|
|
|
# UC Cookie 最小长度阈值(与夸克一致)
|
|
MIN_COOKIE_LENGTH: int = 50
|
|
|
|
# UC 网盘 Referer
|
|
REFERER: str = "https://drive.uc.cn/"
|
|
|
|
def __init__(self, cookie: str = "") -> None:
|
|
"""初始化凭证管理器。
|
|
|
|
Args:
|
|
cookie: UC 网盘的 Cookie 字符串。
|
|
"""
|
|
self.cookie: str = cookie
|
|
|
|
def validate(self) -> bool:
|
|
"""验证 Cookie 是否满足最小长度要求。
|
|
|
|
Returns:
|
|
True 表示 Cookie 长度 >= MIN_COOKIE_LENGTH,否则为 False。
|
|
"""
|
|
if not self.cookie:
|
|
logger.warning("[UcCredential] Cookie is empty")
|
|
return False
|
|
|
|
valid = len(self.cookie) >= self.MIN_COOKIE_LENGTH
|
|
if not valid:
|
|
logger.warning(
|
|
"[UcCredential] Cookie too short: len=%d, min=%d",
|
|
len(self.cookie),
|
|
self.MIN_COOKIE_LENGTH,
|
|
)
|
|
return valid
|
|
|
|
def is_valid(self) -> bool:
|
|
"""validate() 的别名,便于适配器层调用。"""
|
|
return self.validate()
|
|
|
|
def get_headers(self) -> Dict[str, str]:
|
|
"""构建带 Cookie 认证的 HTTP 请求头。
|
|
|
|
UC API 需要在每次请求头中携带完整的 Cookie 字符串,
|
|
以及 Referer: https://drive.uc.cn/。
|
|
|
|
Returns:
|
|
包含 Cookie 和 Referer 字段的请求头字典。
|
|
Cookie 无效时仍返回空字典。
|
|
"""
|
|
if not self.validate():
|
|
logger.warning("[UcCredential] Cannot build headers: cookie invalid")
|
|
return {}
|
|
|
|
return {
|
|
"Cookie": self.cookie,
|
|
"Referer": self.REFERER,
|
|
}
|
|
|
|
def update_cookie(self, cookie: str) -> None:
|
|
"""更新 Cookie 字符串(用于手动刷新场景)。
|
|
|
|
Args:
|
|
cookie: 新的 Cookie 字符串。
|
|
"""
|
|
self.cookie = cookie
|
|
logger.info("[UcCredential] Cookie updated, new length=%d", len(cookie))
|
|
|
|
def __repr__(self) -> str:
|
|
return (
|
|
f"UcCredentialManager(cookie_len={len(self.cookie) if self.cookie else 0}, "
|
|
f"valid={self.validate()})"
|
|
)
|