feat: extend web tracking and integration controls

This commit is contained in:
kris
2026-03-22 14:13:10 +08:00
parent dab444a83c
commit 652f0c9f79
4 changed files with 683 additions and 55 deletions

View File

@@ -1885,6 +1885,33 @@ def register_douyin_routes(app: Any, legacy: Any) -> None:
"items": items[: max(1, min(limit, 100))]
}
async def _refresh_tracked_account_workspace(
owner: dict[str, Any],
tracked_account_id: str,
discovery_note: str = "tracking_refresh"
) -> dict[str, Any]:
account_row = _require_owned_account(tracked_account_id, owner["id"])
profile_url = _first_non_empty(
account_row.get("canonical_profile_url"),
account_row.get("profile_url")
)
if not profile_url:
raise HTTPException(status_code=400, detail="Tracked account has no profile_url to refresh")
request = DouyinAccountSyncRequest(
profile_url=profile_url,
compact_response=True,
discovery_note=discovery_note
)
public_data = await _collect_public_profile(profile_url, None)
creator_data = {"pages": [], "errors": []}
return await run_in_threadpool(
_finalize_sync_workspace,
owner,
request,
public_data,
creator_data
)
def _normalize_report_text(value: Any) -> str:
text = str(value or "").strip()
if not text:
@@ -3374,6 +3401,71 @@ def register_douyin_routes(app: Any, legacy: Any) -> None:
"items": _list_tracked_accounts(account["id"])
}
@app.post("/v2/douyin/tracking/accounts/{tracked_account_id}/refresh")
async def refresh_douyin_tracked_account(
tracked_account_id: str,
account: dict[str, Any] = Depends(legacy.require_approved)
) -> dict[str, Any]:
account_row = _require_owned_account(tracked_account_id, account["id"])
account_payload = _build_account_payload(account_row, include_recent_videos=6)
try:
refreshed = await _refresh_tracked_account_workspace(account, tracked_account_id)
return {
"success": True,
"tracked_account_id": tracked_account_id,
"account": refreshed.get("account", {}),
"sync_errors": refreshed.get("sync_errors", []),
"public_video_count": refreshed.get("public_video_count", 0),
"creator_page_count": refreshed.get("creator_page_count", 0)
}
except HTTPException as exc:
detail = exc.detail if isinstance(exc.detail, dict) else {"message": str(exc.detail)}
return {
"success": False,
"tracked_account_id": tracked_account_id,
"account": account_payload,
"message": detail.get("message") or str(exc.detail),
"detail": detail,
"sync_errors": detail.get("public_errors", []) + detail.get("creator_errors", [])
}
@app.post("/v2/douyin/tracking/refresh")
async def refresh_all_douyin_tracked_accounts(
account: dict[str, Any] = Depends(legacy.require_approved)
) -> dict[str, Any]:
tracked_accounts = _list_tracked_accounts(account["id"])
items: list[dict[str, Any]] = []
errors: list[dict[str, Any]] = []
for tracked in tracked_accounts:
try:
refreshed = await _refresh_tracked_account_workspace(account, tracked["tracked_account_id"])
items.append({
"tracking_id": tracked["id"],
"tracked_account_id": tracked["tracked_account_id"],
"nickname": (refreshed.get("account") or {}).get("nickname", ""),
"sync_errors": refreshed.get("sync_errors", []),
"public_video_count": refreshed.get("public_video_count", 0)
})
except HTTPException as exc:
errors.append({
"tracking_id": tracked["id"],
"tracked_account_id": tracked["tracked_account_id"],
"message": str(exc.detail)
})
except Exception as exc:
errors.append({
"tracking_id": tracked["id"],
"tracked_account_id": tracked["tracked_account_id"],
"message": str(exc)
})
return {
"tracked_count": len(tracked_accounts),
"refreshed": len(items),
"failed": len(errors),
"items": items,
"errors": errors
}
@app.post("/v2/douyin/tracking/cursor")
def update_douyin_tracking_cursor(
request: DouyinTrackingCursorRequest,