feat(douyu): 落地账号平台同步与登录配额
This commit is contained in:
@@ -12,15 +12,26 @@ from sqlalchemy.orm import Session
|
||||
from core.douyu import DouyuActivityClient, DouyuActivityError
|
||||
|
||||
from ..models import Account, DouyuEsportsGoodsSnapshot, DouyuGoodsSnapshot, DouyuTask
|
||||
from .account_sync_service import (
|
||||
DOMAIN_EXCHANGE_RECORDS,
|
||||
finish_sync_failure,
|
||||
finish_sync_success,
|
||||
start_sync_run,
|
||||
sync_storehome_snapshot,
|
||||
)
|
||||
from .business_events import record_exchange
|
||||
from .douyu_activity_query import (
|
||||
query_activity_goods,
|
||||
query_activity_goods_result,
|
||||
query_csrf,
|
||||
query_elite_user_info,
|
||||
query_esports_goods_result,
|
||||
query_exchange_records_result,
|
||||
query_limited_goods_result,
|
||||
)
|
||||
from .douyu_goods_service import upsert_goods_snapshot
|
||||
from .douyu_read_gateway import storehome_snapshot_from_result
|
||||
from .douyu_runner_overview import record_official_exchange_records
|
||||
from .high_value_service import (
|
||||
DEFAULT_HIGH_VALUE_GOODS,
|
||||
HIGH_VALUE_STATUS_OWNED,
|
||||
@@ -78,12 +89,29 @@ class GoodsMixin:
|
||||
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
|
||||
):
|
||||
client = self._client(cookie)
|
||||
goods = query_activity_goods(
|
||||
result = query_activity_goods_result(
|
||||
client,
|
||||
manual_id=str(config["manual_id"]),
|
||||
rid=str(config["rid"]),
|
||||
)
|
||||
self._upsert_goods(db, goods)
|
||||
goods = [
|
||||
dict(item)
|
||||
for item in result.get("goods") or []
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
snapshot = storehome_snapshot_from_result(
|
||||
result,
|
||||
manual_id=str(config["manual_id"]),
|
||||
rid=str(config["rid"]),
|
||||
)
|
||||
upsert_goods_snapshot(db, goods, commit=False)
|
||||
sync_storehome_snapshot(
|
||||
db,
|
||||
account,
|
||||
snapshot,
|
||||
source="douyu_task.refresh_goods",
|
||||
task_id=task.id,
|
||||
)
|
||||
high_value_goods = extract_high_value_goods(goods, DEFAULT_HIGH_VALUE_GOODS, [])
|
||||
apply_high_value_goods(account, high_value_goods)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
@@ -723,7 +751,19 @@ class GoodsMixin:
|
||||
client, manual_id=str(config["manual_id"]), rid=str(config["rid"])
|
||||
)
|
||||
limited = result["limited_goods"]
|
||||
goods = (result.get("raw") or {}).get("data", {}).get("list") or []
|
||||
snapshot = storehome_snapshot_from_result(
|
||||
result,
|
||||
manual_id=str(config["manual_id"]),
|
||||
rid=str(config["rid"]),
|
||||
)
|
||||
goods = list(snapshot.items)
|
||||
sync_storehome_snapshot(
|
||||
db,
|
||||
account,
|
||||
snapshot,
|
||||
source="douyu_task.query_limited_goods",
|
||||
task_id=task.id,
|
||||
)
|
||||
high_value_goods = extract_high_value_goods(goods, DEFAULT_HIGH_VALUE_GOODS, [])
|
||||
apply_high_value_goods(account, high_value_goods)
|
||||
names = [
|
||||
@@ -760,6 +800,45 @@ class GoodsMixin:
|
||||
offset=0,
|
||||
)
|
||||
records = result.get("records") or []
|
||||
observed_at = datetime.now(UTC)
|
||||
sync_run = start_sync_run(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_EXCHANGE_RECORDS,
|
||||
source="douyu_task.query_exchange_records",
|
||||
task_id=task.id,
|
||||
)
|
||||
try:
|
||||
added = record_official_exchange_records(
|
||||
db,
|
||||
account,
|
||||
[("elite", record) for record in records if isinstance(record, dict)],
|
||||
)
|
||||
finish_sync_success(
|
||||
db,
|
||||
sync_run,
|
||||
item_count=len(records),
|
||||
changed_count=added,
|
||||
observed_at=observed_at,
|
||||
raw_payload=(
|
||||
result.get("raw")
|
||||
if isinstance(result.get("raw"), dict)
|
||||
else None
|
||||
),
|
||||
)
|
||||
except Exception as exc:
|
||||
finish_sync_failure(
|
||||
db,
|
||||
sync_run,
|
||||
error=exc,
|
||||
raw_payload=(
|
||||
result.get("raw")
|
||||
if isinstance(result.get("raw"), dict)
|
||||
else None
|
||||
),
|
||||
)
|
||||
raise
|
||||
result["exchange_events_added"] = added
|
||||
account.updated_at = datetime.now(UTC)
|
||||
self._mark_task(
|
||||
db,
|
||||
|
||||
Reference in New Issue
Block a user