feat(huya): 多选登录改单线程队列并拆分路由与工作台
默认同时只登一个号,失败两次跳下一个,号间留间隔,结束后可一键重试失败账号。 顺带拆分虎牙路由与斗鱼工作台页面,并补齐 raw_cookie 重建入口。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -43,6 +43,8 @@ class HuyaBatchRunner(
|
||||
if not task or not account:
|
||||
return
|
||||
|
||||
self._wss_sessions.account_id = account.id
|
||||
|
||||
if self._stop.is_set():
|
||||
self._mark_task(worker_db, task, "stopped", "任务已停止")
|
||||
return
|
||||
@@ -127,6 +129,7 @@ class HuyaBatchRunner(
|
||||
self._mark_task(worker_db, task, "error", f"执行异常: {exc}")
|
||||
self._push_log("error", f"[{current}] {name} 执行异常: {exc}")
|
||||
finally:
|
||||
self._wss_sessions.account_id = None
|
||||
self._close_wss_sessions()
|
||||
worker_db.close()
|
||||
|
||||
|
||||
@@ -62,8 +62,30 @@ class HuyaBatchRunnerCore:
|
||||
logger=lambda message: self._push_log("info", f"[{uid}] {message}"),
|
||||
)
|
||||
self._session_bucket().append(session)
|
||||
self._persist_session_cookie(uid, cookie, session.cookie)
|
||||
return session
|
||||
|
||||
def _persist_session_cookie(
|
||||
self, uid: int, original_cookie: str, filled_cookie: str
|
||||
):
|
||||
"""活动会话补齐网页字段后回写账号,避免下次任务重新生成设备态。"""
|
||||
account_id = getattr(self._wss_sessions, "account_id", None)
|
||||
if not account_id:
|
||||
return
|
||||
from ..database import SessionLocal
|
||||
from .huya_service import persist_huya_session_cookie
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
if persist_huya_session_cookie(
|
||||
db, int(account_id), original_cookie, filled_cookie
|
||||
):
|
||||
self._push_log("info", f"[{uid}] 已回写活动会话补齐后的网页 Cookie")
|
||||
except Exception: # noqa: BLE001 回写失败不能打断任务
|
||||
logger.debug("[huya] 回写会话 Cookie 失败")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def _session_bucket(self) -> list[Any]:
|
||||
bucket = getattr(self._wss_sessions, "items", None)
|
||||
if bucket is None:
|
||||
|
||||
@@ -369,6 +369,74 @@ def import_huya_cookies(db: Session, text: str, tag: str = "") -> tuple[int, int
|
||||
return created_or_updated, skipped
|
||||
|
||||
|
||||
def persist_huya_session_cookie(
|
||||
db: Session,
|
||||
account_id: int,
|
||||
original_cookie: str,
|
||||
filled_cookie: str,
|
||||
) -> bool:
|
||||
"""把活动会话补齐后的网页 Cookie 回写到账号。
|
||||
|
||||
原始登录态只有 App 字段时,wsLaunch 会补齐 guid 等网页设备字段;
|
||||
不回写则下次任务又会重新生成设备态。
|
||||
"""
|
||||
filled = normalize_huya_cookie(filled_cookie or "")
|
||||
original = normalize_huya_cookie(original_cookie or "")
|
||||
if not filled or filled == original:
|
||||
return False
|
||||
account = db.query(HuyaAccount).filter(HuyaAccount.id == account_id).first()
|
||||
if account is None:
|
||||
return False
|
||||
if normalize_huya_cookie(account.cookie or "") == filled:
|
||||
return False
|
||||
account.cookie = filled
|
||||
account.updated_at = datetime.now(UTC)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
|
||||
def rebuild_web_cookie_from_raw(db: Session, account: HuyaAccount) -> str:
|
||||
"""用 App 原始凭据重建网页运行 Cookie,不发起登录或支付。"""
|
||||
raw = normalize_huya_cookie(account.raw_cookie or "")
|
||||
if not raw:
|
||||
raise ValueError("该账号没有 App 原始凭据,无法重建网页 Cookie")
|
||||
|
||||
from core.huya.cookie_utils import cookie_pairs
|
||||
from core.huya.device_profile import _load_db, canonical_account_key
|
||||
from core.huya.web_cookie_fields import (
|
||||
fill_web_cookie_fields,
|
||||
qimei_feature_from_device_profile,
|
||||
)
|
||||
|
||||
uid = 0
|
||||
for candidate in (account.uid, account.yyuid, cookie_value(raw, "udb_uid")):
|
||||
text = str(candidate or "").strip()
|
||||
if text.isdigit():
|
||||
uid = int(text)
|
||||
break
|
||||
|
||||
feature = None
|
||||
username = (account.username or "").strip()
|
||||
if username:
|
||||
profiles = _load_db()
|
||||
profile = profiles.get(canonical_account_key(username)) or profiles.get(
|
||||
username
|
||||
)
|
||||
if profile:
|
||||
feature = qimei_feature_from_device_profile(profile)
|
||||
|
||||
filled = fill_web_cookie_fields(raw, guid="", uid=uid, qimei_feature=feature)
|
||||
pairs = dict(cookie_pairs(filled))
|
||||
pairs["udb_appid"] = "5002"
|
||||
rebuilt = "; ".join(f"{key}={value}" for key, value in pairs.items())
|
||||
account.cookie = rebuilt
|
||||
account.status = "login_success"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
db.commit()
|
||||
db.refresh(account)
|
||||
return account.cookie
|
||||
|
||||
|
||||
def ensure_huya_config(db: Session) -> HuyaConfig:
|
||||
"""获取单条虎牙配置,不存在则创建。"""
|
||||
config = db.query(HuyaConfig).first()
|
||||
|
||||
Reference in New Issue
Block a user