노래 생성까지 완료
This commit is contained in:
1 parent
dd855ff11a
commit
caf7b1ab60
4 files changed
+287
-7
No files matched your search
@@ -6,21 +6,30 @@ Song API Router
|
||||
엔드포인트 목록:
|
||||
- POST /song/generate: 노래 생성 요청
|
||||
- GET /song/status/{task_id}: 노래 생성 상태 조회
|
||||
- GET /song/download/{task_id}: 노래 다운로드
|
||||
|
||||
사용 예시:
|
||||
from app.song.api.routers.v1.song import router
|
||||
app.include_router(router, prefix="/api/v1")
|
||||
"""
|
||||
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
|
||||
import aiofiles
|
||||
import httpx
|
||||
from fastapi import APIRouter
|
||||
from uuid_extensions import uuid7str
|
||||
|
||||
from app.song.schemas.song_schema import (
|
||||
DownloadSongResponse,
|
||||
GenerateSongRequest,
|
||||
GenerateSongResponse,
|
||||
PollingSongResponse,
|
||||
SongClipData,
|
||||
)
|
||||
from app.utils.suno import SunoService
|
||||
from config import prj_settings
|
||||
|
||||
|
||||
def _parse_suno_status_response(result: dict) -> PollingSongResponse:
|
||||
@@ -39,25 +48,30 @@ def _parse_suno_status_response(result: dict) -> PollingSongResponse:
|
||||
)
|
||||
|
||||
status = data.get("status", "unknown")
|
||||
clips_data = data.get("data", [])
|
||||
|
||||
# 상태별 메시지
|
||||
# 클립 데이터는 data.response.sunoData에 있음 (camelCase)
|
||||
response_data = data.get("response", {})
|
||||
clips_data = response_data.get("sunoData", [])
|
||||
|
||||
# 상태별 메시지 (Suno API는 다양한 상태값 반환)
|
||||
status_messages = {
|
||||
"pending": "노래 생성 대기 중입니다.",
|
||||
"processing": "노래를 생성하고 있습니다.",
|
||||
"complete": "노래 생성이 완료되었습니다.",
|
||||
"SUCCESS": "노래 생성이 완료되었습니다.",
|
||||
"TEXT_SUCCESS": "노래 생성이 완료되었습니다.",
|
||||
"failed": "노래 생성에 실패했습니다.",
|
||||
}
|
||||
|
||||
# 클립 데이터 파싱
|
||||
# 클립 데이터 파싱 (Suno API는 camelCase 사용)
|
||||
clips = None
|
||||
if clips_data:
|
||||
clips = [
|
||||
SongClipData(
|
||||
id=clip.get("id"),
|
||||
audio_url=clip.get("audio_url"),
|
||||
stream_audio_url=clip.get("stream_audio_url"),
|
||||
image_url=clip.get("image_url"),
|
||||
audio_url=clip.get("audioUrl"),
|
||||
stream_audio_url=clip.get("streamAudioUrl"),
|
||||
image_url=clip.get("imageUrl"),
|
||||
title=clip.get("title"),
|
||||
status=clip.get("status"),
|
||||
duration=clip.get("duration"),
|
||||
@@ -194,3 +208,129 @@ async def get_song_status(
|
||||
raw_response=None,
|
||||
error_message=str(e),
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/download/{task_id}",
|
||||
summary="노래 다운로드",
|
||||
description="""
|
||||
완료된 노래를 서버에 다운로드하고 접근 가능한 URL을 반환합니다.
|
||||
|
||||
## 경로 파라미터
|
||||
- **task_id**: 노래 생성 시 반환된 작업 ID (필수)
|
||||
|
||||
## 반환 정보
|
||||
- **success**: 다운로드 성공 여부
|
||||
- **message**: 응답 메시지
|
||||
- **file_path**: 저장된 파일의 상대 경로
|
||||
- **file_url**: 프론트엔드에서 접근 가능한 파일 URL
|
||||
|
||||
## 사용 예시
|
||||
```
|
||||
GET /song/download/abc123...
|
||||
```
|
||||
|
||||
## 참고
|
||||
- 노래 생성이 완료된 상태(complete)에서만 다운로드 가능합니다.
|
||||
- 파일은 /media/{날짜}/{uuid7}/song.mp3 경로에 저장됩니다.
|
||||
- 반환된 file_url을 사용하여 프론트엔드에서 MP3를 재생할 수 있습니다.
|
||||
""",
|
||||
response_model=DownloadSongResponse,
|
||||
responses={
|
||||
200: {"description": "다운로드 성공"},
|
||||
400: {"description": "노래 생성이 완료되지 않음"},
|
||||
500: {"description": "다운로드 실패"},
|
||||
},
|
||||
)
|
||||
async def download_song(
|
||||
task_id: str,
|
||||
) -> DownloadSongResponse:
|
||||
"""완료된 노래를 다운로드하여 서버에 저장하고 접근 URL을 반환합니다."""
|
||||
try:
|
||||
suno_service = SunoService()
|
||||
result = await suno_service.get_task_status(task_id)
|
||||
|
||||
# API 응답 확인
|
||||
if result.get("code") != 200:
|
||||
return DownloadSongResponse(
|
||||
success=False,
|
||||
message="Suno API 응답 오류",
|
||||
error_message=result.get("msg", "Unknown error"),
|
||||
)
|
||||
|
||||
data = result.get("data", {})
|
||||
status = data.get("status", "unknown")
|
||||
|
||||
# 완료 상태 확인 (Suno API는 다양한 완료 상태값 반환)
|
||||
completed_statuses = {"complete", "SUCCESS", "TEXT_SUCCESS"}
|
||||
if status not in completed_statuses:
|
||||
return DownloadSongResponse(
|
||||
success=False,
|
||||
message=f"노래 생성이 완료되지 않았습니다. 현재 상태: {status}",
|
||||
error_message="노래 생성 완료 후 다운로드해 주세요.",
|
||||
)
|
||||
|
||||
# 클립 데이터는 data.response.sunoData에 있음 (camelCase)
|
||||
response_data = data.get("response", {})
|
||||
clips_data = response_data.get("sunoData", [])
|
||||
if not clips_data:
|
||||
return DownloadSongResponse(
|
||||
success=False,
|
||||
message="생성된 노래 클립이 없습니다.",
|
||||
error_message="sunoData is empty",
|
||||
)
|
||||
|
||||
# 첫 번째 클립의 streamAudioUrl 가져오기 (camelCase)
|
||||
first_clip = clips_data[0]
|
||||
stream_audio_url = first_clip.get("streamAudioUrl")
|
||||
|
||||
if not stream_audio_url:
|
||||
return DownloadSongResponse(
|
||||
success=False,
|
||||
message="스트리밍 오디오 URL을 찾을 수 없습니다.",
|
||||
error_message="stream_audio_url is missing",
|
||||
)
|
||||
|
||||
# 저장 경로 생성: media/{날짜}/{uuid7}/song.mp3
|
||||
today = date.today().isoformat()
|
||||
unique_id = uuid7str()
|
||||
relative_dir = f"{today}/{unique_id}"
|
||||
file_name = "song.mp3"
|
||||
|
||||
# 절대 경로 생성
|
||||
media_dir = Path("media") / today / unique_id
|
||||
media_dir.mkdir(parents=True, exist_ok=True)
|
||||
file_path = media_dir / file_name
|
||||
|
||||
# 오디오 파일 다운로드 (비동기 파일 쓰기)
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(stream_audio_url, timeout=60.0)
|
||||
response.raise_for_status()
|
||||
|
||||
async with aiofiles.open(file_path, "wb") as f:
|
||||
await f.write(response.content)
|
||||
|
||||
# 프론트엔드에서 접근 가능한 URL 생성
|
||||
relative_path = f"/media/{relative_dir}/{file_name}"
|
||||
base_url = f"http://{prj_settings.PROJECT_DOMAIN}"
|
||||
file_url = f"{base_url}{relative_path}"
|
||||
|
||||
return DownloadSongResponse(
|
||||
success=True,
|
||||
message="노래 다운로드가 완료되었습니다.",
|
||||
file_path=relative_path,
|
||||
file_url=file_url,
|
||||
)
|
||||
|
||||
except httpx.HTTPError as e:
|
||||
return DownloadSongResponse(
|
||||
success=False,
|
||||
message="오디오 파일 다운로드에 실패했습니다.",
|
||||
error_message=str(e),
|
||||
)
|
||||
except Exception as e:
|
||||
return DownloadSongResponse(
|
||||
success=False,
|
||||
message="노래 다운로드에 실패했습니다.",
|
||||
error_message=str(e),
|
||||
)
|
||||
@@ -122,6 +122,29 @@ class PollingSongResponse(BaseModel):
|
||||
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시)")
|
||||
|
||||
|
||||
class DownloadSongResponse(BaseModel):
|
||||
"""노래 다운로드 응답 스키마
|
||||
|
||||
Usage:
|
||||
GET /song/download/{task_id}
|
||||
Downloads the generated song and returns the local file path.
|
||||
|
||||
Example Response:
|
||||
{
|
||||
"success": true,
|
||||
"message": "노래 다운로드가 완료되었습니다.",
|
||||
"file_path": "/media/2025-01-15/01234567-89ab-7def-0123-456789abcdef/song.mp3",
|
||||
"file_url": "http://localhost:8000/media/2025-01-15/01234567-89ab-7def-0123-456789abcdef/song.mp3"
|
||||
}
|
||||
"""
|
||||
|
||||
success: bool = Field(..., description="다운로드 성공 여부")
|
||||
message: str = Field(..., description="응답 메시지")
|
||||
file_path: Optional[str] = Field(None, description="저장된 파일 경로 (상대 경로)")
|
||||
file_url: Optional[str] = Field(None, description="파일 접근 URL")
|
||||
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시)")
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Dataclass Schemas (Legacy)
|
||||
# =============================================================================
|
||||
|
||||
Reference in new issue
Block a user