243 lines
7.5 KiB
Python
243 lines
7.5 KiB
Python
"""斗鱼官方兑换记录同步与事件补录。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import UTC, datetime
|
||
from typing import Any
|
||
|
||
from sqlalchemy.orm import Session
|
||
|
||
from core.douyu import DouyuActivityClient
|
||
|
||
from ..models import Account
|
||
from .account_sync_service import (
|
||
DOMAIN_EXCHANGE_RECORDS,
|
||
get_sync_state,
|
||
record_sync_success,
|
||
)
|
||
from .business_events import record_official_exchange
|
||
from .douyu_activity_query import (
|
||
query_exchange_records,
|
||
query_xpd_embed,
|
||
)
|
||
|
||
EXCHANGE_RECORD_FETCH_LIMIT = 100
|
||
|
||
|
||
def _to_int(value: Any) -> int | None:
|
||
if value is None or value == "" or isinstance(value, bool):
|
||
return None
|
||
try:
|
||
return int(float(value))
|
||
except (TypeError, ValueError, OverflowError):
|
||
return None
|
||
|
||
|
||
def _parse_official_time(value: Any) -> datetime | None:
|
||
if value is None or isinstance(value, bool):
|
||
return None
|
||
if isinstance(value, (int, float)):
|
||
seconds = float(value)
|
||
else:
|
||
text = str(value).strip()
|
||
if not text:
|
||
return None
|
||
if text.isdigit():
|
||
seconds = float(text)
|
||
else:
|
||
for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d"):
|
||
try:
|
||
return datetime.strptime(text, fmt).replace(tzinfo=UTC)
|
||
except ValueError:
|
||
continue
|
||
return None
|
||
if seconds > 10_000_000_000:
|
||
seconds /= 1000
|
||
try:
|
||
return datetime.fromtimestamp(seconds, tz=UTC)
|
||
except (OverflowError, OSError, ValueError):
|
||
return None
|
||
|
||
|
||
def _group_by_scope(
|
||
pairs: list[tuple[str, dict[str, Any]]],
|
||
) -> dict[str, list[dict[str, Any]]]:
|
||
grouped: dict[str, list[dict[str, Any]]] = {}
|
||
for scope, record in pairs:
|
||
grouped.setdefault(scope, []).append(record)
|
||
return grouped
|
||
|
||
|
||
def persist_official_exchange_records(
|
||
db: Session,
|
||
account: Account,
|
||
pairs: list[tuple[str, dict[str, Any]]],
|
||
) -> int:
|
||
"""把官方记录幂等补成账号事件,返回新写入数量。"""
|
||
added = 0
|
||
for scope, record in pairs:
|
||
if not isinstance(record, dict):
|
||
continue
|
||
occurred_at = _parse_official_time(record.get("time"))
|
||
if record_official_exchange(
|
||
db,
|
||
account,
|
||
commodity_id=str(
|
||
record.get("commodityId") or record.get("commodity_id") or ""
|
||
),
|
||
commodity_name=str(
|
||
record.get("commodityName") or record.get("commodity_name") or ""
|
||
),
|
||
score=_to_int(record.get("score")),
|
||
occurred_at=occurred_at,
|
||
show_name=str(
|
||
record.get("showName") or record.get("show_name") or ""
|
||
),
|
||
exchange_num=record.get("exchangeNum") or record.get("count"),
|
||
scope=scope,
|
||
):
|
||
added += 1
|
||
return added
|
||
|
||
|
||
def fetch_exchange_records(
|
||
client: DouyuActivityClient,
|
||
config: dict[str, Any],
|
||
*,
|
||
limit: int = EXCHANGE_RECORD_FETCH_LIMIT,
|
||
scopes: tuple[str, ...] | None = None,
|
||
max_pages: int = 1,
|
||
) -> tuple[list[tuple[str, dict[str, Any]]], dict[str, str]]:
|
||
"""拉取已配置作用域的官方兑换记录,单项失败不阻断其他作用域。"""
|
||
pairs: list[tuple[str, dict[str, Any]]] = []
|
||
errors: dict[str, str] = {}
|
||
requested_scopes = set(scopes or ("elite", "esports", "peace"))
|
||
page_limit = max(1, min(int(max_pages), 10))
|
||
manual_id = str(config.get("manual_id") or "").strip()
|
||
esports_manual_id = str(config.get("esports_manual_id") or "").strip()
|
||
configured = False
|
||
successful = 0
|
||
|
||
for scope, label, selected_manual_id in (
|
||
("elite", "精英宝典", manual_id),
|
||
("esports", "电竞手册", esports_manual_id),
|
||
):
|
||
if scope not in requested_scopes:
|
||
continue
|
||
if not selected_manual_id:
|
||
continue
|
||
configured = True
|
||
try:
|
||
rows: list[dict[str, Any]] = []
|
||
for page in range(page_limit):
|
||
page_rows = query_exchange_records(
|
||
client,
|
||
manual_id=selected_manual_id,
|
||
limit=limit,
|
||
offset=page * limit,
|
||
)
|
||
rows.extend(page_rows)
|
||
if len(page_rows) < limit:
|
||
break
|
||
except Exception as exc: # noqa: BLE001 - 单个外部查询域独立失败
|
||
errors[f"exchange_records_{scope}"] = (
|
||
f"{type(exc).__name__}: {exc}"[:200]
|
||
)
|
||
continue
|
||
successful += 1
|
||
pairs.extend((scope, dict(row)) for row in rows if isinstance(row, dict))
|
||
|
||
if "peace" in requested_scopes and bool(config.get("peace_enabled")):
|
||
xpd_act_alias = str(config.get("xpd_act_alias") or "").strip()
|
||
xpd_rid = str(config.get("xpd_rid") or "").strip()
|
||
xpd_act_id = str(config.get("xpd_act_id") or "").strip()
|
||
if xpd_act_alias and xpd_rid and xpd_act_id:
|
||
configured = True
|
||
try:
|
||
embed = query_xpd_embed(
|
||
client,
|
||
act_alias=xpd_act_alias,
|
||
rid=xpd_rid,
|
||
)
|
||
rows = (
|
||
client.xpd_purchase_records(
|
||
embed_query=embed["query"],
|
||
act_id=xpd_act_id,
|
||
).get("records")
|
||
or []
|
||
)
|
||
pairs.extend(
|
||
("peace", dict(row)) for row in rows if isinstance(row, dict)
|
||
)
|
||
successful += 1
|
||
except Exception as exc: # noqa: BLE001 - 单个外部查询域独立失败
|
||
errors["exchange_records_peace"] = (
|
||
f"{type(exc).__name__}: {exc}"[:200]
|
||
)
|
||
|
||
if not configured:
|
||
raise RuntimeError("未配置可同步的斗鱼活动参数")
|
||
if successful == 0:
|
||
raise RuntimeError(";".join(errors.values()) or "兑换记录查询失败")
|
||
return pairs, errors
|
||
|
||
|
||
def sync_exchange_records(
|
||
db: Session,
|
||
account: Account,
|
||
client: DouyuActivityClient,
|
||
config: dict[str, Any],
|
||
*,
|
||
source: str,
|
||
task_id: int | None = None,
|
||
scopes: tuple[str, ...] | None = None,
|
||
limit: int = EXCHANGE_RECORD_FETCH_LIMIT,
|
||
max_pages: int = 1,
|
||
) -> dict[str, Any]:
|
||
"""同步官方记录、补事件并保存可回放的按作用域原始投影。"""
|
||
observed_at = datetime.now(UTC)
|
||
pairs, errors = fetch_exchange_records(
|
||
client,
|
||
config,
|
||
limit=limit,
|
||
scopes=scopes,
|
||
max_pages=max_pages,
|
||
)
|
||
changed = persist_official_exchange_records(db, account, pairs)
|
||
previous = get_sync_state(
|
||
db,
|
||
account_id=int(account.id),
|
||
domain=DOMAIN_EXCHANGE_RECORDS,
|
||
)
|
||
grouped = (
|
||
dict(previous.raw_payload_json)
|
||
if previous is not None and isinstance(previous.raw_payload_json, dict)
|
||
else {}
|
||
)
|
||
grouped.update(_group_by_scope(pairs))
|
||
record_sync_success(
|
||
db,
|
||
account_id=int(account.id),
|
||
domain=DOMAIN_EXCHANGE_RECORDS,
|
||
source=source,
|
||
observed_at=observed_at,
|
||
item_count=len(pairs),
|
||
changed_count=changed,
|
||
raw_payload=grouped,
|
||
task_id=task_id,
|
||
)
|
||
return {
|
||
"item_count": len(pairs),
|
||
"changed_count": changed,
|
||
"records": grouped,
|
||
"errors": errors,
|
||
}
|
||
|
||
|
||
__all__ = [
|
||
"EXCHANGE_RECORD_FETCH_LIMIT",
|
||
"fetch_exchange_records",
|
||
"persist_official_exchange_records",
|
||
"sync_exchange_records",
|
||
]
|