From 3b9dcfe0ddc2a27d123e5c371277b7ae436dd4ed Mon Sep 17 00:00:00 2001 From: gahusb Date: Fri, 1 May 2026 12:22:33 +0900 Subject: [PATCH] =?UTF-8?q?fix(agent-office):=20YouTubeResearchAgent=20?= =?UTF-8?q?=ED=92=88=EC=A7=88=20=EA=B0=9C=EC=84=A0=20(=EB=8F=99=EC=8B=9C?= =?UTF-8?q?=EC=8B=A4=ED=96=89=20=EA=B0=80=EB=93=9C=C2=B7=EC=97=90=EB=9F=AC?= =?UTF-8?q?=20=EB=A1=9C=EA=B9=85=C2=B7=ED=83=80=EC=9E=85=20=EC=88=98?= =?UTF-8?q?=EC=A0=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- agent-office/app/agents/youtube.py | 22 +++++++++++++++------- agent-office/app/db.py | 2 +- agent-office/app/main.py | 6 +++--- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/agent-office/app/agents/youtube.py b/agent-office/app/agents/youtube.py index 11276a3..4305ec5 100644 --- a/agent-office/app/agents/youtube.py +++ b/agent-office/app/agents/youtube.py @@ -1,15 +1,20 @@ # agent-office/app/agents/youtube.py import asyncio +import logging from datetime import date +import httpx + from .base import BaseAgent -from ..db import add_youtube_research_job, update_youtube_research_job +from ..db import add_youtube_research_job, update_youtube_research_job, add_log from ..youtube_researcher import ( - TARGET_COUNTRIES, TREND_KEYWORDS, + TARGET_COUNTRIES, TREND_KEYWORDS, MUSIC_LAB_URL, fetch_youtube_trending, fetch_google_trends, fetch_billboard_top20, push_to_music_lab, ) +logger = logging.getLogger(__name__) + class YouTubeResearchAgent(BaseAgent): agent_id = "youtube" @@ -20,6 +25,8 @@ class YouTubeResearchAgent(BaseAgent): async def on_command(self, command: str, params: dict) -> dict: if command == "research": + if self.state == "working": + return {"ok": False, "message": "이미 수집 중"} countries = params.get("countries", TARGET_COUNTRIES) asyncio.create_task(self._run_research(countries)) return {"ok": True, "message": f"리서치 시작: {countries}"} @@ -59,15 +66,15 @@ class YouTubeResearchAgent(BaseAgent): async def send_weekly_report(self) -> None: """매주 월요일 08:00 — 주간 인사이트 텔레그램 발송.""" - import httpx - from ..youtube_researcher import MUSIC_LAB_URL try: async with httpx.AsyncClient(timeout=10.0) as client: resp = await client.get(f"{MUSIC_LAB_URL}/api/music/market/report/latest") if resp.status_code != 200: return report = resp.json() - except Exception: + except Exception as e: + add_log(self.agent_id, f"주간 리포트 조회 실패: {e}", level="error") + logger.error("send_weekly_report: music-lab 조회 실패: %s", e) return top = report.get("top_genres", [])[:3] @@ -81,5 +88,6 @@ class YouTubeResearchAgent(BaseAgent): try: from ..telegram_bot import send_message await send_message(text) - except (ImportError, Exception): - pass + except (ImportError, Exception) as e: + add_log(self.agent_id, f"주간 리포트 텔레그램 발송 실패: {e}", level="error") + logger.error("send_weekly_report: 텔레그램 발송 실패: %s", e) diff --git a/agent-office/app/db.py b/agent-office/app/db.py index eab9f93..392d087 100644 --- a/agent-office/app/db.py +++ b/agent-office/app/db.py @@ -542,7 +542,7 @@ def update_youtube_research_job( def get_latest_youtube_research_job() -> Optional[Dict[str, Any]]: with _conn() as conn: row = conn.execute( - "SELECT * FROM youtube_research_jobs ORDER BY started_at DESC LIMIT 1" + "SELECT * FROM youtube_research_jobs ORDER BY id DESC LIMIT 1" ).fetchone() if not row: return None diff --git a/agent-office/app/main.py b/agent-office/app/main.py index 25c8b12..1c9a48e 100644 --- a/agent-office/app/main.py +++ b/agent-office/app/main.py @@ -185,7 +185,7 @@ def activity_feed(limit: int = 50, offset: int = 0): # --- Realestate Agent Push Endpoint --- from pydantic import BaseModel -from typing import List, Dict, Any +from typing import List, Dict, Any, Optional class RealestateNotifyBody(BaseModel): @@ -208,8 +208,8 @@ class YouTubeResearchBody(BaseModel): @app.post("/api/agent-office/youtube/research") -async def trigger_youtube_research(body: YouTubeResearchBody = None): - agent = AGENT_REGISTRY.get("youtube") +async def trigger_youtube_research(body: Optional[YouTubeResearchBody] = None): + agent = get_agent("youtube") if not agent: raise HTTPException(status_code=503, detail="YouTubeResearchAgent 없음") params = {}