fix(douyu): 修复查询工作台冷却筛选与 CSV 导出安全 (v0.4.27)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# 直播账号运营后台
|
# 直播账号运营后台
|
||||||
|
|
||||||
斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.26)。
|
斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.27)。
|
||||||
|
|
||||||
## 能做什么
|
## 能做什么
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "live-hub-py"
|
name = "live-hub-py"
|
||||||
version = "0.4.26"
|
version = "0.4.27"
|
||||||
description = "直播账号运营 Web 后台"
|
description = "直播账号运营 Web 后台"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.12,<3.13"
|
requires-python = ">=3.12,<3.13"
|
||||||
|
|||||||
@@ -316,6 +316,19 @@ class TestQueryWorkbenchRouterAndExport:
|
|||||||
status="imported",
|
status="imported",
|
||||||
assigned_to=self.support_with_login.id,
|
assigned_to=self.support_with_login.id,
|
||||||
)
|
)
|
||||||
|
self.acc_wait_seconds = Account(
|
||||||
|
username="dy_wait_seconds",
|
||||||
|
password="pw",
|
||||||
|
email="w@e.com",
|
||||||
|
email_password="ep",
|
||||||
|
cookie="acf_uid=333",
|
||||||
|
ck_check_status="valid",
|
||||||
|
role_bind_status="bound",
|
||||||
|
game_name="角色C",
|
||||||
|
role_change_available_at=None,
|
||||||
|
change_role_wait_time=3600,
|
||||||
|
assigned_to=self.support_with_login.id,
|
||||||
|
)
|
||||||
self.acc_missing_cred = Account(
|
self.acc_missing_cred = Account(
|
||||||
username="dy_nocred",
|
username="dy_nocred",
|
||||||
password="",
|
password="",
|
||||||
@@ -326,7 +339,13 @@ class TestQueryWorkbenchRouterAndExport:
|
|||||||
assigned_to=self.support_with_login.id,
|
assigned_to=self.support_with_login.id,
|
||||||
)
|
)
|
||||||
self.session.add_all(
|
self.session.add_all(
|
||||||
[self.acc_valid, self.acc_invalid_ck, self.acc_no_ck, self.acc_missing_cred]
|
[
|
||||||
|
self.acc_valid,
|
||||||
|
self.acc_invalid_ck,
|
||||||
|
self.acc_no_ck,
|
||||||
|
self.acc_wait_seconds,
|
||||||
|
self.acc_missing_cred,
|
||||||
|
]
|
||||||
)
|
)
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
|
|
||||||
@@ -359,7 +378,11 @@ class TestQueryWorkbenchRouterAndExport:
|
|||||||
db=self.session,
|
db=self.session,
|
||||||
current=self.admin,
|
current=self.admin,
|
||||||
)
|
)
|
||||||
assert {item.username for item in default_items} == {"dy_valid", "dy_invalid"}
|
assert {item.username for item in default_items} == {
|
||||||
|
"dy_valid",
|
||||||
|
"dy_invalid",
|
||||||
|
"dy_wait_seconds",
|
||||||
|
}
|
||||||
|
|
||||||
all_items = list_task_accounts(
|
all_items = list_task_accounts(
|
||||||
search="",
|
search="",
|
||||||
@@ -375,6 +398,7 @@ class TestQueryWorkbenchRouterAndExport:
|
|||||||
"dy_valid",
|
"dy_valid",
|
||||||
"dy_invalid",
|
"dy_invalid",
|
||||||
"dy_nock",
|
"dy_nock",
|
||||||
|
"dy_wait_seconds",
|
||||||
"dy_nocred",
|
"dy_nocred",
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -402,7 +426,10 @@ class TestQueryWorkbenchRouterAndExport:
|
|||||||
db=self.session,
|
db=self.session,
|
||||||
current=self.admin,
|
current=self.admin,
|
||||||
)
|
)
|
||||||
assert [item.username for item in cooldown_waiting] == ["dy_invalid"]
|
assert [item.username for item in cooldown_waiting] == [
|
||||||
|
"dy_wait_seconds",
|
||||||
|
"dy_invalid",
|
||||||
|
]
|
||||||
|
|
||||||
cooldown_ready = list_task_accounts(
|
cooldown_ready = list_task_accounts(
|
||||||
search="",
|
search="",
|
||||||
@@ -418,6 +445,8 @@ class TestQueryWorkbenchRouterAndExport:
|
|||||||
assert [item.username for item in cooldown_ready] == ["dy_valid"]
|
assert [item.username for item in cooldown_ready] == ["dy_valid"]
|
||||||
|
|
||||||
def test_select_invalid_ids_and_export_csv_order(self):
|
def test_select_invalid_ids_and_export_csv_order(self):
|
||||||
|
self.acc_valid.nickname = '=HYPERLINK("https://example.invalid")'
|
||||||
|
self.session.flush()
|
||||||
desired = [self.acc_no_ck.id, self.acc_valid.id, self.acc_invalid_ck.id]
|
desired = [self.acc_no_ck.id, self.acc_valid.id, self.acc_invalid_ck.id]
|
||||||
update_workbench_accounts(
|
update_workbench_accounts(
|
||||||
DouyuWorkbenchAccountsUpdate(handbook_scope="query", account_ids=desired),
|
DouyuWorkbenchAccountsUpdate(handbook_scope="query", account_ids=desired),
|
||||||
@@ -448,6 +477,7 @@ class TestQueryWorkbenchRouterAndExport:
|
|||||||
assert rows[0][0] == "账号"
|
assert rows[0][0] == "账号"
|
||||||
assert [r[0] for r in rows[1:]] == ["dy_nock", "dy_valid", "dy_invalid"]
|
assert [r[0] for r in rows[1:]] == ["dy_nock", "dy_valid", "dy_invalid"]
|
||||||
assert rows[2][6] == "有效"
|
assert rows[2][6] == "有效"
|
||||||
|
assert rows[2][2] == '\'=HYPERLINK("https://example.invalid")'
|
||||||
assert rows[2][8] == "100"
|
assert rows[2][8] == "100"
|
||||||
assert rows[2][10] == "1280"
|
assert rows[2][10] == "1280"
|
||||||
assert rows[2][15] == "可换绑"
|
assert rows[2][15] == "可换绑"
|
||||||
|
|||||||
@@ -381,7 +381,7 @@ wheels = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "live-hub-py"
|
name = "live-hub-py"
|
||||||
version = "0.4.26"
|
version = "0.4.27"
|
||||||
source = { editable = "." }
|
source = { editable = "." }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "alembic" },
|
{ name = "alembic" },
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ from fastapi import (
|
|||||||
WebSocketDisconnect,
|
WebSocketDisconnect,
|
||||||
)
|
)
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from sqlalchemy import case, func, or_, select
|
from sqlalchemy import and_, case, func, or_, select
|
||||||
from sqlalchemy.orm import Session, joinedload
|
from sqlalchemy.orm import Session, joinedload
|
||||||
from sqlalchemy.sql import Select
|
from sqlalchemy.sql import Select
|
||||||
from starlette.concurrency import run_in_threadpool
|
from starlette.concurrency import run_in_threadpool
|
||||||
@@ -672,6 +672,7 @@ def _apply_task_account_filters(
|
|||||||
query = query.filter(
|
query = query.filter(
|
||||||
Account.cookie.isnot(None),
|
Account.cookie.isnot(None),
|
||||||
Account.cookie != "",
|
Account.cookie != "",
|
||||||
|
or_(Account.status.is_(None), Account.status != "invalid"),
|
||||||
or_(
|
or_(
|
||||||
Account.ck_check_status.is_(None),
|
Account.ck_check_status.is_(None),
|
||||||
Account.ck_check_status.notin_(("valid", "invalid")),
|
Account.ck_check_status.notin_(("valid", "invalid")),
|
||||||
@@ -682,15 +683,27 @@ def _apply_task_account_filters(
|
|||||||
query = query.filter(
|
query = query.filter(
|
||||||
Account.role_bind_status == BIND_BOUND,
|
Account.role_bind_status == BIND_BOUND,
|
||||||
or_(
|
or_(
|
||||||
Account.role_change_available_at.is_(None),
|
|
||||||
Account.role_change_available_at <= now,
|
Account.role_change_available_at <= now,
|
||||||
|
and_(
|
||||||
|
Account.role_change_available_at.is_(None),
|
||||||
|
or_(
|
||||||
|
Account.change_role_wait_time.is_(None),
|
||||||
|
Account.change_role_wait_time <= 0,
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
elif ck_filter == "cooldown_waiting":
|
elif ck_filter == "cooldown_waiting":
|
||||||
now = datetime.now(UTC)
|
now = datetime.now(UTC)
|
||||||
query = query.filter(
|
query = query.filter(
|
||||||
Account.role_bind_status == BIND_BOUND,
|
Account.role_bind_status == BIND_BOUND,
|
||||||
Account.role_change_available_at > now,
|
or_(
|
||||||
|
Account.role_change_available_at > now,
|
||||||
|
and_(
|
||||||
|
Account.role_change_available_at.is_(None),
|
||||||
|
Account.change_role_wait_time > 0,
|
||||||
|
),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
search_text = (search or "").strip()
|
search_text = (search or "").strip()
|
||||||
if search_text:
|
if search_text:
|
||||||
@@ -1211,7 +1224,7 @@ _ACCOUNT_STATUS_LABELS = {
|
|||||||
|
|
||||||
|
|
||||||
def _manual_budget_summary(db: Session) -> dict[str, Any]:
|
def _manual_budget_summary(db: Session) -> dict[str, Any]:
|
||||||
overview = get_login_quota_usage_summary(db)
|
overview = cast(dict[str, Any], get_login_quota_usage_summary(db))
|
||||||
manual_cat = next(
|
manual_cat = next(
|
||||||
(
|
(
|
||||||
item
|
item
|
||||||
@@ -1281,6 +1294,15 @@ def _format_cooldown_duration(seconds: int) -> str:
|
|||||||
return f"{minutes}分"
|
return f"{minutes}分"
|
||||||
|
|
||||||
|
|
||||||
|
def _csv_safe_cell(value: Any) -> Any:
|
||||||
|
"""Prevent spreadsheet apps from evaluating exported text as a formula."""
|
||||||
|
if isinstance(value, str) and value.lstrip(" \t\r\n").startswith(
|
||||||
|
("=", "+", "-", "@")
|
||||||
|
):
|
||||||
|
return f"'{value}"
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
@router.post("/query-workbench/lookup")
|
@router.post("/query-workbench/lookup")
|
||||||
def lookup_query_workbench_accounts(
|
def lookup_query_workbench_accounts(
|
||||||
req: DouyuQueryWorkbenchLookupRequest,
|
req: DouyuQueryWorkbenchLookupRequest,
|
||||||
@@ -1382,7 +1404,9 @@ def relogin_query_workbench_invalid_accounts(
|
|||||||
current: User = Depends(require_permission("douyu:task")),
|
current: User = Depends(require_permission("douyu:task")),
|
||||||
):
|
):
|
||||||
"""从查询工作台发起失效/无 CK 账号重新登录,强制走「人工登录」预算(manual_login)。"""
|
"""从查询工作台发起失效/无 CK 账号重新登录,强制走「人工登录」预算(manual_login)。"""
|
||||||
if current.role != "super_admin" and not user_has_permission(current, "login:batch"):
|
if current.role != "super_admin" and not user_has_permission(
|
||||||
|
current, "login:batch"
|
||||||
|
):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=403, detail="无权限发起重登(需超级管理员或 login:batch 权限)"
|
status_code=403, detail="无权限发起重登(需超级管理员或 login:batch 权限)"
|
||||||
)
|
)
|
||||||
@@ -1533,9 +1557,7 @@ def export_query_workbench_csv(
|
|||||||
)
|
)
|
||||||
rows_by_id = {int(acc.id): acc for acc in visible_accounts}
|
rows_by_id = {int(acc.id): acc for acc in visible_accounts}
|
||||||
|
|
||||||
ordered_accounts = [
|
ordered_accounts = [rows_by_id[aid] for aid in requested_ids if aid in rows_by_id]
|
||||||
rows_by_id[aid] for aid in requested_ids if aid in rows_by_id
|
|
||||||
]
|
|
||||||
now = datetime.now(UTC)
|
now = datetime.now(UTC)
|
||||||
output = io.StringIO()
|
output = io.StringIO()
|
||||||
writer = csv.writer(output)
|
writer = csv.writer(output)
|
||||||
@@ -1574,7 +1596,9 @@ def export_query_workbench_csv(
|
|||||||
ck_label = "未检测"
|
ck_label = "未检测"
|
||||||
|
|
||||||
bind_info = bind_payload(acc)
|
bind_info = bind_payload(acc)
|
||||||
bind_status = str(bind_info.get("role_bind_status") or acc.role_bind_status or "")
|
bind_status = str(
|
||||||
|
bind_info.get("role_bind_status") or acc.role_bind_status or ""
|
||||||
|
)
|
||||||
bind_label = str(bind_info.get("role_bind_label") or "")
|
bind_label = str(bind_info.get("role_bind_label") or "")
|
||||||
|
|
||||||
change_status = "未知"
|
change_status = "未知"
|
||||||
@@ -1593,43 +1617,42 @@ def export_query_workbench_csv(
|
|||||||
)
|
)
|
||||||
if aware > now:
|
if aware > now:
|
||||||
change_status = "冷却中"
|
change_status = "冷却中"
|
||||||
cooldown_note = (
|
cooldown_note = f"剩余 {_format_cooldown_duration(int((aware - now).total_seconds()))}"
|
||||||
f"剩余 {_format_cooldown_duration(int((aware - now).total_seconds()))}"
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
change_status = "可换绑"
|
change_status = "可换绑"
|
||||||
cooldown_note = "已过冷却"
|
cooldown_note = "已过冷却"
|
||||||
elif (acc.change_role_wait_time or 0) > 0:
|
elif (acc.change_role_wait_time or 0) > 0:
|
||||||
change_status = "冷却中"
|
change_status = "冷却中"
|
||||||
cooldown_note = (
|
cooldown_note = f"剩余 {_format_cooldown_duration(int(acc.change_role_wait_time or 0))}"
|
||||||
f"剩余 {_format_cooldown_duration(int(acc.change_role_wait_time or 0))}"
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
change_status = "可换绑"
|
change_status = "可换绑"
|
||||||
cooldown_note = "可直接换绑"
|
cooldown_note = "可直接换绑"
|
||||||
|
|
||||||
writer.writerow(
|
writer.writerow(
|
||||||
[
|
[
|
||||||
acc.username or "",
|
_csv_safe_cell(value)
|
||||||
acc.uid or "",
|
for value in [
|
||||||
acc.nickname or "",
|
acc.username or "",
|
||||||
acc.tag or "",
|
acc.uid or "",
|
||||||
acc.assigned_user.username if acc.assigned_user else "未分配",
|
acc.nickname or "",
|
||||||
_ACCOUNT_STATUS_LABELS.get(acc.status or "", acc.status or ""),
|
acc.tag or "",
|
||||||
ck_label,
|
acc.assigned_user.username if acc.assigned_user else "未分配",
|
||||||
_format_shanghai_dt(acc.ck_checked_at),
|
_ACCOUNT_STATUS_LABELS.get(acc.status or "", acc.status or ""),
|
||||||
acc.gold_balance if acc.gold_balance is not None else "",
|
ck_label,
|
||||||
acc.exchange_balance if acc.exchange_balance is not None else "",
|
_format_shanghai_dt(acc.ck_checked_at),
|
||||||
acc.points if acc.points is not None else "",
|
acc.gold_balance if acc.gold_balance is not None else "",
|
||||||
_format_shanghai_dt(acc.points_expire_at),
|
acc.exchange_balance if acc.exchange_balance is not None else "",
|
||||||
bind_label,
|
acc.points if acc.points is not None else "",
|
||||||
acc.game_name or "",
|
_format_shanghai_dt(acc.points_expire_at),
|
||||||
acc.game_channel or "",
|
bind_label,
|
||||||
change_status,
|
acc.game_name or "",
|
||||||
_format_shanghai_dt(acc.role_change_available_at),
|
acc.game_channel or "",
|
||||||
_format_shanghai_dt(acc.role_change_official_at),
|
change_status,
|
||||||
cooldown_note,
|
_format_shanghai_dt(acc.role_change_available_at),
|
||||||
_format_shanghai_dt(acc.updated_at or acc.overview_queried_at),
|
_format_shanghai_dt(acc.role_change_official_at),
|
||||||
|
cooldown_note,
|
||||||
|
_format_shanghai_dt(acc.updated_at or acc.overview_queried_at),
|
||||||
|
]
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
filename = f"douyu_query_workbench_{datetime.now(_SHANGHAI_TZ).strftime('%Y%m%d_%H%M%S')}.csv"
|
filename = f"douyu_query_workbench_{datetime.now(_SHANGHAI_TZ).strftime('%Y%m%d_%H%M%S')}.csv"
|
||||||
|
|||||||
@@ -202,6 +202,17 @@ class OverviewMixin:
|
|||||||
def _client(self, cookie: str) -> DouyuActivityClient: ...
|
def _client(self, cookie: str) -> DouyuActivityClient: ...
|
||||||
def _mark_task(self, *args: Any, **kwargs: Any) -> None: ...
|
def _mark_task(self, *args: Any, **kwargs: Any) -> None: ...
|
||||||
def _push_log(self, level: str, message: str) -> None: ...
|
def _push_log(self, level: str, message: str) -> None: ...
|
||||||
|
def _refresh_account_gold_balance(
|
||||||
|
self, client: DouyuActivityClient, account: Account
|
||||||
|
) -> dict[str, Any]: ...
|
||||||
|
def _refresh_account_points(
|
||||||
|
self,
|
||||||
|
client: DouyuActivityClient,
|
||||||
|
account: Account,
|
||||||
|
cookie: str,
|
||||||
|
*,
|
||||||
|
ctn: Any = None,
|
||||||
|
) -> dict[str, Any]: ...
|
||||||
def _pick_change_wait_bind_info(
|
def _pick_change_wait_bind_info(
|
||||||
self, candidates: list[dict], config: dict
|
self, candidates: list[dict], config: dict
|
||||||
) -> dict | None: ...
|
) -> dict | None: ...
|
||||||
@@ -216,6 +227,7 @@ class OverviewMixin:
|
|||||||
if isinstance(runtime_payload, dict):
|
if isinstance(runtime_payload, dict):
|
||||||
payload = {**payload, **runtime_payload}
|
payload = {**payload, **runtime_payload}
|
||||||
return str(payload.get("view") or "").strip()
|
return str(payload.get("view") or "").strip()
|
||||||
|
|
||||||
def _refresh_account_gold_balance(
|
def _refresh_account_gold_balance(
|
||||||
self, client: DouyuActivityClient, account: Account
|
self, client: DouyuActivityClient, account: Account
|
||||||
) -> dict: ...
|
) -> dict: ...
|
||||||
@@ -455,11 +467,7 @@ class OverviewMixin:
|
|||||||
snapshot = account.overview_snapshot
|
snapshot = account.overview_snapshot
|
||||||
if not isinstance(snapshot, dict) or account.overview_queried_at is None:
|
if not isinstance(snapshot, dict) or account.overview_queried_at is None:
|
||||||
return None
|
return None
|
||||||
payload = (
|
payload = self._task_payload(task) if hasattr(self, "_task_payload") else {}
|
||||||
self._task_payload(task)
|
|
||||||
if hasattr(self, "_task_payload")
|
|
||||||
else {}
|
|
||||||
)
|
|
||||||
if bool(payload.get("force_refresh")):
|
if bool(payload.get("force_refresh")):
|
||||||
return None
|
return None
|
||||||
age = (now - account.overview_queried_at).total_seconds()
|
age = (now - account.overview_queried_at).total_seconds()
|
||||||
@@ -491,9 +499,7 @@ class OverviewMixin:
|
|||||||
cached["view"] = view or str(cached.get("view") or "")
|
cached["view"] = view or str(cached.get("view") or "")
|
||||||
message = _overview_success_message(
|
message = _overview_success_message(
|
||||||
cached,
|
cached,
|
||||||
cached.get("errors")
|
cached.get("errors") if isinstance(cached.get("errors"), dict) else {},
|
||||||
if isinstance(cached.get("errors"), dict)
|
|
||||||
else {},
|
|
||||||
view=view,
|
view=view,
|
||||||
)
|
)
|
||||||
self._push_log(
|
self._push_log(
|
||||||
@@ -788,9 +794,7 @@ class OverviewMixin:
|
|||||||
previous_overview.get("high_value_goods") or []
|
previous_overview.get("high_value_goods") or []
|
||||||
)
|
)
|
||||||
overview["pending_domains"].append(DOMAIN_STOREHOME)
|
overview["pending_domains"].append(DOMAIN_STOREHOME)
|
||||||
errors["limited_goods"] = (
|
errors["limited_goods"] = "本地商品快照已过期,已加入后台同步队列"
|
||||||
"本地商品快照已过期,已加入后台同步队列"
|
|
||||||
)
|
|
||||||
enqueue_account_work(
|
enqueue_account_work(
|
||||||
db,
|
db,
|
||||||
account_id=int(account.id),
|
account_id=int(account.id),
|
||||||
@@ -1006,9 +1010,7 @@ class OverviewMixin:
|
|||||||
]
|
]
|
||||||
all_records.extend((scope, r) for r in rows)
|
all_records.extend((scope, r) for r in rows)
|
||||||
overview[f"exchange_record_count_{scope}"] = len(rows)
|
overview[f"exchange_record_count_{scope}"] = len(rows)
|
||||||
overview["domain_sources"][DOMAIN_EXCHANGE_RECORDS] = (
|
overview["domain_sources"][DOMAIN_EXCHANGE_RECORDS] = "official_bootstrap"
|
||||||
"official_bootstrap"
|
|
||||||
)
|
|
||||||
|
|
||||||
overview["exchange_record_count"] = len(all_records)
|
overview["exchange_record_count"] = len(all_records)
|
||||||
# 兑换记录开始前结束前一步可能留下的角色快照事务。
|
# 兑换记录开始前结束前一步可能留下的角色快照事务。
|
||||||
@@ -1249,9 +1251,7 @@ class OverviewMixin:
|
|||||||
else:
|
else:
|
||||||
bind_label = "可换绑"
|
bind_label = "可换绑"
|
||||||
elif (account.change_role_wait_time or 0) > 0:
|
elif (account.change_role_wait_time or 0) > 0:
|
||||||
bind_label = (
|
bind_label = f"换绑冷却 {self._format_wait_time(account.change_role_wait_time)}"
|
||||||
f"换绑冷却 {self._format_wait_time(account.change_role_wait_time)}"
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
bind_label = "可换绑"
|
bind_label = "可换绑"
|
||||||
elif account.role_bind_status == "pending":
|
elif account.role_bind_status == "pending":
|
||||||
|
|||||||
Reference in New Issue
Block a user