优化(登录预算与任务): 建议上限封顶2000、错误中文化及补全跳过触发来源

This commit is contained in:
yml
2026-11-26 17:13:12 +08:00
parent 917c11f203
commit 3431705096
8 changed files with 498 additions and 34 deletions
+102 -1
View File
@@ -4,6 +4,7 @@ from __future__ import annotations
from dataclasses import dataclass
from datetime import UTC, date, datetime, timedelta
import re
from zoneinfo import ZoneInfo
from sqlalchemy import and_, func, or_, select
@@ -57,14 +58,104 @@ _TIER_BY_CATEGORY: dict[str, int] = {
CATEGORY_ONBOARDING: 4,
}
CATEGORY_LABELS: dict[str, str] = {
CATEGORY_ONBOARDING: "首次登录",
CATEGORY_RELOGIN_HIGH: "高价值重登",
CATEGORY_RELOGIN_ORDINARY: "普通重登",
CATEGORY_RELOGIN_PROTECTED: "履约保护重登",
CATEGORY_LEASE_REFRESH: "履约快过期续期",
CATEGORY_MANUAL: "人工登录",
"manual": "人工登录",
CATEGORY_ACCOUNT_CHECK: "账号检测",
}
AUTOMATED_CATEGORIES: frozenset[str] = frozenset(
{
CATEGORY_ONBOARDING,
CATEGORY_RELOGIN_HIGH,
CATEGORY_RELOGIN_ORDINARY,
CATEGORY_RELOGIN_PROTECTED,
CATEGORY_LEASE_REFRESH,
}
)
_HTTP_POOL_RE = re.compile(
r"HTTPS?ConnectionPool\([^\n]*?(?=(?:(旧 Cookie 已保留)|$))"
)
_QUOTA_KEY_RE = re.compile(
r"(relogin_protected|lease_refresh|relogin_high_value|relogin_ordinary|onboarding|manual_login|account_check|all)\s*今日登录预算已用完"
)
BUSINESS_TZ = ZoneInfo("Asia/Shanghai")
def category_label(category: str | None) -> str:
"""将登录分类 key 映射为中文展示名。"""
key = (category or "").strip()
if key == CATEGORY_ALL:
return "全局"
return CATEGORY_LABELS.get(key, key)
def infer_category_from_message(message: str | None) -> str:
"""从跳过或失败文案中提取登录分类 key(兼容历史原始英文 key 与中文标签)。"""
text = message or ""
if not text:
return ""
for key, label in CATEGORY_LABELS.items():
if key == "manual":
continue
if key in text or f"{label}今日登录预算已用完" in text:
return key
return ""
def humanize_login_message(message: str | None) -> str:
"""把历史或底层英文登录错误、跳过原因整理为可读中文。"""
text = str(message or "")
if not text:
return ""
text = _QUOTA_KEY_RE.sub(
lambda match: f"{category_label(match.group(1))}今日登录预算已用完",
text,
)
text = text.replace("极验 slide 验证失败: forbidden", "极验滑块验证失败: 触发风控拦截")
text = text.replace("极验 slide 验证失败: fail", "极验滑块验证失败: 滑块未通过")
text = text.replace("极验 slide 验证失败:", "极验滑块验证失败:")
text = text.replace("极验 slide 验证失败", "极验滑块验证失败")
if (
"HTTPSConnectionPool(" in text
or "HTTPConnectionPool(" in text
or "Max retries exceeded" in text
):
lower = text.lower()
if "proxyerror" in lower or "unable to connect to proxy" in lower:
network_reason = "代理连接失败"
elif "timed out" in lower or "timeouterror" in lower:
network_reason = "网络或代理请求超时"
elif "ssl" in lower:
network_reason = "SSL 安全连接失败"
elif "max retries exceeded" in lower:
network_reason = "网络连接失败(重试次数超限)"
else:
network_reason = "网络连接异常"
if _HTTP_POOL_RE.search(text):
text = _HTTP_POOL_RE.sub(network_reason, text)
else:
text = text.replace("Max retries exceeded", "网络连接失败(重试次数超限)")
text = text.replace("Read timed out", "网络读取超时")
return text
@dataclass(frozen=True)
class QuotaDecision:
allowed: bool
reason: str = ""
attempt: LoginAttempt | None = None
category: str = ""
def _bucket(now: datetime | None = None) -> tuple[date, int]:
@@ -620,7 +711,12 @@ def reserve_login_attempt(
if _category_daily_total(db, quota_day, resolved_category) >= _category_limit(
setting, resolved_category
):
return QuotaDecision(False, reason=f"{resolved_category} 今日登录预算已用完")
label = category_label(resolved_category)
return QuotaDecision(
False,
reason=f"{label}今日登录预算已用完",
category=resolved_category,
)
attempt = LoginAttempt(
account_id=int(account.id),
@@ -707,7 +803,9 @@ def cleanup_orphan_login_attempts(
__all__ = [
"AUTOMATED_CATEGORIES",
"CATEGORY_ACCOUNT_CHECK",
"CATEGORY_LABELS",
"CATEGORY_LEASE_REFRESH",
"CATEGORY_MANUAL",
"CATEGORY_ONBOARDING",
@@ -715,10 +813,13 @@ __all__ = [
"CATEGORY_RELOGIN_ORDINARY",
"CATEGORY_RELOGIN_PROTECTED",
"QuotaDecision",
"category_label",
"classify_login_category",
"cleanup_orphan_login_attempts",
"finish_login_attempt",
"get_login_quota_usage_summary",
"humanize_login_message",
"infer_category_from_message",
"login_concurrency_limit",
"reserve_login_attempt",
]