修复虎牙绑定角色识别
This commit is contained in:
@@ -22,6 +22,10 @@ HUYA_RECHARGE_SOURCE_ID = "yellowcarlist"
|
||||
HUYA_RECHARGE_SCENE = 4
|
||||
HUYA_PAYMENT_POLL_SECONDS = 180
|
||||
HUYA_PAYMENT_POLL_INTERVAL = 3
|
||||
HUYA_BIND_ROLE_POLL_SECONDS = 180
|
||||
HUYA_BIND_ROLE_POLL_INTERVAL = 3
|
||||
HUYA_BIND_ZT_UUID = "b02faae1"
|
||||
HUYA_BIND_ROOM_ID = "30596253"
|
||||
HUYA_RECHARGE_EXTRA_PRODUCTS = [
|
||||
{
|
||||
"spu_id": "hy-5879340",
|
||||
@@ -96,7 +100,11 @@ class HuyaBatchRunner:
|
||||
@staticmethod
|
||||
def _role_name(bind_status) -> str:
|
||||
account_data = bind_status.accountData
|
||||
return account_data.gameRole.roleName or account_data.gameAccount.nick or ""
|
||||
return account_data.gameRole.roleName or ""
|
||||
|
||||
@staticmethod
|
||||
def _has_bind_role(bind_status) -> bool:
|
||||
return bool(bind_status and HuyaBatchRunner._role_name(bind_status))
|
||||
|
||||
@staticmethod
|
||||
def _bind_role_result(bind_status) -> dict:
|
||||
@@ -118,7 +126,7 @@ class HuyaBatchRunner:
|
||||
@staticmethod
|
||||
def _role_channel(bind_status) -> str:
|
||||
game_role = bind_status.accountData.gameRole
|
||||
parts = [bind_status.gameName, game_role.areaName, game_role.platName]
|
||||
parts = [game_role.platName, game_role.areaName]
|
||||
return " / ".join(part for part in parts if part)
|
||||
|
||||
@staticmethod
|
||||
@@ -167,13 +175,163 @@ class HuyaBatchRunner:
|
||||
"change_bind_day": int(bind_status.changeBindDay or 0),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def _bind_ready_result(cls, bind_status) -> dict:
|
||||
role_info = cls._bind_role_result(bind_status)
|
||||
return {
|
||||
**role_info,
|
||||
**cls._bind_change_state(bind_status),
|
||||
"bind_status": bind_status.to_dict(),
|
||||
"bind_ready_for_confirm": bool(role_info["role_name"]),
|
||||
"bind_phase": "role_ready" if role_info["role_name"] else "waiting_role",
|
||||
}
|
||||
|
||||
def _resolve_bind_status(
|
||||
self,
|
||||
client: HuyaHttpClient,
|
||||
uid: int,
|
||||
cookie: str,
|
||||
b_act_id_int: int,
|
||||
):
|
||||
"""按活动页逻辑解析绑定状态,优先返回含角色的状态。"""
|
||||
outer_status = client.check_user_bind_game_account(
|
||||
uid=uid,
|
||||
cookie=cookie,
|
||||
b_act_id=b_act_id_int,
|
||||
is_use_outer_act_id=1,
|
||||
)
|
||||
if outer_status is None:
|
||||
return None, {}
|
||||
|
||||
query_result = {
|
||||
"bind_status_source": "outer",
|
||||
"outer_bind_status": outer_status.to_dict(),
|
||||
}
|
||||
if outer_status.status != 200:
|
||||
return outer_status, query_result
|
||||
|
||||
chosen_status = outer_status
|
||||
account_data = outer_status.accountData
|
||||
should_check_inner = (
|
||||
not self._has_bind_role(outer_status)
|
||||
and bool(account_data.isNeedActCheck or not account_data.isBindAcount or not account_data.isBindRole)
|
||||
)
|
||||
if should_check_inner:
|
||||
inner_status = client.check_user_bind_game_account(
|
||||
uid=uid,
|
||||
cookie=cookie,
|
||||
b_act_id=b_act_id_int,
|
||||
is_use_outer_act_id=0,
|
||||
)
|
||||
if inner_status is not None:
|
||||
query_result["inner_bind_status"] = inner_status.to_dict()
|
||||
if inner_status.status == 200 and self._has_bind_role(inner_status):
|
||||
chosen_status = inner_status
|
||||
query_result["bind_status_source"] = "inner"
|
||||
|
||||
query_result["bind_status"] = chosen_status.to_dict()
|
||||
return chosen_status, query_result
|
||||
|
||||
def _apply_role_to_account(self, account: HuyaAccount, bind_status, status: str):
|
||||
role_name = self._role_name(bind_status)
|
||||
account.status = status
|
||||
account.game_name = role_name or bind_status.gameName or account.game_name
|
||||
account.game_name = role_name or account.game_name
|
||||
account.game_channel = self._role_channel(bind_status) or account.game_channel
|
||||
account.updated_at = datetime.now(timezone.utc)
|
||||
|
||||
@staticmethod
|
||||
def _bind_redirect_url(config_info: dict) -> str:
|
||||
room_pid = str(config_info.get("room_pid") or "").strip()
|
||||
if not room_pid:
|
||||
return ""
|
||||
return (
|
||||
f"https://zt.huya.com/{HUYA_BIND_ZT_UUID}/pc/index.html"
|
||||
f"?sourceId={HUYA_RECHARGE_SOURCE_ID}"
|
||||
f"&pid={room_pid}"
|
||||
f"&anchorUid={room_pid}"
|
||||
f"&roomid={HUYA_BIND_ROOM_ID}"
|
||||
)
|
||||
|
||||
def _wait_bind_role_result(
|
||||
self,
|
||||
client: HuyaHttpClient,
|
||||
worker_db: Session,
|
||||
task: HuyaTask,
|
||||
account: HuyaAccount,
|
||||
uid: int,
|
||||
cookie: str,
|
||||
b_act_id_int: int,
|
||||
result: dict,
|
||||
) -> tuple[str, dict]:
|
||||
deadline = time.monotonic() + HUYA_BIND_ROLE_POLL_SECONDS
|
||||
qrcode_token = str(result.get("qrcode_token") or "")
|
||||
qrcode_finished = not qrcode_token
|
||||
while not self._stop.is_set() and time.monotonic() < deadline:
|
||||
if self._stop.wait(HUYA_BIND_ROLE_POLL_INTERVAL):
|
||||
break
|
||||
|
||||
if qrcode_token and not qrcode_finished:
|
||||
qrcode_status = client.get_livelink_qrcode_status(qrcode_token, timeout=10.0)
|
||||
if qrcode_status is not None:
|
||||
result["qrcode_status"] = qrcode_status
|
||||
if qrcode_status["is_expired"] or qrcode_status["is_failure"]:
|
||||
result.update({
|
||||
"bind_phase": "qrcode_expired",
|
||||
"bind_ready_for_confirm": False,
|
||||
})
|
||||
self._update_task_progress(worker_db, task, "running", "绑定小程序码已失效,请重新获取", result)
|
||||
return "", result
|
||||
if qrcode_status["is_completed"]:
|
||||
qrcode_finished = True
|
||||
result["bind_phase"] = "qrcode_completed"
|
||||
elif qrcode_status["is_scan"]:
|
||||
result["bind_phase"] = "qrcode_scanned"
|
||||
else:
|
||||
result["bind_phase"] = "waiting_scan"
|
||||
|
||||
bind_status, bind_query_result = self._resolve_bind_status(
|
||||
client=client,
|
||||
uid=uid,
|
||||
cookie=cookie,
|
||||
b_act_id_int=b_act_id_int,
|
||||
)
|
||||
if bind_status is None:
|
||||
continue
|
||||
result.update(bind_query_result)
|
||||
|
||||
if bind_status.status != 200:
|
||||
result.update({
|
||||
"bind_phase": "role_check_failed",
|
||||
"bind_status": bind_status.to_dict(),
|
||||
})
|
||||
self._update_task_progress(worker_db, task, "running", bind_status.msg or "等待绑定角色同步", result)
|
||||
continue
|
||||
|
||||
previous_phase = result.get("bind_phase")
|
||||
ready = self._bind_ready_result(bind_status)
|
||||
if not ready["role_name"] and previous_phase in {"waiting_scan", "qrcode_scanned", "qrcode_completed"}:
|
||||
ready["bind_phase"] = previous_phase
|
||||
result.update(ready)
|
||||
role_name = ready["role_name"]
|
||||
if role_name:
|
||||
self._apply_role_to_account(account, bind_status, "game_queried")
|
||||
self._update_task_progress(worker_db, task, "running", f"已识别角色: {role_name},待确认绑定", result)
|
||||
return role_name, result
|
||||
|
||||
if result.get("bind_phase") == "qrcode_completed":
|
||||
message = "小程序绑定已完成,等待角色同步"
|
||||
elif result.get("bind_phase") == "qrcode_scanned":
|
||||
message = "已扫码,等待小程序绑定完成"
|
||||
else:
|
||||
message = "已生成绑定小程序码,等待扫码绑定"
|
||||
self._update_task_progress(worker_db, task, "running", message, result)
|
||||
|
||||
result.update({
|
||||
"bind_phase": "role_timeout" if not self._stop.is_set() else "stopped",
|
||||
"bind_ready_for_confirm": False,
|
||||
})
|
||||
return "", result
|
||||
|
||||
def _mark_task(
|
||||
self,
|
||||
worker_db: Session,
|
||||
@@ -913,11 +1071,11 @@ class HuyaBatchRunner:
|
||||
return
|
||||
|
||||
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
|
||||
bind_status = client.check_user_bind_game_account(
|
||||
bind_status, bind_query_result = self._resolve_bind_status(
|
||||
client=client,
|
||||
uid=uid,
|
||||
cookie=cookie,
|
||||
b_act_id=b_act_id_int,
|
||||
is_use_outer_act_id=1,
|
||||
b_act_id_int=b_act_id_int,
|
||||
)
|
||||
if bind_status is None:
|
||||
self._mark_task(worker_db, task, "error", "虎牙绑定状态接口无响应")
|
||||
@@ -928,7 +1086,7 @@ class HuyaBatchRunner:
|
||||
task,
|
||||
"failed",
|
||||
bind_status.msg or f"虎牙绑定状态查询失败: {bind_status.status}",
|
||||
bind_status.to_dict(),
|
||||
bind_query_result or bind_status.to_dict(),
|
||||
)
|
||||
return
|
||||
|
||||
@@ -981,12 +1139,14 @@ class HuyaBatchRunner:
|
||||
profile_nick = profile.nick or profile.passport or profile_nick
|
||||
profile_avatar = profile.avatar or ""
|
||||
|
||||
bind_redirect_url = self._bind_redirect_url(config_info)
|
||||
urls = client.build_bind_urls(
|
||||
live_link.livelinkParam,
|
||||
b_act_id_int,
|
||||
game_auth_scene=bind_status.gameAuthScene,
|
||||
nick_name=profile_nick,
|
||||
face_url=profile_avatar,
|
||||
redirect_url=bind_redirect_url,
|
||||
)
|
||||
mini_qrcode = client.get_livelink_mini_qrcode(urls["qr_url"])
|
||||
if not mini_qrcode:
|
||||
@@ -1003,8 +1163,11 @@ class HuyaBatchRunner:
|
||||
result = {
|
||||
"bind_act_id": b_act_id_int,
|
||||
"mini_qrcode_image": mini_qrcode["mini_qrcode_image"],
|
||||
"qrcode_token": mini_qrcode["qrcode_token"],
|
||||
"bind_status": bind_status.to_dict(),
|
||||
"qrcode_token": mini_qrcode.get("qrcode_token") or "",
|
||||
**bind_query_result,
|
||||
"bind_phase": "waiting_scan" if mini_qrcode.get("qrcode_token") else "waiting_role",
|
||||
"bind_ready_for_confirm": False,
|
||||
"bind_redirect_url": bind_redirect_url,
|
||||
**role_info,
|
||||
**change_state,
|
||||
"profile": {
|
||||
@@ -1014,10 +1177,33 @@ class HuyaBatchRunner:
|
||||
}
|
||||
|
||||
account.status = "bind_qr_generated"
|
||||
account.game_name = role_info["role_name"] or bind_status.gameName or account.game_name
|
||||
account.game_name = role_info["role_name"] or account.game_name
|
||||
account.game_channel = self._role_channel(bind_status) or account.game_channel
|
||||
account.nickname = profile_nick or account.nickname
|
||||
account.updated_at = datetime.now(timezone.utc)
|
||||
self._mark_task(worker_db, task, "success", "已生成绑定小程序码", result)
|
||||
self._update_task_progress(worker_db, task, "running", "已生成绑定小程序码,等待绑定角色", result)
|
||||
|
||||
role_name, result = self._wait_bind_role_result(
|
||||
client=client,
|
||||
worker_db=worker_db,
|
||||
task=task,
|
||||
account=account,
|
||||
uid=uid,
|
||||
cookie=cookie,
|
||||
b_act_id_int=b_act_id_int,
|
||||
result=result,
|
||||
)
|
||||
if role_name:
|
||||
self._mark_task(worker_db, task, "success", f"已识别角色: {role_name},待确认绑定", result)
|
||||
return
|
||||
|
||||
if result.get("bind_phase") == "stopped":
|
||||
self._mark_task(worker_db, task, "failed", "任务已停止", result)
|
||||
return
|
||||
if result.get("bind_phase") == "qrcode_expired":
|
||||
self._mark_task(worker_db, task, "failed", "绑定小程序码已失效,请重新获取", result)
|
||||
return
|
||||
self._mark_task(worker_db, task, "success", "已生成绑定小程序码,未检测到绑定角色", result)
|
||||
|
||||
def _execute_query_game_name(
|
||||
self,
|
||||
@@ -1048,11 +1234,11 @@ class HuyaBatchRunner:
|
||||
return
|
||||
|
||||
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
|
||||
bind_status = client.check_user_bind_game_account(
|
||||
bind_status, bind_query_result = self._resolve_bind_status(
|
||||
client=client,
|
||||
uid=uid,
|
||||
cookie=cookie,
|
||||
b_act_id=b_act_id_int,
|
||||
is_use_outer_act_id=1,
|
||||
b_act_id_int=b_act_id_int,
|
||||
)
|
||||
if bind_status is None:
|
||||
self._mark_task(worker_db, task, "error", "虎牙角色信息接口无响应")
|
||||
@@ -1063,7 +1249,7 @@ class HuyaBatchRunner:
|
||||
task,
|
||||
"failed",
|
||||
bind_status.msg or f"虎牙角色信息查询失败: {bind_status.status}",
|
||||
{"bind_act_id": b_act_id_int, "bind_status": bind_status.to_dict()},
|
||||
{"bind_act_id": b_act_id_int, **bind_query_result},
|
||||
)
|
||||
return
|
||||
|
||||
@@ -1073,7 +1259,7 @@ class HuyaBatchRunner:
|
||||
"bind_act_id": b_act_id_int,
|
||||
**role_info,
|
||||
**change_state,
|
||||
"bind_status": bind_status.to_dict(),
|
||||
**bind_query_result,
|
||||
}
|
||||
role_name = role_info["role_name"]
|
||||
if role_name:
|
||||
@@ -1114,11 +1300,11 @@ class HuyaBatchRunner:
|
||||
return
|
||||
|
||||
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
|
||||
role_status = client.check_user_bind_game_account(
|
||||
role_status, role_query_result = self._resolve_bind_status(
|
||||
client=client,
|
||||
uid=uid,
|
||||
cookie=cookie,
|
||||
b_act_id=b_act_id_int,
|
||||
is_use_outer_act_id=0,
|
||||
b_act_id_int=b_act_id_int,
|
||||
)
|
||||
if role_status is None:
|
||||
self._mark_task(worker_db, task, "error", "虎牙绑定角色查询接口无响应")
|
||||
@@ -1129,17 +1315,19 @@ class HuyaBatchRunner:
|
||||
task,
|
||||
"failed",
|
||||
role_status.msg or f"虎牙绑定角色查询失败: {role_status.status}",
|
||||
{"bind_act_id": b_act_id_int, "bind_status": role_status.to_dict()},
|
||||
{"bind_act_id": b_act_id_int, **role_query_result},
|
||||
)
|
||||
return
|
||||
|
||||
if not role_status.accountData.isBindAcount or not role_status.accountData.isBindRole:
|
||||
role_info = self._bind_role_result(role_status)
|
||||
role_name = role_info["role_name"]
|
||||
if not role_name:
|
||||
result = {
|
||||
"bind_act_id": b_act_id_int,
|
||||
"bind_confirmed": False,
|
||||
"bind_status": role_status.to_dict(),
|
||||
**role_query_result,
|
||||
}
|
||||
self._mark_task(worker_db, task, "failed", "尚未绑定游戏角色,请先扫码完成绑定", result)
|
||||
self._mark_task(worker_db, task, "failed", "尚未识别到待确认角色,请先扫码完成绑定", result)
|
||||
return
|
||||
|
||||
confirm_resp = client.confirm_bind_act_account(
|
||||
@@ -1156,6 +1344,7 @@ class HuyaBatchRunner:
|
||||
"bind_confirmed": False,
|
||||
"confirm_result": confirm_resp.to_dict(),
|
||||
"before_bind_status": role_status.to_dict(),
|
||||
**role_query_result,
|
||||
}
|
||||
self._mark_task(
|
||||
worker_db,
|
||||
@@ -1166,57 +1355,57 @@ class HuyaBatchRunner:
|
||||
)
|
||||
return
|
||||
|
||||
refreshed_status = client.check_user_bind_game_account(
|
||||
refreshed_status, refreshed_query_result = self._resolve_bind_status(
|
||||
client=client,
|
||||
uid=uid,
|
||||
cookie=cookie,
|
||||
b_act_id=b_act_id_int,
|
||||
is_use_outer_act_id=1,
|
||||
b_act_id_int=b_act_id_int,
|
||||
)
|
||||
if refreshed_status is None:
|
||||
result = {
|
||||
"bind_act_id": b_act_id_int,
|
||||
"bind_confirmed": False,
|
||||
"bind_confirmed": True,
|
||||
"confirm_result": confirm_resp.to_dict(),
|
||||
"before_bind_status": role_status.to_dict(),
|
||||
**role_info,
|
||||
**role_query_result,
|
||||
}
|
||||
self._mark_task(worker_db, task, "error", "虎牙活动绑定状态刷新无响应", result)
|
||||
self._apply_role_to_account(account, role_status, "bind_confirmed")
|
||||
self._mark_task(worker_db, task, "success", f"确认绑定: {role_name}", result)
|
||||
return
|
||||
if refreshed_status.status != 200:
|
||||
result = {
|
||||
"bind_act_id": b_act_id_int,
|
||||
"bind_confirmed": False,
|
||||
"bind_confirmed": True,
|
||||
"confirm_result": confirm_resp.to_dict(),
|
||||
"before_bind_status": role_status.to_dict(),
|
||||
"bind_status": refreshed_status.to_dict(),
|
||||
**role_info,
|
||||
**role_query_result,
|
||||
"refresh_error": refreshed_query_result,
|
||||
}
|
||||
self._mark_task(
|
||||
worker_db,
|
||||
task,
|
||||
"failed",
|
||||
refreshed_status.msg or f"虎牙活动绑定状态刷新失败: {refreshed_status.status}",
|
||||
result,
|
||||
)
|
||||
self._apply_role_to_account(account, role_status, "bind_confirmed")
|
||||
self._mark_task(worker_db, task, "success", f"确认绑定: {role_name}", result)
|
||||
return
|
||||
|
||||
bind_confirmed = bool(
|
||||
refreshed_confirmed = bool(
|
||||
refreshed_status.accountData.isBindAcount
|
||||
and refreshed_status.accountData.isBindRole
|
||||
)
|
||||
role_info = self._bind_role_result(refreshed_status)
|
||||
refreshed_role_info = self._bind_role_result(refreshed_status)
|
||||
final_status = refreshed_status if refreshed_role_info["role_name"] else role_status
|
||||
final_role_info = refreshed_role_info if refreshed_role_info["role_name"] else role_info
|
||||
result = {
|
||||
"bind_act_id": b_act_id_int,
|
||||
"bind_confirmed": bind_confirmed,
|
||||
"bind_confirmed": True,
|
||||
"refreshed_is_bound": refreshed_confirmed,
|
||||
"confirm_result": confirm_resp.to_dict(),
|
||||
"before_bind_status": role_status.to_dict(),
|
||||
"bind_status": refreshed_status.to_dict(),
|
||||
**role_info,
|
||||
**role_query_result,
|
||||
"refresh_result": refreshed_query_result,
|
||||
**final_role_info,
|
||||
}
|
||||
if not bind_confirmed:
|
||||
self._mark_task(worker_db, task, "failed", "确认后仍未检测到活动绑定角色", result)
|
||||
return
|
||||
|
||||
self._apply_role_to_account(account, refreshed_status, "bind_confirmed")
|
||||
role_name = self._role_name(refreshed_status) or "已绑定"
|
||||
self._apply_role_to_account(account, final_status, "bind_confirmed")
|
||||
role_name = final_role_info["role_name"] or role_name or "已绑定"
|
||||
self._mark_task(worker_db, task, "success", f"确认绑定: {role_name}", result)
|
||||
|
||||
def _execute_one(self, task_id: int, account_info: dict, config_info: dict, total: int):
|
||||
|
||||
Reference in New Issue
Block a user