统一推送前Python代码格式

This commit is contained in:
yml2213
2026-09-05 19:25:46 +08:00
parent 90a0065918
commit 8309072120
17 changed files with 492 additions and 189 deletions
+57 -24
View File
@@ -30,7 +30,11 @@ from core.huya import (
login_huya_sms,
send_huya_sms_code,
)
from core.huya.cookie_utils import browser_login_script, cookie_pairs, normalize_huya_cookie
from core.huya.cookie_utils import (
browser_login_script,
cookie_pairs,
normalize_huya_cookie,
)
from core.huya.device_fingerprint import account_state_dir as _fp_state_dir
from core.huya.device_profile import (
_load_db as _load_profile_db,
@@ -119,15 +123,24 @@ def create_browser_exchange_kit(
登录态由前端引导用户在自己的 Chrome/Edge 中写入,服务端不托管浏览器会话。
"""
_require_huya_perm(current, "huya:task")
account = _visible_huya_accounts_query(db, current).filter(HuyaAccount.id == account_id).first()
account = (
_visible_huya_accounts_query(db, current)
.filter(HuyaAccount.id == account_id)
.first()
)
if account is None:
raise HTTPException(status_code=404, detail="虎牙账号不存在或无权操作")
if not account.cookie:
raise HTTPException(status_code=400, detail="该账号没有可用 Cookie,请先登录")
config = ensure_huya_config(db)
target_url = BindMixin._bind_redirect_url({
"room_pid": huya_config_value("room_pid", config.room_pid),
}) or "https://zt.huya.com/b02faae1/pc/index.html"
target_url = (
BindMixin._bind_redirect_url(
{
"room_pid": huya_config_value("room_pid", config.room_pid),
}
)
or "https://zt.huya.com/b02faae1/pc/index.html"
)
pairs = dict(cookie_pairs(normalize_huya_cookie(account.cookie)))
return {
"account_id": account.id,
@@ -321,10 +334,13 @@ def _workbench_account_ids(db: Session, current: User, scope: str) -> list[int]:
HuyaWorkbenchAccount.handbook_scope == scope,
)
if not _can_view_huya_all(current):
query = query.join(HuyaAccount, HuyaAccount.id == HuyaWorkbenchAccount.account_id).filter(
HuyaAccount.assigned_to == current.id
)
return [account_id for (account_id,) in query.order_by(HuyaWorkbenchAccount.id.asc()).all()]
query = query.join(
HuyaAccount, HuyaAccount.id == HuyaWorkbenchAccount.account_id
).filter(HuyaAccount.assigned_to == current.id)
return [
account_id
for (account_id,) in query.order_by(HuyaWorkbenchAccount.id.asc()).all()
]
def _require_huya_batch_owner(db: Session, current: User, batch_id: str) -> None:
@@ -1847,10 +1863,15 @@ def list_huya_workbench_accounts(
):
"""读取当前用户指定工作台(精英宝典/浏览器兑换)的账号集合。"""
ids = _workbench_account_ids(db, current, handbook_scope)
configured = db.query(HuyaWorkbench.id).filter(
HuyaWorkbench.user_id == current.id,
HuyaWorkbench.handbook_scope == handbook_scope,
).first() is not None
configured = (
db.query(HuyaWorkbench.id)
.filter(
HuyaWorkbench.user_id == current.id,
HuyaWorkbench.handbook_scope == handbook_scope,
)
.first()
is not None
)
return {"account_ids": ids, "configured": configured}
@@ -1866,10 +1887,14 @@ def update_huya_workbench_accounts(
raise HTTPException(status_code=400, detail="无效的账号 ID")
if ids:
_require_huya_task_account_access(db, current, ids)
workbench = db.query(HuyaWorkbench).filter(
HuyaWorkbench.user_id == current.id,
HuyaWorkbench.handbook_scope == req.handbook_scope,
).first()
workbench = (
db.query(HuyaWorkbench)
.filter(
HuyaWorkbench.user_id == current.id,
HuyaWorkbench.handbook_scope == req.handbook_scope,
)
.first()
)
if workbench is None:
db.add(HuyaWorkbench(user_id=current.id, handbook_scope=req.handbook_scope))
else:
@@ -1878,12 +1903,16 @@ def update_huya_workbench_accounts(
HuyaWorkbenchAccount.user_id == current.id,
HuyaWorkbenchAccount.handbook_scope == req.handbook_scope,
).delete(synchronize_session=False)
db.add_all([
HuyaWorkbenchAccount(
user_id=current.id, handbook_scope=req.handbook_scope, account_id=account_id
)
for account_id in ids
])
db.add_all(
[
HuyaWorkbenchAccount(
user_id=current.id,
handbook_scope=req.handbook_scope,
account_id=account_id,
)
for account_id in ids
]
)
db.commit()
return {"account_ids": ids, "success": True}
@@ -1907,7 +1936,11 @@ def list_recharge_goods(
current: User = Depends(require_permission("huya:task")),
):
"""查看已缓存的虎牙充值商品快照。"""
rows = db.query(HuyaRechargeGoodsSnapshot).order_by(HuyaRechargeGoodsSnapshot.id.desc()).all()
rows = (
db.query(HuyaRechargeGoodsSnapshot)
.order_by(HuyaRechargeGoodsSnapshot.id.desc())
.all()
)
latest = {}
for row in rows:
latest.setdefault((row.sid, row.spu_id), row)