Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c8c9c592cf | ||
|
|
98cd620f23 | ||
|
|
5e8e10e2fa | ||
|
|
af0e2fca8a | ||
|
|
11852bf48c |
4
.github/workflows/deploy.yml
vendored
4
.github/workflows/deploy.yml
vendored
@@ -90,10 +90,10 @@ jobs:
|
||||
IMAGE="${DOCKERHUB_USER}/${IMAGE_NAME}:latest"
|
||||
if [ -n "${VERSION_TAG}" ]; then
|
||||
VERSIONED_IMAGE="${DOCKERHUB_USER}/${IMAGE_NAME}:${VERSION_TAG}"
|
||||
docker build -t "${IMAGE}" -t "${VERSIONED_IMAGE}" .
|
||||
docker build --no-cache -t "${IMAGE}" -t "${VERSIONED_IMAGE}" .
|
||||
docker push "${VERSIONED_IMAGE}"
|
||||
else
|
||||
docker build -t "${IMAGE}" .
|
||||
docker build --no-cache -t "${IMAGE}" .
|
||||
fi
|
||||
|
||||
docker push "${IMAGE}"
|
||||
|
||||
@@ -107,3 +107,29 @@ async def send_to_discord(title: str, video_url: str, summary: str) -> None:
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.post(settings.discord_webhook_url, json=payload)
|
||||
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 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.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:
|
||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||
|
||||
video_id = extract_video_id(req.video_url)
|
||||
transcript = fetch_transcript(video_id)
|
||||
title = req.title or video_id
|
||||
summary = summarize(transcript, title)
|
||||
await send_to_discord(title, req.video_url, summary)
|
||||
title = req.title or "제목 없음"
|
||||
|
||||
try:
|
||||
video_id = extract_video_id(req.video_url)
|
||||
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)}
|
||||
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
import httpx
|
||||
import yt_dlp
|
||||
|
||||
COOKIES_SRC = "/app/cookies.txt"
|
||||
|
||||
|
||||
def extract_video_id(url: str) -> str:
|
||||
"""YouTube URL에서 video ID 추출."""
|
||||
@@ -17,16 +23,21 @@ def fetch_transcript(video_id: str) -> str:
|
||||
|
||||
ydl_opts = {
|
||||
"skip_download": True,
|
||||
"writeautomaticsub": True,
|
||||
"subtitleslangs": ["ko", "en"],
|
||||
"subtitlesformat": "json3",
|
||||
"quiet": True,
|
||||
"no_warnings": True,
|
||||
"cookiefile": "/app/cookies.txt",
|
||||
}
|
||||
|
||||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||
info = ydl.extract_info(url, download=False)
|
||||
if os.path.isfile(COOKIES_SRC):
|
||||
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, ie_key="Youtube", download=False, process=False)
|
||||
finally:
|
||||
if "cookiefile" in ydl_opts:
|
||||
os.unlink(ydl_opts["cookiefile"])
|
||||
|
||||
subs = info.get("automatic_captions", {})
|
||||
lang = "ko" if "ko" in subs else "en" if "en" in subs else None
|
||||
|
||||
Reference in New Issue
Block a user