feat(douyu): 高价值套装改用官方 storehome 三态判定
高价值套装的准入判定改为只认官方精英宝典 storehome 里账号级的 status:0=可兑换、1=已达兑换上限,商品缺失/接口异常=unknown。 兑换记录和积分记录只作历史展示,不参与是否拥有的判定。 原来的 high_value_owned(JSON 名单)无法区分「确认可兑换」和 「官方接口没返回商品」,也无法表达部分失败。改为两套独立状态 + 检查时间(迁移 20261106_0065),旧快照保守回填:status 缺失的 一律 unknown,不猜。 CK 判定失效时在数据层作废结论(降为 unknown),不是展示层降级。 官方 status 是账号维度的,但 CK 失效那段时间账号可能被别人手工 登录并兑换(这些号共享给客服),缓存里的「可兑换」就不成立了; 只在展示层降级的话,CK 恢复后那条陈旧的「可换」会重新变绿。 error(查不到)不作废 —— 查不到不等于账号出问题了。 高价值结论有自己的时钟:资料刷新除了看 overview_queried_at,还看 high_value_checked_at。两者会脱钩(商品接口单独失败时资料时间被刷新、 高价值时间停在原地),少了这一条这类账号会永久停在旧结论。作废的 结论按 6 小时慢速重试,但「从来没查到过」不因此重选 —— 那是接口对 该账号稳定失败,每轮都重选只会反复撞墙。 商城商品列表改用精英宝典的 manualID 拉取,不再寄生在电竞手册分支里 (否则没配电竞手册的配置就永远拉不到商城,三态全部停在待确认)。 筛选拆成三类「查不到」:unqueried(未查资料,旧口径不变)、 high_value_never(从未读到商城)、high_value_unknown(查过但结论 未知/过期)。之前第二类无处可寻 —— 它既不满足 unqueried 也不满足 high_value_unknown。
This commit is contained in:
@@ -36,9 +36,11 @@ from ..models import Account, DouyuTask
|
||||
from .account_binding import derive_from_bind_info, set_bind_status
|
||||
from .business_events import record_official_exchange
|
||||
from .douyu_runner_donate import _parse_expire_at, _points_expire_at
|
||||
|
||||
#: 默认关注的高价值商品。必须精确关注两款核心套装,排除头盔、背包等配件散件。
|
||||
DEFAULT_HIGH_VALUE_GOODS: tuple[str, ...] = ("套装-兔兔白日梦", "套装-狂战萌兔")
|
||||
from .high_value_service import (
|
||||
DEFAULT_HIGH_VALUE_GOODS,
|
||||
apply_high_value_goods,
|
||||
extract_high_value_goods,
|
||||
)
|
||||
|
||||
#: 官方兑换记录默认取多少条(完整记录可能很多,一次性拉回来没必要)
|
||||
EXCHANGE_RECORD_LIMIT = 20
|
||||
@@ -46,115 +48,11 @@ EXCHANGE_RECORD_LIMIT = 20
|
||||
#: 单项错误的摘要长度(避免把整个 traceback 塞进 task.result)
|
||||
_ERROR_MAX = 200
|
||||
|
||||
#: 配件散件排除关键词列表(避免把头盔、背包误识别为高价值套装)
|
||||
SUIT_EXCLUDE_KEYWORDS = ("头盔", "背包", "手榴弹", "挂件", "降落伞", "枪")
|
||||
|
||||
|
||||
def _err(exc: BaseException) -> str:
|
||||
return f"{type(exc).__name__}: {exc}"[:_ERROR_MAX]
|
||||
|
||||
|
||||
def _match_suit_item(want: str, rows: list[dict[str, Any]]) -> dict[str, Any] | None:
|
||||
"""在商品列表中精确匹配套装商品,严格排除头盔、背包等配件干扰。"""
|
||||
# 1. 优先完全一致匹配
|
||||
for it in rows:
|
||||
cname = str(it.get("commodityName") or "").strip()
|
||||
if cname == want:
|
||||
return it
|
||||
|
||||
# 2. 核心词(如 兔兔白日梦 / 狂战萌兔)精准匹配,排除配件关键词
|
||||
core = want.replace("套装-", "").strip()
|
||||
# 优先找带「套装」前缀的
|
||||
for it in rows:
|
||||
cname = str(it.get("commodityName") or "").strip()
|
||||
if (
|
||||
core in cname
|
||||
and "套装" in cname
|
||||
and not any(k in cname for k in SUIT_EXCLUDE_KEYWORDS)
|
||||
):
|
||||
return it
|
||||
|
||||
# 3. 兜底匹配不带排除词的项
|
||||
for it in rows:
|
||||
cname = str(it.get("commodityName") or "").strip()
|
||||
if core in cname and not any(k in cname for k in SUIT_EXCLUDE_KEYWORDS):
|
||||
return it
|
||||
return None
|
||||
|
||||
|
||||
def _match_suit_records(want: str, recs: list[dict[str, Any]]) -> list[str]:
|
||||
"""在官方兑换记录中匹配对应套装,严格排除配件干扰。"""
|
||||
core = want.replace("套装-", "").strip()
|
||||
got: list[str] = []
|
||||
for r in recs:
|
||||
cname = str(r.get("commodityName") or r.get("name") or "").strip()
|
||||
if core in cname and not any(k in cname for k in SUIT_EXCLUDE_KEYWORDS):
|
||||
got.append(cname)
|
||||
return got
|
||||
|
||||
|
||||
def extract_high_value_goods(
|
||||
items: list[dict[str, Any]] | None,
|
||||
wanted: tuple[str, ...] | list[str] | None = DEFAULT_HIGH_VALUE_GOODS,
|
||||
records: list[dict[str, Any]] | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""挑出高价值套装,并判定「该账号是否已获得 / 已达兑换上限」。
|
||||
|
||||
## 判定依据:**官方商城商品 status** + 官方兑换记录双重保障
|
||||
|
||||
在斗鱼官方精英手册商城中:
|
||||
- `item.status == 1`:斗鱼官方页面按钮展示为【已达兑换上限】(该账号限量 1 件已满,已兑换,不可再兑);
|
||||
- `item.status == 0`:斗鱼官方页面按钮展示为【立即兑换】(该账号未兑换,可兑换);
|
||||
- `records`(官方兑换记录):斗鱼官方常会清理历史兑换记录导致 records 返回空列表(0 条),
|
||||
但若 records 中仍存在记录,同样确认为已兑换;若商品已在商城下架不在列表里,records 作为历史兜底。
|
||||
"""
|
||||
names = [str(w).strip() for w in (wanted or []) if str(w).strip()]
|
||||
rows = [it for it in (items or []) if isinstance(it, dict)]
|
||||
recs = [r for r in (records or []) if isinstance(r, dict)]
|
||||
out: list[dict[str, Any]] = []
|
||||
for want in names:
|
||||
hit = _match_suit_item(want, rows)
|
||||
got = _match_suit_records(want, recs)
|
||||
|
||||
# 🎯 核心判定:
|
||||
# 1. 官方商城明确声明 status == 1 即为「已达兑换上限」(已兑换)
|
||||
# 2. 官方历史兑换记录中存在记录即为「已兑换」
|
||||
status_blocked = hit is not None and str(hit.get("status") or "") == "1"
|
||||
records_owned = bool(got)
|
||||
is_owned = status_blocked or records_owned
|
||||
|
||||
reason = ""
|
||||
if hit is None and not got:
|
||||
reason = "商品列表和官方兑换记录里都没有这个商品"
|
||||
elif status_blocked and not records_owned:
|
||||
reason = "官方商城显示已达兑换上限"
|
||||
elif records_owned and not status_blocked:
|
||||
reason = "官方兑换记录显示已兑换"
|
||||
elif status_blocked and records_owned:
|
||||
reason = "已达兑换上限且有官方兑换记录"
|
||||
|
||||
out.append(
|
||||
{
|
||||
"name": str(hit.get("commodityName") or want) if hit else want,
|
||||
"commodity_id": str(hit.get("commodityId") or "") if hit else "",
|
||||
# 🎯 是否已获得 / 已达上限
|
||||
"owned": is_owned,
|
||||
"obtained_names": got,
|
||||
# 以下是全局的展示信息
|
||||
"score": hit.get("score") if hit else None,
|
||||
"count": hit.get("count") if hit else None,
|
||||
"count_limited": hit.get("countLimited") if hit else None,
|
||||
"start_time": _ts(hit.get("startTime")) if hit else None,
|
||||
"end_time": _ts(hit.get("endTime")) if hit else None,
|
||||
"pic": str(hit.get("pic") or "") if hit else "",
|
||||
"in_goods_list": hit is not None,
|
||||
"missing": hit is None and not got,
|
||||
"reason": reason,
|
||||
}
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
def _group_by_scope(
|
||||
pairs: list[tuple[str, dict[str, Any]]],
|
||||
) -> dict[str, list[dict[str, Any]]]:
|
||||
@@ -357,8 +255,9 @@ class OverviewMixin:
|
||||
esports_manual_id = str(config.get("esports_manual_id") or "").strip()
|
||||
rid = str(config.get("rid") or "").strip()
|
||||
|
||||
queried_at = datetime.now(UTC)
|
||||
overview: dict[str, Any] = {
|
||||
"queried_at": datetime.now(UTC).isoformat(),
|
||||
"queried_at": queried_at.isoformat(),
|
||||
"assets": {},
|
||||
"elite": {},
|
||||
"esports": {},
|
||||
@@ -370,6 +269,7 @@ class OverviewMixin:
|
||||
# (作用域, 官方记录) 扁平列表,便于补事件
|
||||
all_records: list[tuple[str, dict[str, Any]]] = []
|
||||
pending_goods: list[dict[str, Any]] = []
|
||||
goods_query_ok = False
|
||||
|
||||
# ---- 鱼翅 + 钱包兑换余额 ----
|
||||
try:
|
||||
@@ -436,12 +336,23 @@ class OverviewMixin:
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["esports"] = _err(exc)
|
||||
|
||||
# ---- 精英宝典商城商品列表(高价值套装的**唯一**判据来源)----
|
||||
#
|
||||
# ⚠️ 必须用**精英宝典**的 manual_id(与 `refresh_goods` 任务一致),
|
||||
# 而且**不能**寄生在上面的「电竞手册」分支里:那是两个无关的活动。
|
||||
# 之前商品列表被写在电竞手册的 else 分支内 —— 于是一旦没配
|
||||
# `esports_manual_id`,商城商品就永远拉不到,高价值三态就永远
|
||||
# 落不到 `available`/`owned`(全部显示「待确认」)。
|
||||
if not manual_id:
|
||||
errors["limited_goods"] = "未配置精英宝典 manualID"
|
||||
else:
|
||||
goods_items: list[dict[str, Any]] = []
|
||||
try:
|
||||
# 🎯 拉取完整商城商品列表(含每个商品准确的 status 状态:1=已达兑换上限,0=可兑换)
|
||||
goods = client.list_goods(manual_id=manual_id, rid=rid)
|
||||
goods_items = goods.get("goods") or []
|
||||
overview["limited_goods_total"] = len(goods_items)
|
||||
goods_query_ok = True
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["limited_goods"] = _err(exc)
|
||||
# 商品列表带有 status 准入状态,结合兑换记录在后文判定高价值资产
|
||||
@@ -545,6 +456,13 @@ class OverviewMixin:
|
||||
_goods_config(config),
|
||||
[rec for _scope, rec in all_records],
|
||||
)
|
||||
if goods_query_ok:
|
||||
apply_high_value_goods(
|
||||
account,
|
||||
overview["high_value_goods"],
|
||||
checked_at=queried_at,
|
||||
)
|
||||
overview["high_value_checked_at"] = queried_at.isoformat()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["high_value_goods"] = _err(exc)
|
||||
|
||||
@@ -566,11 +484,7 @@ class OverviewMixin:
|
||||
# 落到账号上:任务会被保留策略清掉,而「这个账号现在什么情况」是账号的信息,
|
||||
# 应该在账号管理里看,不该依赖一条日志的寿命。
|
||||
account.overview_snapshot = overview
|
||||
account.overview_queried_at = datetime.now(UTC)
|
||||
# 再落「已兑换过的高价值套装名」给列表用(列表不能带整份快照)
|
||||
account.high_value_owned = [
|
||||
g["name"] for g in overview.get("high_value_goods", []) if g.get("owned")
|
||||
]
|
||||
account.overview_queried_at = queried_at
|
||||
|
||||
logger.info(f"[douyu] 一键查询账号 {account.id}: {message}")
|
||||
self._mark_task(db, task, "success", message, overview)
|
||||
|
||||
Reference in New Issue
Block a user