Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af0e2fca8a | ||
|
|
11852bf48c |
@@ -107,3 +107,29 @@ async def send_to_discord(title: str, video_url: str, summary: str) -> None:
|
|||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
resp = await client.post(settings.discord_webhook_url, json=payload)
|
resp = await client.post(settings.discord_webhook_url, json=payload)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
async def send_error_to_discord(
|
||||||
|
title: str, video_url: str, error: Exception
|
||||||
|
) -> None:
|
||||||
|
"""에러 발생 시 Discord 웹훅으로 에러 내용 전송."""
|
||||||
|
error_type = type(error).__name__
|
||||||
|
error_msg = str(error)[:1024]
|
||||||
|
|
||||||
|
embed = {
|
||||||
|
"title": "❌ 뉴스 요약 실패",
|
||||||
|
"color": 0xED4245,
|
||||||
|
"fields": [
|
||||||
|
{"name": "영상 제목", "value": title or "(제목 없음)", "inline": False},
|
||||||
|
{"name": "영상 URL", "value": video_url, "inline": False},
|
||||||
|
{"name": "에러 타입", "value": f"`{error_type}`", "inline": True},
|
||||||
|
{"name": "에러 내용", "value": f"```\n{error_msg}\n```", "inline": False},
|
||||||
|
],
|
||||||
|
"footer": {"text": "YouTube 뉴스 요약 봇 - 에러 알림"},
|
||||||
|
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||||
|
}
|
||||||
|
|
||||||
|
payload = {"embeds": [embed]}
|
||||||
|
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
await client.post(settings.discord_webhook_url, json=payload)
|
||||||
|
|||||||
17
app/main.py
17
app/main.py
@@ -2,7 +2,7 @@ from fastapi import FastAPI, Header, HTTPException
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from app.config import settings
|
from app.config import settings
|
||||||
from app.discord import send_to_discord
|
from app.discord import send_error_to_discord, send_to_discord
|
||||||
from app.summarizer import summarize
|
from app.summarizer import summarize
|
||||||
from app.transcript import extract_video_id, fetch_transcript
|
from app.transcript import extract_video_id, fetch_transcript
|
||||||
|
|
||||||
@@ -22,11 +22,16 @@ async def summarize_video(
|
|||||||
if settings.api_secret and x_api_secret != settings.api_secret:
|
if settings.api_secret and x_api_secret != settings.api_secret:
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
video_id = extract_video_id(req.video_url)
|
title = req.title or "제목 없음"
|
||||||
transcript = fetch_transcript(video_id)
|
|
||||||
title = req.title or video_id
|
try:
|
||||||
summary = summarize(transcript, title)
|
video_id = extract_video_id(req.video_url)
|
||||||
await send_to_discord(title, req.video_url, summary)
|
transcript = fetch_transcript(video_id)
|
||||||
|
summary = summarize(transcript, title)
|
||||||
|
await send_to_discord(title, req.video_url, summary)
|
||||||
|
except Exception as e:
|
||||||
|
await send_error_to_discord(title, req.video_url, e)
|
||||||
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
return {"status": "ok", "title": title, "summary_length": len(summary)}
|
return {"status": "ok", "title": title, "summary_length": len(summary)}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import tempfile
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import yt_dlp
|
import yt_dlp
|
||||||
|
|
||||||
|
COOKIES_SRC = "/app/cookies.txt"
|
||||||
|
|
||||||
|
|
||||||
def extract_video_id(url: str) -> str:
|
def extract_video_id(url: str) -> str:
|
||||||
"""YouTube URL에서 video ID 추출."""
|
"""YouTube URL에서 video ID 추출."""
|
||||||
@@ -20,13 +26,22 @@ def fetch_transcript(video_id: str) -> str:
|
|||||||
"writeautomaticsub": True,
|
"writeautomaticsub": True,
|
||||||
"subtitleslangs": ["ko", "en"],
|
"subtitleslangs": ["ko", "en"],
|
||||||
"subtitlesformat": "json3",
|
"subtitlesformat": "json3",
|
||||||
|
"format": "worst",
|
||||||
"quiet": True,
|
"quiet": True,
|
||||||
"no_warnings": True,
|
"no_warnings": True,
|
||||||
"cookiefile": "/app/cookies.txt",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
if os.path.isfile(COOKIES_SRC):
|
||||||
info = ydl.extract_info(url, download=False)
|
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
|
||||||
|
shutil.copy2(COOKIES_SRC, tmp.name)
|
||||||
|
ydl_opts["cookiefile"] = tmp.name
|
||||||
|
|
||||||
|
try:
|
||||||
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||||
|
info = ydl.extract_info(url, download=False)
|
||||||
|
finally:
|
||||||
|
if "cookiefile" in ydl_opts:
|
||||||
|
os.unlink(ydl_opts["cookiefile"])
|
||||||
|
|
||||||
subs = info.get("automatic_captions", {})
|
subs = info.get("automatic_captions", {})
|
||||||
lang = "ko" if "ko" in subs else "en" if "en" in subs else None
|
lang = "ko" if "ko" in subs else "en" if "en" in subs else None
|
||||||
|
|||||||
Reference in New Issue
Block a user