import logging import httpx from yuxi.channel.protocols import ChannelAccountSnapshot logger = logging.getLogger(__name__) PROBE_URL = "https://open.douyin.com/oauth/client_token/" class DouyinStatus: def __init__(self, gateway=None): self._gateway = gateway async def probe(self, account: dict | None = None) -> bool: gateway = self._gateway if gateway is None: return False token = gateway.client_token if not token: return False url = f"{PROBE_URL}?access_token={token}" try: client = gateway.http if client is None: client = httpx.AsyncClient(timeout=10.0) try: resp = await client.get(url) data = resp.json() finally: await client.aclose() else: resp = await client.get(url) data = resp.json() if "data" in data and data.get("data", {}).get("error_code", -1) == 0: return True logger.warning("Douyin probe failed: %s", data.get("message", "unknown error")) return False except Exception: logger.exception("Douyin probe failed") return False def build_summary(self, snapshot: object) -> dict: if not isinstance(snapshot, ChannelAccountSnapshot): return {} return { "account_id": snapshot.account_id, "running": snapshot.running, "connected": snapshot.connected, "health_state": snapshot.health_state, } def build_account_snapshot( self, account: dict, config: dict, runtime: ChannelAccountSnapshot | None = None, probe_result: object | None = None, audit: object | None = None, ) -> ChannelAccountSnapshot: gateway = self._gateway token = gateway.client_token if gateway else None return ChannelAccountSnapshot( account_id=account.get("account_id", "default"), name=account.get("name", ""), enabled=account.get("enabled", True), configured=bool(account.get("client_key") and account.get("client_secret")), status_state="linked" if token else "not-linked", running=getattr(runtime, "running", False) if runtime else False, connected=bool(token), last_message_at=getattr(runtime, "last_message_at", None) if runtime else None, health_state="ok" if probe_result else "unknown", dm_policy=account.get("dm_policy", "open"), ) def collect_status_issues(self, accounts: list[ChannelAccountSnapshot]) -> list: issues = [] for acc in accounts: if not acc.configured: issues.append( { "channel": "douyin", "account_id": acc.account_id, "kind": "not-configured", "message": "Douyin credentials not configured", "fix": "Set DOUYIN_CLIENT_KEY and DOUYIN_CLIENT_SECRET", } ) elif not acc.connected: issues.append( { "channel": "douyin", "account_id": acc.account_id, "kind": "not-connected", "message": "Douyin gateway not running", "fix": "Check environment variables and gateway status", } ) return issues