Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba26284451 | ||
|
|
3f75b6d61d | ||
|
|
2e1ccebe43 | ||
|
|
b84c07c325 | ||
|
|
94aae50564 | ||
|
|
b7edba8c80 | ||
|
|
2b777f5314 | ||
|
|
1199eca649 | ||
|
|
073777081e | ||
|
|
56d4c690bf | ||
|
|
efddee217a | ||
|
|
8671a45d96 | ||
|
|
5c99610e00 | ||
|
|
153b9f0ca4 | ||
|
|
95d90dcb50 | ||
|
|
c6d9edbb42 | ||
|
|
f81d158f0f | ||
|
|
c6a2fa6808 | ||
|
|
47da24a12e | ||
|
|
d4bce083ab | ||
|
|
5dddbaeda2 | ||
|
|
3bfb5c81b6 | ||
|
|
52520d770b | ||
|
|
586dd5ccc9 | ||
|
|
62dd681b83 | ||
|
|
266a51fe1d | ||
|
|
3432d5189b | ||
|
|
12e6f7357c | ||
|
|
6917a76d60 | ||
|
|
1516f2807c |
No files matched your search
@@ -26,4 +26,7 @@ build/
|
|||||||
# Media files
|
# Media files
|
||||||
*.mp3
|
*.mp3
|
||||||
*.mp4
|
*.mp4
|
||||||
media/
|
media/
|
||||||
|
|
||||||
|
|
||||||
|
*.ipynb_checkpoint*
|
||||||
@@ -1,38 +1,38 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from sqladmin import Admin
|
from sqladmin import Admin
|
||||||
|
|
||||||
from app.database.session import engine
|
from app.database.session import engine
|
||||||
from app.home.api.home_admin import ImageAdmin, ProjectAdmin
|
from app.home.api.home_admin import ImageAdmin, ProjectAdmin
|
||||||
from app.lyric.api.lyrics_admin import LyricAdmin
|
from app.lyric.api.lyrics_admin import LyricAdmin
|
||||||
from app.song.api.song_admin import SongAdmin
|
from app.song.api.song_admin import SongAdmin
|
||||||
from app.video.api.video_admin import VideoAdmin
|
from app.video.api.video_admin import VideoAdmin
|
||||||
from config import prj_settings
|
from config import prj_settings
|
||||||
|
|
||||||
# https://github.com/aminalaee/sqladmin
|
# https://github.com/aminalaee/sqladmin
|
||||||
|
|
||||||
|
|
||||||
def init_admin(
|
def init_admin(
|
||||||
app: FastAPI,
|
app: FastAPI,
|
||||||
db_engine: engine,
|
db_engine: engine,
|
||||||
base_url: str = prj_settings.ADMIN_BASE_URL,
|
base_url: str = prj_settings.ADMIN_BASE_URL,
|
||||||
) -> Admin:
|
) -> Admin:
|
||||||
admin = Admin(
|
admin = Admin(
|
||||||
app,
|
app,
|
||||||
db_engine,
|
db_engine,
|
||||||
base_url=base_url,
|
base_url=base_url,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 프로젝트 관리
|
# 프로젝트 관리
|
||||||
admin.add_view(ProjectAdmin)
|
admin.add_view(ProjectAdmin)
|
||||||
admin.add_view(ImageAdmin)
|
admin.add_view(ImageAdmin)
|
||||||
|
|
||||||
# 가사 관리
|
# 가사 관리
|
||||||
admin.add_view(LyricAdmin)
|
admin.add_view(LyricAdmin)
|
||||||
|
|
||||||
# 노래 관리
|
# 노래 관리
|
||||||
admin.add_view(SongAdmin)
|
admin.add_view(SongAdmin)
|
||||||
|
|
||||||
# 영상 관리
|
# 영상 관리
|
||||||
admin.add_view(VideoAdmin)
|
admin.add_view(VideoAdmin)
|
||||||
|
|
||||||
return admin
|
return admin
|
||||||
@@ -1,43 +1,51 @@
|
|||||||
# app/main.py
|
# app/main.py
|
||||||
import asyncio
|
import asyncio
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
"""FastAPI 애플리케이션 생명주기 관리"""
|
"""FastAPI 애플리케이션 생명주기 관리"""
|
||||||
# Startup - 애플리케이션 시작 시
|
# Startup - 애플리케이션 시작 시
|
||||||
print("Starting up...")
|
print("Starting up...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from config import prj_settings
|
from config import prj_settings
|
||||||
|
|
||||||
# DEBUG 모드일 때만 데이터베이스 테이블 자동 생성
|
# DEBUG 모드일 때만 데이터베이스 테이블 자동 생성
|
||||||
if prj_settings.DEBUG:
|
if prj_settings.DEBUG:
|
||||||
from app.database.session import create_db_tables
|
from app.database.session import create_db_tables
|
||||||
|
|
||||||
await create_db_tables()
|
await create_db_tables()
|
||||||
print("Database tables created (DEBUG mode)")
|
print("Database tables created (DEBUG mode)")
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
print("Database initialization timed out")
|
print("Database initialization timed out")
|
||||||
# 타임아웃 시 앱 시작 중단하려면 raise, 계속하려면 pass
|
# 타임아웃 시 앱 시작 중단하려면 raise, 계속하려면 pass
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Database initialization failed: {e}")
|
print(f"Database initialization failed: {e}")
|
||||||
# 에러 시 앱 시작 중단하려면 raise, 계속하려면 pass
|
# 에러 시 앱 시작 중단하려면 raise, 계속하려면 pass
|
||||||
raise
|
raise
|
||||||
|
|
||||||
yield # 애플리케이션 실행 중
|
yield # 애플리케이션 실행 중
|
||||||
|
|
||||||
# Shutdown - 애플리케이션 종료 시
|
# Shutdown - 애플리케이션 종료 시
|
||||||
print("Shutting down...")
|
print("Shutting down...")
|
||||||
from app.database.session import engine
|
|
||||||
|
# 공유 HTTP 클라이언트 종료
|
||||||
await engine.dispose()
|
from app.utils.creatomate import close_shared_client
|
||||||
print("Database engine disposed")
|
from app.utils.upload_blob_as_request import close_shared_blob_client
|
||||||
|
|
||||||
|
await close_shared_client()
|
||||||
# FastAPI 앱 생성 (lifespan 적용)
|
await close_shared_blob_client()
|
||||||
app = FastAPI(title="CastAD", lifespan=lifespan)
|
|
||||||
|
# 데이터베이스 엔진 종료
|
||||||
|
from app.database.session import dispose_engine
|
||||||
|
|
||||||
|
await dispose_engine()
|
||||||
|
|
||||||
|
|
||||||
|
# FastAPI 앱 생성 (lifespan 적용)
|
||||||
|
app = FastAPI(title="CastAD", lifespan=lifespan)
|
||||||
@@ -1,114 +1,313 @@
|
|||||||
from fastapi import FastAPI, HTTPException, Request, Response, status
|
import logging
|
||||||
from fastapi.responses import JSONResponse
|
import traceback
|
||||||
|
from functools import wraps
|
||||||
|
from typing import Any, Callable, TypeVar
|
||||||
class FastShipError(Exception):
|
|
||||||
"""Base exception for all exceptions in fastship api"""
|
from fastapi import FastAPI, HTTPException, Request, Response, status
|
||||||
# status_code to be returned for this exception
|
from fastapi.responses import JSONResponse
|
||||||
# when it is handled
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
status = status.HTTP_400_BAD_REQUEST
|
|
||||||
|
# 로거 설정
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
class EntityNotFound(FastShipError):
|
|
||||||
"""Entity not found in database"""
|
T = TypeVar("T")
|
||||||
|
|
||||||
status = status.HTTP_404_NOT_FOUND
|
|
||||||
|
class FastShipError(Exception):
|
||||||
|
"""Base exception for all exceptions in fastship api"""
|
||||||
class BadPassword(FastShipError):
|
# status_code to be returned for this exception
|
||||||
"""Password is not strong enough or invalid"""
|
# when it is handled
|
||||||
|
status = status.HTTP_400_BAD_REQUEST
|
||||||
status = status.HTTP_400_BAD_REQUEST
|
|
||||||
|
|
||||||
|
class EntityNotFound(FastShipError):
|
||||||
class ClientNotAuthorized(FastShipError):
|
"""Entity not found in database"""
|
||||||
"""Client is not authorized to perform the action"""
|
|
||||||
|
status = status.HTTP_404_NOT_FOUND
|
||||||
status = status.HTTP_401_UNAUTHORIZED
|
|
||||||
|
|
||||||
|
class BadPassword(FastShipError):
|
||||||
class ClientNotVerified(FastShipError):
|
"""Password is not strong enough or invalid"""
|
||||||
"""Client is not verified"""
|
|
||||||
|
status = status.HTTP_400_BAD_REQUEST
|
||||||
status = status.HTTP_401_UNAUTHORIZED
|
|
||||||
|
|
||||||
|
class ClientNotAuthorized(FastShipError):
|
||||||
class NothingToUpdate(FastShipError):
|
"""Client is not authorized to perform the action"""
|
||||||
"""No data provided to update"""
|
|
||||||
|
status = status.HTTP_401_UNAUTHORIZED
|
||||||
|
|
||||||
class BadCredentials(FastShipError):
|
|
||||||
"""User email or password is incorrect"""
|
class ClientNotVerified(FastShipError):
|
||||||
|
"""Client is not verified"""
|
||||||
status = status.HTTP_401_UNAUTHORIZED
|
|
||||||
|
status = status.HTTP_401_UNAUTHORIZED
|
||||||
|
|
||||||
class InvalidToken(FastShipError):
|
|
||||||
"""Access token is invalid or expired"""
|
class NothingToUpdate(FastShipError):
|
||||||
|
"""No data provided to update"""
|
||||||
status = status.HTTP_401_UNAUTHORIZED
|
|
||||||
|
|
||||||
|
class BadCredentials(FastShipError):
|
||||||
class DeliveryPartnerNotAvailable(FastShipError):
|
"""User email or password is incorrect"""
|
||||||
"""Delivery partner/s do not service the destination"""
|
|
||||||
|
status = status.HTTP_401_UNAUTHORIZED
|
||||||
status = status.HTTP_406_NOT_ACCEPTABLE
|
|
||||||
|
|
||||||
|
class InvalidToken(FastShipError):
|
||||||
class DeliveryPartnerCapacityExceeded(FastShipError):
|
"""Access token is invalid or expired"""
|
||||||
"""Delivery partner has reached their max handling capacity"""
|
|
||||||
|
status = status.HTTP_401_UNAUTHORIZED
|
||||||
status = status.HTTP_406_NOT_ACCEPTABLE
|
|
||||||
|
|
||||||
|
class DeliveryPartnerNotAvailable(FastShipError):
|
||||||
def _get_handler(status: int, detail: str):
|
"""Delivery partner/s do not service the destination"""
|
||||||
# Define
|
|
||||||
def handler(request: Request, exception: Exception) -> Response:
|
status = status.HTTP_406_NOT_ACCEPTABLE
|
||||||
# DEBUG PRINT STATEMENT 👇
|
|
||||||
from rich import print, panel
|
|
||||||
print(
|
class DeliveryPartnerCapacityExceeded(FastShipError):
|
||||||
panel.Panel(
|
"""Delivery partner has reached their max handling capacity"""
|
||||||
exception.__class__.__name__,
|
|
||||||
title="Handled Exception",
|
status = status.HTTP_406_NOT_ACCEPTABLE
|
||||||
border_style="red",
|
|
||||||
),
|
|
||||||
)
|
# =============================================================================
|
||||||
# DEBUG PRINT STATEMENT 👆
|
# 데이터베이스 관련 예외
|
||||||
|
# =============================================================================
|
||||||
# Raise HTTPException with given status and detail
|
|
||||||
# can return JSONResponse as well
|
|
||||||
raise HTTPException(
|
class DatabaseError(FastShipError):
|
||||||
status_code=status,
|
"""Database operation failed"""
|
||||||
detail=detail,
|
|
||||||
)
|
status = status.HTTP_503_SERVICE_UNAVAILABLE
|
||||||
# Return ExceptionHandler required with given
|
|
||||||
# status and detail for HTTPExcetion above
|
|
||||||
return handler
|
class DatabaseConnectionError(DatabaseError):
|
||||||
|
"""Database connection failed"""
|
||||||
|
|
||||||
def add_exception_handlers(app: FastAPI):
|
status = status.HTTP_503_SERVICE_UNAVAILABLE
|
||||||
# Get all subclass of 👇, our custom exceptions
|
|
||||||
exception_classes = FastShipError.__subclasses__()
|
|
||||||
|
class DatabaseTimeoutError(DatabaseError):
|
||||||
for exception_class in exception_classes:
|
"""Database operation timed out"""
|
||||||
# Add exception handler
|
|
||||||
app.add_exception_handler(
|
status = status.HTTP_504_GATEWAY_TIMEOUT
|
||||||
# Custom exception class
|
|
||||||
exception_class,
|
|
||||||
# Get handler function
|
# =============================================================================
|
||||||
_get_handler(
|
# 외부 서비스 관련 예외
|
||||||
status=exception_class.status,
|
# =============================================================================
|
||||||
detail=exception_class.__doc__,
|
|
||||||
),
|
|
||||||
)
|
class ExternalServiceError(FastShipError):
|
||||||
|
"""External service call failed"""
|
||||||
@app.exception_handler(status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
||||||
def internal_server_error_handler(request, exception):
|
status = status.HTTP_502_BAD_GATEWAY
|
||||||
return JSONResponse(
|
|
||||||
content={"detail": "Something went wrong..."},
|
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
class GPTServiceError(ExternalServiceError):
|
||||||
headers={
|
"""GPT API call failed"""
|
||||||
"X-Error": f"{exception}",
|
|
||||||
}
|
status = status.HTTP_502_BAD_GATEWAY
|
||||||
)
|
|
||||||
|
|
||||||
|
class CrawlingError(ExternalServiceError):
|
||||||
|
"""Web crawling failed"""
|
||||||
|
|
||||||
|
status = status.HTTP_502_BAD_GATEWAY
|
||||||
|
|
||||||
|
|
||||||
|
class BlobStorageError(ExternalServiceError):
|
||||||
|
"""Azure Blob Storage operation failed"""
|
||||||
|
|
||||||
|
status = status.HTTP_502_BAD_GATEWAY
|
||||||
|
|
||||||
|
|
||||||
|
class CreatomateError(ExternalServiceError):
|
||||||
|
"""Creatomate API call failed"""
|
||||||
|
|
||||||
|
status = status.HTTP_502_BAD_GATEWAY
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# 예외 처리 데코레이터
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
|
||||||
|
def handle_db_exceptions(
|
||||||
|
error_message: str = "데이터베이스 작업 중 오류가 발생했습니다.",
|
||||||
|
):
|
||||||
|
"""데이터베이스 예외를 처리하는 데코레이터.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
error_message: 오류 발생 시 반환할 메시지
|
||||||
|
|
||||||
|
Example:
|
||||||
|
@handle_db_exceptions("사용자 조회 중 오류 발생")
|
||||||
|
async def get_user(user_id: int):
|
||||||
|
...
|
||||||
|
"""
|
||||||
|
|
||||||
|
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
|
||||||
|
@wraps(func)
|
||||||
|
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||||
|
try:
|
||||||
|
return await func(*args, **kwargs)
|
||||||
|
except HTTPException:
|
||||||
|
# HTTPException은 그대로 raise
|
||||||
|
raise
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
logger.error(f"[DB Error] {func.__name__}: {e}")
|
||||||
|
logger.error(traceback.format_exc())
|
||||||
|
print(f"[DB Error] {func.__name__}: {e}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||||
|
detail=error_message,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[Unexpected Error] {func.__name__}: {e}")
|
||||||
|
logger.error(traceback.format_exc())
|
||||||
|
print(f"[Unexpected Error] {func.__name__}: {e}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail="서비스 처리 중 예기치 않은 오류가 발생했습니다.",
|
||||||
|
)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
def handle_external_service_exceptions(
|
||||||
|
service_name: str = "외부 서비스",
|
||||||
|
error_message: str | None = None,
|
||||||
|
):
|
||||||
|
"""외부 서비스 호출 예외를 처리하는 데코레이터.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
service_name: 서비스 이름 (로그용)
|
||||||
|
error_message: 오류 발생 시 반환할 메시지
|
||||||
|
|
||||||
|
Example:
|
||||||
|
@handle_external_service_exceptions("GPT")
|
||||||
|
async def call_gpt():
|
||||||
|
...
|
||||||
|
"""
|
||||||
|
|
||||||
|
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
|
||||||
|
@wraps(func)
|
||||||
|
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||||
|
try:
|
||||||
|
return await func(*args, **kwargs)
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
msg = error_message or f"{service_name} 서비스 호출 중 오류가 발생했습니다."
|
||||||
|
logger.error(f"[{service_name} Error] {func.__name__}: {e}")
|
||||||
|
logger.error(traceback.format_exc())
|
||||||
|
print(f"[{service_name} Error] {func.__name__}: {e}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_502_BAD_GATEWAY,
|
||||||
|
detail=msg,
|
||||||
|
)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
def handle_api_exceptions(
|
||||||
|
error_message: str = "요청 처리 중 오류가 발생했습니다.",
|
||||||
|
):
|
||||||
|
"""API 엔드포인트 예외를 처리하는 데코레이터.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
error_message: 오류 발생 시 반환할 메시지
|
||||||
|
|
||||||
|
Example:
|
||||||
|
@handle_api_exceptions("가사 생성 중 오류 발생")
|
||||||
|
async def generate_lyric():
|
||||||
|
...
|
||||||
|
"""
|
||||||
|
|
||||||
|
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
|
||||||
|
@wraps(func)
|
||||||
|
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||||
|
try:
|
||||||
|
return await func(*args, **kwargs)
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
logger.error(f"[API DB Error] {func.__name__}: {e}")
|
||||||
|
logger.error(traceback.format_exc())
|
||||||
|
print(f"[API DB Error] {func.__name__}: {e}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||||
|
detail="데이터베이스 연결에 문제가 발생했습니다.",
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[API Error] {func.__name__}: {e}")
|
||||||
|
logger.error(traceback.format_exc())
|
||||||
|
print(f"[API Error] {func.__name__}: {e}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=error_message,
|
||||||
|
)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
def _get_handler(status: int, detail: str):
|
||||||
|
# Define
|
||||||
|
def handler(request: Request, exception: Exception) -> Response:
|
||||||
|
# DEBUG PRINT STATEMENT 👇
|
||||||
|
from rich import print, panel
|
||||||
|
print(
|
||||||
|
panel.Panel(
|
||||||
|
exception.__class__.__name__,
|
||||||
|
title="Handled Exception",
|
||||||
|
border_style="red",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
# DEBUG PRINT STATEMENT 👆
|
||||||
|
|
||||||
|
# Raise HTTPException with given status and detail
|
||||||
|
# can return JSONResponse as well
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status,
|
||||||
|
detail=detail,
|
||||||
|
)
|
||||||
|
# Return ExceptionHandler required with given
|
||||||
|
# status and detail for HTTPExcetion above
|
||||||
|
return handler
|
||||||
|
|
||||||
|
|
||||||
|
def add_exception_handlers(app: FastAPI):
|
||||||
|
# Get all subclass of 👇, our custom exceptions
|
||||||
|
exception_classes = FastShipError.__subclasses__()
|
||||||
|
|
||||||
|
for exception_class in exception_classes:
|
||||||
|
# Add exception handler
|
||||||
|
app.add_exception_handler(
|
||||||
|
# Custom exception class
|
||||||
|
exception_class,
|
||||||
|
# Get handler function
|
||||||
|
_get_handler(
|
||||||
|
status=exception_class.status,
|
||||||
|
detail=exception_class.__doc__,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.exception_handler(status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
def internal_server_error_handler(request, exception):
|
||||||
|
return JSONResponse(
|
||||||
|
content={"detail": "Something went wrong..."},
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
headers={
|
||||||
|
"X-Error": f"{exception}",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
@@ -1,30 +1,30 @@
|
|||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from redis.asyncio import Redis
|
from redis.asyncio import Redis
|
||||||
|
|
||||||
from app.config import db_settings
|
from app.config import db_settings
|
||||||
|
|
||||||
|
|
||||||
_token_blacklist = Redis(
|
_token_blacklist = Redis(
|
||||||
host=db_settings.REDIS_HOST,
|
host=db_settings.REDIS_HOST,
|
||||||
port=db_settings.REDIS_PORT,
|
port=db_settings.REDIS_PORT,
|
||||||
db=0,
|
db=0,
|
||||||
)
|
)
|
||||||
_shipment_verification_codes = Redis(
|
_shipment_verification_codes = Redis(
|
||||||
host=db_settings.REDIS_HOST,
|
host=db_settings.REDIS_HOST,
|
||||||
port=db_settings.REDIS_PORT,
|
port=db_settings.REDIS_PORT,
|
||||||
db=1,
|
db=1,
|
||||||
decode_responses=True,
|
decode_responses=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def add_jti_to_blacklist(jti: str):
|
async def add_jti_to_blacklist(jti: str):
|
||||||
await _token_blacklist.set(jti, "blacklisted")
|
await _token_blacklist.set(jti, "blacklisted")
|
||||||
|
|
||||||
|
|
||||||
async def is_jti_blacklisted(jti: str) -> bool:
|
async def is_jti_blacklisted(jti: str) -> bool:
|
||||||
return await _token_blacklist.exists(jti)
|
return await _token_blacklist.exists(jti)
|
||||||
|
|
||||||
async def add_shipment_verification_code(id: UUID, code: int):
|
async def add_shipment_verification_code(id: UUID, code: int):
|
||||||
await _shipment_verification_codes.set(str(id), code)
|
await _shipment_verification_codes.set(str(id), code)
|
||||||
|
|
||||||
async def get_shipment_verification_code(id: UUID) -> str:
|
async def get_shipment_verification_code(id: UUID) -> str:
|
||||||
return str(await _shipment_verification_codes.get(str(id)))
|
return str(await _shipment_verification_codes.get(str(id)))
|
||||||
@@ -1,97 +1,97 @@
|
|||||||
from asyncio import current_task
|
from asyncio import current_task
|
||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator
|
||||||
|
|
||||||
from sqlalchemy.ext.asyncio import (
|
from sqlalchemy.ext.asyncio import (
|
||||||
AsyncSession,
|
AsyncSession,
|
||||||
async_sessionmaker,
|
async_sessionmaker,
|
||||||
create_async_engine,
|
create_async_engine,
|
||||||
)
|
)
|
||||||
from sqlalchemy.orm import DeclarativeBase
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
from sqlalchemy.pool import AsyncQueuePool # 비동기 풀 클래스
|
from sqlalchemy.pool import AsyncQueuePool # 비동기 풀 클래스
|
||||||
|
|
||||||
from config import db_settings
|
from config import db_settings
|
||||||
|
|
||||||
|
|
||||||
# Base 클래스 정의
|
# Base 클래스 정의
|
||||||
class Base(DeclarativeBase):
|
class Base(DeclarativeBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
engine = create_async_engine(
|
engine = create_async_engine(
|
||||||
# MySQL async URL (asyncmy 드라이버)
|
# MySQL async URL (asyncmy 드라이버)
|
||||||
url=db_settings.MYSQL_URL, # 예: "mysql+asyncmy://test:test@host:3306/poc"
|
url=db_settings.MYSQL_URL, # 예: "mysql+asyncmy://test:test@host:3306/poc"
|
||||||
# === Connection Pool 설정 ===
|
# === Connection Pool 설정 ===
|
||||||
pool_size=10, # 기본 풀 크기: 10개 연결 유지
|
pool_size=10, # 기본 풀 크기: 10개 연결 유지
|
||||||
max_overflow=10, # 최대 증가: 10개 (총 20개까지 가능)
|
max_overflow=10, # 최대 증가: 10개 (총 20개까지 가능)
|
||||||
poolclass=AsyncQueuePool, # 비동기 큐 풀 사용 (기본값, 명시적 지정)
|
poolclass=AsyncQueuePool, # 비동기 큐 풀 사용 (기본값, 명시적 지정)
|
||||||
pool_timeout=30, # 풀에서 연결 대기 시간: 30초 (기본 30초)
|
pool_timeout=30, # 풀에서 연결 대기 시간: 30초 (기본 30초)
|
||||||
pool_recycle=3600, # 연결 재사용 주기: 1시간 (기본 3600초)
|
pool_recycle=3600, # 연결 재사용 주기: 1시간 (기본 3600초)
|
||||||
pool_pre_ping=True, # 연결 사용 전 유효성 검사: True로 설정
|
pool_pre_ping=True, # 연결 사용 전 유효성 검사: True로 설정
|
||||||
pool_reset_on_return="rollback", # 연결 반환 시 자동 롤백
|
pool_reset_on_return="rollback", # 연결 반환 시 자동 롤백
|
||||||
# === MySQL 특화 설정 ===
|
# === MySQL 특화 설정 ===
|
||||||
echo=False, # SQL 쿼리 로깅 (디버깅 시 True)
|
echo=False, # SQL 쿼리 로깅 (디버깅 시 True)
|
||||||
# === 연결 타임아웃 및 재시도 ===
|
# === 연결 타임아웃 및 재시도 ===
|
||||||
connect_args={
|
connect_args={
|
||||||
"connect_timeout": 10, # MySQL 연결 타임아웃: 10초
|
"connect_timeout": 10, # MySQL 연결 타임아웃: 10초
|
||||||
"read_timeout": 30, # 읽기 타임아웃: 30초
|
"read_timeout": 30, # 읽기 타임아웃: 30초
|
||||||
"write_timeout": 30, # 쓰기 타임아웃: 30초
|
"write_timeout": 30, # 쓰기 타임아웃: 30초
|
||||||
"charset": "utf8mb4", # 문자셋 (이모지 지원)
|
"charset": "utf8mb4", # 문자셋 (이모지 지원)
|
||||||
"sql_mode": "STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE",
|
"sql_mode": "STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE",
|
||||||
"init_command": "SET SESSION time_zone = '+00:00'", # 초기 연결 시 실행
|
"init_command": "SET SESSION time_zone = '+00:00'", # 초기 연결 시 실행
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# Async 세션 팩토리 생성
|
# Async 세션 팩토리 생성
|
||||||
async_session_factory = async_sessionmaker(
|
async_session_factory = async_sessionmaker(
|
||||||
bind=engine,
|
bind=engine,
|
||||||
class_=AsyncSession,
|
class_=AsyncSession,
|
||||||
expire_on_commit=False, # 커밋 후 객체 상태 유지
|
expire_on_commit=False, # 커밋 후 객체 상태 유지
|
||||||
autoflush=True, # 변경 감지 자동 플러시
|
autoflush=True, # 변경 감지 자동 플러시
|
||||||
)
|
)
|
||||||
|
|
||||||
# async_scoped_session 생성
|
# async_scoped_session 생성
|
||||||
AsyncScopedSession = async_session_factory(
|
AsyncScopedSession = async_session_factory(
|
||||||
async_session_factory,
|
async_session_factory,
|
||||||
scopefunc=current_task,
|
scopefunc=current_task,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# 테이블 생성 함수
|
# 테이블 생성 함수
|
||||||
async def create_db_tables() -> None:
|
async def create_db_tables() -> None:
|
||||||
async with engine.begin() as conn:
|
async with engine.begin() as conn:
|
||||||
# from app.database.models import Shipment, Seller # noqa: F401
|
# from app.database.models import Shipment, Seller # noqa: F401
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
print("MySQL tables created successfully")
|
print("MySQL tables created successfully")
|
||||||
|
|
||||||
|
|
||||||
# 세션 제너레이터 (FastAPI Depends에 사용)
|
# 세션 제너레이터 (FastAPI Depends에 사용)
|
||||||
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
||||||
"""
|
"""
|
||||||
Async 세션 컨텍스트 매니저
|
Async 세션 컨텍스트 매니저
|
||||||
- FastAPI dependency로 사용
|
- FastAPI dependency로 사용
|
||||||
- Connection Pool에서 연결 획득/반환 자동 관리
|
- Connection Pool에서 연결 획득/반환 자동 관리
|
||||||
"""
|
"""
|
||||||
async with async_session_factory() as session:
|
async with async_session_factory() as session:
|
||||||
# pre-commit 훅 (선택적: 트랜잭션 시작 전 실행)
|
# pre-commit 훅 (선택적: 트랜잭션 시작 전 실행)
|
||||||
# await session.begin() # async_sessionmaker에서 자동 begin
|
# await session.begin() # async_sessionmaker에서 자동 begin
|
||||||
|
|
||||||
try:
|
try:
|
||||||
yield session
|
yield session
|
||||||
# FastAPI 요청 완료 시 자동 commit (예외 발생 시 rollback)
|
# FastAPI 요청 완료 시 자동 commit (예외 발생 시 rollback)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await session.rollback() # 명시적 롤백 (선택적)
|
await session.rollback() # 명시적 롤백 (선택적)
|
||||||
print(f"Session rollback due to: {e}") # 로깅
|
print(f"Session rollback due to: {e}") # 로깅
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
# 명시적 세션 종료 (Connection Pool에 반환)
|
# 명시적 세션 종료 (Connection Pool에 반환)
|
||||||
# context manager가 자동 처리하지만, 명시적으로 유지
|
# context manager가 자동 처리하지만, 명시적으로 유지
|
||||||
await session.close()
|
await session.close()
|
||||||
print("session closed successfully")
|
print("session closed successfully")
|
||||||
# 또는 session.aclose() - Python 3.10+
|
# 또는 session.aclose() - Python 3.10+
|
||||||
|
|
||||||
|
|
||||||
# 애플리케이션 종료 시 엔진 정리 (선택적)
|
# 애플리케이션 종료 시 엔진 정리 (선택적)
|
||||||
async def dispose_engine() -> None:
|
async def dispose_engine() -> None:
|
||||||
"""애플리케이션 종료 시 모든 연결 해제"""
|
"""애플리케이션 종료 시 모든 연결 해제"""
|
||||||
await engine.dispose()
|
await engine.dispose()
|
||||||
print("Database engine disposed")
|
print("Database engine disposed")
|
||||||
@@ -1,127 +1,161 @@
|
|||||||
from contextlib import asynccontextmanager
|
import time
|
||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator
|
||||||
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||||
from sqlalchemy.orm import DeclarativeBase
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
from sqlalchemy.pool import NullPool
|
|
||||||
|
from config import db_settings
|
||||||
from config import db_settings
|
|
||||||
|
|
||||||
|
class Base(DeclarativeBase):
|
||||||
class Base(DeclarativeBase):
|
pass
|
||||||
pass
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
# 데이터베이스 엔진 생성
|
# 메인 엔진 (FastAPI 요청용)
|
||||||
engine = create_async_engine(
|
# =============================================================================
|
||||||
url=db_settings.MYSQL_URL,
|
engine = create_async_engine(
|
||||||
echo=False,
|
url=db_settings.MYSQL_URL,
|
||||||
pool_size=10,
|
echo=False,
|
||||||
max_overflow=10,
|
pool_size=20, # 기본 풀 크기: 20
|
||||||
pool_timeout=5,
|
max_overflow=20, # 추가 연결: 20 (총 최대 40)
|
||||||
pool_recycle=3600,
|
pool_timeout=30, # 풀에서 연결 대기 시간 (초)
|
||||||
pool_pre_ping=True,
|
pool_recycle=280, # MySQL wait_timeout(기본 28800s, 클라우드는 보통 300s) 보다 짧게 설정
|
||||||
pool_reset_on_return="rollback",
|
pool_pre_ping=True, # 연결 유효성 검사 (죽은 연결 자동 재연결)
|
||||||
connect_args={
|
pool_reset_on_return="rollback", # 반환 시 롤백으로 초기화
|
||||||
"connect_timeout": 3,
|
connect_args={
|
||||||
"charset": "utf8mb4",
|
"connect_timeout": 10, # DB 연결 타임아웃
|
||||||
# "allow_public_key_retrieval": True,
|
"charset": "utf8mb4",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# Async sessionmaker 생성
|
# 메인 세션 팩토리 (FastAPI DI용)
|
||||||
AsyncSessionLocal = async_sessionmaker(
|
AsyncSessionLocal = async_sessionmaker(
|
||||||
bind=engine,
|
bind=engine,
|
||||||
class_=AsyncSession,
|
class_=AsyncSession,
|
||||||
expire_on_commit=False,
|
expire_on_commit=False,
|
||||||
autoflush=False, # 명시적 flush 권장
|
autoflush=False, # 명시적 flush 권장
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def create_db_tables():
|
# =============================================================================
|
||||||
import asyncio
|
# 백그라운드 태스크 전용 엔진 (메인 풀과 분리)
|
||||||
|
# =============================================================================
|
||||||
# 모델 import (테이블 메타데이터 등록용)
|
background_engine = create_async_engine(
|
||||||
from app.home.models import Image, Project # noqa: F401
|
url=db_settings.MYSQL_URL,
|
||||||
from app.lyric.models import Lyric # noqa: F401
|
echo=False,
|
||||||
from app.song.models import Song # noqa: F401
|
pool_size=10, # 백그라운드용 풀 크기: 10
|
||||||
from app.video.models import Video # noqa: F401
|
max_overflow=10, # 추가 연결: 10 (총 최대 20)
|
||||||
|
pool_timeout=60, # 백그라운드는 대기 시간 여유있게
|
||||||
print("Creating database tables...")
|
pool_recycle=280, # MySQL wait_timeout 보다 짧게 설정
|
||||||
|
pool_pre_ping=True, # 연결 유효성 검사 (죽은 연결 자동 재연결)
|
||||||
async with asyncio.timeout(10):
|
pool_reset_on_return="rollback",
|
||||||
async with engine.begin() as connection:
|
connect_args={
|
||||||
await connection.run_sync(Base.metadata.create_all)
|
"connect_timeout": 10,
|
||||||
|
"charset": "utf8mb4",
|
||||||
|
},
|
||||||
# FastAPI 의존성용 세션 제너레이터
|
)
|
||||||
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
|
||||||
async with AsyncSessionLocal() as session:
|
# 백그라운드 세션 팩토리
|
||||||
try:
|
BackgroundSessionLocal = async_sessionmaker(
|
||||||
yield session
|
bind=background_engine,
|
||||||
# print("Session commited")
|
class_=AsyncSession,
|
||||||
# await session.commit()
|
expire_on_commit=False,
|
||||||
except Exception as e:
|
autoflush=False,
|
||||||
await session.rollback()
|
)
|
||||||
print(f"Session rollback due to: {e}")
|
|
||||||
raise e
|
|
||||||
# async with 종료 시 session.close()가 자동 호출됨
|
async def create_db_tables():
|
||||||
|
import asyncio
|
||||||
|
|
||||||
# 앱 종료 시 엔진 리소스 정리 함수
|
# 모델 import (테이블 메타데이터 등록용)
|
||||||
async def dispose_engine() -> None:
|
from app.home.models import Image, Project # noqa: F401
|
||||||
await engine.dispose()
|
from app.lyric.models import Lyric # noqa: F401
|
||||||
print("Database engine disposed")
|
from app.song.models import Song # noqa: F401
|
||||||
|
from app.video.models import Video # noqa: F401
|
||||||
|
|
||||||
# =============================================================================
|
print("Creating database tables...")
|
||||||
# 백그라운드 태스크용 세션 (별도 이벤트 루프에서 사용)
|
|
||||||
# =============================================================================
|
async with asyncio.timeout(10):
|
||||||
|
async with engine.begin() as connection:
|
||||||
|
await connection.run_sync(Base.metadata.create_all)
|
||||||
@asynccontextmanager
|
|
||||||
async def get_worker_session() -> AsyncGenerator[AsyncSession, None]:
|
|
||||||
"""백그라운드 태스크용 세션 컨텍스트 매니저
|
# FastAPI 의존성용 세션 제너레이터
|
||||||
|
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
||||||
asyncio.run()으로 새 이벤트 루프를 생성하는 백그라운드 태스크에서 사용합니다.
|
start_time = time.perf_counter()
|
||||||
NullPool을 사용하여 연결 풀링을 비활성화하고, 이벤트 루프 충돌을 방지합니다.
|
pool = engine.pool
|
||||||
|
|
||||||
get_session()과의 차이점:
|
# 커넥션 풀 상태 로깅 (디버깅용)
|
||||||
- get_session(): FastAPI DI용, 메인 이벤트 루프의 연결 풀 사용
|
print(
|
||||||
- get_worker_session(): 백그라운드 태스크용, NullPool로 매번 새 연결 생성
|
f"[get_session] ACQUIRE - pool_size: {pool.size()}, "
|
||||||
|
f"in: {pool.checkedin()}, out: {pool.checkedout()}, "
|
||||||
Usage:
|
f"overflow: {pool.overflow()}"
|
||||||
async with get_worker_session() as session:
|
)
|
||||||
result = await session.execute(select(Model))
|
|
||||||
await session.commit()
|
async with AsyncSessionLocal() as session:
|
||||||
|
acquire_time = time.perf_counter()
|
||||||
Note:
|
print(
|
||||||
- 매 호출마다 엔진을 생성하고 dispose하므로 오버헤드가 있음
|
f"[get_session] Session acquired in "
|
||||||
- 빈번한 호출이 필요한 경우 방법 1(모듈 레벨 엔진)을 고려
|
f"{(acquire_time - start_time)*1000:.1f}ms"
|
||||||
"""
|
)
|
||||||
worker_engine = create_async_engine(
|
try:
|
||||||
url=db_settings.MYSQL_URL,
|
yield session
|
||||||
poolclass=NullPool,
|
except Exception as e:
|
||||||
connect_args={
|
await session.rollback()
|
||||||
"connect_timeout": 3,
|
print(
|
||||||
"charset": "utf8mb4",
|
f"[get_session] ROLLBACK - error: {type(e).__name__}: {e}, "
|
||||||
},
|
f"duration: {(time.perf_counter() - start_time)*1000:.1f}ms"
|
||||||
)
|
)
|
||||||
session_factory = async_sessionmaker(
|
raise e
|
||||||
bind=worker_engine,
|
finally:
|
||||||
class_=AsyncSession,
|
total_time = time.perf_counter() - start_time
|
||||||
expire_on_commit=False,
|
print(
|
||||||
autoflush=False,
|
f"[get_session] RELEASE - duration: {total_time*1000:.1f}ms, "
|
||||||
)
|
f"pool_out: {pool.checkedout()}"
|
||||||
|
)
|
||||||
async with session_factory() as session:
|
|
||||||
try:
|
|
||||||
yield session
|
# 백그라운드 태스크용 세션 제너레이터
|
||||||
except Exception as e:
|
async def get_background_session() -> AsyncGenerator[AsyncSession, None]:
|
||||||
await session.rollback()
|
start_time = time.perf_counter()
|
||||||
print(f"Worker session rollback due to: {e}")
|
pool = background_engine.pool
|
||||||
raise e
|
|
||||||
finally:
|
print(
|
||||||
await session.close()
|
f"[get_background_session] ACQUIRE - pool_size: {pool.size()}, "
|
||||||
|
f"in: {pool.checkedin()}, out: {pool.checkedout()}, "
|
||||||
await worker_engine.dispose()
|
f"overflow: {pool.overflow()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
async with BackgroundSessionLocal() as session:
|
||||||
|
acquire_time = time.perf_counter()
|
||||||
|
print(
|
||||||
|
f"[get_background_session] Session acquired in "
|
||||||
|
f"{(acquire_time - start_time)*1000:.1f}ms"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
yield session
|
||||||
|
except Exception as e:
|
||||||
|
await session.rollback()
|
||||||
|
print(
|
||||||
|
f"[get_background_session] ROLLBACK - "
|
||||||
|
f"error: {type(e).__name__}: {e}, "
|
||||||
|
f"duration: {(time.perf_counter() - start_time)*1000:.1f}ms"
|
||||||
|
)
|
||||||
|
raise e
|
||||||
|
finally:
|
||||||
|
total_time = time.perf_counter() - start_time
|
||||||
|
print(
|
||||||
|
f"[get_background_session] RELEASE - "
|
||||||
|
f"duration: {total_time*1000:.1f}ms, "
|
||||||
|
f"pool_out: {pool.checkedout()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# 앱 종료 시 엔진 리소스 정리 함수
|
||||||
|
async def dispose_engine() -> None:
|
||||||
|
print("[dispose_engine] Disposing database engines...")
|
||||||
|
await engine.dispose()
|
||||||
|
print("[dispose_engine] Main engine disposed")
|
||||||
|
await background_engine.dispose()
|
||||||
|
print("[dispose_engine] Background engine disposed - ALL DONE")
|
||||||
@@ -1,102 +1,102 @@
|
|||||||
from sqladmin import ModelView
|
from sqladmin import ModelView
|
||||||
|
|
||||||
from app.home.models import Image, Project
|
from app.home.models import Image, Project
|
||||||
|
|
||||||
|
|
||||||
class ProjectAdmin(ModelView, model=Project):
|
class ProjectAdmin(ModelView, model=Project):
|
||||||
name = "프로젝트"
|
name = "프로젝트"
|
||||||
name_plural = "프로젝트 목록"
|
name_plural = "프로젝트 목록"
|
||||||
icon = "fa-solid fa-folder"
|
icon = "fa-solid fa-folder"
|
||||||
category = "프로젝트 관리"
|
category = "프로젝트 관리"
|
||||||
page_size = 20
|
page_size = 20
|
||||||
|
|
||||||
column_list = [
|
column_list = [
|
||||||
"id",
|
"id",
|
||||||
"store_name",
|
"store_name",
|
||||||
"region",
|
"region",
|
||||||
"task_id",
|
"task_id",
|
||||||
"created_at",
|
"created_at",
|
||||||
]
|
]
|
||||||
|
|
||||||
column_details_list = [
|
column_details_list = [
|
||||||
"id",
|
"id",
|
||||||
"store_name",
|
"store_name",
|
||||||
"region",
|
"region",
|
||||||
"task_id",
|
"task_id",
|
||||||
"detail_region_info",
|
"detail_region_info",
|
||||||
"created_at",
|
"created_at",
|
||||||
]
|
]
|
||||||
|
|
||||||
# 폼(생성/수정)에서 제외
|
# 폼(생성/수정)에서 제외
|
||||||
form_excluded_columns = ["created_at", "lyrics", "songs", "videos"]
|
form_excluded_columns = ["created_at", "lyrics", "songs", "videos"]
|
||||||
|
|
||||||
column_searchable_list = [
|
column_searchable_list = [
|
||||||
Project.store_name,
|
Project.store_name,
|
||||||
Project.region,
|
Project.region,
|
||||||
Project.task_id,
|
Project.task_id,
|
||||||
]
|
]
|
||||||
|
|
||||||
column_default_sort = (Project.created_at, True) # True: DESC (최신순)
|
column_default_sort = (Project.created_at, True) # True: DESC (최신순)
|
||||||
|
|
||||||
column_sortable_list = [
|
column_sortable_list = [
|
||||||
Project.id,
|
Project.id,
|
||||||
Project.store_name,
|
Project.store_name,
|
||||||
Project.region,
|
Project.region,
|
||||||
Project.created_at,
|
Project.created_at,
|
||||||
]
|
]
|
||||||
|
|
||||||
column_labels = {
|
column_labels = {
|
||||||
"id": "ID",
|
"id": "ID",
|
||||||
"store_name": "가게명",
|
"store_name": "가게명",
|
||||||
"region": "지역",
|
"region": "지역",
|
||||||
"task_id": "작업 ID",
|
"task_id": "작업 ID",
|
||||||
"detail_region_info": "상세 지역 정보",
|
"detail_region_info": "상세 지역 정보",
|
||||||
"created_at": "생성일시",
|
"created_at": "생성일시",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class ImageAdmin(ModelView, model=Image):
|
class ImageAdmin(ModelView, model=Image):
|
||||||
name = "이미지"
|
name = "이미지"
|
||||||
name_plural = "이미지 목록"
|
name_plural = "이미지 목록"
|
||||||
icon = "fa-solid fa-image"
|
icon = "fa-solid fa-image"
|
||||||
category = "프로젝트 관리"
|
category = "프로젝트 관리"
|
||||||
page_size = 20
|
page_size = 20
|
||||||
|
|
||||||
column_list = [
|
column_list = [
|
||||||
"id",
|
"id",
|
||||||
"task_id",
|
"task_id",
|
||||||
"img_name",
|
"img_name",
|
||||||
"created_at",
|
"created_at",
|
||||||
]
|
]
|
||||||
|
|
||||||
column_details_list = [
|
column_details_list = [
|
||||||
"id",
|
"id",
|
||||||
"task_id",
|
"task_id",
|
||||||
"img_name",
|
"img_name",
|
||||||
"img_url",
|
"img_url",
|
||||||
"created_at",
|
"created_at",
|
||||||
]
|
]
|
||||||
|
|
||||||
# 폼(생성/수정)에서 제외
|
# 폼(생성/수정)에서 제외
|
||||||
form_excluded_columns = ["created_at"]
|
form_excluded_columns = ["created_at"]
|
||||||
|
|
||||||
column_searchable_list = [
|
column_searchable_list = [
|
||||||
Image.task_id,
|
Image.task_id,
|
||||||
Image.img_name,
|
Image.img_name,
|
||||||
]
|
]
|
||||||
|
|
||||||
column_default_sort = (Image.created_at, True) # True: DESC (최신순)
|
column_default_sort = (Image.created_at, True) # True: DESC (최신순)
|
||||||
|
|
||||||
column_sortable_list = [
|
column_sortable_list = [
|
||||||
Image.id,
|
Image.id,
|
||||||
Image.img_name,
|
Image.img_name,
|
||||||
Image.created_at,
|
Image.created_at,
|
||||||
]
|
]
|
||||||
|
|
||||||
column_labels = {
|
column_labels = {
|
||||||
"id": "ID",
|
"id": "ID",
|
||||||
"task_id": "작업 ID",
|
"task_id": "작업 ID",
|
||||||
"img_name": "이미지명",
|
"img_name": "이미지명",
|
||||||
"img_url": "이미지 URL",
|
"img_url": "이미지 URL",
|
||||||
"created_at": "생성일시",
|
"created_at": "생성일시",
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
"""API 1 Version Router Module."""
|
"""API 1 Version Router Module."""
|
||||||
|
|
||||||
# from fastapi import APIRouter, Depends
|
# from fastapi import APIRouter, Depends
|
||||||
|
|
||||||
# API 버전 1 라우터를 정의합니다.
|
# API 버전 1 라우터를 정의합니다.
|
||||||
# router = APIRouter(
|
# router = APIRouter(
|
||||||
# prefix="/api/v1",
|
# prefix="/api/v1",
|
||||||
# dependencies=[Depends(check_use_api), Depends(set_current_connect)],
|
# dependencies=[Depends(check_use_api), Depends(set_current_connect)],
|
||||||
# )
|
# )
|
||||||
# router = APIRouter(
|
# router = APIRouter(
|
||||||
# prefix="/api/v1",
|
# prefix="/api/v1",
|
||||||
# dependencies=[Depends(check_use_api), Depends(set_current_connect)],
|
# dependencies=[Depends(check_use_api), Depends(set_current_connect)],
|
||||||
# )
|
# )
|
||||||
# router.include_router(auth.router, tags=[Tags.AUTH])
|
# router.include_router(auth.router, tags=[Tags.AUTH])
|
||||||
# router.include_router(board.router, prefix="/boards", tags=[Tags.BOARD])
|
# router.include_router(board.router, prefix="/boards", tags=[Tags.BOARD])
|
||||||
@@ -1,215 +1,215 @@
|
|||||||
"""
|
"""
|
||||||
Home 모듈 SQLAlchemy 모델 정의
|
Home 모듈 SQLAlchemy 모델 정의
|
||||||
|
|
||||||
이 모듈은 영상 제작 파이프라인의 핵심 데이터 모델을 정의합니다.
|
이 모듈은 영상 제작 파이프라인의 핵심 데이터 모델을 정의합니다.
|
||||||
- Project: 프로젝트(사용자 입력 이력) 관리
|
- Project: 프로젝트(사용자 입력 이력) 관리
|
||||||
- Image: 업로드된 이미지 URL 관리
|
- Image: 업로드된 이미지 URL 관리
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import TYPE_CHECKING, List, Optional
|
from typing import TYPE_CHECKING, List, Optional
|
||||||
|
|
||||||
from sqlalchemy import DateTime, Index, Integer, String, Text, func
|
from sqlalchemy import DateTime, Index, Integer, String, Text, func
|
||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
from app.database.session import Base
|
from app.database.session import Base
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from app.lyric.models import Lyric
|
from app.lyric.models import Lyric
|
||||||
from app.song.models import Song
|
from app.song.models import Song
|
||||||
from app.video.models import Video
|
from app.video.models import Video
|
||||||
|
|
||||||
|
|
||||||
class Project(Base):
|
class Project(Base):
|
||||||
"""
|
"""
|
||||||
프로젝트 테이블 (사용자 입력 이력)
|
프로젝트 테이블 (사용자 입력 이력)
|
||||||
|
|
||||||
영상 제작 요청의 시작점으로, 고객 정보와 지역 정보를 저장합니다.
|
영상 제작 요청의 시작점으로, 고객 정보와 지역 정보를 저장합니다.
|
||||||
하위 테이블(Lyric, Song, Video)의 부모 테이블 역할을 합니다.
|
하위 테이블(Lyric, Song, Video)의 부모 테이블 역할을 합니다.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
id: 고유 식별자 (자동 증가)
|
id: 고유 식별자 (자동 증가)
|
||||||
store_name: 고객명 (필수)
|
store_name: 고객명 (필수)
|
||||||
region: 지역명 (필수, 예: 서울, 부산, 대구 등)
|
region: 지역명 (필수, 예: 서울, 부산, 대구 등)
|
||||||
task_id: 작업 고유 식별자 (UUID 형식, 36자)
|
task_id: 작업 고유 식별자 (UUID 형식, 36자)
|
||||||
detail_region_info: 상세 지역 정보 (선택, JSON 또는 텍스트 형식)
|
detail_region_info: 상세 지역 정보 (선택, JSON 또는 텍스트 형식)
|
||||||
created_at: 생성 일시 (자동 설정)
|
created_at: 생성 일시 (자동 설정)
|
||||||
|
|
||||||
Relationships:
|
Relationships:
|
||||||
lyrics: 생성된 가사 목록
|
lyrics: 생성된 가사 목록
|
||||||
songs: 생성된 노래 목록
|
songs: 생성된 노래 목록
|
||||||
videos: 최종 영상 결과 목록
|
videos: 최종 영상 결과 목록
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__tablename__ = "project"
|
__tablename__ = "project"
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
Index("idx_project_task_id", "task_id"),
|
Index("idx_project_task_id", "task_id"),
|
||||||
Index("idx_project_store_name", "store_name"),
|
Index("idx_project_store_name", "store_name"),
|
||||||
Index("idx_project_region", "region"),
|
Index("idx_project_region", "region"),
|
||||||
{
|
{
|
||||||
"mysql_engine": "InnoDB",
|
"mysql_engine": "InnoDB",
|
||||||
"mysql_charset": "utf8mb4",
|
"mysql_charset": "utf8mb4",
|
||||||
"mysql_collate": "utf8mb4_unicode_ci",
|
"mysql_collate": "utf8mb4_unicode_ci",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(
|
id: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
autoincrement=True,
|
autoincrement=True,
|
||||||
comment="고유 식별자",
|
comment="고유 식별자",
|
||||||
)
|
)
|
||||||
|
|
||||||
store_name: Mapped[str] = mapped_column(
|
store_name: Mapped[str] = mapped_column(
|
||||||
String(255),
|
String(255),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True,
|
index=True,
|
||||||
comment="가게명",
|
comment="가게명",
|
||||||
)
|
)
|
||||||
|
|
||||||
region: Mapped[str] = mapped_column(
|
region: Mapped[str] = mapped_column(
|
||||||
String(100),
|
String(100),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True,
|
index=True,
|
||||||
comment="지역명 (예: 군산)",
|
comment="지역명 (예: 군산)",
|
||||||
)
|
)
|
||||||
|
|
||||||
task_id: Mapped[str] = mapped_column(
|
task_id: Mapped[str] = mapped_column(
|
||||||
String(36),
|
String(36),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
comment="프로젝트 작업 고유 식별자 (UUID)",
|
comment="프로젝트 작업 고유 식별자 (UUID)",
|
||||||
)
|
)
|
||||||
|
|
||||||
detail_region_info: Mapped[Optional[str]] = mapped_column(
|
detail_region_info: Mapped[Optional[str]] = mapped_column(
|
||||||
Text,
|
Text,
|
||||||
nullable=True,
|
nullable=True,
|
||||||
comment="상세 지역 정보",
|
comment="상세 지역 정보",
|
||||||
)
|
)
|
||||||
|
|
||||||
language: Mapped[str] = mapped_column(
|
language: Mapped[str] = mapped_column(
|
||||||
String(50),
|
String(50),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
default="Korean",
|
default="Korean",
|
||||||
comment="출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
comment="출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
||||||
)
|
)
|
||||||
|
|
||||||
created_at: Mapped[datetime] = mapped_column(
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
DateTime,
|
DateTime,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
server_default=func.now(),
|
server_default=func.now(),
|
||||||
comment="생성 일시",
|
comment="생성 일시",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Relationships
|
# Relationships
|
||||||
lyrics: Mapped[List["Lyric"]] = relationship(
|
lyrics: Mapped[List["Lyric"]] = relationship(
|
||||||
"Lyric",
|
"Lyric",
|
||||||
back_populates="project",
|
back_populates="project",
|
||||||
cascade="all, delete-orphan",
|
cascade="all, delete-orphan",
|
||||||
lazy="selectin",
|
lazy="selectin",
|
||||||
)
|
)
|
||||||
|
|
||||||
songs: Mapped[List["Song"]] = relationship(
|
songs: Mapped[List["Song"]] = relationship(
|
||||||
"Song",
|
"Song",
|
||||||
back_populates="project",
|
back_populates="project",
|
||||||
cascade="all, delete-orphan",
|
cascade="all, delete-orphan",
|
||||||
lazy="selectin",
|
lazy="selectin",
|
||||||
)
|
)
|
||||||
|
|
||||||
videos: Mapped[List["Video"]] = relationship(
|
videos: Mapped[List["Video"]] = relationship(
|
||||||
"Video",
|
"Video",
|
||||||
back_populates="project",
|
back_populates="project",
|
||||||
cascade="all, delete-orphan",
|
cascade="all, delete-orphan",
|
||||||
lazy="selectin",
|
lazy="selectin",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
def truncate(value: str | None, max_len: int = 10) -> str:
|
def truncate(value: str | None, max_len: int = 10) -> str:
|
||||||
if value is None:
|
if value is None:
|
||||||
return "None"
|
return "None"
|
||||||
return (value[:max_len] + "...") if len(value) > max_len else value
|
return (value[:max_len] + "...") if len(value) > max_len else value
|
||||||
|
|
||||||
return (
|
return (
|
||||||
f"<Project("
|
f"<Project("
|
||||||
f"id={self.id}, "
|
f"id={self.id}, "
|
||||||
f"store_name='{self.store_name}', "
|
f"store_name='{self.store_name}', "
|
||||||
f"task_id='{truncate(self.task_id)}'"
|
f"task_id='{truncate(self.task_id)}'"
|
||||||
f")>"
|
f")>"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Image(Base):
|
class Image(Base):
|
||||||
"""
|
"""
|
||||||
업로드 이미지 테이블
|
업로드 이미지 테이블
|
||||||
|
|
||||||
사용자가 업로드한 이미지의 URL을 저장합니다.
|
사용자가 업로드한 이미지의 URL을 저장합니다.
|
||||||
독립적으로 관리되며 Project와 직접적인 관계가 없습니다.
|
독립적으로 관리되며 Project와 직접적인 관계가 없습니다.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
id: 고유 식별자 (자동 증가)
|
id: 고유 식별자 (자동 증가)
|
||||||
task_id: 이미지 업로드 작업 고유 식별자 (UUID)
|
task_id: 이미지 업로드 작업 고유 식별자 (UUID)
|
||||||
img_name: 이미지명
|
img_name: 이미지명
|
||||||
img_url: 이미지 URL (S3, CDN 등의 경로)
|
img_url: 이미지 URL (S3, CDN 등의 경로)
|
||||||
created_at: 생성 일시 (자동 설정)
|
created_at: 생성 일시 (자동 설정)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__tablename__ = "image"
|
__tablename__ = "image"
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
{
|
{
|
||||||
"mysql_engine": "InnoDB",
|
"mysql_engine": "InnoDB",
|
||||||
"mysql_charset": "utf8mb4",
|
"mysql_charset": "utf8mb4",
|
||||||
"mysql_collate": "utf8mb4_unicode_ci",
|
"mysql_collate": "utf8mb4_unicode_ci",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(
|
id: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
autoincrement=True,
|
autoincrement=True,
|
||||||
comment="고유 식별자",
|
comment="고유 식별자",
|
||||||
)
|
)
|
||||||
|
|
||||||
task_id: Mapped[str] = mapped_column(
|
task_id: Mapped[str] = mapped_column(
|
||||||
String(36),
|
String(36),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
comment="이미지 업로드 작업 고유 식별자 (UUID)",
|
comment="이미지 업로드 작업 고유 식별자 (UUID)",
|
||||||
)
|
)
|
||||||
|
|
||||||
img_name: Mapped[str] = mapped_column(
|
img_name: Mapped[str] = mapped_column(
|
||||||
String(255),
|
String(255),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
comment="이미지명",
|
comment="이미지명",
|
||||||
)
|
)
|
||||||
|
|
||||||
img_url: Mapped[str] = mapped_column(
|
img_url: Mapped[str] = mapped_column(
|
||||||
String(2048),
|
String(2048),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
comment="이미지 URL (blob, CDN 경로)",
|
comment="이미지 URL (blob, CDN 경로)",
|
||||||
)
|
)
|
||||||
|
|
||||||
img_order: Mapped[int] = mapped_column(
|
img_order: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
default=0,
|
default=0,
|
||||||
comment="이미지 순서",
|
comment="이미지 순서",
|
||||||
)
|
)
|
||||||
|
|
||||||
created_at: Mapped[datetime] = mapped_column(
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
DateTime,
|
DateTime,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
server_default=func.now(),
|
server_default=func.now(),
|
||||||
comment="생성 일시",
|
comment="생성 일시",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
task_id_str = (
|
task_id_str = (
|
||||||
(self.task_id[:10] + "...") if len(self.task_id) > 10 else self.task_id
|
(self.task_id[:10] + "...") if len(self.task_id) > 10 else self.task_id
|
||||||
)
|
)
|
||||||
img_name_str = (
|
img_name_str = (
|
||||||
(self.img_name[:10] + "...") if len(self.img_name) > 10 else self.img_name
|
(self.img_name[:10] + "...") if len(self.img_name) > 10 else self.img_name
|
||||||
)
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
f"<Image(id={self.id}, task_id='{task_id_str}', img_name='{img_name_str}')>"
|
f"<Image(id={self.id}, task_id='{task_id_str}', img_name='{img_name_str}')>"
|
||||||
)
|
)
|
||||||
@@ -1,161 +1,260 @@
|
|||||||
from typing import Literal, Optional
|
from typing import Literal, Optional
|
||||||
|
|
||||||
from pydantic import BaseModel, ConfigDict, Field
|
from pydantic import BaseModel, ConfigDict, Field
|
||||||
|
|
||||||
|
|
||||||
class AttributeInfo(BaseModel):
|
class AttributeInfo(BaseModel):
|
||||||
"""음악 속성 정보"""
|
"""음악 속성 정보"""
|
||||||
|
|
||||||
genre: str = Field(..., description="음악 장르")
|
genre: str = Field(..., description="음악 장르")
|
||||||
vocal: str = Field(..., description="보컬 스타일")
|
vocal: str = Field(..., description="보컬 스타일")
|
||||||
tempo: str = Field(..., description="템포")
|
tempo: str = Field(..., description="템포")
|
||||||
mood: str = Field(..., description="분위기")
|
mood: str = Field(..., description="분위기")
|
||||||
|
|
||||||
|
|
||||||
class GenerateRequestImg(BaseModel):
|
class GenerateRequestImg(BaseModel):
|
||||||
"""이미지 URL 스키마"""
|
"""이미지 URL 스키마"""
|
||||||
|
|
||||||
url: str = Field(..., description="이미지 URL")
|
url: str = Field(..., description="이미지 URL")
|
||||||
name: Optional[str] = Field(None, description="이미지명 (없으면 URL에서 추출)")
|
name: Optional[str] = Field(None, description="이미지명 (없으면 URL에서 추출)")
|
||||||
|
|
||||||
|
|
||||||
class GenerateRequestInfo(BaseModel):
|
class GenerateRequestInfo(BaseModel):
|
||||||
"""생성 요청 정보 스키마 (이미지 제외)"""
|
"""생성 요청 정보 스키마 (이미지 제외)"""
|
||||||
|
|
||||||
customer_name: str = Field(..., description="고객명/가게명")
|
customer_name: str = Field(..., description="고객명/가게명")
|
||||||
region: str = Field(..., description="지역명")
|
region: str = Field(..., description="지역명")
|
||||||
detail_region_info: Optional[str] = Field(None, description="상세 지역 정보")
|
detail_region_info: Optional[str] = Field(None, description="상세 지역 정보")
|
||||||
attribute: AttributeInfo = Field(..., description="음악 속성 정보")
|
attribute: AttributeInfo = Field(..., description="음악 속성 정보")
|
||||||
language: str = Field(
|
language: str = Field(
|
||||||
default="Korean",
|
default="Korean",
|
||||||
description="가사 출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
description="가사 출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class GenerateRequest(GenerateRequestInfo):
|
class GenerateRequest(GenerateRequestInfo):
|
||||||
"""기본 생성 요청 스키마 (이미지 없음, JSON body)
|
"""기본 생성 요청 스키마 (이미지 없음, JSON body)
|
||||||
|
|
||||||
이미지 없이 프로젝트 정보만 전달합니다.
|
이미지 없이 프로젝트 정보만 전달합니다.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
json_schema_extra={
|
json_schema_extra={
|
||||||
"example": {
|
"example": {
|
||||||
"customer_name": "스테이 머뭄",
|
"customer_name": "스테이 머뭄",
|
||||||
"region": "군산",
|
"region": "군산",
|
||||||
"detail_region_info": "군산 신흥동 말랭이 마을",
|
"detail_region_info": "군산 신흥동 말랭이 마을",
|
||||||
"attribute": {
|
"attribute": {
|
||||||
"genre": "K-Pop",
|
"genre": "K-Pop",
|
||||||
"vocal": "Raspy",
|
"vocal": "Raspy",
|
||||||
"tempo": "110 BPM",
|
"tempo": "110 BPM",
|
||||||
"mood": "happy",
|
"mood": "happy",
|
||||||
},
|
},
|
||||||
"language": "Korean",
|
"language": "Korean",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class GenerateUrlsRequest(GenerateRequestInfo):
|
class GenerateUrlsRequest(GenerateRequestInfo):
|
||||||
"""URL 기반 생성 요청 스키마 (JSON body)
|
"""URL 기반 생성 요청 스키마 (JSON body)
|
||||||
|
|
||||||
GenerateRequestInfo를 상속받아 이미지 목록을 추가합니다.
|
GenerateRequestInfo를 상속받아 이미지 목록을 추가합니다.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
json_schema_extra={
|
json_schema_extra={
|
||||||
"example": {
|
"example": {
|
||||||
"customer_name": "스테이 머뭄",
|
"customer_name": "스테이 머뭄",
|
||||||
"region": "군산",
|
"region": "군산",
|
||||||
"detail_region_info": "군산 신흥동 말랭이 마을",
|
"detail_region_info": "군산 신흥동 말랭이 마을",
|
||||||
"attribute": {
|
"attribute": {
|
||||||
"genre": "K-Pop",
|
"genre": "K-Pop",
|
||||||
"vocal": "Raspy",
|
"vocal": "Raspy",
|
||||||
"tempo": "110 BPM",
|
"tempo": "110 BPM",
|
||||||
"mood": "happy",
|
"mood": "happy",
|
||||||
},
|
},
|
||||||
"language": "Korean",
|
"language": "Korean",
|
||||||
"images": [
|
"images": [
|
||||||
{"url": "https://example.com/images/image_001.jpg"},
|
{"url": "https://example.com/images/image_001.jpg"},
|
||||||
{"url": "https://example.com/images/image_002.jpg", "name": "외관"},
|
{"url": "https://example.com/images/image_002.jpg", "name": "외관"},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
images: list[GenerateRequestImg] = Field(
|
images: list[GenerateRequestImg] = Field(
|
||||||
..., description="이미지 URL 목록", min_length=1
|
..., description="이미지 URL 목록", min_length=1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class GenerateUploadResponse(BaseModel):
|
class GenerateUploadResponse(BaseModel):
|
||||||
"""파일 업로드 기반 생성 응답 스키마"""
|
"""파일 업로드 기반 생성 응답 스키마"""
|
||||||
|
|
||||||
task_id: str = Field(..., description="작업 고유 식별자 (UUID7)")
|
task_id: str = Field(..., description="작업 고유 식별자 (UUID7)")
|
||||||
status: Literal["processing", "completed", "failed"] = Field(
|
status: Literal["processing", "completed", "failed"] = Field(
|
||||||
..., description="작업 상태"
|
..., description="작업 상태"
|
||||||
)
|
)
|
||||||
message: str = Field(..., description="응답 메시지")
|
message: str = Field(..., description="응답 메시지")
|
||||||
uploaded_count: int = Field(..., description="업로드된 이미지 개수")
|
uploaded_count: int = Field(..., description="업로드된 이미지 개수")
|
||||||
|
|
||||||
|
|
||||||
class GenerateResponse(BaseModel):
|
class GenerateResponse(BaseModel):
|
||||||
"""생성 응답 스키마"""
|
"""생성 응답 스키마"""
|
||||||
|
|
||||||
task_id: str = Field(..., description="작업 고유 식별자 (UUID7)")
|
task_id: str = Field(..., description="작업 고유 식별자 (UUID7)")
|
||||||
status: Literal["processing", "completed", "failed"] = Field(
|
status: Literal["processing", "completed", "failed"] = Field(
|
||||||
..., description="작업 상태"
|
..., description="작업 상태"
|
||||||
)
|
)
|
||||||
message: str = Field(..., description="응답 메시지")
|
message: str = Field(..., description="응답 메시지")
|
||||||
|
|
||||||
|
|
||||||
class CrawlingRequest(BaseModel):
|
class CrawlingRequest(BaseModel):
|
||||||
"""크롤링 요청 스키마"""
|
"""크롤링 요청 스키마"""
|
||||||
|
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
json_schema_extra={
|
json_schema_extra={
|
||||||
"example": {
|
"example": {
|
||||||
"url": "https://map.naver.com/p/search/%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84/place/1133638931?c=14.70,0,0,0,dh&placePath=/photo?businessCategory=pension&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5×tamp=202512191123&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5×tamp=202512191007&from=map&entry=bmp&filterType=%EC%97%85%EC%B2%B4&businessCategory=pension"
|
"url": "https://map.naver.com/p/search/%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84/place/1133638931?c=14.70,0,0,0,dh&placePath=/photo?businessCategory=pension&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5×tamp=202512191123&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5×tamp=202512191007&from=map&entry=bmp&filterType=%EC%97%85%EC%B2%B4&businessCategory=pension"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
url: str = Field(..., description="네이버 지도 장소 URL")
|
url: str = Field(..., description="네이버 지도 장소 URL")
|
||||||
|
|
||||||
|
|
||||||
class ProcessedInfo(BaseModel):
|
class ProcessedInfo(BaseModel):
|
||||||
"""가공된 장소 정보 스키마"""
|
"""가공된 장소 정보 스키마"""
|
||||||
|
|
||||||
customer_name: str = Field(..., description="고객명/가게명 (base_info.name)")
|
customer_name: str = Field(..., description="고객명/가게명 (base_info.name)")
|
||||||
region: str = Field(..., description="지역명 (roadAddress에서 추출한 시 이름)")
|
region: str = Field(..., description="지역명 (roadAddress에서 추출한 시 이름)")
|
||||||
detail_region_info: str = Field(..., description="상세 지역 정보 (roadAddress)")
|
detail_region_info: str = Field(..., description="상세 지역 정보 (roadAddress)")
|
||||||
|
|
||||||
|
|
||||||
class MarketingAnalysis(BaseModel):
|
class MarketingAnalysis(BaseModel):
|
||||||
"""마케팅 분석 결과 스키마"""
|
"""마케팅 분석 결과 스키마"""
|
||||||
|
|
||||||
report: str = Field(..., description="마케팅 분석 리포트")
|
report: str = Field(..., description="마케팅 분석 리포트")
|
||||||
tags: list[str] = Field(default_factory=list, description="추천 태그 목록")
|
tags: list[str] = Field(default_factory=list, description="추천 태그 목록")
|
||||||
facilities: list[str] = Field(default_factory=list, description="추천 부대시설 목록")
|
facilities: list[str] = Field(default_factory=list, description="추천 부대시설 목록")
|
||||||
|
|
||||||
|
|
||||||
class CrawlingResponse(BaseModel):
|
class CrawlingResponse(BaseModel):
|
||||||
"""크롤링 응답 스키마"""
|
"""크롤링 응답 스키마"""
|
||||||
|
|
||||||
image_list: Optional[list[str]] = Field(None, description="이미지 URL 목록")
|
image_list: Optional[list[str]] = Field(None, description="이미지 URL 목록")
|
||||||
image_count: int = Field(..., description="이미지 개수")
|
image_count: int = Field(..., description="이미지 개수")
|
||||||
processed_info: Optional[ProcessedInfo] = Field(
|
processed_info: Optional[ProcessedInfo] = Field(
|
||||||
None, description="가공된 장소 정보 (customer_name, region, detail_region_info)"
|
None, description="가공된 장소 정보 (customer_name, region, detail_region_info)"
|
||||||
)
|
)
|
||||||
marketing_analysis: Optional[MarketingAnalysis] = Field(
|
marketing_analysis: Optional[MarketingAnalysis] = Field(
|
||||||
None, description="마케팅 분석 결과 (report, tags, facilities)"
|
None, description="마케팅 분석 결과 (report, tags, facilities)"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ErrorResponse(BaseModel):
|
class ErrorResponse(BaseModel):
|
||||||
"""에러 응답 스키마"""
|
"""에러 응답 스키마"""
|
||||||
|
|
||||||
success: bool = Field(default=False, description="요청 성공 여부")
|
success: bool = Field(default=False, description="요청 성공 여부")
|
||||||
error_code: str = Field(..., description="에러 코드")
|
error_code: str = Field(..., description="에러 코드")
|
||||||
message: str = Field(..., description="에러 메시지")
|
message: str = Field(..., description="에러 메시지")
|
||||||
detail: Optional[str] = Field(None, description="상세 에러 정보")
|
detail: Optional[str] = Field(None, description="상세 에러 정보")
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Image Upload Schemas
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
|
||||||
|
class ImageUrlItem(BaseModel):
|
||||||
|
"""이미지 URL 아이템 스키마"""
|
||||||
|
|
||||||
|
url: str = Field(..., description="외부 이미지 URL")
|
||||||
|
name: Optional[str] = Field(None, description="이미지명 (없으면 URL에서 추출)")
|
||||||
|
|
||||||
|
|
||||||
|
class ImageUploadRequest(BaseModel):
|
||||||
|
"""이미지 업로드 요청 스키마 (JSON body 부분)
|
||||||
|
|
||||||
|
URL 이미지 목록을 전달합니다.
|
||||||
|
바이너리 파일은 multipart/form-data로 별도 전달됩니다.
|
||||||
|
"""
|
||||||
|
|
||||||
|
model_config = ConfigDict(
|
||||||
|
json_schema_extra={
|
||||||
|
"example": {
|
||||||
|
"images": [
|
||||||
|
{"url": "https://example.com/images/image_001.jpg"},
|
||||||
|
{"url": "https://example.com/images/image_002.jpg", "name": "외관"},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
images: Optional[list[ImageUrlItem]] = Field(
|
||||||
|
None, description="외부 이미지 URL 목록"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ImageUploadResultItem(BaseModel):
|
||||||
|
"""업로드된 이미지 결과 아이템"""
|
||||||
|
|
||||||
|
id: int = Field(..., description="이미지 ID")
|
||||||
|
img_name: str = Field(..., description="이미지명")
|
||||||
|
img_url: str = Field(..., description="이미지 URL")
|
||||||
|
img_order: int = Field(..., description="이미지 순서")
|
||||||
|
source: Literal["url", "file", "blob"] = Field(
|
||||||
|
..., description="이미지 소스 (url: 외부 URL, file: 로컬 서버, blob: Azure Blob)"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ImageUploadResponse(BaseModel):
|
||||||
|
"""이미지 업로드 응답 스키마"""
|
||||||
|
|
||||||
|
model_config = ConfigDict(
|
||||||
|
json_schema_extra={
|
||||||
|
"example": {
|
||||||
|
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
||||||
|
"total_count": 3,
|
||||||
|
"url_count": 2,
|
||||||
|
"file_count": 1,
|
||||||
|
"saved_count": 3,
|
||||||
|
"images": [
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"img_name": "외관",
|
||||||
|
"img_url": "https://example.com/images/image_001.jpg",
|
||||||
|
"img_order": 0,
|
||||||
|
"source": "url",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"img_name": "내부",
|
||||||
|
"img_url": "https://example.com/images/image_002.jpg",
|
||||||
|
"img_order": 1,
|
||||||
|
"source": "url",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 3,
|
||||||
|
"img_name": "uploaded_image.jpg",
|
||||||
|
"img_url": "/media/image/2024-01-15/0694b716-dbff-7219-8000-d08cb5fce431/uploaded_image_002.jpg",
|
||||||
|
"img_order": 2,
|
||||||
|
"source": "file",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"image_urls": [
|
||||||
|
"https://example.com/images/image_001.jpg",
|
||||||
|
"https://example.com/images/image_002.jpg",
|
||||||
|
"/media/image/2024-01-15/0694b716-dbff-7219-8000-d08cb5fce431/uploaded_image_002.jpg",
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
task_id: str = Field(..., description="작업 고유 식별자 (새로 생성된 UUID7)")
|
||||||
|
total_count: int = Field(..., description="총 업로드된 이미지 개수")
|
||||||
|
url_count: int = Field(..., description="URL로 등록된 이미지 개수")
|
||||||
|
file_count: int = Field(..., description="파일로 업로드된 이미지 개수")
|
||||||
|
saved_count: int = Field(..., description="Image 테이블에 저장된 row 수")
|
||||||
|
images: list[ImageUploadResultItem] = Field(..., description="업로드된 이미지 목록")
|
||||||
|
image_urls: list[str] = Field(..., description="Image 테이블에 저장된 현재 task_id의 이미지 URL 목록")
|
||||||
@@ -1,24 +1,24 @@
|
|||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlmodel import SQLModel
|
from sqlmodel import SQLModel
|
||||||
|
|
||||||
|
|
||||||
class BaseService:
|
class BaseService:
|
||||||
def __init__(self, model, session: AsyncSession):
|
def __init__(self, model, session: AsyncSession):
|
||||||
self.model = model
|
self.model = model
|
||||||
self.session = session
|
self.session = session
|
||||||
|
|
||||||
async def _get(self, id: UUID):
|
async def _get(self, id: UUID):
|
||||||
return await self.session.get(self.model, id)
|
return await self.session.get(self.model, id)
|
||||||
|
|
||||||
async def _add(self, entity):
|
async def _add(self, entity):
|
||||||
self.session.add(entity)
|
self.session.add(entity)
|
||||||
await self.session.commit()
|
await self.session.commit()
|
||||||
await self.session.refresh(entity)
|
await self.session.refresh(entity)
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
async def _update(self, entity):
|
async def _update(self, entity):
|
||||||
return await self._add(entity)
|
return await self._add(entity)
|
||||||
|
|
||||||
async def _delete(self, entity):
|
async def _delete(self, entity):
|
||||||
await self.session.delete(entity)
|
await self.session.delete(entity)
|
||||||
@@ -1,48 +1,48 @@
|
|||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator
|
||||||
|
|
||||||
import pytest_asyncio
|
import pytest_asyncio
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||||
from sqlalchemy.pool import NullPool
|
from sqlalchemy.pool import NullPool
|
||||||
|
|
||||||
from app.database.session import Base
|
from app.database.session import Base
|
||||||
from config import db_settings
|
from config import db_settings
|
||||||
|
|
||||||
# 테스트 전용 DB URL
|
# 테스트 전용 DB URL
|
||||||
TEST_DB_URL = db_settings.MYSQL_URL.replace(
|
TEST_DB_URL = db_settings.MYSQL_URL.replace(
|
||||||
f"/{db_settings.MYSQL_DB}",
|
f"/{db_settings.MYSQL_DB}",
|
||||||
"/test_db", # 별도 테스트 DB 사용
|
"/test_db", # 별도 테스트 DB 사용
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
async def test_engine():
|
async def test_engine():
|
||||||
"""각 테스트마다 생성되는 테스트 엔진"""
|
"""각 테스트마다 생성되는 테스트 엔진"""
|
||||||
engine = create_async_engine(
|
engine = create_async_engine(
|
||||||
TEST_DB_URL,
|
TEST_DB_URL,
|
||||||
poolclass=NullPool, # 테스트에서는 풀 비활성화
|
poolclass=NullPool, # 테스트에서는 풀 비활성화
|
||||||
echo=True, # SQL 쿼리 로깅
|
echo=True, # SQL 쿼리 로깅
|
||||||
)
|
)
|
||||||
|
|
||||||
# 테스트 테이블 생성
|
# 테스트 테이블 생성
|
||||||
async with engine.begin() as conn:
|
async with engine.begin() as conn:
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
|
|
||||||
yield engine
|
yield engine
|
||||||
|
|
||||||
# 테스트 테이블 삭제
|
# 테스트 테이블 삭제
|
||||||
async with engine.begin() as conn:
|
async with engine.begin() as conn:
|
||||||
await conn.run_sync(Base.metadata.drop_all)
|
await conn.run_sync(Base.metadata.drop_all)
|
||||||
|
|
||||||
await engine.dispose()
|
await engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
async def db_session(test_engine) -> AsyncGenerator[AsyncSession, None]:
|
async def db_session(test_engine) -> AsyncGenerator[AsyncSession, None]:
|
||||||
"""각 테스트마다 새로운 세션 (격리 보장)"""
|
"""각 테스트마다 새로운 세션 (격리 보장)"""
|
||||||
async_session = async_sessionmaker(
|
async_session = async_sessionmaker(
|
||||||
test_engine, class_=AsyncSession, expire_on_commit=False
|
test_engine, class_=AsyncSession, expire_on_commit=False
|
||||||
)
|
)
|
||||||
|
|
||||||
async with async_session() as session:
|
async with async_session() as session:
|
||||||
yield session
|
yield session
|
||||||
await session.rollback() # 테스트 후 롤백
|
await session.rollback() # 테스트 후 롤백
|
||||||
@@ -1,17 +1,17 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_database_connection(test_engine):
|
async def test_database_connection(test_engine):
|
||||||
"""테스트 엔진을 사용한 연결 테스트"""
|
"""테스트 엔진을 사용한 연결 테스트"""
|
||||||
async with test_engine.begin() as connection:
|
async with test_engine.begin() as connection:
|
||||||
result = await connection.execute(text("SELECT 1"))
|
result = await connection.execute(text("SELECT 1"))
|
||||||
assert result.scalar() == 1
|
assert result.scalar() == 1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_session_usage(db_session):
|
async def test_session_usage(db_session):
|
||||||
"""세션을 사용한 테스트"""
|
"""세션을 사용한 테스트"""
|
||||||
result = await db_session.execute(text("SELECT 1 as num"))
|
result = await db_session.execute(text("SELECT 1 as num"))
|
||||||
assert result.scalar() == 1
|
assert result.scalar() == 1
|
||||||
@@ -1,30 +1,30 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
|
|
||||||
from app.database.session import AsyncSessionLocal, engine
|
from app.database.session import AsyncSessionLocal, engine
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_database_connection():
|
async def test_database_connection():
|
||||||
"""데이터베이스 연결 테스트"""
|
"""데이터베이스 연결 테스트"""
|
||||||
async with engine.begin() as connection:
|
async with engine.begin() as connection:
|
||||||
result = await connection.execute(text("SELECT 1"))
|
result = await connection.execute(text("SELECT 1"))
|
||||||
assert result.scalar() == 1
|
assert result.scalar() == 1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_session_creation():
|
async def test_session_creation():
|
||||||
"""세션 생성 테스트"""
|
"""세션 생성 테스트"""
|
||||||
async with AsyncSessionLocal() as session:
|
async with AsyncSessionLocal() as session:
|
||||||
result = await session.execute(text("SELECT 1"))
|
result = await session.execute(text("SELECT 1"))
|
||||||
assert result.scalar() == 1
|
assert result.scalar() == 1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_database_version():
|
async def test_database_version():
|
||||||
"""MySQL 버전 확인 테스트"""
|
"""MySQL 버전 확인 테스트"""
|
||||||
async with AsyncSessionLocal() as session:
|
async with AsyncSessionLocal() as session:
|
||||||
result = await session.execute(text("SELECT VERSION()"))
|
result = await session.execute(text("SELECT VERSION()"))
|
||||||
version = result.scalar()
|
version = result.scalar()
|
||||||
assert version is not None
|
assert version is not None
|
||||||
print(f"MySQL Version: {version}")
|
print(f"MySQL Version: {version}")
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
"""
|
||||||
|
Home Worker 모듈
|
||||||
|
|
||||||
|
이미지 업로드 관련 백그라운드 작업을 처리합니다.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import aiofiles
|
||||||
|
from fastapi import UploadFile
|
||||||
|
|
||||||
|
from app.utils.upload_blob_as_request import AzureBlobUploader
|
||||||
|
|
||||||
|
MEDIA_ROOT = Path("media")
|
||||||
|
|
||||||
|
|
||||||
|
async def save_upload_file(file: UploadFile, save_path: Path) -> None:
|
||||||
|
"""업로드 파일을 지정된 경로에 저장"""
|
||||||
|
save_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
async with aiofiles.open(save_path, "wb") as f:
|
||||||
|
content = await file.read()
|
||||||
|
await f.write(content)
|
||||||
|
|
||||||
|
|
||||||
|
async def upload_image_to_blob(
|
||||||
|
task_id: str,
|
||||||
|
file: UploadFile,
|
||||||
|
filename: str,
|
||||||
|
save_dir: Path,
|
||||||
|
) -> tuple[bool, str, str]:
|
||||||
|
"""
|
||||||
|
이미지 파일을 media에 저장하고 Azure Blob Storage에 업로드합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task_id: 작업 고유 식별자
|
||||||
|
file: 업로드할 파일 객체
|
||||||
|
filename: 저장될 파일명
|
||||||
|
save_dir: media 저장 디렉토리 경로
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
tuple[bool, str, str]: (업로드 성공 여부, blob_url 또는 에러 메시지, media_path)
|
||||||
|
"""
|
||||||
|
save_path = save_dir / filename
|
||||||
|
media_path = str(save_path)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 1. media에 파일 저장
|
||||||
|
await save_upload_file(file, save_path)
|
||||||
|
|
||||||
|
# 2. Azure Blob Storage에 업로드
|
||||||
|
uploader = AzureBlobUploader(task_id=task_id)
|
||||||
|
upload_success = await uploader.upload_image(file_path=str(save_path))
|
||||||
|
|
||||||
|
if upload_success:
|
||||||
|
return True, uploader.public_url, media_path
|
||||||
|
else:
|
||||||
|
return False, f"Failed to upload {filename} to Blob", media_path
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return False, str(e), media_path
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
|
|
||||||
from sqlalchemy import select
|
|
||||||
|
|
||||||
from app.database.session import get_worker_session
|
|
||||||
from app.home.schemas.home import GenerateRequest
|
|
||||||
from app.lyric.models import Lyric
|
|
||||||
from app.utils.chatgpt_prompt import ChatgptService
|
|
||||||
|
|
||||||
|
|
||||||
async def _save_lyric(task_id: str, project_id: int, lyric_prompt: str) -> int:
|
|
||||||
"""Lyric 레코드를 DB에 저장 (status=processing, lyric_result=null)"""
|
|
||||||
async with get_worker_session() as session:
|
|
||||||
lyric = Lyric(
|
|
||||||
task_id=task_id,
|
|
||||||
project_id=project_id,
|
|
||||||
status="processing",
|
|
||||||
lyric_prompt=lyric_prompt,
|
|
||||||
lyric_result=None,
|
|
||||||
)
|
|
||||||
session.add(lyric)
|
|
||||||
await session.commit()
|
|
||||||
await session.refresh(lyric)
|
|
||||||
print(f"Lyric saved: id={lyric.id}, task_id={task_id}, status=processing")
|
|
||||||
return lyric.id
|
|
||||||
|
|
||||||
|
|
||||||
async def _update_lyric_status(lyric_id: int, status: str, lyric_result: str | None = None) -> None:
|
|
||||||
"""Lyric 레코드의 status와 lyric_result를 업데이트"""
|
|
||||||
async with get_worker_session() as session:
|
|
||||||
result = await session.execute(select(Lyric).where(Lyric.id == lyric_id))
|
|
||||||
lyric = result.scalar_one_or_none()
|
|
||||||
if lyric:
|
|
||||||
lyric.status = status
|
|
||||||
if lyric_result is not None:
|
|
||||||
lyric.lyric_result = lyric_result
|
|
||||||
await session.commit()
|
|
||||||
print(f"Lyric updated: id={lyric_id}, status={status}")
|
|
||||||
|
|
||||||
|
|
||||||
async def lyric_task(
|
|
||||||
task_id: str,
|
|
||||||
project_id: int,
|
|
||||||
customer_name: str,
|
|
||||||
region: str,
|
|
||||||
detail_region_info: str,
|
|
||||||
language: str = "Korean",
|
|
||||||
) -> None:
|
|
||||||
"""가사 생성 작업: ChatGPT로 가사 생성 및 Lyric 테이블 저장/업데이트"""
|
|
||||||
service = ChatgptService(
|
|
||||||
customer_name=customer_name,
|
|
||||||
region=region,
|
|
||||||
detail_region_info=detail_region_info,
|
|
||||||
language=language,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Lyric 레코드 저장 (status=processing, lyric_result=null)
|
|
||||||
lyric_prompt = service.build_lyrics_prompt()
|
|
||||||
lyric_id = await _save_lyric(task_id, project_id, lyric_prompt)
|
|
||||||
|
|
||||||
# GPT 호출
|
|
||||||
result = await service.generate(prompt=lyric_prompt)
|
|
||||||
|
|
||||||
print(f"GPT Response:\n{result}")
|
|
||||||
|
|
||||||
# 결과에 ERROR가 포함되어 있으면 status를 failed로 업데이트
|
|
||||||
if "ERROR:" in result:
|
|
||||||
await _update_lyric_status(lyric_id, "failed", lyric_result=result)
|
|
||||||
else:
|
|
||||||
await _update_lyric_status(lyric_id, "completed", lyric_result=result)
|
|
||||||
|
|
||||||
|
|
||||||
async def _task_process_async(request_body: GenerateRequest, task_id: str, project_id: int) -> None:
|
|
||||||
"""백그라운드 작업 처리 (async 버전)"""
|
|
||||||
customer_name = request_body.customer_name
|
|
||||||
region = request_body.region
|
|
||||||
detail_region_info = request_body.detail_region_info or ""
|
|
||||||
language = request_body.language
|
|
||||||
|
|
||||||
print(f"customer_name: {customer_name}")
|
|
||||||
print(f"region: {region}")
|
|
||||||
print(f"detail_region_info: {detail_region_info}")
|
|
||||||
print(f"language: {language}")
|
|
||||||
|
|
||||||
# 가사 생성 작업
|
|
||||||
await lyric_task(task_id, project_id, customer_name, region, detail_region_info, language)
|
|
||||||
|
|
||||||
|
|
||||||
def task_process(request_body: GenerateRequest, task_id: str, project_id: int) -> None:
|
|
||||||
"""백그라운드 작업 처리 함수 (sync wrapper)"""
|
|
||||||
asyncio.run(_task_process_async(request_body, task_id, project_id))
|
|
||||||
@@ -1,61 +1,61 @@
|
|||||||
from sqladmin import ModelView
|
from sqladmin import ModelView
|
||||||
|
|
||||||
from app.lyric.models import Lyric
|
from app.lyric.models import Lyric
|
||||||
|
|
||||||
|
|
||||||
class LyricAdmin(ModelView, model=Lyric):
|
class LyricAdmin(ModelView, model=Lyric):
|
||||||
name = "가사"
|
name = "가사"
|
||||||
name_plural = "가사 목록"
|
name_plural = "가사 목록"
|
||||||
icon = "fa-solid fa-music"
|
icon = "fa-solid fa-music"
|
||||||
category = "가사 관리"
|
category = "가사 관리"
|
||||||
page_size = 20
|
page_size = 20
|
||||||
|
|
||||||
column_list = [
|
column_list = [
|
||||||
"id",
|
"id",
|
||||||
"project_id",
|
"project_id",
|
||||||
"task_id",
|
"task_id",
|
||||||
"status",
|
"status",
|
||||||
"language",
|
"language",
|
||||||
"created_at",
|
"created_at",
|
||||||
]
|
]
|
||||||
|
|
||||||
column_details_list = [
|
column_details_list = [
|
||||||
"id",
|
"id",
|
||||||
"project_id",
|
"project_id",
|
||||||
"task_id",
|
"task_id",
|
||||||
"status",
|
"status",
|
||||||
"language",
|
"language",
|
||||||
"lyric_prompt",
|
"lyric_prompt",
|
||||||
"lyric_result",
|
"lyric_result",
|
||||||
"created_at",
|
"created_at",
|
||||||
]
|
]
|
||||||
|
|
||||||
# 폼(생성/수정)에서 제외
|
# 폼(생성/수정)에서 제외
|
||||||
form_excluded_columns = ["created_at", "songs", "videos"]
|
form_excluded_columns = ["created_at", "songs", "videos"]
|
||||||
|
|
||||||
column_searchable_list = [
|
column_searchable_list = [
|
||||||
Lyric.task_id,
|
Lyric.task_id,
|
||||||
Lyric.status,
|
Lyric.status,
|
||||||
Lyric.language,
|
Lyric.language,
|
||||||
]
|
]
|
||||||
|
|
||||||
column_default_sort = (Lyric.created_at, True) # True: DESC (최신순)
|
column_default_sort = (Lyric.created_at, True) # True: DESC (최신순)
|
||||||
|
|
||||||
column_sortable_list = [
|
column_sortable_list = [
|
||||||
Lyric.id,
|
Lyric.id,
|
||||||
Lyric.project_id,
|
Lyric.project_id,
|
||||||
Lyric.status,
|
Lyric.status,
|
||||||
Lyric.language,
|
Lyric.language,
|
||||||
Lyric.created_at,
|
Lyric.created_at,
|
||||||
]
|
]
|
||||||
|
|
||||||
column_labels = {
|
column_labels = {
|
||||||
"id": "ID",
|
"id": "ID",
|
||||||
"project_id": "프로젝트 ID",
|
"project_id": "프로젝트 ID",
|
||||||
"task_id": "작업 ID",
|
"task_id": "작업 ID",
|
||||||
"status": "상태",
|
"status": "상태",
|
||||||
"language": "언어",
|
"language": "언어",
|
||||||
"lyric_prompt": "프롬프트",
|
"lyric_prompt": "프롬프트",
|
||||||
"lyric_result": "생성 결과",
|
"lyric_result": "생성 결과",
|
||||||
"created_at": "생성일시",
|
"created_at": "생성일시",
|
||||||
}
|
}
|
||||||
@@ -1,445 +1,448 @@
|
|||||||
"""
|
"""
|
||||||
Lyric API Router
|
Lyric API Router
|
||||||
|
|
||||||
이 모듈은 가사 관련 API 엔드포인트를 정의합니다.
|
이 모듈은 가사 관련 API 엔드포인트를 정의합니다.
|
||||||
모든 엔드포인트는 재사용 가능하도록 설계되었습니다.
|
모든 엔드포인트는 재사용 가능하도록 설계되었습니다.
|
||||||
|
|
||||||
엔드포인트 목록:
|
엔드포인트 목록:
|
||||||
- POST /lyric/generate: 가사 생성
|
- POST /lyric/generate: 가사 생성
|
||||||
- GET /lyric/status/{task_id}: 가사 생성 상태 조회
|
- GET /lyric/status/{task_id}: 가사 생성 상태 조회
|
||||||
- GET /lyric/{task_id}: 가사 상세 조회
|
- GET /lyric/{task_id}: 가사 상세 조회
|
||||||
- GET /lyrics: 가사 목록 조회 (페이지네이션)
|
- GET /lyrics: 가사 목록 조회 (페이지네이션)
|
||||||
|
|
||||||
사용 예시:
|
사용 예시:
|
||||||
from app.lyric.api.routers.v1.lyric import router
|
from app.lyric.api.routers.v1.lyric import router
|
||||||
app.include_router(router, prefix="/api/v1")
|
app.include_router(router, prefix="/api/v1")
|
||||||
|
|
||||||
다른 서비스에서 재사용:
|
다른 서비스에서 재사용:
|
||||||
# 이 파일의 헬퍼 함수들을 import하여 사용 가능
|
# 이 파일의 헬퍼 함수들을 import하여 사용 가능
|
||||||
from app.lyric.api.routers.v1.lyric import (
|
from app.lyric.api.routers.v1.lyric import (
|
||||||
get_lyric_status_by_task_id,
|
get_lyric_status_by_task_id,
|
||||||
get_lyric_by_task_id,
|
get_lyric_by_task_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 페이지네이션은 pagination 모듈 사용
|
# 페이지네이션은 pagination 모듈 사용
|
||||||
from app.utils.pagination import PaginatedResponse, get_paginated
|
from app.utils.pagination import PaginatedResponse, get_paginated
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Optional
|
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Query, status
|
||||||
|
from sqlalchemy import select
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy import select
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from app.database.session import get_session
|
||||||
|
from app.home.models import Project
|
||||||
from app.database.session import get_session
|
from app.lyric.models import Lyric
|
||||||
from app.home.models import Project
|
from app.lyric.schemas.lyric import (
|
||||||
from app.lyric.models import Lyric
|
GenerateLyricRequest,
|
||||||
from app.lyric.schemas.lyric import (
|
GenerateLyricResponse,
|
||||||
GenerateLyricRequest,
|
LyricDetailResponse,
|
||||||
GenerateLyricResponse,
|
LyricListItem,
|
||||||
LyricDetailResponse,
|
LyricStatusResponse,
|
||||||
LyricListItem,
|
)
|
||||||
LyricStatusResponse,
|
from app.lyric.worker.lyric_task import generate_lyric_background
|
||||||
)
|
from app.utils.chatgpt_prompt import ChatgptService
|
||||||
from app.utils.chatgpt_prompt import ChatgptService
|
from app.utils.pagination import PaginatedResponse, get_paginated
|
||||||
from app.utils.common import generate_task_id
|
|
||||||
from app.utils.pagination import PaginatedResponse, get_paginated
|
router = APIRouter(prefix="/lyric", tags=["lyric"])
|
||||||
|
|
||||||
router = APIRouter(prefix="/lyric", tags=["lyric"])
|
|
||||||
|
# =============================================================================
|
||||||
|
# Reusable Service Functions (다른 모듈에서 import하여 사용 가능)
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
# Reusable Service Functions (다른 모듈에서 import하여 사용 가능)
|
|
||||||
# =============================================================================
|
|
||||||
|
async def get_lyric_status_by_task_id(
|
||||||
|
session: AsyncSession, task_id: str
|
||||||
async def get_lyric_status_by_task_id(
|
) -> LyricStatusResponse:
|
||||||
session: AsyncSession, task_id: str
|
"""task_id로 가사 생성 작업의 상태를 조회합니다.
|
||||||
) -> LyricStatusResponse:
|
|
||||||
"""task_id로 가사 생성 작업의 상태를 조회합니다.
|
Args:
|
||||||
|
session: SQLAlchemy AsyncSession
|
||||||
Args:
|
task_id: 작업 고유 식별자
|
||||||
session: SQLAlchemy AsyncSession
|
|
||||||
task_id: 작업 고유 식별자
|
Returns:
|
||||||
|
LyricStatusResponse: 상태 정보
|
||||||
Returns:
|
|
||||||
LyricStatusResponse: 상태 정보
|
Raises:
|
||||||
|
HTTPException: 404 - task_id에 해당하는 가사가 없는 경우
|
||||||
Raises:
|
|
||||||
HTTPException: 404 - task_id에 해당하는 가사가 없는 경우
|
Usage:
|
||||||
|
# 다른 서비스에서 사용
|
||||||
Usage:
|
from app.lyric.api.routers.v1.lyric import get_lyric_status_by_task_id
|
||||||
# 다른 서비스에서 사용
|
|
||||||
from app.lyric.api.routers.v1.lyric import get_lyric_status_by_task_id
|
status_info = await get_lyric_status_by_task_id(session, "some-task-id")
|
||||||
|
if status_info.status == "completed":
|
||||||
status_info = await get_lyric_status_by_task_id(session, "some-task-id")
|
# 완료 처리
|
||||||
if status_info.status == "completed":
|
"""
|
||||||
# 완료 처리
|
print(f"[get_lyric_status_by_task_id] START - task_id: {task_id}")
|
||||||
"""
|
result = await session.execute(
|
||||||
print(f"[get_lyric_status_by_task_id] START - task_id: {task_id}")
|
select(Lyric)
|
||||||
result = await session.execute(select(Lyric).where(Lyric.task_id == task_id))
|
.where(Lyric.task_id == task_id)
|
||||||
lyric = result.scalar_one_or_none()
|
.order_by(Lyric.created_at.desc())
|
||||||
|
.limit(1)
|
||||||
if not lyric:
|
)
|
||||||
print(f"[get_lyric_status_by_task_id] NOT FOUND - task_id: {task_id}")
|
lyric = result.scalar_one_or_none()
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
if not lyric:
|
||||||
detail=f"task_id '{task_id}'에 해당하는 가사를 찾을 수 없습니다.",
|
print(f"[get_lyric_status_by_task_id] NOT FOUND - task_id: {task_id}")
|
||||||
)
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
status_messages = {
|
detail=f"task_id '{task_id}'에 해당하는 가사를 찾을 수 없습니다.",
|
||||||
"processing": "가사 생성 중입니다.",
|
)
|
||||||
"completed": "가사 생성이 완료되었습니다.",
|
|
||||||
"failed": "가사 생성에 실패했습니다.",
|
status_messages = {
|
||||||
}
|
"processing": "가사 생성 중입니다.",
|
||||||
|
"completed": "가사 생성이 완료되었습니다.",
|
||||||
print(
|
"failed": "가사 생성에 실패했습니다.",
|
||||||
f"[get_lyric_status_by_task_id] SUCCESS - task_id: {task_id}, status: {lyric.status}"
|
}
|
||||||
)
|
|
||||||
return LyricStatusResponse(
|
print(
|
||||||
task_id=lyric.task_id,
|
f"[get_lyric_status_by_task_id] SUCCESS - task_id: {task_id}, status: {lyric.status}"
|
||||||
status=lyric.status,
|
)
|
||||||
message=status_messages.get(lyric.status, "알 수 없는 상태입니다."),
|
return LyricStatusResponse(
|
||||||
)
|
task_id=lyric.task_id,
|
||||||
|
status=lyric.status,
|
||||||
|
message=status_messages.get(lyric.status, "알 수 없는 상태입니다."),
|
||||||
async def get_lyric_by_task_id(
|
)
|
||||||
session: AsyncSession, task_id: str
|
|
||||||
) -> LyricDetailResponse:
|
|
||||||
"""task_id로 생성된 가사 상세 정보를 조회합니다.
|
async def get_lyric_by_task_id(
|
||||||
|
session: AsyncSession, task_id: str
|
||||||
Args:
|
) -> LyricDetailResponse:
|
||||||
session: SQLAlchemy AsyncSession
|
"""task_id로 생성된 가사 상세 정보를 조회합니다.
|
||||||
task_id: 작업 고유 식별자
|
|
||||||
|
Args:
|
||||||
Returns:
|
session: SQLAlchemy AsyncSession
|
||||||
LyricDetailResponse: 가사 상세 정보
|
task_id: 작업 고유 식별자
|
||||||
|
|
||||||
Raises:
|
Returns:
|
||||||
HTTPException: 404 - task_id에 해당하는 가사가 없는 경우
|
LyricDetailResponse: 가사 상세 정보
|
||||||
|
|
||||||
Usage:
|
Raises:
|
||||||
# 다른 서비스에서 사용
|
HTTPException: 404 - task_id에 해당하는 가사가 없는 경우
|
||||||
from app.lyric.api.routers.v1.lyric import get_lyric_by_task_id
|
|
||||||
|
Usage:
|
||||||
lyric = await get_lyric_by_task_id(session, task_id)
|
# 다른 서비스에서 사용
|
||||||
"""
|
from app.lyric.api.routers.v1.lyric import get_lyric_by_task_id
|
||||||
print(f"[get_lyric_by_task_id] START - task_id: {task_id}")
|
|
||||||
result = await session.execute(select(Lyric).where(Lyric.task_id == task_id))
|
lyric = await get_lyric_by_task_id(session, task_id)
|
||||||
lyric = result.scalar_one_or_none()
|
"""
|
||||||
|
print(f"[get_lyric_by_task_id] START - task_id: {task_id}")
|
||||||
if not lyric:
|
result = await session.execute(
|
||||||
print(f"[get_lyric_by_task_id] NOT FOUND - task_id: {task_id}")
|
select(Lyric)
|
||||||
raise HTTPException(
|
.where(Lyric.task_id == task_id)
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
.order_by(Lyric.created_at.desc())
|
||||||
detail=f"task_id '{task_id}'에 해당하는 가사를 찾을 수 없습니다.",
|
.limit(1)
|
||||||
)
|
)
|
||||||
|
lyric = result.scalar_one_or_none()
|
||||||
print(f"[get_lyric_by_task_id] SUCCESS - task_id: {task_id}, lyric_id: {lyric.id}")
|
|
||||||
return LyricDetailResponse(
|
if not lyric:
|
||||||
id=lyric.id,
|
print(f"[get_lyric_by_task_id] NOT FOUND - task_id: {task_id}")
|
||||||
task_id=lyric.task_id,
|
raise HTTPException(
|
||||||
project_id=lyric.project_id,
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
status=lyric.status,
|
detail=f"task_id '{task_id}'에 해당하는 가사를 찾을 수 없습니다.",
|
||||||
lyric_prompt=lyric.lyric_prompt,
|
)
|
||||||
lyric_result=lyric.lyric_result,
|
|
||||||
created_at=lyric.created_at,
|
print(f"[get_lyric_by_task_id] SUCCESS - task_id: {task_id}, lyric_id: {lyric.id}")
|
||||||
)
|
return LyricDetailResponse(
|
||||||
|
id=lyric.id,
|
||||||
|
task_id=lyric.task_id,
|
||||||
# =============================================================================
|
project_id=lyric.project_id,
|
||||||
# API Endpoints
|
status=lyric.status,
|
||||||
# =============================================================================
|
lyric_prompt=lyric.lyric_prompt,
|
||||||
|
lyric_result=lyric.lyric_result,
|
||||||
|
created_at=lyric.created_at,
|
||||||
@router.post(
|
)
|
||||||
"/generate",
|
|
||||||
summary="가사 생성",
|
|
||||||
description="""
|
# =============================================================================
|
||||||
고객 정보를 기반으로 ChatGPT를 이용하여 가사를 생성합니다.
|
# API Endpoints
|
||||||
|
# =============================================================================
|
||||||
## 요청 필드
|
|
||||||
- **customer_name**: 고객명/가게명 (필수)
|
|
||||||
- **region**: 지역명 (필수)
|
@router.post(
|
||||||
- **detail_region_info**: 상세 지역 정보 (선택)
|
"/generate",
|
||||||
- **language**: 가사 출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)
|
summary="가사 생성",
|
||||||
|
description="""
|
||||||
## 반환 정보
|
고객 정보를 기반으로 ChatGPT를 이용하여 가사를 생성합니다.
|
||||||
- **success**: 생성 성공 여부
|
백그라운드에서 비동기로 처리되며, 즉시 task_id를 반환합니다.
|
||||||
- **task_id**: 작업 고유 식별자
|
|
||||||
- **lyric**: 생성된 가사 (성공 시)
|
## 요청 필드
|
||||||
- **language**: 가사 언어
|
- **task_id**: 작업 고유 식별자 (이미지 업로드 시 생성된 task_id, 필수)
|
||||||
- **error_message**: 에러 메시지 (실패 시)
|
- **customer_name**: 고객명/가게명 (필수)
|
||||||
|
- **region**: 지역명 (필수)
|
||||||
## 실패 조건
|
- **detail_region_info**: 상세 지역 정보 (선택)
|
||||||
- ChatGPT API 오류
|
- **language**: 가사 출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)
|
||||||
- ChatGPT 거부 응답 (I'm sorry, I cannot 등)
|
|
||||||
- 응답에 ERROR: 포함
|
## 반환 정보
|
||||||
|
- **success**: 요청 접수 성공 여부
|
||||||
## 사용 예시
|
- **task_id**: 작업 고유 식별자
|
||||||
```
|
- **lyric**: null (백그라운드 처리 중)
|
||||||
POST /lyric/generate
|
- **language**: 가사 언어
|
||||||
{
|
- **error_message**: 에러 메시지 (요청 접수 실패 시)
|
||||||
"customer_name": "스테이 머뭄",
|
|
||||||
"region": "군산",
|
## 상태 확인
|
||||||
"detail_region_info": "군산 신흥동 말랭이 마을",
|
- GET /lyric/status/{task_id} 로 처리 상태 확인
|
||||||
"language": "Korean"
|
- GET /lyric/{task_id} 로 생성된 가사 조회
|
||||||
}
|
|
||||||
```
|
## 사용 예시
|
||||||
|
```
|
||||||
## 응답 예시 (성공)
|
POST /lyric/generate
|
||||||
```json
|
{
|
||||||
{
|
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
||||||
"success": true,
|
"customer_name": "스테이 머뭄",
|
||||||
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
"region": "군산",
|
||||||
"lyric": "인스타 감성의 스테이 머뭄...",
|
"detail_region_info": "군산 신흥동 말랭이 마을",
|
||||||
"language": "Korean",
|
"language": "Korean"
|
||||||
"error_message": null
|
}
|
||||||
}
|
```
|
||||||
```
|
|
||||||
|
## 응답 예시
|
||||||
## 응답 예시 (실패)
|
```json
|
||||||
```json
|
{
|
||||||
{
|
"success": true,
|
||||||
"success": false,
|
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
||||||
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
"lyric": null,
|
||||||
"lyric": null,
|
"language": "Korean",
|
||||||
"language": "Korean",
|
"error_message": null
|
||||||
"error_message": "I'm sorry, I can't comply with that request."
|
}
|
||||||
}
|
```
|
||||||
```
|
""",
|
||||||
""",
|
response_model=GenerateLyricResponse,
|
||||||
response_model=GenerateLyricResponse,
|
responses={
|
||||||
responses={
|
200: {"description": "가사 생성 요청 접수 성공"},
|
||||||
200: {"description": "가사 생성 성공 또는 실패 (success 필드로 구분)"},
|
500: {"description": "서버 내부 오류"},
|
||||||
500: {"description": "서버 내부 오류"},
|
},
|
||||||
},
|
)
|
||||||
)
|
async def generate_lyric(
|
||||||
async def generate_lyric(
|
request_body: GenerateLyricRequest,
|
||||||
request_body: GenerateLyricRequest,
|
background_tasks: BackgroundTasks,
|
||||||
session: AsyncSession = Depends(get_session),
|
session: AsyncSession = Depends(get_session),
|
||||||
) -> GenerateLyricResponse:
|
) -> GenerateLyricResponse:
|
||||||
"""고객 정보를 기반으로 가사를 생성합니다."""
|
"""고객 정보를 기반으로 가사를 생성합니다. (백그라운드 처리)"""
|
||||||
task_id = await generate_task_id(session=session, table_name=Project)
|
import time
|
||||||
print(
|
|
||||||
f"[generate_lyric] START - task_id: {task_id}, customer_name: {request_body.customer_name}, region: {request_body.region}"
|
request_start = time.perf_counter()
|
||||||
)
|
task_id = request_body.task_id
|
||||||
|
|
||||||
try:
|
print(f"[generate_lyric] ========== START ==========")
|
||||||
# 1. ChatGPT 서비스 초기화 및 프롬프트 생성
|
print(
|
||||||
service = ChatgptService(
|
f"[generate_lyric] task_id: {task_id}, "
|
||||||
customer_name=request_body.customer_name,
|
f"customer_name: {request_body.customer_name}, "
|
||||||
region=request_body.region,
|
f"region: {request_body.region}"
|
||||||
detail_region_info=request_body.detail_region_info or "",
|
)
|
||||||
language=request_body.language,
|
|
||||||
)
|
try:
|
||||||
prompt = service.build_lyrics_prompt()
|
# ========== Step 1: ChatGPT 서비스 초기화 및 프롬프트 생성 ==========
|
||||||
|
step1_start = time.perf_counter()
|
||||||
# 2. Project 테이블에 데이터 저장
|
print(f"[generate_lyric] Step 1: 서비스 초기화 및 프롬프트 생성...")
|
||||||
project = Project(
|
|
||||||
store_name=request_body.customer_name,
|
service = ChatgptService(
|
||||||
region=request_body.region,
|
customer_name=request_body.customer_name,
|
||||||
task_id=task_id,
|
region=request_body.region,
|
||||||
detail_region_info=request_body.detail_region_info,
|
detail_region_info=request_body.detail_region_info or "",
|
||||||
language=request_body.language,
|
language=request_body.language,
|
||||||
)
|
)
|
||||||
session.add(project)
|
prompt = service.build_lyrics_prompt()
|
||||||
await session.commit()
|
|
||||||
await session.refresh(project) # commit 후 project.id 동기화
|
step1_elapsed = (time.perf_counter() - step1_start) * 1000
|
||||||
print(
|
print(f"[generate_lyric] Step 1 완료 - 프롬프트 {len(prompt)}자 ({step1_elapsed:.1f}ms)")
|
||||||
f"[generate_lyric] Project saved - project_id: {project.id}, task_id: {task_id}"
|
|
||||||
)
|
# ========== Step 2: Project 테이블에 데이터 저장 ==========
|
||||||
|
step2_start = time.perf_counter()
|
||||||
# 3. Lyric 테이블에 데이터 저장 (status: processing)
|
print(f"[generate_lyric] Step 2: Project 저장...")
|
||||||
lyric = Lyric(
|
|
||||||
project_id=project.id,
|
project = Project(
|
||||||
task_id=task_id,
|
store_name=request_body.customer_name,
|
||||||
status="processing",
|
region=request_body.region,
|
||||||
lyric_prompt=prompt,
|
task_id=task_id,
|
||||||
lyric_result=None,
|
detail_region_info=request_body.detail_region_info,
|
||||||
language=request_body.language,
|
language=request_body.language,
|
||||||
)
|
)
|
||||||
session.add(lyric)
|
session.add(project)
|
||||||
await (
|
await session.commit()
|
||||||
session.commit()
|
await session.refresh(project)
|
||||||
) # processing 상태를 확실히 저장 (다른 트랜잭션에서 조회 가능)
|
|
||||||
await session.refresh(lyric) # commit 후 객체 상태 동기화
|
step2_elapsed = (time.perf_counter() - step2_start) * 1000
|
||||||
print(
|
print(f"[generate_lyric] Step 2 완료 - project_id: {project.id} ({step2_elapsed:.1f}ms)")
|
||||||
f"[generate_lyric] Lyric saved (processing) - lyric_id: {lyric.id}, task_id: {task_id}"
|
|
||||||
)
|
# ========== Step 3: Lyric 테이블에 데이터 저장 ==========
|
||||||
|
step3_start = time.perf_counter()
|
||||||
# 4. ChatGPT를 통해 가사 생성
|
print(f"[generate_lyric] Step 3: Lyric 저장 (processing)...")
|
||||||
print(f"[generate_lyric] ChatGPT generation started - task_id: {task_id}")
|
|
||||||
result = await service.generate(prompt=prompt)
|
lyric = Lyric(
|
||||||
print(f"[generate_lyric] ChatGPT generation completed - task_id: {task_id}")
|
project_id=project.id,
|
||||||
|
task_id=task_id,
|
||||||
# 5. 실패 응답 검사 (ERROR 또는 ChatGPT 거부 응답)
|
status="processing",
|
||||||
failure_patterns = [
|
lyric_prompt=prompt,
|
||||||
"ERROR:",
|
lyric_result=None,
|
||||||
"I'm sorry",
|
language=request_body.language,
|
||||||
"I cannot",
|
)
|
||||||
"I can't",
|
session.add(lyric)
|
||||||
"I apologize",
|
await session.commit()
|
||||||
"I'm unable",
|
await session.refresh(lyric)
|
||||||
"I am unable",
|
|
||||||
"I'm not able",
|
step3_elapsed = (time.perf_counter() - step3_start) * 1000
|
||||||
"I am not able",
|
print(f"[generate_lyric] Step 3 완료 - lyric_id: {lyric.id} ({step3_elapsed:.1f}ms)")
|
||||||
]
|
|
||||||
is_failure = any(
|
# ========== Step 4: 백그라운드 태스크 스케줄링 ==========
|
||||||
pattern.lower() in result.lower() for pattern in failure_patterns
|
step4_start = time.perf_counter()
|
||||||
)
|
print(f"[generate_lyric] Step 4: 백그라운드 태스크 스케줄링...")
|
||||||
|
|
||||||
if is_failure:
|
background_tasks.add_task(
|
||||||
print(f"[generate_lyric] FAILED - task_id: {task_id}, error: {result}")
|
generate_lyric_background,
|
||||||
lyric.status = "failed"
|
task_id=task_id,
|
||||||
lyric.lyric_result = result
|
prompt=prompt,
|
||||||
await session.commit()
|
language=request_body.language,
|
||||||
|
)
|
||||||
return GenerateLyricResponse(
|
|
||||||
success=False,
|
step4_elapsed = (time.perf_counter() - step4_start) * 1000
|
||||||
task_id=task_id,
|
print(f"[generate_lyric] Step 4 완료 ({step4_elapsed:.1f}ms)")
|
||||||
lyric=None,
|
|
||||||
language=request_body.language,
|
# ========== 완료 ==========
|
||||||
error_message=result,
|
total_elapsed = (time.perf_counter() - request_start) * 1000
|
||||||
)
|
print(f"[generate_lyric] ========== COMPLETE ==========")
|
||||||
|
print(f"[generate_lyric] API 응답 소요시간: {total_elapsed:.1f}ms")
|
||||||
# 6. 성공 시 Lyric 테이블 업데이트 (status: completed)
|
print(f"[generate_lyric] - Step 1 (프롬프트 생성): {step1_elapsed:.1f}ms")
|
||||||
lyric.status = "completed"
|
print(f"[generate_lyric] - Step 2 (Project 저장): {step2_elapsed:.1f}ms")
|
||||||
lyric.lyric_result = result
|
print(f"[generate_lyric] - Step 3 (Lyric 저장): {step3_elapsed:.1f}ms")
|
||||||
await session.commit()
|
print(f"[generate_lyric] - Step 4 (태스크 스케줄링): {step4_elapsed:.1f}ms")
|
||||||
|
print(f"[generate_lyric] (GPT API 호출은 백그라운드에서 별도 진행)")
|
||||||
print(f"[generate_lyric] SUCCESS - task_id: {task_id}")
|
|
||||||
return GenerateLyricResponse(
|
# 5. 즉시 응답 반환
|
||||||
success=True,
|
return GenerateLyricResponse(
|
||||||
task_id=task_id,
|
success=True,
|
||||||
lyric=result,
|
task_id=task_id,
|
||||||
language=request_body.language,
|
lyric=None,
|
||||||
error_message=None,
|
language=request_body.language,
|
||||||
)
|
error_message=None,
|
||||||
except Exception as e:
|
)
|
||||||
print(f"[generate_lyric] EXCEPTION - task_id: {task_id}, error: {e}")
|
|
||||||
await session.rollback()
|
except Exception as e:
|
||||||
return GenerateLyricResponse(
|
elapsed = (time.perf_counter() - request_start) * 1000
|
||||||
success=False,
|
print(f"[generate_lyric] EXCEPTION - task_id: {task_id}, error: {e} ({elapsed:.1f}ms)")
|
||||||
task_id=task_id,
|
await session.rollback()
|
||||||
lyric=None,
|
return GenerateLyricResponse(
|
||||||
language=request_body.language,
|
success=False,
|
||||||
error_message=str(e),
|
task_id=task_id,
|
||||||
)
|
lyric=None,
|
||||||
|
language=request_body.language,
|
||||||
|
error_message=str(e),
|
||||||
@router.get(
|
)
|
||||||
"/status/{task_id}",
|
|
||||||
summary="가사 생성 상태 조회",
|
|
||||||
description="""
|
@router.get(
|
||||||
task_id로 가사 생성 작업의 현재 상태를 조회합니다.
|
"/status/{task_id}",
|
||||||
|
summary="가사 생성 상태 조회",
|
||||||
## 상태 값
|
description="""
|
||||||
- **processing**: 가사 생성 중
|
task_id로 가사 생성 작업의 현재 상태를 조회합니다.
|
||||||
- **completed**: 가사 생성 완료
|
|
||||||
- **failed**: 가사 생성 실패
|
## 상태 값
|
||||||
|
- **processing**: 가사 생성 중
|
||||||
## 사용 예시
|
- **completed**: 가사 생성 완료
|
||||||
```
|
- **failed**: 가사 생성 실패
|
||||||
GET /lyric/status/019123ab-cdef-7890-abcd-ef1234567890
|
|
||||||
```
|
## 사용 예시
|
||||||
""",
|
```
|
||||||
response_model=LyricStatusResponse,
|
GET /lyric/status/019123ab-cdef-7890-abcd-ef1234567890
|
||||||
responses={
|
```
|
||||||
200: {"description": "상태 조회 성공"},
|
""",
|
||||||
404: {"description": "해당 task_id를 찾을 수 없음"},
|
response_model=LyricStatusResponse,
|
||||||
},
|
responses={
|
||||||
)
|
200: {"description": "상태 조회 성공"},
|
||||||
async def get_lyric_status(
|
404: {"description": "해당 task_id를 찾을 수 없음"},
|
||||||
task_id: str,
|
},
|
||||||
session: AsyncSession = Depends(get_session),
|
)
|
||||||
) -> LyricStatusResponse:
|
async def get_lyric_status(
|
||||||
"""task_id로 가사 생성 작업 상태를 조회합니다."""
|
task_id: str,
|
||||||
return await get_lyric_status_by_task_id(session, task_id)
|
session: AsyncSession = Depends(get_session),
|
||||||
|
) -> LyricStatusResponse:
|
||||||
|
"""task_id로 가사 생성 작업 상태를 조회합니다."""
|
||||||
@router.get(
|
return await get_lyric_status_by_task_id(session, task_id)
|
||||||
"s",
|
|
||||||
summary="가사 목록 조회 (페이지네이션)",
|
|
||||||
description="""
|
@router.get(
|
||||||
생성 완료된 가사를 페이지네이션으로 조회합니다.
|
"s",
|
||||||
|
summary="가사 목록 조회 (페이지네이션)",
|
||||||
## 파라미터
|
description="""
|
||||||
- **page**: 페이지 번호 (1부터 시작, 기본값: 1)
|
생성 완료된 가사를 페이지네이션으로 조회합니다.
|
||||||
- **page_size**: 페이지당 데이터 수 (기본값: 20, 최대: 100)
|
|
||||||
|
## 파라미터
|
||||||
## 반환 정보
|
- **page**: 페이지 번호 (1부터 시작, 기본값: 1)
|
||||||
- **items**: 가사 목록 (completed 상태만)
|
- **page_size**: 페이지당 데이터 수 (기본값: 20, 최대: 100)
|
||||||
- **total**: 전체 데이터 수
|
|
||||||
- **page**: 현재 페이지
|
## 반환 정보
|
||||||
- **page_size**: 페이지당 데이터 수
|
- **items**: 가사 목록 (completed 상태만)
|
||||||
- **total_pages**: 전체 페이지 수
|
- **total**: 전체 데이터 수
|
||||||
- **has_next**: 다음 페이지 존재 여부
|
- **page**: 현재 페이지
|
||||||
- **has_prev**: 이전 페이지 존재 여부
|
- **page_size**: 페이지당 데이터 수
|
||||||
|
- **total_pages**: 전체 페이지 수
|
||||||
## 사용 예시
|
- **has_next**: 다음 페이지 존재 여부
|
||||||
```
|
- **has_prev**: 이전 페이지 존재 여부
|
||||||
GET /lyrics # 기본 조회 (1페이지, 20개)
|
|
||||||
GET /lyrics?page=2 # 2페이지 조회
|
## 사용 예시
|
||||||
GET /lyrics?page=1&page_size=50 # 50개씩 조회
|
```
|
||||||
```
|
GET /lyrics # 기본 조회 (1페이지, 20개)
|
||||||
|
GET /lyrics?page=2 # 2페이지 조회
|
||||||
## 참고
|
GET /lyrics?page=1&page_size=50 # 50개씩 조회
|
||||||
- 생성 완료(completed)된 가사만 조회됩니다.
|
```
|
||||||
- processing, failed 상태의 가사는 조회되지 않습니다.
|
|
||||||
""",
|
## 참고
|
||||||
response_model=PaginatedResponse[LyricListItem],
|
- 생성 완료(completed)된 가사만 조회됩니다.
|
||||||
responses={
|
- processing, failed 상태의 가사는 조회되지 않습니다.
|
||||||
200: {"description": "가사 목록 조회 성공"},
|
""",
|
||||||
},
|
response_model=PaginatedResponse[LyricListItem],
|
||||||
)
|
responses={
|
||||||
async def list_lyrics(
|
200: {"description": "가사 목록 조회 성공"},
|
||||||
page: int = Query(1, ge=1, description="페이지 번호 (1부터 시작)"),
|
},
|
||||||
page_size: int = Query(20, ge=1, le=100, description="페이지당 데이터 수"),
|
)
|
||||||
session: AsyncSession = Depends(get_session),
|
async def list_lyrics(
|
||||||
) -> PaginatedResponse[LyricListItem]:
|
page: int = Query(1, ge=1, description="페이지 번호 (1부터 시작)"),
|
||||||
"""페이지네이션으로 완료된 가사 목록을 조회합니다."""
|
page_size: int = Query(20, ge=1, le=100, description="페이지당 데이터 수"),
|
||||||
return await get_paginated(
|
session: AsyncSession = Depends(get_session),
|
||||||
session=session,
|
) -> PaginatedResponse[LyricListItem]:
|
||||||
model=Lyric,
|
"""페이지네이션으로 완료된 가사 목록을 조회합니다."""
|
||||||
item_schema=LyricListItem,
|
return await get_paginated(
|
||||||
page=page,
|
session=session,
|
||||||
page_size=page_size,
|
model=Lyric,
|
||||||
filters={"status": "completed"},
|
item_schema=LyricListItem,
|
||||||
order_by="created_at",
|
page=page,
|
||||||
order_desc=True,
|
page_size=page_size,
|
||||||
)
|
filters={"status": "completed"},
|
||||||
|
order_by="created_at",
|
||||||
|
order_desc=True,
|
||||||
@router.get(
|
)
|
||||||
"/{task_id}",
|
|
||||||
summary="가사 상세 조회",
|
|
||||||
description="""
|
@router.get(
|
||||||
task_id로 생성된 가사의 상세 정보를 조회합니다.
|
"/{task_id}",
|
||||||
|
summary="가사 상세 조회",
|
||||||
## 반환 정보
|
description="""
|
||||||
- **id**: 가사 ID
|
task_id로 생성된 가사의 상세 정보를 조회합니다.
|
||||||
- **task_id**: 작업 고유 식별자
|
|
||||||
- **project_id**: 프로젝트 ID
|
## 반환 정보
|
||||||
- **status**: 처리 상태
|
- **id**: 가사 ID
|
||||||
- **lyric_prompt**: 가사 생성에 사용된 프롬프트
|
- **task_id**: 작업 고유 식별자
|
||||||
- **lyric_result**: 생성된 가사 (완료 시)
|
- **project_id**: 프로젝트 ID
|
||||||
- **created_at**: 생성 일시
|
- **status**: 처리 상태
|
||||||
|
- **lyric_prompt**: 가사 생성에 사용된 프롬프트
|
||||||
## 사용 예시
|
- **lyric_result**: 생성된 가사 (완료 시)
|
||||||
```
|
- **created_at**: 생성 일시
|
||||||
GET /lyric/019123ab-cdef-7890-abcd-ef1234567890
|
|
||||||
```
|
## 사용 예시
|
||||||
""",
|
```
|
||||||
response_model=LyricDetailResponse,
|
GET /lyric/019123ab-cdef-7890-abcd-ef1234567890
|
||||||
responses={
|
```
|
||||||
200: {"description": "가사 조회 성공"},
|
""",
|
||||||
404: {"description": "해당 task_id를 찾을 수 없음"},
|
response_model=LyricDetailResponse,
|
||||||
},
|
responses={
|
||||||
)
|
200: {"description": "가사 조회 성공"},
|
||||||
async def get_lyric_detail(
|
404: {"description": "해당 task_id를 찾을 수 없음"},
|
||||||
task_id: str,
|
},
|
||||||
session: AsyncSession = Depends(get_session),
|
)
|
||||||
) -> LyricDetailResponse:
|
async def get_lyric_detail(
|
||||||
"""task_id로 생성된 가사를 조회합니다."""
|
task_id: str,
|
||||||
return await get_lyric_by_task_id(session, task_id)
|
session: AsyncSession = Depends(get_session),
|
||||||
|
) -> LyricDetailResponse:
|
||||||
|
"""task_id로 생성된 가사를 조회합니다."""
|
||||||
|
return await get_lyric_by_task_id(session, task_id)
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from app.database.session import get_session
|
from app.database.session import get_session
|
||||||
|
|
||||||
SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
||||||
@@ -1,133 +1,133 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import TYPE_CHECKING, List
|
from typing import TYPE_CHECKING, List
|
||||||
|
|
||||||
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, func
|
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, func
|
||||||
from sqlalchemy.dialects.mysql import LONGTEXT
|
from sqlalchemy.dialects.mysql import LONGTEXT
|
||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
from app.database.session import Base
|
from app.database.session import Base
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from app.home.models import Project
|
from app.home.models import Project
|
||||||
from app.song.models import Song
|
from app.song.models import Song
|
||||||
from app.video.models import Video
|
from app.video.models import Video
|
||||||
|
|
||||||
|
|
||||||
class Lyric(Base):
|
class Lyric(Base):
|
||||||
"""
|
"""
|
||||||
가사 테이블
|
가사 테이블
|
||||||
|
|
||||||
AI를 통해 생성된 가사 정보를 저장합니다.
|
AI를 통해 생성된 가사 정보를 저장합니다.
|
||||||
프롬프트와 생성 결과, 처리 상태를 관리합니다.
|
프롬프트와 생성 결과, 처리 상태를 관리합니다.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
id: 고유 식별자 (자동 증가)
|
id: 고유 식별자 (자동 증가)
|
||||||
project_id: 연결된 Project의 id (외래키)
|
project_id: 연결된 Project의 id (외래키)
|
||||||
task_id: 가사 생성 작업의 고유 식별자 (UUID 형식)
|
task_id: 가사 생성 작업의 고유 식별자 (UUID 형식)
|
||||||
status: 처리 상태 (pending, processing, completed, failed 등)
|
status: 처리 상태 (pending, processing, completed, failed 등)
|
||||||
lyric_prompt: 가사 생성에 사용된 프롬프트
|
lyric_prompt: 가사 생성에 사용된 프롬프트
|
||||||
lyric_result: 생성된 가사 결과 (LONGTEXT로 긴 가사 지원)
|
lyric_result: 생성된 가사 결과 (LONGTEXT로 긴 가사 지원)
|
||||||
created_at: 생성 일시 (자동 설정)
|
created_at: 생성 일시 (자동 설정)
|
||||||
|
|
||||||
Relationships:
|
Relationships:
|
||||||
project: 연결된 Project
|
project: 연결된 Project
|
||||||
songs: 이 가사를 사용한 노래 목록
|
songs: 이 가사를 사용한 노래 목록
|
||||||
videos: 이 가사를 사용한 영상 목록
|
videos: 이 가사를 사용한 영상 목록
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__tablename__ = "lyric"
|
__tablename__ = "lyric"
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
{
|
{
|
||||||
"mysql_engine": "InnoDB",
|
"mysql_engine": "InnoDB",
|
||||||
"mysql_charset": "utf8mb4",
|
"mysql_charset": "utf8mb4",
|
||||||
"mysql_collate": "utf8mb4_unicode_ci",
|
"mysql_collate": "utf8mb4_unicode_ci",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(
|
id: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
autoincrement=True,
|
autoincrement=True,
|
||||||
comment="고유 식별자",
|
comment="고유 식별자",
|
||||||
)
|
)
|
||||||
|
|
||||||
project_id: Mapped[int] = mapped_column(
|
project_id: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
ForeignKey("project.id", ondelete="CASCADE"),
|
ForeignKey("project.id", ondelete="CASCADE"),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True,
|
index=True,
|
||||||
comment="연결된 Project의 id",
|
comment="연결된 Project의 id",
|
||||||
)
|
)
|
||||||
|
|
||||||
task_id: Mapped[str] = mapped_column(
|
task_id: Mapped[str] = mapped_column(
|
||||||
String(36),
|
String(36),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
comment="가사 생성 작업 고유 식별자 (UUID)",
|
comment="가사 생성 작업 고유 식별자 (UUID)",
|
||||||
)
|
)
|
||||||
|
|
||||||
status: Mapped[str] = mapped_column(
|
status: Mapped[str] = mapped_column(
|
||||||
String(50),
|
String(50),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
comment="처리 상태 (processing, completed, failed)",
|
comment="처리 상태 (processing, completed, failed)",
|
||||||
)
|
)
|
||||||
|
|
||||||
lyric_prompt: Mapped[str] = mapped_column(
|
lyric_prompt: Mapped[str] = mapped_column(
|
||||||
Text,
|
Text,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
comment="가사 생성에 사용된 프롬프트",
|
comment="가사 생성에 사용된 프롬프트",
|
||||||
)
|
)
|
||||||
|
|
||||||
lyric_result: Mapped[str] = mapped_column(
|
lyric_result: Mapped[str] = mapped_column(
|
||||||
LONGTEXT,
|
LONGTEXT,
|
||||||
nullable=True,
|
nullable=True,
|
||||||
comment="생성된 가사 결과",
|
comment="생성된 가사 결과",
|
||||||
)
|
)
|
||||||
|
|
||||||
language: Mapped[str] = mapped_column(
|
language: Mapped[str] = mapped_column(
|
||||||
String(50),
|
String(50),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
default="Korean",
|
default="Korean",
|
||||||
comment="가사 출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
comment="가사 출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
||||||
)
|
)
|
||||||
|
|
||||||
created_at: Mapped[datetime] = mapped_column(
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
DateTime,
|
DateTime,
|
||||||
nullable=True,
|
nullable=True,
|
||||||
server_default=func.now(),
|
server_default=func.now(),
|
||||||
comment="생성 일시",
|
comment="생성 일시",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Relationships
|
# Relationships
|
||||||
project: Mapped["Project"] = relationship(
|
project: Mapped["Project"] = relationship(
|
||||||
"Project",
|
"Project",
|
||||||
back_populates="lyrics",
|
back_populates="lyrics",
|
||||||
)
|
)
|
||||||
|
|
||||||
songs: Mapped[List["Song"]] = relationship(
|
songs: Mapped[List["Song"]] = relationship(
|
||||||
"Song",
|
"Song",
|
||||||
back_populates="lyric",
|
back_populates="lyric",
|
||||||
cascade="all, delete-orphan",
|
cascade="all, delete-orphan",
|
||||||
lazy="selectin",
|
lazy="selectin",
|
||||||
)
|
)
|
||||||
|
|
||||||
videos: Mapped[List["Video"]] = relationship(
|
videos: Mapped[List["Video"]] = relationship(
|
||||||
"Video",
|
"Video",
|
||||||
back_populates="lyric",
|
back_populates="lyric",
|
||||||
cascade="all, delete-orphan",
|
cascade="all, delete-orphan",
|
||||||
lazy="selectin",
|
lazy="selectin",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
def truncate(value: str | None, max_len: int = 10) -> str:
|
def truncate(value: str | None, max_len: int = 10) -> str:
|
||||||
if value is None:
|
if value is None:
|
||||||
return "None"
|
return "None"
|
||||||
return (value[:max_len] + "...") if len(value) > max_len else value
|
return (value[:max_len] + "...") if len(value) > max_len else value
|
||||||
|
|
||||||
return (
|
return (
|
||||||
f"<Lyric("
|
f"<Lyric("
|
||||||
f"id={self.id}, "
|
f"id={self.id}, "
|
||||||
f"task_id='{truncate(self.task_id)}', "
|
f"task_id='{truncate(self.task_id)}', "
|
||||||
f"status='{self.status}'"
|
f"status='{self.status}'"
|
||||||
f")>"
|
f")>"
|
||||||
)
|
)
|
||||||
@@ -1,165 +1,182 @@
|
|||||||
"""
|
"""
|
||||||
Lyric API Schemas
|
Lyric API Schemas
|
||||||
|
|
||||||
이 모듈은 가사 관련 API 엔드포인트에서 사용되는 Pydantic 스키마를 정의합니다.
|
이 모듈은 가사 관련 API 엔드포인트에서 사용되는 Pydantic 스키마를 정의합니다.
|
||||||
|
|
||||||
사용 예시:
|
사용 예시:
|
||||||
from app.lyric.schemas.lyric import (
|
from app.lyric.schemas.lyric import (
|
||||||
LyricStatusResponse,
|
LyricStatusResponse,
|
||||||
LyricDetailResponse,
|
LyricDetailResponse,
|
||||||
LyricListItem,
|
LyricListItem,
|
||||||
)
|
)
|
||||||
from app.utils.pagination import PaginatedResponse
|
from app.utils.pagination import PaginatedResponse
|
||||||
|
|
||||||
# 라우터에서 response_model로 사용
|
# 라우터에서 response_model로 사용
|
||||||
@router.get("/lyric/{task_id}", response_model=LyricDetailResponse)
|
@router.get("/lyric/{task_id}", response_model=LyricDetailResponse)
|
||||||
async def get_lyric(task_id: str):
|
async def get_lyric(task_id: str):
|
||||||
...
|
...
|
||||||
|
|
||||||
# 페이지네이션 응답 (공통 스키마 사용)
|
# 페이지네이션 응답 (공통 스키마 사용)
|
||||||
@router.get("/songs", response_model=PaginatedResponse[SongListItem])
|
@router.get("/songs", response_model=PaginatedResponse[SongListItem])
|
||||||
async def list_songs(...):
|
async def list_songs(...):
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, ConfigDict, Field
|
||||||
|
|
||||||
|
|
||||||
class GenerateLyricRequest(BaseModel):
|
class GenerateLyricRequest(BaseModel):
|
||||||
"""가사 생성 요청 스키마
|
"""가사 생성 요청 스키마
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
POST /lyric/generate
|
POST /lyric/generate
|
||||||
Request body for generating lyrics.
|
Request body for generating lyrics.
|
||||||
|
|
||||||
Example Request:
|
Example Request:
|
||||||
{
|
{
|
||||||
"customer_name": "스테이 머뭄",
|
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
||||||
"region": "군산",
|
"customer_name": "스테이 머뭄",
|
||||||
"detail_region_info": "군산 신흥동 말랭이 마을",
|
"region": "군산",
|
||||||
"language": "Korean"
|
"detail_region_info": "군산 신흥동 말랭이 마을",
|
||||||
}
|
"language": "Korean"
|
||||||
"""
|
}
|
||||||
|
"""
|
||||||
model_config = {
|
|
||||||
"json_schema_extra": {
|
model_config = ConfigDict(
|
||||||
"example": {
|
json_schema_extra={
|
||||||
"customer_name": "스테이 머뭄",
|
"example": {
|
||||||
"region": "군산",
|
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
||||||
"detail_region_info": "군산 신흥동 말랭이 마을",
|
"customer_name": "스테이 머뭄",
|
||||||
"language": "Korean",
|
"region": "군산",
|
||||||
}
|
"detail_region_info": "군산 신흥동 말랭이 마을",
|
||||||
}
|
"language": "Korean",
|
||||||
}
|
}
|
||||||
|
}
|
||||||
customer_name: str = Field(..., description="고객명/가게명")
|
)
|
||||||
region: str = Field(..., description="지역명")
|
|
||||||
detail_region_info: Optional[str] = Field(None, description="상세 지역 정보")
|
task_id: str = Field(
|
||||||
language: str = Field(
|
..., description="작업 고유 식별자 (이미지 업로드 시 생성된 task_id)"
|
||||||
default="Korean",
|
)
|
||||||
description="가사 출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
customer_name: str = Field(..., description="고객명/가게명")
|
||||||
)
|
region: str = Field(..., description="지역명")
|
||||||
|
detail_region_info: Optional[str] = Field(None, description="상세 지역 정보")
|
||||||
|
language: str = Field(
|
||||||
class GenerateLyricResponse(BaseModel):
|
default="Korean",
|
||||||
"""가사 생성 응답 스키마
|
description="가사 출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
||||||
|
)
|
||||||
Usage:
|
|
||||||
POST /lyric/generate
|
|
||||||
Returns the generated lyrics.
|
class GenerateLyricResponse(BaseModel):
|
||||||
|
"""가사 생성 응답 스키마
|
||||||
Note:
|
|
||||||
실패 조건:
|
Usage:
|
||||||
- ChatGPT API 오류
|
POST /lyric/generate
|
||||||
- ChatGPT 거부 응답 (I'm sorry, I cannot, I can't, I apologize 등)
|
Returns the generated lyrics.
|
||||||
- 응답에 ERROR: 포함
|
|
||||||
|
Note:
|
||||||
Example Response (Success):
|
실패 조건:
|
||||||
{
|
- ChatGPT API 오류
|
||||||
"success": true,
|
- ChatGPT 거부 응답 (I'm sorry, I cannot, I can't, I apologize 등)
|
||||||
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
- 응답에 ERROR: 포함
|
||||||
"lyric": "인스타 감성의 스테이 머뭄...",
|
"""
|
||||||
"language": "Korean",
|
|
||||||
"error_message": null
|
model_config = ConfigDict(
|
||||||
}
|
json_schema_extra={
|
||||||
|
"example": {
|
||||||
Example Response (Failure):
|
"success": True,
|
||||||
{
|
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
||||||
"success": false,
|
"lyric": "인스타 감성의 스테이 머뭄\n군산 신흥동 말랭이 마을에서\n여유로운 하루를 보내며\n추억을 만들어가요",
|
||||||
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
"language": "Korean",
|
||||||
"lyric": null,
|
"error_message": None,
|
||||||
"language": "Korean",
|
}
|
||||||
"error_message": "I'm sorry, I can't comply with that request."
|
}
|
||||||
}
|
)
|
||||||
"""
|
|
||||||
|
success: bool = Field(..., description="생성 성공 여부")
|
||||||
success: bool = Field(..., description="생성 성공 여부")
|
task_id: Optional[str] = Field(None, description="작업 고유 식별자 (uuid7)")
|
||||||
task_id: Optional[str] = Field(None, description="작업 고유 식별자 (uuid7)")
|
lyric: Optional[str] = Field(None, description="생성된 가사 (성공 시)")
|
||||||
lyric: Optional[str] = Field(None, description="생성된 가사 (성공 시)")
|
language: str = Field(..., description="가사 언어")
|
||||||
language: str = Field(..., description="가사 언어")
|
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시, ChatGPT 거부 응답 포함)")
|
||||||
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시, ChatGPT 거부 응답 포함)")
|
|
||||||
|
|
||||||
|
class LyricStatusResponse(BaseModel):
|
||||||
class LyricStatusResponse(BaseModel):
|
"""가사 상태 조회 응답 스키마
|
||||||
"""가사 상태 조회 응답 스키마
|
|
||||||
|
Usage:
|
||||||
Usage:
|
GET /lyric/status/{task_id}
|
||||||
GET /lyric/status/{task_id}
|
Returns the current processing status of a lyric generation task.
|
||||||
Returns the current processing status of a lyric generation task.
|
"""
|
||||||
|
|
||||||
Example Response:
|
model_config = ConfigDict(
|
||||||
{
|
json_schema_extra={
|
||||||
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
"example": {
|
||||||
"status": "completed",
|
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
||||||
"message": "가사 생성이 완료되었습니다."
|
"status": "completed",
|
||||||
}
|
"message": "가사 생성이 완료되었습니다.",
|
||||||
"""
|
}
|
||||||
|
}
|
||||||
task_id: str = Field(..., description="작업 고유 식별자")
|
)
|
||||||
status: str = Field(..., description="처리 상태 (processing, completed, failed)")
|
|
||||||
message: str = Field(..., description="상태 메시지")
|
task_id: str = Field(..., description="작업 고유 식별자")
|
||||||
|
status: str = Field(..., description="처리 상태 (processing, completed, failed)")
|
||||||
|
message: str = Field(..., description="상태 메시지")
|
||||||
class LyricDetailResponse(BaseModel):
|
|
||||||
"""가사 상세 조회 응답 스키마
|
|
||||||
|
class LyricDetailResponse(BaseModel):
|
||||||
Usage:
|
"""가사 상세 조회 응답 스키마
|
||||||
GET /lyric/{task_id}
|
|
||||||
Returns the generated lyric content for a specific task.
|
Usage:
|
||||||
|
GET /lyric/{task_id}
|
||||||
Example Response:
|
Returns the generated lyric content for a specific task.
|
||||||
{
|
"""
|
||||||
"id": 1,
|
|
||||||
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
model_config = ConfigDict(
|
||||||
"project_id": 1,
|
json_schema_extra={
|
||||||
"status": "completed",
|
"example": {
|
||||||
"lyric_prompt": "...",
|
"id": 1,
|
||||||
"lyric_result": "생성된 가사...",
|
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
||||||
"created_at": "2024-01-01T12:00:00"
|
"project_id": 1,
|
||||||
}
|
"status": "completed",
|
||||||
"""
|
"lyric_prompt": "고객명: 스테이 머뭄, 지역: 군산...",
|
||||||
|
"lyric_result": "인스타 감성의 스테이 머뭄\n군산 신흥동 말랭이 마을에서\n여유로운 하루를 보내며\n추억을 만들어가요",
|
||||||
id: int = Field(..., description="가사 ID")
|
"created_at": "2024-01-15T12:00:00",
|
||||||
task_id: str = Field(..., description="작업 고유 식별자")
|
}
|
||||||
project_id: int = Field(..., description="프로젝트 ID")
|
}
|
||||||
status: str = Field(..., description="처리 상태")
|
)
|
||||||
lyric_prompt: str = Field(..., description="가사 생성 프롬프트")
|
|
||||||
lyric_result: Optional[str] = Field(None, description="생성된 가사")
|
id: int = Field(..., description="가사 ID")
|
||||||
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
task_id: str = Field(..., description="작업 고유 식별자")
|
||||||
|
project_id: int = Field(..., description="프로젝트 ID")
|
||||||
|
status: str = Field(..., description="처리 상태")
|
||||||
class LyricListItem(BaseModel):
|
lyric_prompt: str = Field(..., description="가사 생성 프롬프트")
|
||||||
"""가사 목록 아이템 스키마
|
lyric_result: Optional[str] = Field(None, description="생성된 가사")
|
||||||
|
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
||||||
Usage:
|
|
||||||
Used as individual items in paginated lyric list responses.
|
|
||||||
"""
|
class LyricListItem(BaseModel):
|
||||||
|
"""가사 목록 아이템 스키마
|
||||||
id: int = Field(..., description="가사 ID")
|
|
||||||
task_id: str = Field(..., description="작업 고유 식별자")
|
Usage:
|
||||||
status: str = Field(..., description="처리 상태")
|
Used as individual items in paginated lyric list responses.
|
||||||
lyric_result: Optional[str] = Field(None, description="생성된 가사 (미리보기)")
|
"""
|
||||||
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
|
||||||
|
model_config = ConfigDict(
|
||||||
|
json_schema_extra={
|
||||||
|
"example": {
|
||||||
|
"id": 1,
|
||||||
|
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
||||||
|
"status": "completed",
|
||||||
|
"lyric_result": "인스타 감성의 스테이 머뭄\n군산 신흥동 말랭이 마을에서...",
|
||||||
|
"created_at": "2024-01-15T12:00:00",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
id: int = Field(..., description="가사 ID")
|
||||||
|
task_id: str = Field(..., description="작업 고유 식별자")
|
||||||
|
status: str = Field(..., description="처리 상태")
|
||||||
|
lyric_result: Optional[str] = Field(None, description="생성된 가사 (미리보기)")
|
||||||
|
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
||||||
@@ -1,91 +1,91 @@
|
|||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
|
|
||||||
from fastapi import Request
|
from fastapi import Request
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class StoreData:
|
class StoreData:
|
||||||
id: int
|
id: int
|
||||||
created_at: datetime
|
created_at: datetime
|
||||||
store_name: str
|
store_name: str
|
||||||
store_category: str | None = None
|
store_category: str | None = None
|
||||||
store_region: str | None = None
|
store_region: str | None = None
|
||||||
store_address: str | None = None
|
store_address: str | None = None
|
||||||
store_phone_number: str | None = None
|
store_phone_number: str | None = None
|
||||||
store_info: str | None = None
|
store_info: str | None = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class AttributeData:
|
class AttributeData:
|
||||||
id: int
|
id: int
|
||||||
attr_category: str
|
attr_category: str
|
||||||
attr_value: str
|
attr_value: str
|
||||||
created_at: datetime
|
created_at: datetime
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class SongSampleData:
|
class SongSampleData:
|
||||||
id: int
|
id: int
|
||||||
ai: str
|
ai: str
|
||||||
ai_model: str
|
ai_model: str
|
||||||
sample_song: str
|
sample_song: str
|
||||||
season: str | None = None
|
season: str | None = None
|
||||||
num_of_people: int | None = None
|
num_of_people: int | None = None
|
||||||
people_category: str | None = None
|
people_category: str | None = None
|
||||||
genre: str | None = None
|
genre: str | None = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class PromptTemplateData:
|
class PromptTemplateData:
|
||||||
id: int
|
id: int
|
||||||
prompt: str
|
prompt: str
|
||||||
description: str | None = None
|
description: str | None = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class SongFormData:
|
class SongFormData:
|
||||||
store_name: str
|
store_name: str
|
||||||
store_id: str
|
store_id: str
|
||||||
prompts: str
|
prompts: str
|
||||||
attributes: Dict[str, str] = field(default_factory=dict)
|
attributes: Dict[str, str] = field(default_factory=dict)
|
||||||
attributes_str: str = ""
|
attributes_str: str = ""
|
||||||
lyrics_ids: List[int] = field(default_factory=list)
|
lyrics_ids: List[int] = field(default_factory=list)
|
||||||
llm_model: str = "gpt-4o"
|
llm_model: str = "gpt-5-mini"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def from_form(cls, request: Request):
|
async def from_form(cls, request: Request):
|
||||||
"""Request의 form 데이터로부터 dataclass 인스턴스 생성"""
|
"""Request의 form 데이터로부터 dataclass 인스턴스 생성"""
|
||||||
form_data = await request.form()
|
form_data = await request.form()
|
||||||
|
|
||||||
# 고정 필드명들
|
# 고정 필드명들
|
||||||
fixed_keys = {"store_info_name", "store_id", "llm_model", "prompts"}
|
fixed_keys = {"store_info_name", "store_id", "llm_model", "prompts"}
|
||||||
|
|
||||||
# lyrics-{id} 형태의 모든 키를 찾아서 ID 추출
|
# lyrics-{id} 형태의 모든 키를 찾아서 ID 추출
|
||||||
lyrics_ids = []
|
lyrics_ids = []
|
||||||
attributes = {}
|
attributes = {}
|
||||||
|
|
||||||
for key, value in form_data.items():
|
for key, value in form_data.items():
|
||||||
if key.startswith("lyrics-"):
|
if key.startswith("lyrics-"):
|
||||||
lyrics_id = key.split("-")[1]
|
lyrics_id = key.split("-")[1]
|
||||||
lyrics_ids.append(int(lyrics_id))
|
lyrics_ids.append(int(lyrics_id))
|
||||||
elif key not in fixed_keys:
|
elif key not in fixed_keys:
|
||||||
attributes[key] = value
|
attributes[key] = value
|
||||||
|
|
||||||
# attributes를 문자열로 변환
|
# attributes를 문자열로 변환
|
||||||
attributes_str = (
|
attributes_str = (
|
||||||
"\r\n\r\n".join([f"{key} : {value}" for key, value in attributes.items()])
|
"\r\n\r\n".join([f"{key} : {value}" for key, value in attributes.items()])
|
||||||
if attributes
|
if attributes
|
||||||
else ""
|
else ""
|
||||||
)
|
)
|
||||||
|
|
||||||
return cls(
|
return cls(
|
||||||
store_name=form_data.get("store_info_name", ""),
|
store_name=form_data.get("store_info_name", ""),
|
||||||
store_id=form_data.get("store_id", ""),
|
store_id=form_data.get("store_id", ""),
|
||||||
attributes=attributes,
|
attributes=attributes,
|
||||||
attributes_str=attributes_str,
|
attributes_str=attributes_str,
|
||||||
lyrics_ids=lyrics_ids,
|
lyrics_ids=lyrics_ids,
|
||||||
llm_model=form_data.get("llm_model", "gpt-4o"),
|
llm_model=form_data.get("llm_model", "gpt-5-mini"),
|
||||||
prompts=form_data.get("prompts", ""),
|
prompts=form_data.get("prompts", ""),
|
||||||
)
|
)
|
||||||
@@ -1,24 +1,24 @@
|
|||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlmodel import SQLModel
|
from sqlmodel import SQLModel
|
||||||
|
|
||||||
|
|
||||||
class BaseService:
|
class BaseService:
|
||||||
def __init__(self, model, session: AsyncSession):
|
def __init__(self, model, session: AsyncSession):
|
||||||
self.model = model
|
self.model = model
|
||||||
self.session = session
|
self.session = session
|
||||||
|
|
||||||
async def _get(self, id: UUID):
|
async def _get(self, id: UUID):
|
||||||
return await self.session.get(self.model, id)
|
return await self.session.get(self.model, id)
|
||||||
|
|
||||||
async def _add(self, entity):
|
async def _add(self, entity):
|
||||||
self.session.add(entity)
|
self.session.add(entity)
|
||||||
await self.session.commit()
|
await self.session.commit()
|
||||||
await self.session.refresh(entity)
|
await self.session.refresh(entity)
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
async def _update(self, entity):
|
async def _update(self, entity):
|
||||||
return await self._add(entity)
|
return await self._add(entity)
|
||||||
|
|
||||||
async def _delete(self, entity):
|
async def _delete(self, entity):
|
||||||
await self.session.delete(entity)
|
await self.session.delete(entity)
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
"""
|
||||||
|
Lyric Background Tasks
|
||||||
|
|
||||||
|
가사 생성 관련 백그라운드 태스크를 정의합니다.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
from sqlalchemy import select
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
|
||||||
|
from app.database.session import BackgroundSessionLocal
|
||||||
|
from app.lyric.models import Lyric
|
||||||
|
from app.utils.chatgpt_prompt import ChatgptService
|
||||||
|
|
||||||
|
# 로거 설정
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def _update_lyric_status(
|
||||||
|
task_id: str,
|
||||||
|
status: str,
|
||||||
|
result: str | None = None,
|
||||||
|
) -> bool:
|
||||||
|
"""Lyric 테이블의 상태를 업데이트합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task_id: 프로젝트 task_id
|
||||||
|
status: 변경할 상태 ("processing", "completed", "failed")
|
||||||
|
result: 가사 결과 또는 에러 메시지
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 업데이트 성공 여부
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
async with BackgroundSessionLocal() as session:
|
||||||
|
query_result = await session.execute(
|
||||||
|
select(Lyric)
|
||||||
|
.where(Lyric.task_id == task_id)
|
||||||
|
.order_by(Lyric.created_at.desc())
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
lyric = query_result.scalar_one_or_none()
|
||||||
|
|
||||||
|
if lyric:
|
||||||
|
lyric.status = status
|
||||||
|
if result is not None:
|
||||||
|
lyric.lyric_result = result
|
||||||
|
await session.commit()
|
||||||
|
logger.info(f"[Lyric] Status updated - task_id: {task_id}, status: {status}")
|
||||||
|
print(f"[Lyric] Status updated - task_id: {task_id}, status: {status}")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
logger.warning(f"[Lyric] NOT FOUND in DB - task_id: {task_id}")
|
||||||
|
print(f"[Lyric] NOT FOUND in DB - task_id: {task_id}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
logger.error(f"[Lyric] DB Error while updating status - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[Lyric] DB Error while updating status - task_id: {task_id}, error: {e}")
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[Lyric] Unexpected error while updating status - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[Lyric] Unexpected error while updating status - task_id: {task_id}, error: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def generate_lyric_background(
|
||||||
|
task_id: str,
|
||||||
|
prompt: str,
|
||||||
|
language: str,
|
||||||
|
) -> None:
|
||||||
|
"""백그라운드에서 ChatGPT를 통해 가사를 생성하고 Lyric 테이블을 업데이트합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task_id: 프로젝트 task_id
|
||||||
|
prompt: ChatGPT에 전달할 프롬프트
|
||||||
|
language: 가사 언어
|
||||||
|
"""
|
||||||
|
import time
|
||||||
|
|
||||||
|
task_start = time.perf_counter()
|
||||||
|
logger.info(f"[generate_lyric_background] START - task_id: {task_id}")
|
||||||
|
print(f"[generate_lyric_background] ========== START ==========")
|
||||||
|
print(f"[generate_lyric_background] task_id: {task_id}")
|
||||||
|
print(f"[generate_lyric_background] language: {language}")
|
||||||
|
print(f"[generate_lyric_background] prompt length: {len(prompt)}자")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# ========== Step 1: ChatGPT 서비스 초기화 ==========
|
||||||
|
step1_start = time.perf_counter()
|
||||||
|
print(f"[generate_lyric_background] Step 1: ChatGPT 서비스 초기화...")
|
||||||
|
|
||||||
|
service = ChatgptService(
|
||||||
|
customer_name="", # 프롬프트가 이미 생성되었으므로 빈 값
|
||||||
|
region="",
|
||||||
|
detail_region_info="",
|
||||||
|
language=language,
|
||||||
|
)
|
||||||
|
|
||||||
|
step1_elapsed = (time.perf_counter() - step1_start) * 1000
|
||||||
|
print(f"[generate_lyric_background] Step 1 완료 ({step1_elapsed:.1f}ms)")
|
||||||
|
|
||||||
|
# ========== Step 2: ChatGPT API 호출 (가사 생성) ==========
|
||||||
|
step2_start = time.perf_counter()
|
||||||
|
logger.info(f"[generate_lyric_background] Step 2: ChatGPT API 호출 시작 - task_id: {task_id}")
|
||||||
|
print(f"[generate_lyric_background] Step 2: ChatGPT API 호출 시작...")
|
||||||
|
|
||||||
|
result = await service.generate(prompt=prompt)
|
||||||
|
|
||||||
|
step2_elapsed = (time.perf_counter() - step2_start) * 1000
|
||||||
|
logger.info(f"[generate_lyric_background] Step 2 완료 - 응답 {len(result)}자 ({step2_elapsed:.1f}ms)")
|
||||||
|
print(f"[generate_lyric_background] Step 2 완료 - 응답 {len(result)}자 ({step2_elapsed:.1f}ms)")
|
||||||
|
|
||||||
|
# ========== Step 3: DB 상태 업데이트 ==========
|
||||||
|
step3_start = time.perf_counter()
|
||||||
|
print(f"[generate_lyric_background] Step 3: DB 상태 업데이트...")
|
||||||
|
|
||||||
|
await _update_lyric_status(task_id, "completed", result)
|
||||||
|
|
||||||
|
step3_elapsed = (time.perf_counter() - step3_start) * 1000
|
||||||
|
print(f"[generate_lyric_background] Step 3 완료 ({step3_elapsed:.1f}ms)")
|
||||||
|
|
||||||
|
# ========== 완료 ==========
|
||||||
|
total_elapsed = (time.perf_counter() - task_start) * 1000
|
||||||
|
logger.info(f"[generate_lyric_background] SUCCESS - task_id: {task_id}, 총 소요시간: {total_elapsed:.1f}ms")
|
||||||
|
print(f"[generate_lyric_background] ========== SUCCESS ==========")
|
||||||
|
print(f"[generate_lyric_background] 총 소요시간: {total_elapsed:.1f}ms")
|
||||||
|
print(f"[generate_lyric_background] - Step 1 (서비스 초기화): {step1_elapsed:.1f}ms")
|
||||||
|
print(f"[generate_lyric_background] - Step 2 (GPT API 호출): {step2_elapsed:.1f}ms")
|
||||||
|
print(f"[generate_lyric_background] - Step 3 (DB 업데이트): {step3_elapsed:.1f}ms")
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
elapsed = (time.perf_counter() - task_start) * 1000
|
||||||
|
logger.error(f"[generate_lyric_background] DB ERROR - task_id: {task_id}, error: {e} ({elapsed:.1f}ms)")
|
||||||
|
print(f"[generate_lyric_background] DB ERROR - {e} ({elapsed:.1f}ms)")
|
||||||
|
traceback.print_exc()
|
||||||
|
await _update_lyric_status(task_id, "failed", f"Database Error: {str(e)}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
elapsed = (time.perf_counter() - task_start) * 1000
|
||||||
|
logger.error(f"[generate_lyric_background] EXCEPTION - task_id: {task_id}, error: {e} ({elapsed:.1f}ms)")
|
||||||
|
print(f"[generate_lyric_background] EXCEPTION - {e} ({elapsed:.1f}ms)")
|
||||||
|
traceback.print_exc()
|
||||||
|
await _update_lyric_status(task_id, "failed", f"Error: {str(e)}")
|
||||||
@@ -1,69 +1,69 @@
|
|||||||
from sqladmin import ModelView
|
from sqladmin import ModelView
|
||||||
|
|
||||||
from app.song.models import Song
|
from app.song.models import Song
|
||||||
|
|
||||||
|
|
||||||
class SongAdmin(ModelView, model=Song):
|
class SongAdmin(ModelView, model=Song):
|
||||||
name = "노래"
|
name = "노래"
|
||||||
name_plural = "노래 목록"
|
name_plural = "노래 목록"
|
||||||
icon = "fa-solid fa-headphones"
|
icon = "fa-solid fa-headphones"
|
||||||
category = "노래 관리"
|
category = "노래 관리"
|
||||||
page_size = 20
|
page_size = 20
|
||||||
|
|
||||||
column_list = [
|
column_list = [
|
||||||
"id",
|
"id",
|
||||||
"project_id",
|
"project_id",
|
||||||
"lyric_id",
|
"lyric_id",
|
||||||
"task_id",
|
"task_id",
|
||||||
"suno_task_id",
|
"suno_task_id",
|
||||||
"status",
|
"status",
|
||||||
"language",
|
"language",
|
||||||
"created_at",
|
"created_at",
|
||||||
]
|
]
|
||||||
|
|
||||||
column_details_list = [
|
column_details_list = [
|
||||||
"id",
|
"id",
|
||||||
"project_id",
|
"project_id",
|
||||||
"lyric_id",
|
"lyric_id",
|
||||||
"task_id",
|
"task_id",
|
||||||
"suno_task_id",
|
"suno_task_id",
|
||||||
"status",
|
"status",
|
||||||
"language",
|
"language",
|
||||||
"song_prompt",
|
"song_prompt",
|
||||||
"song_result_url",
|
"song_result_url",
|
||||||
"created_at",
|
"created_at",
|
||||||
]
|
]
|
||||||
|
|
||||||
# 폼(생성/수정)에서 제외
|
# 폼(생성/수정)에서 제외
|
||||||
form_excluded_columns = ["created_at", "videos"]
|
form_excluded_columns = ["created_at", "videos"]
|
||||||
|
|
||||||
column_searchable_list = [
|
column_searchable_list = [
|
||||||
Song.task_id,
|
Song.task_id,
|
||||||
Song.suno_task_id,
|
Song.suno_task_id,
|
||||||
Song.status,
|
Song.status,
|
||||||
Song.language,
|
Song.language,
|
||||||
]
|
]
|
||||||
|
|
||||||
column_default_sort = (Song.created_at, True) # True: DESC (최신순)
|
column_default_sort = (Song.created_at, True) # True: DESC (최신순)
|
||||||
|
|
||||||
column_sortable_list = [
|
column_sortable_list = [
|
||||||
Song.id,
|
Song.id,
|
||||||
Song.project_id,
|
Song.project_id,
|
||||||
Song.lyric_id,
|
Song.lyric_id,
|
||||||
Song.status,
|
Song.status,
|
||||||
Song.language,
|
Song.language,
|
||||||
Song.created_at,
|
Song.created_at,
|
||||||
]
|
]
|
||||||
|
|
||||||
column_labels = {
|
column_labels = {
|
||||||
"id": "ID",
|
"id": "ID",
|
||||||
"project_id": "프로젝트 ID",
|
"project_id": "프로젝트 ID",
|
||||||
"lyric_id": "가사 ID",
|
"lyric_id": "가사 ID",
|
||||||
"task_id": "작업 ID",
|
"task_id": "작업 ID",
|
||||||
"suno_task_id": "Suno 작업 ID",
|
"suno_task_id": "Suno 작업 ID",
|
||||||
"status": "상태",
|
"status": "상태",
|
||||||
"language": "언어",
|
"language": "언어",
|
||||||
"song_prompt": "프롬프트",
|
"song_prompt": "프롬프트",
|
||||||
"song_result_url": "결과 URL",
|
"song_result_url": "결과 URL",
|
||||||
"created_at": "생성일시",
|
"created_at": "생성일시",
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from app.database.session import get_session
|
from app.database.session import get_session
|
||||||
|
|
||||||
SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
||||||
@@ -1,147 +1,152 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import TYPE_CHECKING, List, Optional
|
from typing import TYPE_CHECKING, List, Optional
|
||||||
|
|
||||||
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, func
|
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, func
|
||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
from app.database.session import Base
|
from app.database.session import Base
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from app.home.models import Project
|
from app.home.models import Project
|
||||||
from app.lyric.models import Lyric
|
from app.lyric.models import Lyric
|
||||||
from app.video.models import Video
|
from app.video.models import Video
|
||||||
|
|
||||||
|
|
||||||
class Song(Base):
|
class Song(Base):
|
||||||
"""
|
"""
|
||||||
노래 테이블
|
노래 테이블
|
||||||
|
|
||||||
AI를 통해 생성된 노래 정보를 저장합니다.
|
AI를 통해 생성된 노래 정보를 저장합니다.
|
||||||
가사를 기반으로 생성됩니다.
|
가사를 기반으로 생성됩니다.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
id: 고유 식별자 (자동 증가)
|
id: 고유 식별자 (자동 증가)
|
||||||
project_id: 연결된 Project의 id (외래키)
|
project_id: 연결된 Project의 id (외래키)
|
||||||
lyric_id: 연결된 Lyric의 id (외래키)
|
lyric_id: 연결된 Lyric의 id (외래키)
|
||||||
task_id: 노래 생성 작업의 고유 식별자 (UUID 형식)
|
task_id: 노래 생성 작업의 고유 식별자 (UUID 형식)
|
||||||
suno_task_id: Suno API 작업 고유 식별자 (선택)
|
suno_task_id: Suno API 작업 고유 식별자 (선택)
|
||||||
status: 처리 상태 (pending, processing, completed, failed 등)
|
status: 처리 상태 (pending, processing, completed, failed 등)
|
||||||
song_prompt: 노래 생성에 사용된 프롬프트
|
song_prompt: 노래 생성에 사용된 프롬프트
|
||||||
song_result_url: 생성 결과 URL (선택)
|
song_result_url: 생성 결과 URL (선택)
|
||||||
language: 출력 언어
|
language: 출력 언어
|
||||||
created_at: 생성 일시 (자동 설정)
|
created_at: 생성 일시 (자동 설정)
|
||||||
|
|
||||||
Relationships:
|
Relationships:
|
||||||
project: 연결된 Project
|
project: 연결된 Project
|
||||||
lyric: 연결된 Lyric
|
lyric: 연결된 Lyric
|
||||||
videos: 이 노래를 사용한 영상 결과 목록
|
videos: 이 노래를 사용한 영상 결과 목록
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__tablename__ = "song"
|
__tablename__ = "song"
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
{
|
{
|
||||||
"mysql_engine": "InnoDB",
|
"mysql_engine": "InnoDB",
|
||||||
"mysql_charset": "utf8mb4",
|
"mysql_charset": "utf8mb4",
|
||||||
"mysql_collate": "utf8mb4_unicode_ci",
|
"mysql_collate": "utf8mb4_unicode_ci",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(
|
id: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
autoincrement=True,
|
autoincrement=True,
|
||||||
comment="고유 식별자",
|
comment="고유 식별자",
|
||||||
)
|
)
|
||||||
|
|
||||||
project_id: Mapped[int] = mapped_column(
|
project_id: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
ForeignKey("project.id", ondelete="CASCADE"),
|
ForeignKey("project.id", ondelete="CASCADE"),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True,
|
index=True,
|
||||||
comment="연결된 Project의 id",
|
comment="연결된 Project의 id",
|
||||||
)
|
)
|
||||||
|
|
||||||
lyric_id: Mapped[int] = mapped_column(
|
lyric_id: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
ForeignKey("lyric.id", ondelete="CASCADE"),
|
ForeignKey("lyric.id", ondelete="CASCADE"),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True,
|
index=True,
|
||||||
comment="연결된 Lyric의 id",
|
comment="연결된 Lyric의 id",
|
||||||
)
|
)
|
||||||
|
|
||||||
task_id: Mapped[str] = mapped_column(
|
task_id: Mapped[str] = mapped_column(
|
||||||
String(36),
|
String(36),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
comment="노래 생성 작업 고유 식별자 (UUID)",
|
comment="노래 생성 작업 고유 식별자 (UUID)",
|
||||||
)
|
)
|
||||||
|
|
||||||
suno_task_id: Mapped[Optional[str]] = mapped_column(
|
suno_task_id: Mapped[Optional[str]] = mapped_column(
|
||||||
String(64),
|
String(64),
|
||||||
nullable=True,
|
nullable=True,
|
||||||
comment="Suno API 작업 고유 식별자",
|
comment="Suno API 작업 고유 식별자",
|
||||||
)
|
)
|
||||||
|
|
||||||
status: Mapped[str] = mapped_column(
|
status: Mapped[str] = mapped_column(
|
||||||
String(50),
|
String(50),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
comment="처리 상태 (processing, completed, failed)",
|
comment="처리 상태 (processing, completed, failed)",
|
||||||
)
|
)
|
||||||
|
|
||||||
song_prompt: Mapped[str] = mapped_column(
|
song_prompt: Mapped[str] = mapped_column(
|
||||||
Text,
|
Text,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
comment="노래 생성에 사용된 프롬프트",
|
comment="노래 생성에 사용된 프롬프트",
|
||||||
)
|
)
|
||||||
|
|
||||||
song_result_url: Mapped[Optional[str]] = mapped_column(
|
song_result_url: Mapped[Optional[str]] = mapped_column(
|
||||||
String(2048),
|
String(2048),
|
||||||
nullable=True,
|
nullable=True,
|
||||||
comment="노래 결과 URL",
|
comment="노래 결과 URL",
|
||||||
)
|
)
|
||||||
|
|
||||||
language: Mapped[str] = mapped_column(
|
duration: Mapped[Optional[float]] = mapped_column(
|
||||||
String(50),
|
nullable=True,
|
||||||
nullable=False,
|
comment="노래 재생 시간 (초)",
|
||||||
default="Korean",
|
)
|
||||||
comment="출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
|
||||||
)
|
language: Mapped[str] = mapped_column(
|
||||||
|
String(50),
|
||||||
created_at: Mapped[datetime] = mapped_column(
|
nullable=False,
|
||||||
DateTime,
|
default="Korean",
|
||||||
nullable=False,
|
comment="출력 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
||||||
server_default=func.now(),
|
)
|
||||||
comment="생성 일시",
|
|
||||||
)
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
|
DateTime,
|
||||||
# Relationships
|
nullable=False,
|
||||||
project: Mapped["Project"] = relationship(
|
server_default=func.now(),
|
||||||
"Project",
|
comment="생성 일시",
|
||||||
back_populates="songs",
|
)
|
||||||
)
|
|
||||||
|
# Relationships
|
||||||
lyric: Mapped["Lyric"] = relationship(
|
project: Mapped["Project"] = relationship(
|
||||||
"Lyric",
|
"Project",
|
||||||
back_populates="songs",
|
back_populates="songs",
|
||||||
)
|
)
|
||||||
|
|
||||||
videos: Mapped[List["Video"]] = relationship(
|
lyric: Mapped["Lyric"] = relationship(
|
||||||
"Video",
|
"Lyric",
|
||||||
back_populates="song",
|
back_populates="songs",
|
||||||
cascade="all, delete-orphan",
|
)
|
||||||
lazy="selectin",
|
|
||||||
)
|
videos: Mapped[List["Video"]] = relationship(
|
||||||
|
"Video",
|
||||||
def __repr__(self) -> str:
|
back_populates="song",
|
||||||
def truncate(value: str | None, max_len: int = 10) -> str:
|
cascade="all, delete-orphan",
|
||||||
if value is None:
|
lazy="selectin",
|
||||||
return "None"
|
)
|
||||||
return (value[:max_len] + "...") if len(value) > max_len else value
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
return (
|
def truncate(value: str | None, max_len: int = 10) -> str:
|
||||||
f"<Song("
|
if value is None:
|
||||||
f"id={self.id}, "
|
return "None"
|
||||||
f"task_id='{truncate(self.task_id)}', "
|
return (value[:max_len] + "...") if len(value) > max_len else value
|
||||||
f"status='{self.status}'"
|
|
||||||
f")>"
|
return (
|
||||||
)
|
f"<Song("
|
||||||
|
f"id={self.id}, "
|
||||||
|
f"task_id='{truncate(self.task_id)}', "
|
||||||
|
f"status='{self.status}'"
|
||||||
|
f")>"
|
||||||
|
)
|
||||||
@@ -1,374 +1,374 @@
|
|||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from fastapi import Request
|
from fastapi import Request
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
# Pydantic Schemas for Song Generation API
|
# Pydantic Schemas for Song Generation API
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
|
|
||||||
|
|
||||||
class GenerateSongRequest(BaseModel):
|
class GenerateSongRequest(BaseModel):
|
||||||
"""노래 생성 요청 스키마
|
"""노래 생성 요청 스키마
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
POST /song/generate/{task_id}
|
POST /song/generate/{task_id}
|
||||||
Request body for generating a song via Suno API.
|
Request body for generating a song via Suno API.
|
||||||
|
|
||||||
Example Request:
|
Example Request:
|
||||||
{
|
{
|
||||||
"lyrics": "인스타 감성의 스테이 머뭄...",
|
"lyrics": "인스타 감성의 스테이 머뭄...",
|
||||||
"genre": "k-pop",
|
"genre": "k-pop",
|
||||||
"language": "Korean"
|
"language": "Korean"
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
model_config = {
|
model_config = {
|
||||||
"json_schema_extra": {
|
"json_schema_extra": {
|
||||||
"example": {
|
"example": {
|
||||||
"lyrics": "인스타 감성의 스테이 머뭄, 머물러봐요 \n군산 신흥동 말랭이 마을의 마음 힐링 \n사진같은 하루, 여행의 시작 \n보석 같은 이곳은 감성 숙소의 느낌 \n\n인근 명소와 아름다움이 가득한 거리 \n힐링의 바람과 여행의 추억 \n글로벌 감성의 스테이 머뭄, 인스타 감성 \n사진으로 남기고 싶은 그 순간들이 되어줘요",
|
"lyrics": "인스타 감성의 스테이 머뭄, 머물러봐요 \n군산 신흥동 말랭이 마을의 마음 힐링 \n사진같은 하루, 여행의 시작 \n보석 같은 이곳은 감성 숙소의 느낌 \n\n인근 명소와 아름다움이 가득한 거리 \n힐링의 바람과 여행의 추억 \n글로벌 감성의 스테이 머뭄, 인스타 감성 \n사진으로 남기고 싶은 그 순간들이 되어줘요",
|
||||||
"genre": "k-pop",
|
"genre": "k-pop",
|
||||||
"language": "Korean",
|
"language": "Korean",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lyrics: str = Field(..., description="노래에 사용할 가사")
|
lyrics: str = Field(..., description="노래에 사용할 가사")
|
||||||
genre: str = Field(
|
genre: str = Field(
|
||||||
...,
|
...,
|
||||||
description="음악 장르 (K-Pop, Pop, R&B, Hip-Hop, Ballad, EDM, Rock, Jazz 등)",
|
description="음악 장르 (K-Pop, Pop, R&B, Hip-Hop, Ballad, EDM, Rock, Jazz 등)",
|
||||||
)
|
)
|
||||||
language: str = Field(
|
language: str = Field(
|
||||||
default="Korean",
|
default="Korean",
|
||||||
description="노래 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
description="노래 언어 (Korean, English, Chinese, Japanese, Thai, Vietnamese)",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class GenerateSongResponse(BaseModel):
|
class GenerateSongResponse(BaseModel):
|
||||||
"""노래 생성 응답 스키마
|
"""노래 생성 응답 스키마
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
POST /song/generate/{task_id}
|
POST /song/generate/{task_id}
|
||||||
Returns the task IDs for tracking song generation.
|
Returns the task IDs for tracking song generation.
|
||||||
|
|
||||||
Note:
|
Note:
|
||||||
실패 조건:
|
실패 조건:
|
||||||
- task_id에 해당하는 Project가 없는 경우 (404 HTTPException)
|
- task_id에 해당하는 Project가 없는 경우 (404 HTTPException)
|
||||||
- task_id에 해당하는 Lyric이 없는 경우 (404 HTTPException)
|
- task_id에 해당하는 Lyric이 없는 경우 (404 HTTPException)
|
||||||
- Suno API 호출 실패
|
- Suno API 호출 실패
|
||||||
|
|
||||||
Example Response (Success):
|
Example Response (Success):
|
||||||
{
|
{
|
||||||
"success": true,
|
"success": true,
|
||||||
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
||||||
"suno_task_id": "abc123...",
|
"suno_task_id": "abc123...",
|
||||||
"message": "노래 생성 요청이 접수되었습니다. suno_task_id로 상태를 조회하세요.",
|
"message": "노래 생성 요청이 접수되었습니다. suno_task_id로 상태를 조회하세요.",
|
||||||
"error_message": null
|
"error_message": null
|
||||||
}
|
}
|
||||||
|
|
||||||
Example Response (Failure):
|
Example Response (Failure):
|
||||||
{
|
{
|
||||||
"success": false,
|
"success": false,
|
||||||
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
||||||
"suno_task_id": null,
|
"suno_task_id": null,
|
||||||
"message": "노래 생성 요청에 실패했습니다.",
|
"message": "노래 생성 요청에 실패했습니다.",
|
||||||
"error_message": "Suno API connection error"
|
"error_message": "Suno API connection error"
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
success: bool = Field(..., description="요청 성공 여부")
|
success: bool = Field(..., description="요청 성공 여부")
|
||||||
task_id: Optional[str] = Field(None, description="내부 작업 ID (Project/Lyric task_id)")
|
task_id: Optional[str] = Field(None, description="내부 작업 ID (Project/Lyric task_id)")
|
||||||
suno_task_id: Optional[str] = Field(None, description="Suno API 작업 ID")
|
suno_task_id: Optional[str] = Field(None, description="Suno API 작업 ID")
|
||||||
message: str = Field(..., description="응답 메시지")
|
message: str = Field(..., description="응답 메시지")
|
||||||
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시)")
|
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시)")
|
||||||
|
|
||||||
|
|
||||||
class PollingSongRequest(BaseModel):
|
class PollingSongRequest(BaseModel):
|
||||||
"""노래 생성 상태 조회 요청 스키마 (Legacy)
|
"""노래 생성 상태 조회 요청 스키마 (Legacy)
|
||||||
|
|
||||||
Note:
|
Note:
|
||||||
현재 사용되지 않음. GET /song/status/{suno_task_id} 엔드포인트 사용.
|
현재 사용되지 않음. GET /song/status/{suno_task_id} 엔드포인트 사용.
|
||||||
|
|
||||||
Example Request:
|
Example Request:
|
||||||
{
|
{
|
||||||
"task_id": "abc123..."
|
"task_id": "abc123..."
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
task_id: str = Field(..., description="Suno 작업 ID")
|
task_id: str = Field(..., description="Suno 작업 ID")
|
||||||
|
|
||||||
|
|
||||||
class SongClipData(BaseModel):
|
class SongClipData(BaseModel):
|
||||||
"""생성된 노래 클립 정보"""
|
"""생성된 노래 클립 정보"""
|
||||||
|
|
||||||
id: Optional[str] = Field(None, description="클립 ID")
|
id: Optional[str] = Field(None, description="클립 ID")
|
||||||
audio_url: Optional[str] = Field(None, description="오디오 URL")
|
audio_url: Optional[str] = Field(None, description="오디오 URL")
|
||||||
stream_audio_url: Optional[str] = Field(None, description="스트리밍 오디오 URL")
|
stream_audio_url: Optional[str] = Field(None, description="스트리밍 오디오 URL")
|
||||||
image_url: Optional[str] = Field(None, description="이미지 URL")
|
image_url: Optional[str] = Field(None, description="이미지 URL")
|
||||||
title: Optional[str] = Field(None, description="곡 제목")
|
title: Optional[str] = Field(None, description="곡 제목")
|
||||||
status: Optional[str] = Field(None, description="클립 상태")
|
status: Optional[str] = Field(None, description="클립 상태")
|
||||||
duration: Optional[float] = Field(None, description="노래 길이 (초)")
|
duration: Optional[float] = Field(None, description="노래 길이 (초)")
|
||||||
|
|
||||||
|
|
||||||
class PollingSongResponse(BaseModel):
|
class PollingSongResponse(BaseModel):
|
||||||
"""노래 생성 상태 조회 응답 스키마
|
"""노래 생성 상태 조회 응답 스키마
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
GET /song/status/{suno_task_id}
|
GET /song/status/{suno_task_id}
|
||||||
Suno API 작업 상태를 조회합니다.
|
Suno API 작업 상태를 조회합니다.
|
||||||
|
|
||||||
Note:
|
Note:
|
||||||
상태 값:
|
상태 값:
|
||||||
- PENDING: 대기 중
|
- PENDING: 대기 중
|
||||||
- processing: 생성 중
|
- processing: 생성 중
|
||||||
- SUCCESS / TEXT_SUCCESS / complete: 생성 완료
|
- SUCCESS / TEXT_SUCCESS / complete: 생성 완료
|
||||||
- failed: 생성 실패
|
- failed: 생성 실패
|
||||||
- error: API 조회 오류
|
- error: API 조회 오류
|
||||||
|
|
||||||
SUCCESS 상태 시:
|
SUCCESS 상태 시:
|
||||||
- 백그라운드에서 MP3 파일 다운로드 시작
|
- 백그라운드에서 MP3 파일 다운로드 시작
|
||||||
- Song 테이블의 status를 completed로 업데이트
|
- Song 테이블의 status를 completed로 업데이트
|
||||||
- song_result_url에 로컬 파일 경로 저장
|
- song_result_url에 로컬 파일 경로 저장
|
||||||
|
|
||||||
Example Response (Processing):
|
Example Response (Processing):
|
||||||
{
|
{
|
||||||
"success": true,
|
"success": true,
|
||||||
"status": "processing",
|
"status": "processing",
|
||||||
"message": "노래를 생성하고 있습니다.",
|
"message": "노래를 생성하고 있습니다.",
|
||||||
"clips": null,
|
"clips": null,
|
||||||
"raw_response": {...},
|
"raw_response": {...},
|
||||||
"error_message": null
|
"error_message": null
|
||||||
}
|
}
|
||||||
|
|
||||||
Example Response (Success):
|
Example Response (Success):
|
||||||
{
|
{
|
||||||
"success": true,
|
"success": true,
|
||||||
"status": "SUCCESS",
|
"status": "SUCCESS",
|
||||||
"message": "노래 생성이 완료되었습니다.",
|
"message": "노래 생성이 완료되었습니다.",
|
||||||
"clips": [
|
"clips": [
|
||||||
{
|
{
|
||||||
"id": "clip-id",
|
"id": "clip-id",
|
||||||
"audio_url": "https://...",
|
"audio_url": "https://...",
|
||||||
"stream_audio_url": "https://...",
|
"stream_audio_url": "https://...",
|
||||||
"image_url": "https://...",
|
"image_url": "https://...",
|
||||||
"title": "Song Title",
|
"title": "Song Title",
|
||||||
"status": "complete",
|
"status": "complete",
|
||||||
"duration": 60.0
|
"duration": 60.0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"raw_response": {...},
|
"raw_response": {...},
|
||||||
"error_message": null
|
"error_message": null
|
||||||
}
|
}
|
||||||
|
|
||||||
Example Response (Failure):
|
Example Response (Failure):
|
||||||
{
|
{
|
||||||
"success": false,
|
"success": false,
|
||||||
"status": "error",
|
"status": "error",
|
||||||
"message": "상태 조회에 실패했습니다.",
|
"message": "상태 조회에 실패했습니다.",
|
||||||
"clips": null,
|
"clips": null,
|
||||||
"raw_response": null,
|
"raw_response": null,
|
||||||
"error_message": "ConnectionError: ..."
|
"error_message": "ConnectionError: ..."
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
success: bool = Field(..., description="조회 성공 여부")
|
success: bool = Field(..., description="조회 성공 여부")
|
||||||
status: Optional[str] = Field(
|
status: Optional[str] = Field(
|
||||||
None, description="작업 상태 (PENDING, processing, SUCCESS, failed)"
|
None, description="작업 상태 (PENDING, processing, SUCCESS, failed)"
|
||||||
)
|
)
|
||||||
message: str = Field(..., description="상태 메시지")
|
message: str = Field(..., description="상태 메시지")
|
||||||
clips: Optional[List[SongClipData]] = Field(None, description="생성된 노래 클립 목록")
|
clips: Optional[List[SongClipData]] = Field(None, description="생성된 노래 클립 목록")
|
||||||
raw_response: Optional[Dict[str, Any]] = Field(None, description="Suno API 원본 응답")
|
raw_response: Optional[Dict[str, Any]] = Field(None, description="Suno API 원본 응답")
|
||||||
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시)")
|
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시)")
|
||||||
|
|
||||||
|
|
||||||
class SongListItem(BaseModel):
|
class SongListItem(BaseModel):
|
||||||
"""노래 목록 아이템 스키마
|
"""노래 목록 아이템 스키마
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
GET /songs 응답의 개별 노래 정보
|
GET /songs 응답의 개별 노래 정보
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
{
|
{
|
||||||
"store_name": "스테이 머뭄",
|
"store_name": "스테이 머뭄",
|
||||||
"region": "군산",
|
"region": "군산",
|
||||||
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
||||||
"language": "Korean",
|
"language": "Korean",
|
||||||
"song_result_url": "http://localhost:8000/media/2025-01-15/스테이머뭄.mp3",
|
"song_result_url": "http://localhost:8000/media/2025-01-15/스테이머뭄.mp3",
|
||||||
"created_at": "2025-01-15T12:00:00"
|
"created_at": "2025-01-15T12:00:00"
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
store_name: Optional[str] = Field(None, description="업체명")
|
store_name: Optional[str] = Field(None, description="업체명")
|
||||||
region: Optional[str] = Field(None, description="지역명")
|
region: Optional[str] = Field(None, description="지역명")
|
||||||
task_id: str = Field(..., description="작업 고유 식별자")
|
task_id: str = Field(..., description="작업 고유 식별자")
|
||||||
language: Optional[str] = Field(None, description="언어")
|
language: Optional[str] = Field(None, description="언어")
|
||||||
song_result_url: Optional[str] = Field(None, description="노래 결과 URL")
|
song_result_url: Optional[str] = Field(None, description="노래 결과 URL")
|
||||||
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
||||||
|
|
||||||
|
|
||||||
class DownloadSongResponse(BaseModel):
|
class DownloadSongResponse(BaseModel):
|
||||||
"""노래 다운로드 응답 스키마
|
"""노래 다운로드 응답 스키마
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
GET /song/download/{task_id}
|
GET /song/download/{task_id}
|
||||||
Polls for song completion and returns project info with song URL.
|
Polls for song completion and returns project info with song URL.
|
||||||
|
|
||||||
Note:
|
Note:
|
||||||
상태 값:
|
상태 값:
|
||||||
- processing: 노래 생성 진행 중 (song_result_url은 null)
|
- processing: 노래 생성 진행 중 (song_result_url은 null)
|
||||||
- completed: 노래 생성 완료 (song_result_url 포함)
|
- completed: 노래 생성 완료 (song_result_url 포함)
|
||||||
- failed: 노래 생성 실패
|
- failed: 노래 생성 실패
|
||||||
- not_found: task_id에 해당하는 Song 없음
|
- not_found: task_id에 해당하는 Song 없음
|
||||||
- error: 조회 중 오류 발생
|
- error: 조회 중 오류 발생
|
||||||
|
|
||||||
Example Response (Processing):
|
Example Response (Processing):
|
||||||
{
|
{
|
||||||
"success": true,
|
"success": true,
|
||||||
"status": "processing",
|
"status": "processing",
|
||||||
"message": "노래 생성이 진행 중입니다.",
|
"message": "노래 생성이 진행 중입니다.",
|
||||||
"store_name": null,
|
"store_name": null,
|
||||||
"region": null,
|
"region": null,
|
||||||
"detail_region_info": null,
|
"detail_region_info": null,
|
||||||
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
||||||
"language": null,
|
"language": null,
|
||||||
"song_result_url": null,
|
"song_result_url": null,
|
||||||
"created_at": null,
|
"created_at": null,
|
||||||
"error_message": null
|
"error_message": null
|
||||||
}
|
}
|
||||||
|
|
||||||
Example Response (Completed):
|
Example Response (Completed):
|
||||||
{
|
{
|
||||||
"success": true,
|
"success": true,
|
||||||
"status": "completed",
|
"status": "completed",
|
||||||
"message": "노래 다운로드가 완료되었습니다.",
|
"message": "노래 다운로드가 완료되었습니다.",
|
||||||
"store_name": "스테이 머뭄",
|
"store_name": "스테이 머뭄",
|
||||||
"region": "군산",
|
"region": "군산",
|
||||||
"detail_region_info": "군산 신흥동 말랭이 마을",
|
"detail_region_info": "군산 신흥동 말랭이 마을",
|
||||||
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
||||||
"language": "Korean",
|
"language": "Korean",
|
||||||
"song_result_url": "http://localhost:8000/media/2025-01-15/스테이머뭄.mp3",
|
"song_result_url": "http://localhost:8000/media/2025-01-15/스테이머뭄.mp3",
|
||||||
"created_at": "2025-01-15T12:00:00",
|
"created_at": "2025-01-15T12:00:00",
|
||||||
"error_message": null
|
"error_message": null
|
||||||
}
|
}
|
||||||
|
|
||||||
Example Response (Not Found):
|
Example Response (Not Found):
|
||||||
{
|
{
|
||||||
"success": false,
|
"success": false,
|
||||||
"status": "not_found",
|
"status": "not_found",
|
||||||
"message": "task_id 'xxx'에 해당하는 Song을 찾을 수 없습니다.",
|
"message": "task_id 'xxx'에 해당하는 Song을 찾을 수 없습니다.",
|
||||||
"store_name": null,
|
"store_name": null,
|
||||||
"region": null,
|
"region": null,
|
||||||
"detail_region_info": null,
|
"detail_region_info": null,
|
||||||
"task_id": null,
|
"task_id": null,
|
||||||
"language": null,
|
"language": null,
|
||||||
"song_result_url": null,
|
"song_result_url": null,
|
||||||
"created_at": null,
|
"created_at": null,
|
||||||
"error_message": "Song not found"
|
"error_message": "Song not found"
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
success: bool = Field(..., description="다운로드 성공 여부")
|
success: bool = Field(..., description="다운로드 성공 여부")
|
||||||
status: str = Field(..., description="처리 상태 (processing, completed, failed, not_found, error)")
|
status: str = Field(..., description="처리 상태 (processing, completed, failed, not_found, error)")
|
||||||
message: str = Field(..., description="응답 메시지")
|
message: str = Field(..., description="응답 메시지")
|
||||||
store_name: Optional[str] = Field(None, description="업체명")
|
store_name: Optional[str] = Field(None, description="업체명")
|
||||||
region: Optional[str] = Field(None, description="지역명")
|
region: Optional[str] = Field(None, description="지역명")
|
||||||
detail_region_info: Optional[str] = Field(None, description="상세 지역 정보")
|
detail_region_info: Optional[str] = Field(None, description="상세 지역 정보")
|
||||||
task_id: Optional[str] = Field(None, description="작업 고유 식별자")
|
task_id: Optional[str] = Field(None, description="작업 고유 식별자")
|
||||||
language: Optional[str] = Field(None, description="언어")
|
language: Optional[str] = Field(None, description="언어")
|
||||||
song_result_url: Optional[str] = Field(None, description="노래 결과 URL")
|
song_result_url: Optional[str] = Field(None, description="노래 결과 URL")
|
||||||
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
||||||
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시)")
|
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시)")
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
# Dataclass Schemas (Legacy)
|
# Dataclass Schemas (Legacy)
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class StoreData:
|
class StoreData:
|
||||||
id: int
|
id: int
|
||||||
created_at: datetime
|
created_at: datetime
|
||||||
store_name: str
|
store_name: str
|
||||||
store_category: str | None = None
|
store_category: str | None = None
|
||||||
store_region: str | None = None
|
store_region: str | None = None
|
||||||
store_address: str | None = None
|
store_address: str | None = None
|
||||||
store_phone_number: str | None = None
|
store_phone_number: str | None = None
|
||||||
store_info: str | None = None
|
store_info: str | None = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class AttributeData:
|
class AttributeData:
|
||||||
id: int
|
id: int
|
||||||
attr_category: str
|
attr_category: str
|
||||||
attr_value: str
|
attr_value: str
|
||||||
created_at: datetime
|
created_at: datetime
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class SongSampleData:
|
class SongSampleData:
|
||||||
id: int
|
id: int
|
||||||
ai: str
|
ai: str
|
||||||
ai_model: str
|
ai_model: str
|
||||||
sample_song: str
|
sample_song: str
|
||||||
season: str | None = None
|
season: str | None = None
|
||||||
num_of_people: int | None = None
|
num_of_people: int | None = None
|
||||||
people_category: str | None = None
|
people_category: str | None = None
|
||||||
genre: str | None = None
|
genre: str | None = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class PromptTemplateData:
|
class PromptTemplateData:
|
||||||
id: int
|
id: int
|
||||||
prompt: str
|
prompt: str
|
||||||
description: str | None = None
|
description: str | None = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class SongFormData:
|
class SongFormData:
|
||||||
store_name: str
|
store_name: str
|
||||||
store_id: str
|
store_id: str
|
||||||
prompts: str
|
prompts: str
|
||||||
attributes: Dict[str, str] = field(default_factory=dict)
|
attributes: Dict[str, str] = field(default_factory=dict)
|
||||||
attributes_str: str = ""
|
attributes_str: str = ""
|
||||||
lyrics_ids: List[int] = field(default_factory=list)
|
lyrics_ids: List[int] = field(default_factory=list)
|
||||||
llm_model: str = "gpt-4o"
|
llm_model: str = "gpt-5-mini"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def from_form(cls, request: Request):
|
async def from_form(cls, request: Request):
|
||||||
"""Request의 form 데이터로부터 dataclass 인스턴스 생성"""
|
"""Request의 form 데이터로부터 dataclass 인스턴스 생성"""
|
||||||
form_data = await request.form()
|
form_data = await request.form()
|
||||||
|
|
||||||
# 고정 필드명들
|
# 고정 필드명들
|
||||||
fixed_keys = {"store_info_name", "store_id", "llm_model", "prompts"}
|
fixed_keys = {"store_info_name", "store_id", "llm_model", "prompts"}
|
||||||
|
|
||||||
# lyrics-{id} 형태의 모든 키를 찾아서 ID 추출
|
# lyrics-{id} 형태의 모든 키를 찾아서 ID 추출
|
||||||
lyrics_ids = []
|
lyrics_ids = []
|
||||||
attributes = {}
|
attributes = {}
|
||||||
|
|
||||||
for key, value in form_data.items():
|
for key, value in form_data.items():
|
||||||
if key.startswith("lyrics-"):
|
if key.startswith("lyrics-"):
|
||||||
lyrics_id = key.split("-")[1]
|
lyrics_id = key.split("-")[1]
|
||||||
lyrics_ids.append(int(lyrics_id))
|
lyrics_ids.append(int(lyrics_id))
|
||||||
elif key not in fixed_keys:
|
elif key not in fixed_keys:
|
||||||
attributes[key] = value
|
attributes[key] = value
|
||||||
|
|
||||||
# attributes를 문자열로 변환
|
# attributes를 문자열로 변환
|
||||||
attributes_str = (
|
attributes_str = (
|
||||||
"\r\n\r\n".join([f"{key} : {value}" for key, value in attributes.items()])
|
"\r\n\r\n".join([f"{key} : {value}" for key, value in attributes.items()])
|
||||||
if attributes
|
if attributes
|
||||||
else ""
|
else ""
|
||||||
)
|
)
|
||||||
|
|
||||||
return cls(
|
return cls(
|
||||||
store_name=form_data.get("store_info_name", ""),
|
store_name=form_data.get("store_info_name", ""),
|
||||||
store_id=form_data.get("store_id", ""),
|
store_id=form_data.get("store_id", ""),
|
||||||
attributes=attributes,
|
attributes=attributes,
|
||||||
attributes_str=attributes_str,
|
attributes_str=attributes_str,
|
||||||
lyrics_ids=lyrics_ids,
|
lyrics_ids=lyrics_ids,
|
||||||
llm_model=form_data.get("llm_model", "gpt-4o"),
|
llm_model=form_data.get("llm_model", "gpt-5-mini"),
|
||||||
prompts=form_data.get("prompts", ""),
|
prompts=form_data.get("prompts", ""),
|
||||||
)
|
)
|
||||||
@@ -1,24 +1,24 @@
|
|||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlmodel import SQLModel
|
from sqlmodel import SQLModel
|
||||||
|
|
||||||
|
|
||||||
class BaseService:
|
class BaseService:
|
||||||
def __init__(self, model, session: AsyncSession):
|
def __init__(self, model, session: AsyncSession):
|
||||||
self.model = model
|
self.model = model
|
||||||
self.session = session
|
self.session = session
|
||||||
|
|
||||||
async def _get(self, id: UUID):
|
async def _get(self, id: UUID):
|
||||||
return await self.session.get(self.model, id)
|
return await self.session.get(self.model, id)
|
||||||
|
|
||||||
async def _add(self, entity):
|
async def _add(self, entity):
|
||||||
self.session.add(entity)
|
self.session.add(entity)
|
||||||
await self.session.commit()
|
await self.session.commit()
|
||||||
await self.session.refresh(entity)
|
await self.session.refresh(entity)
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
async def _update(self, entity):
|
async def _update(self, entity):
|
||||||
return await self._add(entity)
|
return await self._add(entity)
|
||||||
|
|
||||||
async def _delete(self, entity):
|
async def _delete(self, entity):
|
||||||
await self.session.delete(entity)
|
await self.session.delete(entity)
|
||||||
@@ -1,101 +1,419 @@
|
|||||||
"""
|
"""
|
||||||
Song Background Tasks
|
Song Background Tasks
|
||||||
|
|
||||||
노래 생성 관련 백그라운드 태스크를 정의합니다.
|
노래 생성 관련 백그라운드 태스크를 정의합니다.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from datetime import date
|
import logging
|
||||||
from pathlib import Path
|
import traceback
|
||||||
|
from datetime import date
|
||||||
import aiofiles
|
from pathlib import Path
|
||||||
import httpx
|
|
||||||
from sqlalchemy import select
|
import aiofiles
|
||||||
|
import httpx
|
||||||
from app.database.session import AsyncSessionLocal
|
from sqlalchemy import select
|
||||||
from app.song.models import Song
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from app.utils.common import generate_task_id
|
|
||||||
from config import prj_settings
|
from app.database.session import BackgroundSessionLocal
|
||||||
|
from app.song.models import Song
|
||||||
|
from app.utils.common import generate_task_id
|
||||||
async def download_and_save_song(
|
from app.utils.upload_blob_as_request import AzureBlobUploader
|
||||||
task_id: str,
|
from config import prj_settings
|
||||||
audio_url: str,
|
|
||||||
store_name: str,
|
# 로거 설정
|
||||||
) -> None:
|
logger = logging.getLogger(__name__)
|
||||||
"""백그라운드에서 노래를 다운로드하고 Song 테이블을 업데이트합니다.
|
|
||||||
|
# HTTP 요청 설정
|
||||||
Args:
|
REQUEST_TIMEOUT = 120.0 # 초
|
||||||
task_id: 프로젝트 task_id
|
|
||||||
audio_url: 다운로드할 오디오 URL
|
|
||||||
store_name: 저장할 파일명에 사용할 업체명
|
async def _update_song_status(
|
||||||
"""
|
task_id: str,
|
||||||
print(f"[download_and_save_song] START - task_id: {task_id}, store_name: {store_name}")
|
status: str,
|
||||||
try:
|
song_url: str | None = None,
|
||||||
# 저장 경로 생성: media/{날짜}/{uuid7}/{store_name}.mp3
|
suno_task_id: str | None = None,
|
||||||
today = date.today().isoformat()
|
duration: float | None = None,
|
||||||
unique_id = await generate_task_id()
|
) -> bool:
|
||||||
# 파일명에 사용할 수 없는 문자 제거
|
"""Song 테이블의 상태를 업데이트합니다.
|
||||||
safe_store_name = "".join(
|
|
||||||
c for c in store_name if c.isalnum() or c in (" ", "_", "-")
|
Args:
|
||||||
).strip()
|
task_id: 프로젝트 task_id
|
||||||
safe_store_name = safe_store_name or "song"
|
status: 변경할 상태 ("processing", "completed", "failed")
|
||||||
file_name = f"{safe_store_name}.mp3"
|
song_url: 노래 URL
|
||||||
|
suno_task_id: Suno task ID (선택)
|
||||||
# 절대 경로 생성
|
duration: 노래 길이 (선택)
|
||||||
media_dir = Path("media") / today / unique_id
|
|
||||||
media_dir.mkdir(parents=True, exist_ok=True)
|
Returns:
|
||||||
file_path = media_dir / file_name
|
bool: 업데이트 성공 여부
|
||||||
print(f"[download_and_save_song] Directory created - path: {file_path}")
|
"""
|
||||||
|
try:
|
||||||
# 오디오 파일 다운로드
|
async with BackgroundSessionLocal() as session:
|
||||||
print(f"[download_and_save_song] Downloading audio - task_id: {task_id}, url: {audio_url}")
|
if suno_task_id:
|
||||||
async with httpx.AsyncClient() as client:
|
query_result = await session.execute(
|
||||||
response = await client.get(audio_url, timeout=60.0)
|
select(Song)
|
||||||
response.raise_for_status()
|
.where(Song.suno_task_id == suno_task_id)
|
||||||
|
.order_by(Song.created_at.desc())
|
||||||
async with aiofiles.open(str(file_path), "wb") as f:
|
.limit(1)
|
||||||
await f.write(response.content)
|
)
|
||||||
print(f"[download_and_save_song] File saved - task_id: {task_id}, path: {file_path}")
|
else:
|
||||||
|
query_result = await session.execute(
|
||||||
# 프론트엔드에서 접근 가능한 URL 생성
|
select(Song)
|
||||||
relative_path = f"/media/{today}/{unique_id}/{file_name}"
|
.where(Song.task_id == task_id)
|
||||||
base_url = f"http://{prj_settings.PROJECT_DOMAIN}"
|
.order_by(Song.created_at.desc())
|
||||||
file_url = f"{base_url}{relative_path}"
|
.limit(1)
|
||||||
print(f"[download_and_save_song] URL generated - task_id: {task_id}, url: {file_url}")
|
)
|
||||||
|
|
||||||
# Song 테이블 업데이트 (새 세션 사용)
|
song = query_result.scalar_one_or_none()
|
||||||
async with AsyncSessionLocal() as session:
|
|
||||||
# 여러 개 있을 경우 가장 최근 것 선택
|
if song:
|
||||||
result = await session.execute(
|
song.status = status
|
||||||
select(Song)
|
if song_url is not None:
|
||||||
.where(Song.task_id == task_id)
|
song.song_result_url = song_url
|
||||||
.order_by(Song.created_at.desc())
|
if duration is not None:
|
||||||
.limit(1)
|
song.duration = duration
|
||||||
)
|
await session.commit()
|
||||||
song = result.scalar_one_or_none()
|
logger.info(f"[Song] Status updated - task_id: {task_id}, status: {status}")
|
||||||
|
print(f"[Song] Status updated - task_id: {task_id}, status: {status}")
|
||||||
if song:
|
return True
|
||||||
song.status = "completed"
|
else:
|
||||||
song.song_result_url = file_url
|
logger.warning(f"[Song] NOT FOUND in DB - task_id: {task_id}")
|
||||||
await session.commit()
|
print(f"[Song] NOT FOUND in DB - task_id: {task_id}")
|
||||||
print(f"[download_and_save_song] SUCCESS - task_id: {task_id}, status: completed")
|
return False
|
||||||
else:
|
|
||||||
print(f"[download_and_save_song] Song NOT FOUND in DB - task_id: {task_id}")
|
except SQLAlchemyError as e:
|
||||||
|
logger.error(f"[Song] DB Error while updating status - task_id: {task_id}, error: {e}")
|
||||||
except Exception as e:
|
print(f"[Song] DB Error while updating status - task_id: {task_id}, error: {e}")
|
||||||
print(f"[download_and_save_song] EXCEPTION - task_id: {task_id}, error: {e}")
|
return False
|
||||||
# 실패 시 Song 테이블 업데이트
|
except Exception as e:
|
||||||
async with AsyncSessionLocal() as session:
|
logger.error(f"[Song] Unexpected error while updating status - task_id: {task_id}, error: {e}")
|
||||||
# 여러 개 있을 경우 가장 최근 것 선택
|
print(f"[Song] Unexpected error while updating status - task_id: {task_id}, error: {e}")
|
||||||
result = await session.execute(
|
return False
|
||||||
select(Song)
|
|
||||||
.where(Song.task_id == task_id)
|
|
||||||
.order_by(Song.created_at.desc())
|
async def _download_audio(url: str, task_id: str) -> bytes:
|
||||||
.limit(1)
|
"""URL에서 오디오 파일을 다운로드합니다.
|
||||||
)
|
|
||||||
song = result.scalar_one_or_none()
|
Args:
|
||||||
|
url: 다운로드할 URL
|
||||||
if song:
|
task_id: 로그용 task_id
|
||||||
song.status = "failed"
|
|
||||||
await session.commit()
|
Returns:
|
||||||
print(f"[download_and_save_song] FAILED - task_id: {task_id}, status updated to failed")
|
bytes: 다운로드한 파일 내용
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
httpx.HTTPError: 다운로드 실패 시
|
||||||
|
"""
|
||||||
|
logger.info(f"[Download] Downloading - task_id: {task_id}")
|
||||||
|
print(f"[Download] Downloading - task_id: {task_id}")
|
||||||
|
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
response = await client.get(url, timeout=REQUEST_TIMEOUT)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
logger.info(f"[Download] SUCCESS - task_id: {task_id}, size: {len(response.content)} bytes")
|
||||||
|
print(f"[Download] SUCCESS - task_id: {task_id}, size: {len(response.content)} bytes")
|
||||||
|
return response.content
|
||||||
|
|
||||||
|
|
||||||
|
async def download_and_save_song(
|
||||||
|
task_id: str,
|
||||||
|
audio_url: str,
|
||||||
|
store_name: str,
|
||||||
|
) -> None:
|
||||||
|
"""백그라운드에서 노래를 다운로드하고 Song 테이블을 업데이트합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task_id: 프로젝트 task_id
|
||||||
|
audio_url: 다운로드할 오디오 URL
|
||||||
|
store_name: 저장할 파일명에 사용할 업체명
|
||||||
|
"""
|
||||||
|
logger.info(f"[download_and_save_song] START - task_id: {task_id}, store_name: {store_name}")
|
||||||
|
print(f"[download_and_save_song] START - task_id: {task_id}, store_name: {store_name}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 저장 경로 생성: media/song/{날짜}/{uuid7}/{store_name}.mp3
|
||||||
|
today = date.today().strftime("%Y-%m-%d")
|
||||||
|
unique_id = await generate_task_id()
|
||||||
|
# 파일명에 사용할 수 없는 문자 제거
|
||||||
|
safe_store_name = "".join(
|
||||||
|
c for c in store_name if c.isalnum() or c in (" ", "_", "-")
|
||||||
|
).strip()
|
||||||
|
safe_store_name = safe_store_name or "song"
|
||||||
|
file_name = f"{safe_store_name}.mp3"
|
||||||
|
|
||||||
|
# 절대 경로 생성
|
||||||
|
media_dir = Path("media") / "song" / today / unique_id
|
||||||
|
media_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
file_path = media_dir / file_name
|
||||||
|
logger.info(f"[download_and_save_song] Directory created - path: {file_path}")
|
||||||
|
print(f"[download_and_save_song] Directory created - path: {file_path}")
|
||||||
|
|
||||||
|
# 오디오 파일 다운로드
|
||||||
|
logger.info(f"[download_and_save_song] Downloading audio - task_id: {task_id}, url: {audio_url}")
|
||||||
|
print(f"[download_and_save_song] Downloading audio - task_id: {task_id}, url: {audio_url}")
|
||||||
|
|
||||||
|
content = await _download_audio(audio_url, task_id)
|
||||||
|
|
||||||
|
async with aiofiles.open(str(file_path), "wb") as f:
|
||||||
|
await f.write(content)
|
||||||
|
|
||||||
|
logger.info(f"[download_and_save_song] File saved - task_id: {task_id}, path: {file_path}")
|
||||||
|
print(f"[download_and_save_song] File saved - task_id: {task_id}, path: {file_path}")
|
||||||
|
|
||||||
|
# 프론트엔드에서 접근 가능한 URL 생성
|
||||||
|
relative_path = f"/media/song/{today}/{unique_id}/{file_name}"
|
||||||
|
base_url = f"http://{prj_settings.PROJECT_DOMAIN}"
|
||||||
|
file_url = f"{base_url}{relative_path}"
|
||||||
|
logger.info(f"[download_and_save_song] URL generated - task_id: {task_id}, url: {file_url}")
|
||||||
|
print(f"[download_and_save_song] URL generated - task_id: {task_id}, url: {file_url}")
|
||||||
|
|
||||||
|
# Song 테이블 업데이트
|
||||||
|
await _update_song_status(task_id, "completed", file_url)
|
||||||
|
logger.info(f"[download_and_save_song] SUCCESS - task_id: {task_id}")
|
||||||
|
print(f"[download_and_save_song] SUCCESS - task_id: {task_id}")
|
||||||
|
|
||||||
|
except httpx.HTTPError as e:
|
||||||
|
logger.error(f"[download_and_save_song] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[download_and_save_song] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
await _update_song_status(task_id, "failed")
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
logger.error(f"[download_and_save_song] DB ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[download_and_save_song] DB ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
await _update_song_status(task_id, "failed")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[download_and_save_song] EXCEPTION - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[download_and_save_song] EXCEPTION - task_id: {task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
await _update_song_status(task_id, "failed")
|
||||||
|
|
||||||
|
|
||||||
|
async def download_and_upload_song_to_blob(
|
||||||
|
task_id: str,
|
||||||
|
audio_url: str,
|
||||||
|
store_name: str,
|
||||||
|
) -> None:
|
||||||
|
"""백그라운드에서 노래를 다운로드하고 Azure Blob Storage에 업로드한 뒤 Song 테이블을 업데이트합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task_id: 프로젝트 task_id
|
||||||
|
audio_url: 다운로드할 오디오 URL
|
||||||
|
store_name: 저장할 파일명에 사용할 업체명
|
||||||
|
"""
|
||||||
|
logger.info(f"[download_and_upload_song_to_blob] START - task_id: {task_id}, store_name: {store_name}")
|
||||||
|
print(f"[download_and_upload_song_to_blob] START - task_id: {task_id}, store_name: {store_name}")
|
||||||
|
temp_file_path: Path | None = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 파일명에 사용할 수 없는 문자 제거
|
||||||
|
safe_store_name = "".join(
|
||||||
|
c for c in store_name if c.isalnum() or c in (" ", "_", "-")
|
||||||
|
).strip()
|
||||||
|
safe_store_name = safe_store_name or "song"
|
||||||
|
file_name = f"{safe_store_name}.mp3"
|
||||||
|
|
||||||
|
# 임시 저장 경로 생성
|
||||||
|
temp_dir = Path("media") / "temp" / task_id
|
||||||
|
temp_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
temp_file_path = temp_dir / file_name
|
||||||
|
logger.info(f"[download_and_upload_song_to_blob] Temp directory created - path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_song_to_blob] Temp directory created - path: {temp_file_path}")
|
||||||
|
|
||||||
|
# 오디오 파일 다운로드
|
||||||
|
logger.info(f"[download_and_upload_song_to_blob] Downloading audio - task_id: {task_id}, url: {audio_url}")
|
||||||
|
print(f"[download_and_upload_song_to_blob] Downloading audio - task_id: {task_id}, url: {audio_url}")
|
||||||
|
|
||||||
|
content = await _download_audio(audio_url, task_id)
|
||||||
|
|
||||||
|
async with aiofiles.open(str(temp_file_path), "wb") as f:
|
||||||
|
await f.write(content)
|
||||||
|
|
||||||
|
logger.info(f"[download_and_upload_song_to_blob] File downloaded - task_id: {task_id}, path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_song_to_blob] File downloaded - task_id: {task_id}, path: {temp_file_path}")
|
||||||
|
|
||||||
|
# Azure Blob Storage에 업로드
|
||||||
|
uploader = AzureBlobUploader(task_id=task_id)
|
||||||
|
upload_success = await uploader.upload_music(file_path=str(temp_file_path))
|
||||||
|
|
||||||
|
if not upload_success:
|
||||||
|
raise Exception("Azure Blob Storage 업로드 실패")
|
||||||
|
|
||||||
|
# SAS 토큰이 제외된 public_url 사용
|
||||||
|
blob_url = uploader.public_url
|
||||||
|
logger.info(f"[download_and_upload_song_to_blob] Uploaded to Blob - task_id: {task_id}, url: {blob_url}")
|
||||||
|
print(f"[download_and_upload_song_to_blob] Uploaded to Blob - task_id: {task_id}, url: {blob_url}")
|
||||||
|
|
||||||
|
# Song 테이블 업데이트
|
||||||
|
await _update_song_status(task_id, "completed", blob_url)
|
||||||
|
logger.info(f"[download_and_upload_song_to_blob] SUCCESS - task_id: {task_id}")
|
||||||
|
print(f"[download_and_upload_song_to_blob] SUCCESS - task_id: {task_id}")
|
||||||
|
|
||||||
|
except httpx.HTTPError as e:
|
||||||
|
logger.error(f"[download_and_upload_song_to_blob] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_song_to_blob] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
await _update_song_status(task_id, "failed")
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
logger.error(f"[download_and_upload_song_to_blob] DB ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_song_to_blob] DB ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
await _update_song_status(task_id, "failed")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[download_and_upload_song_to_blob] EXCEPTION - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_song_to_blob] EXCEPTION - task_id: {task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
await _update_song_status(task_id, "failed")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# 임시 파일 삭제
|
||||||
|
if temp_file_path and temp_file_path.exists():
|
||||||
|
try:
|
||||||
|
temp_file_path.unlink()
|
||||||
|
logger.info(f"[download_and_upload_song_to_blob] Temp file deleted - path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_song_to_blob] Temp file deleted - path: {temp_file_path}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[download_and_upload_song_to_blob] Failed to delete temp file: {e}")
|
||||||
|
print(f"[download_and_upload_song_to_blob] Failed to delete temp file: {e}")
|
||||||
|
|
||||||
|
# 임시 디렉토리 삭제 시도
|
||||||
|
temp_dir = Path("media") / "temp" / task_id
|
||||||
|
if temp_dir.exists():
|
||||||
|
try:
|
||||||
|
temp_dir.rmdir()
|
||||||
|
except Exception:
|
||||||
|
pass # 디렉토리가 비어있지 않으면 무시
|
||||||
|
|
||||||
|
|
||||||
|
async def download_and_upload_song_by_suno_task_id(
|
||||||
|
suno_task_id: str,
|
||||||
|
audio_url: str,
|
||||||
|
store_name: str,
|
||||||
|
duration: float | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""suno_task_id로 Song을 조회하여 노래를 다운로드하고 Azure Blob Storage에 업로드한 뒤 Song 테이블을 업데이트합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
suno_task_id: Suno API 작업 ID
|
||||||
|
audio_url: 다운로드할 오디오 URL
|
||||||
|
store_name: 저장할 파일명에 사용할 업체명
|
||||||
|
duration: 노래 재생 시간 (초)
|
||||||
|
"""
|
||||||
|
logger.info(f"[download_and_upload_song_by_suno_task_id] START - suno_task_id: {suno_task_id}, store_name: {store_name}, duration: {duration}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] START - suno_task_id: {suno_task_id}, store_name: {store_name}, duration: {duration}")
|
||||||
|
temp_file_path: Path | None = None
|
||||||
|
task_id: str | None = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
# suno_task_id로 Song 조회하여 task_id 가져오기
|
||||||
|
async with BackgroundSessionLocal() as session:
|
||||||
|
result = await session.execute(
|
||||||
|
select(Song)
|
||||||
|
.where(Song.suno_task_id == suno_task_id)
|
||||||
|
.order_by(Song.created_at.desc())
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
song = result.scalar_one_or_none()
|
||||||
|
|
||||||
|
if not song:
|
||||||
|
logger.warning(f"[download_and_upload_song_by_suno_task_id] Song NOT FOUND - suno_task_id: {suno_task_id}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] Song NOT FOUND - suno_task_id: {suno_task_id}")
|
||||||
|
return
|
||||||
|
|
||||||
|
task_id = song.task_id
|
||||||
|
logger.info(f"[download_and_upload_song_by_suno_task_id] Song found - suno_task_id: {suno_task_id}, task_id: {task_id}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] Song found - suno_task_id: {suno_task_id}, task_id: {task_id}")
|
||||||
|
|
||||||
|
# 파일명에 사용할 수 없는 문자 제거
|
||||||
|
safe_store_name = "".join(
|
||||||
|
c for c in store_name if c.isalnum() or c in (" ", "_", "-")
|
||||||
|
).strip()
|
||||||
|
safe_store_name = safe_store_name or "song"
|
||||||
|
file_name = f"{safe_store_name}.mp3"
|
||||||
|
|
||||||
|
# 임시 저장 경로 생성
|
||||||
|
temp_dir = Path("media") / "temp" / task_id
|
||||||
|
temp_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
temp_file_path = temp_dir / file_name
|
||||||
|
logger.info(f"[download_and_upload_song_by_suno_task_id] Temp directory created - path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] Temp directory created - path: {temp_file_path}")
|
||||||
|
|
||||||
|
# 오디오 파일 다운로드
|
||||||
|
logger.info(f"[download_and_upload_song_by_suno_task_id] Downloading audio - suno_task_id: {suno_task_id}, url: {audio_url}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] Downloading audio - suno_task_id: {suno_task_id}, url: {audio_url}")
|
||||||
|
|
||||||
|
content = await _download_audio(audio_url, task_id)
|
||||||
|
|
||||||
|
async with aiofiles.open(str(temp_file_path), "wb") as f:
|
||||||
|
await f.write(content)
|
||||||
|
|
||||||
|
logger.info(f"[download_and_upload_song_by_suno_task_id] File downloaded - suno_task_id: {suno_task_id}, path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] File downloaded - suno_task_id: {suno_task_id}, path: {temp_file_path}")
|
||||||
|
|
||||||
|
# Azure Blob Storage에 업로드
|
||||||
|
uploader = AzureBlobUploader(task_id=task_id)
|
||||||
|
upload_success = await uploader.upload_music(file_path=str(temp_file_path))
|
||||||
|
|
||||||
|
if not upload_success:
|
||||||
|
raise Exception("Azure Blob Storage 업로드 실패")
|
||||||
|
|
||||||
|
# SAS 토큰이 제외된 public_url 사용
|
||||||
|
blob_url = uploader.public_url
|
||||||
|
logger.info(f"[download_and_upload_song_by_suno_task_id] Uploaded to Blob - suno_task_id: {suno_task_id}, url: {blob_url}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] Uploaded to Blob - suno_task_id: {suno_task_id}, url: {blob_url}")
|
||||||
|
|
||||||
|
# Song 테이블 업데이트
|
||||||
|
await _update_song_status(
|
||||||
|
task_id=task_id,
|
||||||
|
status="completed",
|
||||||
|
song_url=blob_url,
|
||||||
|
suno_task_id=suno_task_id,
|
||||||
|
duration=duration,
|
||||||
|
)
|
||||||
|
logger.info(f"[download_and_upload_song_by_suno_task_id] SUCCESS - suno_task_id: {suno_task_id}, duration: {duration}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] SUCCESS - suno_task_id: {suno_task_id}, duration: {duration}")
|
||||||
|
|
||||||
|
except httpx.HTTPError as e:
|
||||||
|
logger.error(f"[download_and_upload_song_by_suno_task_id] DOWNLOAD ERROR - suno_task_id: {suno_task_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] DOWNLOAD ERROR - suno_task_id: {suno_task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
if task_id:
|
||||||
|
await _update_song_status(task_id, "failed", suno_task_id=suno_task_id)
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
logger.error(f"[download_and_upload_song_by_suno_task_id] DB ERROR - suno_task_id: {suno_task_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] DB ERROR - suno_task_id: {suno_task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
if task_id:
|
||||||
|
await _update_song_status(task_id, "failed", suno_task_id=suno_task_id)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[download_and_upload_song_by_suno_task_id] EXCEPTION - suno_task_id: {suno_task_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] EXCEPTION - suno_task_id: {suno_task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
if task_id:
|
||||||
|
await _update_song_status(task_id, "failed", suno_task_id=suno_task_id)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# 임시 파일 삭제
|
||||||
|
if temp_file_path and temp_file_path.exists():
|
||||||
|
try:
|
||||||
|
temp_file_path.unlink()
|
||||||
|
logger.info(f"[download_and_upload_song_by_suno_task_id] Temp file deleted - path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] Temp file deleted - path: {temp_file_path}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[download_and_upload_song_by_suno_task_id] Failed to delete temp file: {e}")
|
||||||
|
print(f"[download_and_upload_song_by_suno_task_id] Failed to delete temp file: {e}")
|
||||||
|
|
||||||
|
# 임시 디렉토리 삭제 시도
|
||||||
|
if task_id:
|
||||||
|
temp_dir = Path("media") / "temp" / task_id
|
||||||
|
if temp_dir.exists():
|
||||||
|
try:
|
||||||
|
temp_dir.rmdir()
|
||||||
|
except Exception:
|
||||||
|
pass # 디렉토리가 비어있지 않으면 무시
|
||||||
@@ -1,329 +1,404 @@
|
|||||||
import json
|
import json
|
||||||
import re
|
import logging
|
||||||
|
import re
|
||||||
from openai import AsyncOpenAI
|
|
||||||
|
from openai import AsyncOpenAI
|
||||||
from config import apikey_settings
|
|
||||||
|
from config import apikey_settings
|
||||||
# fmt: off
|
|
||||||
LYRICS_PROMPT_TEMPLATE_ORI = """
|
# 로거 설정
|
||||||
1.Act as a content marketing expert with domain knowledges in [pension/staying services] in Korea, Goal: plan viral content creation that lead online reservations and promotion
|
logger = logging.getLogger(__name__)
|
||||||
2.Conduct an in-depth analysis of [업체명:{customer_name}] in [지역명:{region}] by examining their official website or informations, photos on never map and online presence. Create a comprehensive "[지역 상세: {detail_region_info}]_Brand & Marketing Intelligence Report in Korean, that includes:
|
|
||||||
|
# fmt: off
|
||||||
**Core Analysis:**
|
LYRICS_PROMPT_TEMPLATE_ORI = """
|
||||||
- Target customer segments & personas
|
1.Act as a content marketing expert with domain knowledges in [pension/staying services] in Korea, Goal: plan viral content creation that lead online reservations and promotion
|
||||||
- Unique Selling Propositions (USPs) and competitive differentiators
|
2.Conduct an in-depth analysis of [업체명:{customer_name}] in [지역명:{region}] by examining their official website or informations, photos on never map and online presence. Create a comprehensive "[지역 상세: {detail_region_info}]_Brand & Marketing Intelligence Report in Korean, that includes:
|
||||||
- Comprehensive competitor landscape analysis (direct & indirect competitors)
|
|
||||||
- Market positioning assessment
|
**Core Analysis:**
|
||||||
|
- Target customer segments & personas
|
||||||
**Content Strategy Framework:**
|
- Unique Selling Propositions (USPs) and competitive differentiators
|
||||||
- Seasonal content calendar with trend integration
|
- Comprehensive competitor landscape analysis (direct & indirect competitors)
|
||||||
- Visual storytelling direction (shot-by-shot creative guidance)
|
- Market positioning assessment
|
||||||
- Brand tone & voice guidelines
|
|
||||||
- Content themes aligned with target audience behaviors
|
**Content Strategy Framework:**
|
||||||
|
- Seasonal content calendar with trend integration
|
||||||
**SEO & AEO Optimization:**
|
- Visual storytelling direction (shot-by-shot creative guidance)
|
||||||
- Recommended primary and long-tail keywords
|
- Brand tone & voice guidelines
|
||||||
- SEO-optimized taglines and meta descriptions
|
- Content themes aligned with target audience behaviors
|
||||||
- Answer Engine Optimization (AEO) content suggestions
|
|
||||||
- Local search optimization strategies
|
**SEO & AEO Optimization:**
|
||||||
|
- Recommended primary and long-tail keywords
|
||||||
**Actionable Recommendations:**
|
- SEO-optimized taglines and meta descriptions
|
||||||
- Content distribution strategy across platforms
|
- Answer Engine Optimization (AEO) content suggestions
|
||||||
- KPI measurement framework
|
- Local search optimization strategies
|
||||||
- Budget allocation recommendations by content type
|
|
||||||
|
**Actionable Recommendations:**
|
||||||
콘텐츠 기획(Lyrics, Prompt for SUNO)
|
- Content distribution strategy across platforms
|
||||||
1. Based on the Brand & Marketing Intelligence Report for [업체명 + 지역명 / {customer_name} ({region})], create original lyrics and define music attributes (song mood, BPM, genres, and key musical motifs, Prompt for Suno.com) specifically tailored for viral content.
|
- KPI measurement framework
|
||||||
2. The lyrics should include, the name of [ Promotion Subject], [location], [main target],[Famous place, accessible in 10min], promotional words including but not limited to [인스타 감성], [사진같은 하루]
|
- Budget allocation recommendations by content type
|
||||||
|
|
||||||
Deliver outputs optimized for three formats:1 minute. Ensure that each version aligns with the brand's core identity and is suitable for use in digital marketing and social media campaigns, in Korean
|
콘텐츠 기획(Lyrics, Prompt for SUNO)
|
||||||
""".strip()
|
1. Based on the Brand & Marketing Intelligence Report for [업체명 + 지역명 / {customer_name} ({region})], create original lyrics and define music attributes (song mood, BPM, genres, and key musical motifs, Prompt for Suno.com) specifically tailored for viral content.
|
||||||
# fmt: on
|
2. The lyrics should include, the name of [ Promotion Subject], [location], [main target],[Famous place, accessible in 10min], promotional words including but not limited to [인스타 감성], [사진같은 하루]
|
||||||
|
|
||||||
LYRICS_PROMPT_TEMPLATE = """
|
Deliver outputs optimized for three formats:1 minute. Ensure that each version aligns with the brand's core identity and is suitable for use in digital marketing and social media campaigns, in Korean
|
||||||
[ROLE]
|
""".strip()
|
||||||
Content marketing expert and creative songwriter specializing in pension/accommodation services
|
# fmt: on
|
||||||
|
|
||||||
[INPUT]
|
LYRICS_PROMPT_TEMPLATE = """
|
||||||
- Business Name: {customer_name}
|
[ROLE]
|
||||||
- Region: {region}
|
Content marketing expert and creative songwriter specializing in pension/accommodation services
|
||||||
- Region Details: {detail_region_info}
|
|
||||||
- Output Language: {language}
|
[INPUT]
|
||||||
|
- Business Name: {customer_name}
|
||||||
[INTERNAL ANALYSIS - DO NOT OUTPUT]
|
- Region: {region}
|
||||||
Analyze the following internally to inform lyrics creation:
|
- Region Details: {detail_region_info}
|
||||||
- Target customer segments and personas
|
- Output Language: {language}
|
||||||
- Unique Selling Propositions (USPs)
|
|
||||||
- Regional characteristics and nearby attractions (within 10 min access)
|
[INTERNAL ANALYSIS - DO NOT OUTPUT]
|
||||||
- Seasonal appeal points
|
Analyze the following internally to inform lyrics creation:
|
||||||
- Emotional triggers for the target audience
|
- Target customer segments and personas
|
||||||
|
- Unique Selling Propositions (USPs)
|
||||||
[LYRICS REQUIREMENTS]
|
- Regional characteristics and nearby attractions (within 10 min access)
|
||||||
1. Must Include Elements:
|
- Seasonal appeal points
|
||||||
- Business name (TRANSLATED or TRANSLITERATED to {language})
|
- Emotional triggers for the target audience
|
||||||
- Region name (TRANSLATED or TRANSLITERATED to {language})
|
|
||||||
- Main target audience appeal
|
[LYRICS REQUIREMENTS]
|
||||||
- Nearby famous places or regional characteristics
|
1. Must Include Elements:
|
||||||
|
- Business name (TRANSLATED or TRANSLITERATED to {language})
|
||||||
2. Keywords to Incorporate (use language-appropriate trendy expressions):
|
- Region name (TRANSLATED or TRANSLITERATED to {language})
|
||||||
- Korean: 인스타 감성, 사진같은 하루, 힐링, 여행, 감성 숙소
|
- Main target audience appeal
|
||||||
- English: Instagram vibes, picture-perfect day, healing, travel, getaway
|
- Nearby famous places or regional characteristics
|
||||||
- Chinese: 网红打卡, 治愈系, 旅行, 度假, 拍照圣地
|
|
||||||
- Japanese: インスタ映え, 写真のような一日, 癒し, 旅行, 絶景
|
2. Keywords to Incorporate (use language-appropriate trendy expressions):
|
||||||
- Thai: ที่พักสวย, ฮีลใจ, เที่ยว, ถ่ายรูป, วิวสวย
|
- Korean: 인스타 감성, 사진같은 하루, 힐링, 여행, 감성 숙소
|
||||||
- Vietnamese: check-in đẹp, healing, du lịch, nghỉ dưỡng, view đẹp
|
- English: Instagram vibes, picture-perfect day, healing, travel, getaway
|
||||||
|
- Chinese: 网红打卡, 治愈系, 旅行, 度假, 拍照圣地
|
||||||
3. Structure:
|
- Japanese: インスタ映え, 写真のような一日, 癒し, 旅行, 絶景
|
||||||
- Length: For 1-minute video (approximately 8-12 lines)
|
- Thai: ที่พักสวย, ฮีลใจ, เที่ยว, ถ่ายรูป, วิวสวย
|
||||||
- Flow: Verse structure suitable for music
|
- Vietnamese: check-in đẹp, healing, du lịch, nghỉ dưỡng, view đẹp
|
||||||
- Rhythm: Natural speech rhythm in the specified language
|
|
||||||
|
3. Structure:
|
||||||
4. Tone:
|
- Length: For 1-minute video (approximately 8-12 lines)
|
||||||
- Emotional and heartfelt
|
- Flow: Verse structure suitable for music
|
||||||
- Trendy and viral-friendly
|
- Rhythm: Natural speech rhythm in the specified language
|
||||||
- Relatable to target audience
|
|
||||||
|
4. Tone:
|
||||||
[CRITICAL LANGUAGE REQUIREMENT - ABSOLUTE RULE]
|
- Emotional and heartfelt
|
||||||
ALL OUTPUT MUST BE 100% WRITTEN IN {language} - NO EXCEPTIONS
|
- Trendy and viral-friendly
|
||||||
- ALL lyrics content: {language} ONLY
|
- Relatable to target audience
|
||||||
- ALL proper nouns (business names, region names, place names): MUST be translated or transliterated to {language}
|
|
||||||
- Korean input like "군산" must become "Gunsan" in English, "群山" in Chinese, "グンサン" in Japanese, etc.
|
[CRITICAL LANGUAGE REQUIREMENT - ABSOLUTE RULE]
|
||||||
- Korean input like "스테이 머뭄" must become "Stay Meoum" in English, "住留" in Chinese, "ステイモーム" in Japanese, etc.
|
ALL OUTPUT MUST BE 100% WRITTEN IN {language} - NO EXCEPTIONS
|
||||||
- ZERO Korean characters (한글) allowed when output language is NOT Korean
|
- ALL lyrics content: {language} ONLY
|
||||||
- ZERO mixing of languages - the entire output must be monolingual in {language}
|
- ALL proper nouns (business names, region names, place names): MUST be translated or transliterated to {language}
|
||||||
- This is a NON-NEGOTIABLE requirement
|
- Korean input like "군산" must become "Gunsan" in English, "群山" in Chinese, "グンサン" in Japanese, etc.
|
||||||
- Any output containing characters from other languages is considered a COMPLETE FAILURE
|
- Korean input like "스테이 머뭄" must become "Stay Meoum" in English, "住留" in Chinese, "ステイモーム" in Japanese, etc.
|
||||||
- Violation of this rule invalidates the entire response
|
- ZERO Korean characters (한글) allowed when output language is NOT Korean
|
||||||
|
- ZERO mixing of languages - the entire output must be monolingual in {language}
|
||||||
[OUTPUT RULES - STRICTLY ENFORCED]
|
- This is a NON-NEGOTIABLE requirement
|
||||||
- Output lyrics ONLY
|
- Any output containing characters from other languages is considered a COMPLETE FAILURE
|
||||||
- Lyrics MUST be written ENTIRELY in {language} - NO EXCEPTIONS
|
- Violation of this rule invalidates the entire response
|
||||||
- ALL names and places MUST be in {language} script/alphabet
|
|
||||||
- NO Korean (한글), Chinese (漢字), Japanese (仮名), Thai (ไทย), or Vietnamese (Tiếng Việt) characters unless that is the selected output language
|
[OUTPUT RULES - STRICTLY ENFORCED]
|
||||||
- NO titles, descriptions, analysis, or explanations
|
- Output lyrics ONLY
|
||||||
- NO greetings or closing remarks
|
- Lyrics MUST be written ENTIRELY in {language} - NO EXCEPTIONS
|
||||||
- NO additional commentary before or after lyrics
|
- ALL names and places MUST be in {language} script/alphabet
|
||||||
- NO line numbers or labels
|
- NO Korean (한글), Chinese (漢字), Japanese (仮名), Thai (ไทย), or Vietnamese (Tiếng Việt) characters unless that is the selected output language
|
||||||
- Follow the exact format below
|
- NO titles, descriptions, analysis, or explanations
|
||||||
|
- NO greetings or closing remarks
|
||||||
[OUTPUT FORMAT - SUCCESS]
|
- NO additional commentary before or after lyrics
|
||||||
---
|
- NO line numbers or labels
|
||||||
[Lyrics ENTIRELY in {language} here - no other language characters allowed]
|
- Follow the exact format below
|
||||||
---
|
|
||||||
|
[OUTPUT FORMAT - SUCCESS]
|
||||||
[OUTPUT FORMAT - FAILURE]
|
---
|
||||||
If you cannot generate lyrics due to insufficient information, invalid input, or any other reason:
|
[Lyrics ENTIRELY in {language} here - no other language characters allowed]
|
||||||
---
|
---
|
||||||
ERROR: [Brief reason for failure in English]
|
|
||||||
---
|
[OUTPUT FORMAT - FAILURE]
|
||||||
""".strip()
|
If you cannot generate lyrics due to insufficient information, invalid input, or any other reason:
|
||||||
# fmt: on
|
---
|
||||||
|
ERROR: [Brief reason for failure in English]
|
||||||
MARKETING_ANALYSIS_PROMPT_TEMPLATE = """
|
---
|
||||||
[ROLE]
|
""".strip()
|
||||||
Content marketing expert specializing in pension/accommodation services in Korea
|
# fmt: on
|
||||||
|
|
||||||
[INPUT]
|
MARKETING_ANALYSIS_PROMPT_TEMPLATE = """
|
||||||
- Business Name: {customer_name}
|
[ROLE]
|
||||||
- Region: {region}
|
Content marketing expert specializing in pension/accommodation services in Korea
|
||||||
- Region Details: {detail_region_info}
|
|
||||||
|
[INPUT]
|
||||||
[ANALYSIS REQUIREMENTS]
|
- Business Name: {customer_name}
|
||||||
Provide comprehensive marketing analysis including:
|
- Region: {region}
|
||||||
1. Target Customer Segments
|
- Region Details: {detail_region_info}
|
||||||
- Primary and secondary target personas
|
|
||||||
- Age groups, travel preferences, booking patterns
|
[ANALYSIS REQUIREMENTS]
|
||||||
2. Unique Selling Propositions (USPs)
|
Provide comprehensive marketing analysis including:
|
||||||
- Key differentiators based on location and region details
|
1. Target Customer Segments
|
||||||
- Competitive advantages
|
- Primary and secondary target personas
|
||||||
3. Regional Characteristics
|
- Age groups, travel preferences, booking patterns
|
||||||
- Nearby attractions and famous places (within 10 min access)
|
2. Unique Selling Propositions (USPs)
|
||||||
- Local food, activities, and experiences
|
- Key differentiators based on location and region details
|
||||||
- Transportation accessibility
|
- Competitive advantages
|
||||||
4. Seasonal Appeal Points
|
3. Regional Characteristics
|
||||||
- Best seasons to visit
|
- Nearby attractions and famous places (within 10 min access)
|
||||||
- Seasonal activities and events
|
- Local food, activities, and experiences
|
||||||
- Peak/off-peak marketing opportunities
|
- Transportation accessibility
|
||||||
5. Marketing Keywords
|
4. Seasonal Appeal Points
|
||||||
- Recommended hashtags and search keywords
|
- Best seasons to visit
|
||||||
- Trending terms relevant to the property
|
- Seasonal activities and events
|
||||||
|
- Peak/off-peak marketing opportunities
|
||||||
[ADDITIONAL REQUIREMENTS]
|
5. Marketing Keywords
|
||||||
1. Recommended Tags
|
- Recommended hashtags and search keywords
|
||||||
- Generate 5 recommended hashtags/tags based on the business characteristics
|
- Trending terms relevant to the property
|
||||||
- Tags should be trendy, searchable, and relevant to accommodation marketing
|
|
||||||
- Return as JSON with key "tags"
|
[ADDITIONAL REQUIREMENTS]
|
||||||
- **MUST be written in Korean (한국어)**
|
1. Recommended Tags
|
||||||
|
- Generate 5 recommended hashtags/tags based on the business characteristics
|
||||||
2. Facilities
|
- Tags should be trendy, searchable, and relevant to accommodation marketing
|
||||||
- Based on the business name and region details, identify 5 likely facilities/amenities
|
- Return as JSON with key "tags"
|
||||||
- Consider typical facilities for accommodations in the given region
|
- **MUST be written in Korean (한국어)**
|
||||||
- Examples: 바베큐장, 수영장, 주차장, 와이파이, 주방, 테라스, 정원, etc.
|
|
||||||
- Return as JSON with key "facilities"
|
[CRITICAL LANGUAGE REQUIREMENT - ABSOLUTE RULE]
|
||||||
- **MUST be written in Korean (한국어)**
|
ALL OUTPUT MUST BE WRITTEN IN KOREAN (한국어)
|
||||||
|
- Analysis sections: Korean only
|
||||||
[CRITICAL LANGUAGE REQUIREMENT - ABSOLUTE RULE]
|
- Tags: Korean only
|
||||||
ALL OUTPUT MUST BE WRITTEN IN KOREAN (한국어)
|
- This is a NON-NEGOTIABLE requirement
|
||||||
- Analysis sections: Korean only
|
- Any output in English or other languages is considered a FAILURE
|
||||||
- Tags: Korean only
|
- Violation of this rule invalidates the entire response
|
||||||
- Facilities: Korean only
|
|
||||||
- This is a NON-NEGOTIABLE requirement
|
[OUTPUT RULES - STRICTLY ENFORCED]
|
||||||
- Any output in English or other languages is considered a FAILURE
|
- Output analysis ONLY
|
||||||
- Violation of this rule invalidates the entire response
|
- ALL content MUST be written in Korean (한국어) - NO EXCEPTIONS
|
||||||
|
- NO greetings or closing remarks
|
||||||
[OUTPUT RULES - STRICTLY ENFORCED]
|
- NO additional commentary before or after analysis
|
||||||
- Output analysis ONLY
|
- Follow the exact format below
|
||||||
- ALL content MUST be written in Korean (한국어) - NO EXCEPTIONS
|
|
||||||
- NO greetings or closing remarks
|
[OUTPUT FORMAT - SUCCESS]
|
||||||
- NO additional commentary before or after analysis
|
---
|
||||||
- Follow the exact format below
|
## 타겟 고객 분석
|
||||||
|
[한국어로 작성된 타겟 고객 분석]
|
||||||
[OUTPUT FORMAT - SUCCESS]
|
|
||||||
---
|
## 핵심 차별점 (USP)
|
||||||
## 타겟 고객 분석
|
[한국어로 작성된 USP 분석]
|
||||||
[한국어로 작성된 타겟 고객 분석]
|
|
||||||
|
## 지역 특성
|
||||||
## 핵심 차별점 (USP)
|
[한국어로 작성된 지역 특성 분석]
|
||||||
[한국어로 작성된 USP 분석]
|
|
||||||
|
## 시즌별 매력 포인트
|
||||||
## 지역 특성
|
[한국어로 작성된 시즌별 분석]
|
||||||
[한국어로 작성된 지역 특성 분석]
|
|
||||||
|
## 마케팅 키워드
|
||||||
## 시즌별 매력 포인트
|
[한국어로 작성된 마케팅 키워드]
|
||||||
[한국어로 작성된 시즌별 분석]
|
|
||||||
|
## JSON Data
|
||||||
## 마케팅 키워드
|
```json
|
||||||
[한국어로 작성된 마케팅 키워드]
|
{{
|
||||||
|
"tags": ["태그1", "태그2", "태그3", "태그4", "태그5"]
|
||||||
## JSON Data
|
}}
|
||||||
```json
|
```
|
||||||
{{
|
---
|
||||||
"tags": ["태그1", "태그2", "태그3", "태그4", "태그5"],
|
|
||||||
"facilities": ["부대시설1", "부대시설2", "부대시설3", "부대시설4", "부대시설5"]
|
[OUTPUT FORMAT - FAILURE]
|
||||||
}}
|
If you cannot generate analysis due to insufficient information, invalid input, or any other reason:
|
||||||
```
|
---
|
||||||
---
|
ERROR: [Brief reason for failure in English]
|
||||||
|
---
|
||||||
[OUTPUT FORMAT - FAILURE]
|
""".strip()
|
||||||
If you cannot generate analysis due to insufficient information, invalid input, or any other reason:
|
# fmt: on
|
||||||
---
|
|
||||||
ERROR: [Brief reason for failure in English]
|
|
||||||
---
|
class ChatgptService:
|
||||||
""".strip()
|
"""ChatGPT API 서비스 클래스
|
||||||
# fmt: on
|
|
||||||
|
GPT 5.0 모델을 사용하여 마케팅 가사 및 분석을 생성합니다.
|
||||||
|
"""
|
||||||
class ChatgptService:
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
customer_name: str,
|
customer_name: str,
|
||||||
region: str,
|
region: str,
|
||||||
detail_region_info: str = "",
|
detail_region_info: str = "",
|
||||||
language: str = "Korean",
|
language: str = "Korean",
|
||||||
):
|
):
|
||||||
# 최신 모델: GPT-5, GPT-5 mini, GPT-5 nano, GPT-4.1, GPT-4.1 mini, GPT-4.1 nano
|
# 최신 모델: gpt-5-mini
|
||||||
# 이전 세대: GPT-4o, GPT-4o mini, GPT-4 Turbo, GPT-3.5 Turbo
|
self.model = "gpt-5-mini"
|
||||||
self.model = "gpt-4o"
|
self.client = AsyncOpenAI(api_key=apikey_settings.CHATGPT_API_KEY)
|
||||||
self.client = AsyncOpenAI(api_key=apikey_settings.CHATGPT_API_KEY)
|
self.customer_name = customer_name
|
||||||
self.customer_name = customer_name
|
self.region = region
|
||||||
self.region = region
|
self.detail_region_info = detail_region_info
|
||||||
self.detail_region_info = detail_region_info
|
self.language = language
|
||||||
self.language = language
|
|
||||||
|
def build_lyrics_prompt(self) -> str:
|
||||||
def build_lyrics_prompt(self) -> str:
|
"""LYRICS_PROMPT_TEMPLATE에 고객 정보를 대입하여 완성된 프롬프트 반환"""
|
||||||
"""LYRICS_PROMPT_TEMPLATE에 고객 정보를 대입하여 완성된 프롬프트 반환"""
|
return LYRICS_PROMPT_TEMPLATE.format(
|
||||||
return LYRICS_PROMPT_TEMPLATE.format(
|
customer_name=self.customer_name,
|
||||||
customer_name=self.customer_name,
|
region=self.region,
|
||||||
region=self.region,
|
detail_region_info=self.detail_region_info,
|
||||||
detail_region_info=self.detail_region_info,
|
language=self.language,
|
||||||
language=self.language,
|
)
|
||||||
)
|
|
||||||
|
def build_market_analysis_prompt(self) -> str:
|
||||||
def build_market_analysis_prompt(self) -> str:
|
"""MARKETING_ANALYSIS_PROMPT_TEMPLATE에 고객 정보를 대입하여 완성된 프롬프트 반환"""
|
||||||
"""MARKETING_ANALYSIS_PROMPT_TEMPLATE에 고객 정보를 대입하여 완성된 프롬프트 반환"""
|
return MARKETING_ANALYSIS_PROMPT_TEMPLATE.format(
|
||||||
return MARKETING_ANALYSIS_PROMPT_TEMPLATE.format(
|
customer_name=self.customer_name,
|
||||||
customer_name=self.customer_name,
|
region=self.region,
|
||||||
region=self.region,
|
detail_region_info=self.detail_region_info,
|
||||||
detail_region_info=self.detail_region_info,
|
)
|
||||||
)
|
|
||||||
|
async def _call_gpt_api(self, prompt: str) -> str:
|
||||||
async def generate(self, prompt: str | None = None) -> str:
|
"""GPT API를 직접 호출합니다 (내부 메서드).
|
||||||
"""GPT에게 프롬프트를 전달하여 결과를 반환"""
|
|
||||||
if prompt is None:
|
Args:
|
||||||
prompt = self.build_lyrics_prompt()
|
prompt: GPT에 전달할 프롬프트
|
||||||
print("Generated Prompt: ", prompt)
|
|
||||||
completion = await self.client.chat.completions.create(
|
Returns:
|
||||||
model=self.model, messages=[{"role": "user", "content": prompt}]
|
GPT 응답 문자열
|
||||||
)
|
|
||||||
message = completion.choices[0].message.content
|
Raises:
|
||||||
return message or ""
|
APIError, APIConnectionError, RateLimitError: OpenAI API 오류
|
||||||
|
"""
|
||||||
async def summarize_marketing(self, text: str) -> str:
|
completion = await self.client.chat.completions.create(
|
||||||
"""마케팅 텍스트를 항목으로 구분하여 500자로 요약 정리"""
|
model=self.model, messages=[{"role": "user", "content": prompt}]
|
||||||
prompt = f"""[ROLE]
|
)
|
||||||
마케팅 콘텐츠 요약 전문가
|
message = completion.choices[0].message.content
|
||||||
|
return message or ""
|
||||||
[INPUT]
|
|
||||||
{text}
|
async def generate(
|
||||||
|
self,
|
||||||
[TASK]
|
prompt: str | None = None,
|
||||||
위 텍스트를 분석하여 핵심 내용을 항목별로 구분하여 500자 이내로 요약해주세요.
|
) -> str:
|
||||||
|
"""GPT에게 프롬프트를 전달하여 결과를 반환합니다.
|
||||||
[OUTPUT REQUIREMENTS]
|
|
||||||
- 항목별로 구분하여 정리 (예: 타겟 고객, 차별점, 지역 특성 등)
|
Args:
|
||||||
- 총 500자 이내로 요약
|
prompt: GPT에 전달할 프롬프트 (None이면 기본 가사 프롬프트 사용)
|
||||||
- 핵심 정보만 간결하게 포함
|
|
||||||
- 한국어로 작성
|
Returns:
|
||||||
|
GPT 응답 문자열
|
||||||
[OUTPUT FORMAT]
|
|
||||||
---
|
Raises:
|
||||||
[항목별로 구분된 500자 이내 요약]
|
APIError, APIConnectionError, RateLimitError: OpenAI API 오류
|
||||||
---
|
"""
|
||||||
"""
|
if prompt is None:
|
||||||
completion = await self.client.chat.completions.create(
|
prompt = self.build_lyrics_prompt()
|
||||||
model=self.model, messages=[{"role": "user", "content": prompt}]
|
|
||||||
)
|
print(f"[ChatgptService] Generated Prompt (length: {len(prompt)})")
|
||||||
message = completion.choices[0].message.content
|
logger.info(f"[ChatgptService] Starting GPT request with model: {self.model}")
|
||||||
result = message or ""
|
|
||||||
|
# GPT API 호출
|
||||||
# --- 구분자 제거
|
response = await self._call_gpt_api(prompt)
|
||||||
if result.startswith("---"):
|
|
||||||
result = result[3:].strip()
|
print(f"[ChatgptService] SUCCESS - Response length: {len(response)}")
|
||||||
if result.endswith("---"):
|
logger.info(f"[ChatgptService] SUCCESS - Response length: {len(response)}")
|
||||||
result = result[:-3].strip()
|
return response
|
||||||
|
|
||||||
return result
|
async def summarize_marketing(self, text: str) -> str:
|
||||||
|
"""마케팅 텍스트를 항목으로 구분하여 500자로 요약 정리.
|
||||||
async def parse_marketing_analysis(self, raw_response: str) -> dict:
|
|
||||||
"""ChatGPT 마케팅 분석 응답을 파싱하고 요약하여 딕셔너리로 반환
|
Args:
|
||||||
|
text: 요약할 마케팅 텍스트
|
||||||
Returns:
|
|
||||||
dict: {"report": str, "tags": list[str], "facilities": list[str]}
|
Returns:
|
||||||
"""
|
요약된 텍스트
|
||||||
tags: list[str] = []
|
|
||||||
facilities: list[str] = []
|
Raises:
|
||||||
report = raw_response
|
APIError, APIConnectionError, RateLimitError: OpenAI API 오류
|
||||||
|
"""
|
||||||
# JSON 블록 추출 시도
|
prompt = f"""[ROLE]
|
||||||
json_match = re.search(r"```json\s*(\{.*?\})\s*```", raw_response, re.DOTALL)
|
마케팅 콘텐츠 요약 전문가
|
||||||
if json_match:
|
|
||||||
try:
|
[INPUT]
|
||||||
json_data = json.loads(json_match.group(1))
|
{text}
|
||||||
tags = json_data.get("tags", [])
|
|
||||||
facilities = json_data.get("facilities", [])
|
[TASK]
|
||||||
# JSON 블록을 제외한 리포트 부분 추출
|
위 텍스트를 분석하여 핵심 내용을 항목별로 구분하여 500자 이내로 요약해주세요.
|
||||||
report = raw_response[: json_match.start()].strip()
|
|
||||||
# --- 구분자 제거
|
[OUTPUT REQUIREMENTS]
|
||||||
if report.startswith("---"):
|
- 5개 항목으로 구분: 타겟 고객, 핵심 차별점, 지역 특성, 시즌별 포인트, 추천 키워드
|
||||||
report = report[3:].strip()
|
- 각 항목은 줄바꿈으로 구분
|
||||||
if report.endswith("---"):
|
- 총 500자 이내로 요약
|
||||||
report = report[:-3].strip()
|
- 핵심 정보만 간결하게 포함
|
||||||
except json.JSONDecodeError:
|
- 한국어로 작성
|
||||||
pass
|
- 특수문자 사용 금지 (괄호, 슬래시, 하이픈, 물결표 등 제외)
|
||||||
|
- 쉼표와 마침표만 사용하여 자연스러운 문장으로 작성
|
||||||
# 리포트 내용을 500자로 요약
|
|
||||||
if report:
|
[OUTPUT FORMAT - 반드시 아래 형식 준수]
|
||||||
report = await self.summarize_marketing(report)
|
---
|
||||||
|
타겟 고객
|
||||||
return {"report": report, "tags": tags, "facilities": facilities}
|
[대상 고객층을 자연스러운 문장으로 설명]
|
||||||
|
|
||||||
|
핵심 차별점
|
||||||
|
[숙소의 차별화 포인트를 자연스러운 문장으로 설명]
|
||||||
|
|
||||||
|
지역 특성
|
||||||
|
[주변 관광지와 지역 특색을 자연스러운 문장으로 설명]
|
||||||
|
|
||||||
|
시즌별 포인트
|
||||||
|
[계절별 매력 포인트를 자연스러운 문장으로 설명]
|
||||||
|
|
||||||
|
추천 키워드
|
||||||
|
[마케팅에 활용할 키워드를 쉼표로 구분하여 나열]
|
||||||
|
---
|
||||||
|
"""
|
||||||
|
|
||||||
|
result = await self.generate(prompt=prompt)
|
||||||
|
|
||||||
|
# --- 구분자 제거
|
||||||
|
if result.startswith("---"):
|
||||||
|
result = result[3:].strip()
|
||||||
|
if result.endswith("---"):
|
||||||
|
result = result[:-3].strip()
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
async def parse_marketing_analysis(
|
||||||
|
self, raw_response: str, facility_info: str | None = None
|
||||||
|
) -> dict:
|
||||||
|
"""ChatGPT 마케팅 분석 응답을 파싱하고 요약하여 딕셔너리로 반환
|
||||||
|
|
||||||
|
Args:
|
||||||
|
raw_response: ChatGPT 마케팅 분석 응답 원문
|
||||||
|
facility_info: 크롤링에서 가져온 편의시설 정보 문자열
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: {"report": str, "tags": list[str], "facilities": list[str]}
|
||||||
|
"""
|
||||||
|
tags: list[str] = []
|
||||||
|
facilities: list[str] = []
|
||||||
|
report = raw_response
|
||||||
|
|
||||||
|
# JSON 블록 추출 시도
|
||||||
|
json_match = re.search(r"```json\s*(\{.*?\})\s*```", raw_response, re.DOTALL)
|
||||||
|
if json_match:
|
||||||
|
try:
|
||||||
|
json_data = json.loads(json_match.group(1))
|
||||||
|
tags = json_data.get("tags", [])
|
||||||
|
print(f"[parse_marketing_analysis] GPT 응답에서 tags 파싱 완료: {tags}")
|
||||||
|
# JSON 블록을 제외한 리포트 부분 추출
|
||||||
|
report = raw_response[: json_match.start()].strip()
|
||||||
|
# --- 구분자 제거
|
||||||
|
if report.startswith("---"):
|
||||||
|
report = report[3:].strip()
|
||||||
|
if report.endswith("---"):
|
||||||
|
report = report[:-3].strip()
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
print("[parse_marketing_analysis] JSON 파싱 실패")
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 크롤링에서 가져온 facility_info로 facilities 설정
|
||||||
|
print(f"[parse_marketing_analysis] 크롤링 facility_info 원본: {facility_info}")
|
||||||
|
if facility_info:
|
||||||
|
# 쉼표로 구분된 편의시설 문자열을 리스트로 변환
|
||||||
|
facilities = [f.strip() for f in facility_info.split(",") if f.strip()]
|
||||||
|
print(f"[parse_marketing_analysis] facility_info 파싱 결과: {facilities}")
|
||||||
|
else:
|
||||||
|
facilities = ["등록된 정보 없음"]
|
||||||
|
print("[parse_marketing_analysis] facility_info 없음 - '등록된 정보 없음' 설정")
|
||||||
|
|
||||||
|
# 리포트 내용을 500자로 요약
|
||||||
|
if report:
|
||||||
|
report = await self.summarize_marketing(report)
|
||||||
|
|
||||||
|
print(f"[parse_marketing_analysis] 최종 facilities: {facilities}")
|
||||||
|
return {"report": report, "tags": tags, "facilities": facilities}
|
||||||
@@ -1,24 +1,24 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
from config import cors_settings
|
from config import cors_settings
|
||||||
|
|
||||||
# sys.path.append(
|
# sys.path.append(
|
||||||
# os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
# os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
# ) # root 경로 추가
|
# ) # root 경로 추가
|
||||||
|
|
||||||
|
|
||||||
class CustomCORSMiddleware:
|
class CustomCORSMiddleware:
|
||||||
def __init__(self, app: FastAPI):
|
def __init__(self, app: FastAPI):
|
||||||
self.app = app
|
self.app = app
|
||||||
|
|
||||||
def configure_cors(self):
|
def configure_cors(self):
|
||||||
self.app.add_middleware(
|
self.app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=cors_settings.CORS_ALLOW_ORIGINS,
|
allow_origins=cors_settings.CORS_ALLOW_ORIGINS,
|
||||||
allow_credentials=cors_settings.CORS_ALLOW_CREDENTIALS,
|
allow_credentials=cors_settings.CORS_ALLOW_CREDENTIALS,
|
||||||
allow_methods=cors_settings.CORS_ALLOW_METHODS,
|
allow_methods=cors_settings.CORS_ALLOW_METHODS,
|
||||||
allow_headers=cors_settings.CORS_ALLOW_HEADERS,
|
allow_headers=cors_settings.CORS_ALLOW_HEADERS,
|
||||||
expose_headers=cors_settings.CORS_EXPOSE_HEADERS,
|
expose_headers=cors_settings.CORS_EXPOSE_HEADERS,
|
||||||
max_age=cors_settings.CORS_MAX_AGE,
|
max_age=cors_settings.CORS_MAX_AGE,
|
||||||
)
|
)
|
||||||
@@ -1,254 +1,473 @@
|
|||||||
"""
|
"""
|
||||||
Creatomate API 클라이언트 모듈
|
Creatomate API 클라이언트 모듈
|
||||||
|
|
||||||
API 문서: https://creatomate.com/docs/api
|
API 문서: https://creatomate.com/docs/api
|
||||||
|
|
||||||
## 사용법
|
## 사용법
|
||||||
```python
|
```python
|
||||||
from app.utils.creatomate import CreatomateService
|
from app.utils.creatomate import CreatomateService
|
||||||
|
|
||||||
# config에서 자동으로 API 키를 가져옴
|
# config에서 자동으로 API 키를 가져옴
|
||||||
creatomate = CreatomateService()
|
creatomate = CreatomateService()
|
||||||
|
|
||||||
# 또는 명시적으로 API 키 전달
|
# 또는 명시적으로 API 키 전달
|
||||||
creatomate = CreatomateService(api_key="your_api_key")
|
creatomate = CreatomateService(api_key="your_api_key")
|
||||||
|
|
||||||
# 템플릿 목록 조회
|
# 템플릿 목록 조회 (비동기)
|
||||||
templates = creatomate.get_all_templates_data()
|
templates = await creatomate.get_all_templates_data()
|
||||||
|
|
||||||
# 특정 템플릿 조회
|
# 특정 템플릿 조회 (비동기)
|
||||||
template = creatomate.get_one_template_data(template_id)
|
template = await creatomate.get_one_template_data(template_id)
|
||||||
|
|
||||||
# 영상 렌더링 요청
|
# 영상 렌더링 요청 (비동기)
|
||||||
response = creatomate.make_creatomate_call(template_id, modifications)
|
response = await creatomate.make_creatomate_call(template_id, modifications)
|
||||||
```
|
```
|
||||||
"""
|
|
||||||
|
## 성능 최적화
|
||||||
import copy
|
- 템플릿 캐싱: 템플릿 데이터는 메모리에 캐싱되어 반복 조회 시 API 호출을 줄입니다.
|
||||||
|
- HTTP 클라이언트 재사용: 모듈 레벨의 공유 클라이언트로 커넥션 풀을 재사용합니다.
|
||||||
import httpx
|
- 캐시 만료: 기본 5분 후 자동 만료 (CACHE_TTL_SECONDS로 조정 가능)
|
||||||
|
"""
|
||||||
from config import apikey_settings
|
|
||||||
|
import copy
|
||||||
|
import logging
|
||||||
class CreatomateService:
|
import time
|
||||||
"""Creatomate API를 통한 영상 생성 서비스"""
|
from typing import Literal
|
||||||
|
|
||||||
BASE_URL = "https://api.creatomate.com"
|
import httpx
|
||||||
|
|
||||||
def __init__(self, api_key: str | None = None):
|
from config import apikey_settings, creatomate_settings
|
||||||
"""
|
|
||||||
Args:
|
# 로거 설정
|
||||||
api_key: Creatomate API 키 (Bearer token으로 사용)
|
logger = logging.getLogger(__name__)
|
||||||
None일 경우 config에서 자동으로 가져옴
|
|
||||||
"""
|
|
||||||
self.api_key = api_key or apikey_settings.CREATOMATE_API_KEY
|
# Orientation 타입 정의
|
||||||
self.headers = {
|
OrientationType = Literal["horizontal", "vertical"]
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Authorization": f"Bearer {self.api_key}",
|
# =============================================================================
|
||||||
}
|
# 모듈 레벨 캐시 및 HTTP 클라이언트 (싱글톤 패턴)
|
||||||
|
# =============================================================================
|
||||||
def get_all_templates_data(self) -> dict:
|
|
||||||
"""모든 템플릿 정보를 조회합니다."""
|
# 템플릿 캐시: {template_id: {"data": dict, "cached_at": float}}
|
||||||
url = f"{self.BASE_URL}/v1/templates"
|
_template_cache: dict[str, dict] = {}
|
||||||
response = httpx.get(url, headers=self.headers, timeout=30.0)
|
|
||||||
response.raise_for_status()
|
# 캐시 TTL (초) - 기본 5분
|
||||||
return response.json()
|
CACHE_TTL_SECONDS = 300
|
||||||
|
|
||||||
def get_one_template_data(self, template_id: str) -> dict:
|
# 모듈 레벨 공유 HTTP 클라이언트 (커넥션 풀 재사용)
|
||||||
"""특정 템플릿 ID로 템플릿 정보를 조회합니다."""
|
_shared_client: httpx.AsyncClient | None = None
|
||||||
url = f"{self.BASE_URL}/v1/templates/{template_id}"
|
|
||||||
response = httpx.get(url, headers=self.headers, timeout=30.0)
|
|
||||||
response.raise_for_status()
|
async def get_shared_client() -> httpx.AsyncClient:
|
||||||
return response.json()
|
"""공유 HTTP 클라이언트를 반환합니다. 없으면 생성합니다."""
|
||||||
|
global _shared_client
|
||||||
def parse_template_component_name(self, template_source: list) -> dict:
|
if _shared_client is None or _shared_client.is_closed:
|
||||||
"""템플릿 정보를 파싱하여 리소스 이름을 추출합니다."""
|
_shared_client = httpx.AsyncClient(
|
||||||
|
timeout=httpx.Timeout(60.0, connect=10.0),
|
||||||
def recursive_parse_component(element: dict) -> dict:
|
limits=httpx.Limits(max_keepalive_connections=10, max_connections=20),
|
||||||
if "name" in element:
|
)
|
||||||
result_element_name_type = {element["name"]: element["type"]}
|
return _shared_client
|
||||||
else:
|
|
||||||
result_element_name_type = {}
|
|
||||||
|
async def close_shared_client() -> None:
|
||||||
if element["type"] == "composition":
|
"""공유 HTTP 클라이언트를 닫습니다. 앱 종료 시 호출하세요."""
|
||||||
minor_component_list = [
|
global _shared_client
|
||||||
recursive_parse_component(minor) for minor in element["elements"]
|
if _shared_client is not None and not _shared_client.is_closed:
|
||||||
]
|
await _shared_client.aclose()
|
||||||
# WARNING: Same name component should shroud other component
|
_shared_client = None
|
||||||
for minor_component in minor_component_list:
|
print("[CreatomateService] Shared HTTP client closed")
|
||||||
result_element_name_type.update(minor_component)
|
|
||||||
|
|
||||||
return result_element_name_type
|
def clear_template_cache() -> None:
|
||||||
|
"""템플릿 캐시를 전체 삭제합니다."""
|
||||||
result = {}
|
global _template_cache
|
||||||
for result_element_dict in [
|
_template_cache.clear()
|
||||||
recursive_parse_component(component) for component in template_source
|
print("[CreatomateService] Template cache cleared")
|
||||||
]:
|
|
||||||
result.update(result_element_dict)
|
|
||||||
|
def _is_cache_valid(cached_at: float) -> bool:
|
||||||
return result
|
"""캐시가 유효한지 확인합니다."""
|
||||||
|
return (time.time() - cached_at) < CACHE_TTL_SECONDS
|
||||||
def template_connect_resource_blackbox(
|
|
||||||
self,
|
|
||||||
template_id: str,
|
class CreatomateService:
|
||||||
image_url_list: list[str],
|
"""Creatomate API를 통한 영상 생성 서비스
|
||||||
lyric: str,
|
|
||||||
music_url: str,
|
모든 HTTP 호출 메서드는 비동기(async)로 구현되어 있습니다.
|
||||||
) -> dict:
|
"""
|
||||||
"""템플릿 정보와 이미지/가사/음악 리소스를 매핑합니다.
|
|
||||||
|
BASE_URL = "https://api.creatomate.com"
|
||||||
Note:
|
|
||||||
- 이미지는 순차적으로 집어넣기
|
# 템플릿 설정 (config에서 가져옴)
|
||||||
- 가사는 개행마다 한 텍스트 삽입
|
TEMPLATE_CONFIG = {
|
||||||
- Template에 audio-music 항목이 있어야 함
|
"horizontal": {
|
||||||
"""
|
"template_id": creatomate_settings.TEMPLATE_ID_HORIZONTAL,
|
||||||
template_data = self.get_one_template_data(template_id)
|
"duration": creatomate_settings.TEMPLATE_DURATION_HORIZONTAL,
|
||||||
template_component_data = self.parse_template_component_name(
|
},
|
||||||
template_data["source"]["elements"]
|
"vertical": {
|
||||||
)
|
"template_id": creatomate_settings.TEMPLATE_ID_VERTICAL,
|
||||||
|
"duration": creatomate_settings.TEMPLATE_DURATION_VERTICAL,
|
||||||
lyric = lyric.replace("\r", "")
|
},
|
||||||
lyric_splited = lyric.split("\n")
|
}
|
||||||
modifications = {}
|
|
||||||
|
def __init__(
|
||||||
for idx, (template_component_name, template_type) in enumerate(
|
self,
|
||||||
template_component_data.items()
|
api_key: str | None = None,
|
||||||
):
|
orientation: OrientationType = "vertical",
|
||||||
match template_type:
|
target_duration: float | None = None,
|
||||||
case "image":
|
):
|
||||||
modifications[template_component_name] = image_url_list[
|
"""
|
||||||
idx % len(image_url_list)
|
Args:
|
||||||
]
|
api_key: Creatomate API 키 (Bearer token으로 사용)
|
||||||
case "text":
|
None일 경우 config에서 자동으로 가져옴
|
||||||
modifications[template_component_name] = lyric_splited[
|
orientation: 영상 방향 ("horizontal" 또는 "vertical", 기본값: "vertical")
|
||||||
idx % len(lyric_splited)
|
target_duration: 목표 영상 길이 (초)
|
||||||
]
|
None일 경우 orientation에 해당하는 기본값 사용
|
||||||
|
"""
|
||||||
modifications["audio-music"] = music_url
|
self.api_key = api_key or apikey_settings.CREATOMATE_API_KEY
|
||||||
|
self.orientation = orientation
|
||||||
return modifications
|
|
||||||
|
# orientation에 따른 템플릿 설정 가져오기
|
||||||
def elements_connect_resource_blackbox(
|
config = self.TEMPLATE_CONFIG.get(
|
||||||
self,
|
orientation, self.TEMPLATE_CONFIG["vertical"]
|
||||||
elements: list,
|
)
|
||||||
image_url_list: list[str],
|
self.template_id = config["template_id"]
|
||||||
lyric: str,
|
self.target_duration = (
|
||||||
music_url: str,
|
target_duration if target_duration is not None else config["duration"]
|
||||||
) -> dict:
|
)
|
||||||
"""elements 정보와 이미지/가사/음악 리소스를 매핑합니다."""
|
|
||||||
template_component_data = self.parse_template_component_name(elements)
|
self.headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
lyric = lyric.replace("\r", "")
|
"Authorization": f"Bearer {self.api_key}",
|
||||||
lyric_splited = lyric.split("\n")
|
}
|
||||||
modifications = {}
|
|
||||||
|
async def _request(
|
||||||
for idx, (template_component_name, template_type) in enumerate(
|
self,
|
||||||
template_component_data.items()
|
method: str,
|
||||||
):
|
url: str,
|
||||||
match template_type:
|
timeout: float = 30.0,
|
||||||
case "image":
|
**kwargs,
|
||||||
modifications[template_component_name] = image_url_list[
|
) -> httpx.Response:
|
||||||
idx % len(image_url_list)
|
"""HTTP 요청을 수행합니다.
|
||||||
]
|
|
||||||
case "text":
|
Args:
|
||||||
modifications[template_component_name] = lyric_splited[
|
method: HTTP 메서드 ("GET", "POST", etc.)
|
||||||
idx % len(lyric_splited)
|
url: 요청 URL
|
||||||
]
|
timeout: 요청 타임아웃 (초)
|
||||||
|
**kwargs: httpx 요청에 전달할 추가 인자
|
||||||
modifications["audio-music"] = music_url
|
|
||||||
|
Returns:
|
||||||
return modifications
|
httpx.Response: 응답 객체
|
||||||
|
|
||||||
def modify_element(self, elements: list, modification: dict) -> list:
|
Raises:
|
||||||
"""elements의 source를 modification에 따라 수정합니다."""
|
httpx.HTTPError: 요청 실패 시
|
||||||
|
"""
|
||||||
def recursive_modify(element: dict) -> None:
|
logger.info(f"[Creatomate] {method} {url}")
|
||||||
if "name" in element:
|
print(f"[Creatomate] {method} {url}")
|
||||||
match element["type"]:
|
|
||||||
case "image":
|
client = await get_shared_client()
|
||||||
element["source"] = modification[element["name"]]
|
|
||||||
case "audio":
|
if method.upper() == "GET":
|
||||||
element["source"] = modification.get(element["name"], "")
|
response = await client.get(
|
||||||
case "video":
|
url, headers=self.headers, timeout=timeout, **kwargs
|
||||||
element["source"] = modification[element["name"]]
|
)
|
||||||
case "text":
|
elif method.upper() == "POST":
|
||||||
element["source"] = modification.get(element["name"], "")
|
response = await client.post(
|
||||||
case "composition":
|
url, headers=self.headers, timeout=timeout, **kwargs
|
||||||
for minor in element["elements"]:
|
)
|
||||||
recursive_modify(minor)
|
else:
|
||||||
|
raise ValueError(f"Unsupported HTTP method: {method}")
|
||||||
for minor in elements:
|
|
||||||
recursive_modify(minor)
|
logger.info(f"[Creatomate] Response - Status: {response.status_code}")
|
||||||
|
print(f"[Creatomate] Response - Status: {response.status_code}")
|
||||||
return elements
|
return response
|
||||||
|
|
||||||
def make_creatomate_call(self, template_id: str, modifications: dict):
|
async def get_all_templates_data(self) -> dict:
|
||||||
"""Creatomate에 렌더링 요청을 보냅니다.
|
"""모든 템플릿 정보를 조회합니다."""
|
||||||
|
url = f"{self.BASE_URL}/v1/templates"
|
||||||
Note:
|
response = await self._request("GET", url, timeout=30.0)
|
||||||
response에 요청 정보가 있으니 폴링 필요
|
response.raise_for_status()
|
||||||
"""
|
return response.json()
|
||||||
url = f"{self.BASE_URL}/v2/renders"
|
|
||||||
data = {
|
async def get_one_template_data(
|
||||||
"template_id": template_id,
|
self,
|
||||||
"modifications": modifications,
|
template_id: str,
|
||||||
}
|
use_cache: bool = True,
|
||||||
response = httpx.post(url, json=data, headers=self.headers, timeout=60.0)
|
) -> dict:
|
||||||
response.raise_for_status()
|
"""특정 템플릿 ID로 템플릿 정보를 조회합니다.
|
||||||
return response.json()
|
|
||||||
|
Args:
|
||||||
def make_creatomate_custom_call(self, source: dict):
|
template_id: 조회할 템플릿 ID
|
||||||
"""템플릿 없이 Creatomate에 커스텀 렌더링 요청을 보냅니다.
|
use_cache: 캐시 사용 여부 (기본: True)
|
||||||
|
|
||||||
Note:
|
Returns:
|
||||||
response에 요청 정보가 있으니 폴링 필요
|
템플릿 데이터 (deep copy)
|
||||||
"""
|
"""
|
||||||
url = f"{self.BASE_URL}/v2/renders"
|
global _template_cache
|
||||||
response = httpx.post(url, json=source, headers=self.headers, timeout=60.0)
|
|
||||||
response.raise_for_status()
|
# 캐시 확인
|
||||||
return response.json()
|
if use_cache and template_id in _template_cache:
|
||||||
|
cached = _template_cache[template_id]
|
||||||
def calc_scene_duration(self, template: dict) -> float:
|
if _is_cache_valid(cached["cached_at"]):
|
||||||
"""템플릿의 전체 장면 duration을 계산합니다."""
|
print(f"[CreatomateService] Cache HIT - {template_id}")
|
||||||
total_template_duration = 0.0
|
return copy.deepcopy(cached["data"])
|
||||||
|
else:
|
||||||
for elem in template["source"]["elements"]:
|
# 만료된 캐시 삭제
|
||||||
try:
|
del _template_cache[template_id]
|
||||||
if elem["type"] == "audio":
|
print(f"[CreatomateService] Cache EXPIRED - {template_id}")
|
||||||
continue
|
|
||||||
total_template_duration += elem["duration"]
|
# API 호출
|
||||||
if "animations" not in elem:
|
url = f"{self.BASE_URL}/v1/templates/{template_id}"
|
||||||
continue
|
response = await self._request("GET", url, timeout=30.0)
|
||||||
for animation in elem["animations"]:
|
response.raise_for_status()
|
||||||
assert animation["time"] == 0 # 0이 아닌 경우 확인 필요
|
data = response.json()
|
||||||
if animation["transition"]:
|
|
||||||
total_template_duration -= animation["duration"]
|
# 캐시 저장
|
||||||
except Exception as e:
|
_template_cache[template_id] = {
|
||||||
print(f"[calc_scene_duration] Error processing element: {elem}, {e}")
|
"data": data,
|
||||||
|
"cached_at": time.time(),
|
||||||
return total_template_duration
|
}
|
||||||
|
print(f"[CreatomateService] Cache MISS - {template_id} (cached)")
|
||||||
def extend_template_duration(self, template: dict, target_duration: float) -> dict:
|
|
||||||
"""템플릿의 duration을 target_duration으로 확장합니다."""
|
return copy.deepcopy(data)
|
||||||
template["duration"] = target_duration
|
|
||||||
total_template_duration = self.calc_scene_duration(template)
|
# 하위 호환성을 위한 별칭 (deprecated)
|
||||||
extend_rate = target_duration / total_template_duration
|
async def get_one_template_data_async(self, template_id: str) -> dict:
|
||||||
new_template = copy.deepcopy(template)
|
"""특정 템플릿 ID로 템플릿 정보를 조회합니다.
|
||||||
|
|
||||||
for elem in new_template["source"]["elements"]:
|
Deprecated: get_one_template_data()를 사용하세요.
|
||||||
try:
|
"""
|
||||||
if elem["type"] == "audio":
|
return await self.get_one_template_data(template_id)
|
||||||
continue
|
|
||||||
elem["duration"] = elem["duration"] * extend_rate
|
def parse_template_component_name(self, template_source: list) -> dict:
|
||||||
if "animations" not in elem:
|
"""템플릿 정보를 파싱하여 리소스 이름을 추출합니다."""
|
||||||
continue
|
|
||||||
for animation in elem["animations"]:
|
def recursive_parse_component(element: dict) -> dict:
|
||||||
assert animation["time"] == 0 # 0이 아닌 경우 확인 필요
|
if "name" in element:
|
||||||
animation["duration"] = animation["duration"] * extend_rate
|
result_element_name_type = {element["name"]: element["type"]}
|
||||||
except Exception as e:
|
else:
|
||||||
print(
|
result_element_name_type = {}
|
||||||
f"[extend_template_duration] Error processing element: {elem}, {e}"
|
|
||||||
)
|
if element["type"] == "composition":
|
||||||
|
minor_component_list = [
|
||||||
return new_template
|
recursive_parse_component(minor) for minor in element["elements"]
|
||||||
|
]
|
||||||
|
# WARNING: Same name component should shroud other component
|
||||||
|
for minor_component in minor_component_list:
|
||||||
|
result_element_name_type.update(minor_component)
|
||||||
|
|
||||||
|
return result_element_name_type
|
||||||
|
|
||||||
|
result = {}
|
||||||
|
for result_element_dict in [
|
||||||
|
recursive_parse_component(component) for component in template_source
|
||||||
|
]:
|
||||||
|
result.update(result_element_dict)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
async def template_connect_resource_blackbox(
|
||||||
|
self,
|
||||||
|
template_id: str,
|
||||||
|
image_url_list: list[str],
|
||||||
|
lyric: str,
|
||||||
|
music_url: str,
|
||||||
|
) -> dict:
|
||||||
|
"""템플릿 정보와 이미지/가사/음악 리소스를 매핑합니다.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
- 이미지는 순차적으로 집어넣기
|
||||||
|
- 가사는 개행마다 한 텍스트 삽입
|
||||||
|
- Template에 audio-music 항목이 있어야 함
|
||||||
|
"""
|
||||||
|
template_data = await self.get_one_template_data(template_id)
|
||||||
|
template_component_data = self.parse_template_component_name(
|
||||||
|
template_data["source"]["elements"]
|
||||||
|
)
|
||||||
|
|
||||||
|
lyric = lyric.replace("\r", "")
|
||||||
|
lyric_splited = lyric.split("\n")
|
||||||
|
modifications = {}
|
||||||
|
|
||||||
|
for idx, (template_component_name, template_type) in enumerate(
|
||||||
|
template_component_data.items()
|
||||||
|
):
|
||||||
|
match template_type:
|
||||||
|
case "image":
|
||||||
|
modifications[template_component_name] = image_url_list[
|
||||||
|
idx % len(image_url_list)
|
||||||
|
]
|
||||||
|
case "text":
|
||||||
|
modifications[template_component_name] = lyric_splited[
|
||||||
|
idx % len(lyric_splited)
|
||||||
|
]
|
||||||
|
|
||||||
|
modifications["audio-music"] = music_url
|
||||||
|
|
||||||
|
return modifications
|
||||||
|
|
||||||
|
def elements_connect_resource_blackbox(
|
||||||
|
self,
|
||||||
|
elements: list,
|
||||||
|
image_url_list: list[str],
|
||||||
|
lyric: str,
|
||||||
|
music_url: str,
|
||||||
|
) -> dict:
|
||||||
|
"""elements 정보와 이미지/가사/음악 리소스를 매핑합니다."""
|
||||||
|
template_component_data = self.parse_template_component_name(elements)
|
||||||
|
|
||||||
|
lyric = lyric.replace("\r", "")
|
||||||
|
lyric_splited = lyric.split("\n")
|
||||||
|
modifications = {}
|
||||||
|
|
||||||
|
for idx, (template_component_name, template_type) in enumerate(
|
||||||
|
template_component_data.items()
|
||||||
|
):
|
||||||
|
match template_type:
|
||||||
|
case "image":
|
||||||
|
modifications[template_component_name] = image_url_list[
|
||||||
|
idx % len(image_url_list)
|
||||||
|
]
|
||||||
|
case "text":
|
||||||
|
modifications[template_component_name] = lyric_splited[
|
||||||
|
idx % len(lyric_splited)
|
||||||
|
]
|
||||||
|
|
||||||
|
modifications["audio-music"] = music_url
|
||||||
|
|
||||||
|
return modifications
|
||||||
|
|
||||||
|
def modify_element(self, elements: list, modification: dict) -> list:
|
||||||
|
"""elements의 source를 modification에 따라 수정합니다."""
|
||||||
|
|
||||||
|
def recursive_modify(element: dict) -> None:
|
||||||
|
if "name" in element:
|
||||||
|
match element["type"]:
|
||||||
|
case "image":
|
||||||
|
element["source"] = modification[element["name"]]
|
||||||
|
case "audio":
|
||||||
|
element["source"] = modification.get(element["name"], "")
|
||||||
|
case "video":
|
||||||
|
element["source"] = modification[element["name"]]
|
||||||
|
case "text":
|
||||||
|
element["source"] = modification.get(element["name"], "")
|
||||||
|
case "composition":
|
||||||
|
for minor in element["elements"]:
|
||||||
|
recursive_modify(minor)
|
||||||
|
|
||||||
|
for minor in elements:
|
||||||
|
recursive_modify(minor)
|
||||||
|
|
||||||
|
return elements
|
||||||
|
|
||||||
|
async def make_creatomate_call(
|
||||||
|
self, template_id: str, modifications: dict
|
||||||
|
) -> dict:
|
||||||
|
"""Creatomate에 렌더링 요청을 보냅니다.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
response에 요청 정보가 있으니 폴링 필요
|
||||||
|
"""
|
||||||
|
url = f"{self.BASE_URL}/v2/renders"
|
||||||
|
data = {
|
||||||
|
"template_id": template_id,
|
||||||
|
"modifications": modifications,
|
||||||
|
}
|
||||||
|
response = await self._request("POST", url, timeout=60.0, json=data)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
async def make_creatomate_custom_call(self, source: dict) -> dict:
|
||||||
|
"""템플릿 없이 Creatomate에 커스텀 렌더링 요청을 보냅니다.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
response에 요청 정보가 있으니 폴링 필요
|
||||||
|
"""
|
||||||
|
url = f"{self.BASE_URL}/v2/renders"
|
||||||
|
response = await self._request("POST", url, timeout=60.0, json=source)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
# 하위 호환성을 위한 별칭 (deprecated)
|
||||||
|
async def make_creatomate_custom_call_async(self, source: dict) -> dict:
|
||||||
|
"""템플릿 없이 Creatomate에 커스텀 렌더링 요청을 보냅니다.
|
||||||
|
|
||||||
|
Deprecated: make_creatomate_custom_call()을 사용하세요.
|
||||||
|
"""
|
||||||
|
return await self.make_creatomate_custom_call(source)
|
||||||
|
|
||||||
|
async def get_render_status(self, render_id: str) -> dict:
|
||||||
|
"""렌더링 작업의 상태를 조회합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
render_id: Creatomate 렌더 ID
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
렌더링 상태 정보
|
||||||
|
|
||||||
|
Note:
|
||||||
|
상태 값:
|
||||||
|
- planned: 예약됨
|
||||||
|
- waiting: 대기 중
|
||||||
|
- transcribing: 트랜스크립션 중
|
||||||
|
- rendering: 렌더링 중
|
||||||
|
- succeeded: 성공
|
||||||
|
- failed: 실패
|
||||||
|
"""
|
||||||
|
url = f"{self.BASE_URL}/v1/renders/{render_id}"
|
||||||
|
response = await self._request("GET", url, timeout=30.0)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
# 하위 호환성을 위한 별칭 (deprecated)
|
||||||
|
async def get_render_status_async(self, render_id: str) -> dict:
|
||||||
|
"""렌더링 작업의 상태를 조회합니다.
|
||||||
|
|
||||||
|
Deprecated: get_render_status()를 사용하세요.
|
||||||
|
"""
|
||||||
|
return await self.get_render_status(render_id)
|
||||||
|
|
||||||
|
def calc_scene_duration(self, template: dict) -> float:
|
||||||
|
"""템플릿의 전체 장면 duration을 계산합니다."""
|
||||||
|
total_template_duration = 0.0
|
||||||
|
|
||||||
|
for elem in template["source"]["elements"]:
|
||||||
|
try:
|
||||||
|
if elem["type"] == "audio":
|
||||||
|
continue
|
||||||
|
total_template_duration += elem["duration"]
|
||||||
|
if "animations" not in elem:
|
||||||
|
continue
|
||||||
|
for animation in elem["animations"]:
|
||||||
|
assert animation["time"] == 0 # 0이 아닌 경우 확인 필요
|
||||||
|
if animation["transition"]:
|
||||||
|
total_template_duration -= animation["duration"]
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[calc_scene_duration] Error processing element: {elem}, {e}")
|
||||||
|
|
||||||
|
return total_template_duration
|
||||||
|
|
||||||
|
def extend_template_duration(self, template: dict, target_duration: float) -> dict:
|
||||||
|
"""템플릿의 duration을 target_duration으로 확장합니다."""
|
||||||
|
template["duration"] = target_duration
|
||||||
|
total_template_duration = self.calc_scene_duration(template)
|
||||||
|
extend_rate = target_duration / total_template_duration
|
||||||
|
new_template = copy.deepcopy(template)
|
||||||
|
|
||||||
|
for elem in new_template["source"]["elements"]:
|
||||||
|
try:
|
||||||
|
if elem["type"] == "audio":
|
||||||
|
continue
|
||||||
|
elem["duration"] = elem["duration"] * extend_rate
|
||||||
|
if "animations" not in elem:
|
||||||
|
continue
|
||||||
|
for animation in elem["animations"]:
|
||||||
|
assert animation["time"] == 0 # 0이 아닌 경우 확인 필요
|
||||||
|
animation["duration"] = animation["duration"] * extend_rate
|
||||||
|
except Exception as e:
|
||||||
|
print(
|
||||||
|
f"[extend_template_duration] Error processing element: {elem}, {e}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return new_template
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
import asyncio
|
||||||
|
from playwright.async_api import async_playwright
|
||||||
|
from urllib import parse
|
||||||
|
|
||||||
|
class nvMapPwScraper():
|
||||||
|
# cls vars
|
||||||
|
is_ready = False
|
||||||
|
_playwright = None
|
||||||
|
_browser = None
|
||||||
|
_context = None
|
||||||
|
_win_width = 1280
|
||||||
|
_win_height = 720
|
||||||
|
_max_retry = 30 # place id timeout threshold seconds
|
||||||
|
|
||||||
|
# instance var
|
||||||
|
page = None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def default_context_builder(cls):
|
||||||
|
context_builder_dict = {}
|
||||||
|
context_builder_dict['viewport'] = {
|
||||||
|
'width' : cls._win_width,
|
||||||
|
'height' : cls._win_height
|
||||||
|
}
|
||||||
|
context_builder_dict['screen'] = {
|
||||||
|
'width' : cls._win_width,
|
||||||
|
'height' : cls._win_height
|
||||||
|
}
|
||||||
|
context_builder_dict['user_agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
|
||||||
|
context_builder_dict['locale'] = 'ko-KR'
|
||||||
|
context_builder_dict['timezone_id']='Asia/Seoul'
|
||||||
|
|
||||||
|
return context_builder_dict
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def initiate_scraper(cls):
|
||||||
|
if not cls._playwright:
|
||||||
|
cls._playwright = await async_playwright().start()
|
||||||
|
if not cls._browser:
|
||||||
|
cls._browser = await cls._playwright.chromium.launch(headless=True)
|
||||||
|
if not cls._context:
|
||||||
|
cls._context = await cls._browser.new_context(**cls.default_context_builder())
|
||||||
|
cls.is_ready = True
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
if not self.is_ready:
|
||||||
|
raise Exception("nvMapScraper is not initiated")
|
||||||
|
|
||||||
|
async def __aenter__(self):
|
||||||
|
await self.create_page()
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, exc_type, exc, tb):
|
||||||
|
await self.page.close()
|
||||||
|
|
||||||
|
async def create_page(self):
|
||||||
|
self.page = await self._context.new_page()
|
||||||
|
await self.page.add_init_script(
|
||||||
|
'''const defaultGetter = Object.getOwnPropertyDescriptor(
|
||||||
|
Navigator.prototype,
|
||||||
|
"webdriver"
|
||||||
|
).get;
|
||||||
|
defaultGetter.apply(navigator);
|
||||||
|
defaultGetter.toString();
|
||||||
|
Object.defineProperty(Navigator.prototype, "webdriver", {
|
||||||
|
set: undefined,
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true,
|
||||||
|
get: new Proxy(defaultGetter, {
|
||||||
|
apply: (target, thisArg, args) => {
|
||||||
|
Reflect.apply(target, thisArg, args);
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const patchedGetter = Object.getOwnPropertyDescriptor(
|
||||||
|
Navigator.prototype,
|
||||||
|
"webdriver"
|
||||||
|
).get;
|
||||||
|
patchedGetter.apply(navigator);
|
||||||
|
patchedGetter.toString();''')
|
||||||
|
|
||||||
|
await self.page.set_extra_http_headers({
|
||||||
|
'sec-ch-ua': '\"Not?A_Brand\";v=\"99\", \"Chromium\";v=\"130\"'
|
||||||
|
})
|
||||||
|
await self.page.goto("http://google.com")
|
||||||
|
|
||||||
|
async def goto_url(self, url, wait_until="domcontentloaded", timeout=20000):
|
||||||
|
page = self.page
|
||||||
|
await page.goto(url, wait_until=wait_until, timeout=timeout)
|
||||||
|
|
||||||
|
async def get_place_id_url(self, selected):
|
||||||
|
|
||||||
|
title = selected['title'].replace("<b>", "").replace("</b>", "")
|
||||||
|
address = selected.get('roadAddress', selected['address']).replace("<b>", "").replace("</b>", "")
|
||||||
|
encoded_query = parse.quote(f"{address} {title}")
|
||||||
|
url = f"https://map.naver.com/p/search/{encoded_query}"
|
||||||
|
|
||||||
|
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
|
||||||
|
|
||||||
|
if "/place/" in self.page.url:
|
||||||
|
return self.page.url
|
||||||
|
|
||||||
|
url = self.page.url.replace("?","?isCorrectAnswer=true&")
|
||||||
|
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
|
||||||
|
|
||||||
|
if "/place/" in self.page.url:
|
||||||
|
return self.page.url
|
||||||
|
|
||||||
|
if (count == self._max_retry / 2):
|
||||||
|
raise Exception("Failed to identify place id. loading timeout")
|
||||||
|
else:
|
||||||
|
raise Exception("Failed to identify place id. item is ambiguous")
|
||||||
@@ -1,114 +1,203 @@
|
|||||||
import json
|
import asyncio
|
||||||
import re
|
import json
|
||||||
|
import logging
|
||||||
import aiohttp
|
import re
|
||||||
|
|
||||||
from config import crawler_settings
|
import aiohttp
|
||||||
|
import bs4
|
||||||
|
|
||||||
class GraphQLException(Exception):
|
from config import crawler_settings
|
||||||
pass
|
|
||||||
|
# 로거 설정
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
class NvMapScraper:
|
|
||||||
GRAPHQL_URL: str = "https://pcmap-api.place.naver.com/graphql"
|
|
||||||
|
class GraphQLException(Exception):
|
||||||
OVERVIEW_QUERY: str = """
|
"""GraphQL 요청 실패 시 발생하는 예외"""
|
||||||
query getAccommodation($id: String!, $deviceType: String) {
|
pass
|
||||||
business: placeDetail(input: {id: $id, isNx: true, deviceType: $deviceType}) {
|
|
||||||
base {
|
|
||||||
id
|
class CrawlingTimeoutException(Exception):
|
||||||
name
|
"""크롤링 타임아웃 시 발생하는 예외"""
|
||||||
category
|
pass
|
||||||
roadAddress
|
|
||||||
address
|
|
||||||
phone
|
class NvMapScraper:
|
||||||
virtualPhone
|
"""네이버 지도 GraphQL API 스크래퍼
|
||||||
microReviews
|
|
||||||
conveniences
|
네이버 지도에서 숙소/장소 정보를 크롤링합니다.
|
||||||
visitorReviewsTotal
|
"""
|
||||||
}
|
|
||||||
images { images { origin url } }
|
GRAPHQL_URL: str = "https://pcmap-api.place.naver.com/graphql"
|
||||||
cpImages(source: [ugcImage]) { images { origin url } }
|
REQUEST_TIMEOUT = 120 # 초
|
||||||
}
|
|
||||||
}"""
|
OVERVIEW_QUERY: str = """
|
||||||
|
query getAccommodation($id: String!, $deviceType: String) {
|
||||||
DEFAULT_HEADERS: dict = {
|
business: placeDetail(input: {id: $id, isNx: true, deviceType: $deviceType}) {
|
||||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
|
base {
|
||||||
"Referer": "https://map.naver.com/",
|
id
|
||||||
"Origin": "https://map.naver.com",
|
name
|
||||||
"Content-Type": "application/json",
|
category
|
||||||
}
|
roadAddress
|
||||||
|
address
|
||||||
def __init__(self, url: str, cookies: str | None = None):
|
phone
|
||||||
self.url = url
|
virtualPhone
|
||||||
self.cookies = (
|
microReviews
|
||||||
cookies if cookies is not None else crawler_settings.NAVER_COOKIES
|
conveniences
|
||||||
)
|
visitorReviewsTotal
|
||||||
self.scrap_type: str | None = None
|
}
|
||||||
self.rawdata: dict | None = None
|
images { images { origin url } }
|
||||||
self.image_link_list: list[str] | None = None
|
cpImages(source: [ugcImage]) { images { origin url } }
|
||||||
self.base_info: dict | None = None
|
}
|
||||||
|
}"""
|
||||||
def _get_request_headers(self) -> dict:
|
|
||||||
headers = self.DEFAULT_HEADERS.copy()
|
DEFAULT_HEADERS: dict = {
|
||||||
if self.cookies:
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
|
||||||
headers["Cookie"] = self.cookies
|
"Referer": "https://map.naver.com/",
|
||||||
return headers
|
"Origin": "https://map.naver.com",
|
||||||
|
"Content-Type": "application/json",
|
||||||
def parse_url(self) -> str:
|
}
|
||||||
place_pattern = r"/place/(\d+)"
|
|
||||||
match = re.search(place_pattern, self.url)
|
def __init__(self, url: str, cookies: str | None = None):
|
||||||
if not match:
|
self.url = url
|
||||||
raise GraphQLException("Failed to parse place ID from URL")
|
self.cookies = (
|
||||||
return match[1]
|
cookies if cookies is not None else crawler_settings.NAVER_COOKIES
|
||||||
|
)
|
||||||
async def scrap(self):
|
self.scrap_type: str | None = None
|
||||||
try:
|
self.rawdata: dict | None = None
|
||||||
place_id = self.parse_url()
|
self.image_link_list: list[str] | None = None
|
||||||
data = await self._call_get_accommodation(place_id)
|
self.base_info: dict | None = None
|
||||||
self.rawdata = data
|
self.facility_info: str | None = None
|
||||||
self.image_link_list = [
|
|
||||||
nv_image["origin"]
|
def _get_request_headers(self) -> dict:
|
||||||
for nv_image in data["data"]["business"]["images"]["images"]
|
headers = self.DEFAULT_HEADERS.copy()
|
||||||
]
|
if self.cookies:
|
||||||
self.base_info = data["data"]["business"]["base"]
|
headers["Cookie"] = self.cookies
|
||||||
self.scrap_type = "GraphQL"
|
return headers
|
||||||
|
|
||||||
except GraphQLException:
|
async def parse_url(self) -> str:
|
||||||
print("fallback")
|
"""URL에서 place ID를 추출합니다. 단축 URL인 경우 실제 URL로 변환합니다."""
|
||||||
self.scrap_type = "Playwright"
|
place_pattern = r"/place/(\d+)"
|
||||||
pass # 나중에 pw 이용한 crawling으로 fallback 추가
|
|
||||||
|
# URL에 place가 없는 경우 단축 URL 처리
|
||||||
return
|
if "place" not in self.url:
|
||||||
|
if "naver.me" in self.url:
|
||||||
async def _call_get_accommodation(self, place_id: str) -> dict:
|
async with aiohttp.ClientSession() as session:
|
||||||
payload = {
|
async with session.get(self.url) as response:
|
||||||
"operationName": "getAccommodation",
|
self.url = str(response.url)
|
||||||
"variables": {"id": place_id, "deviceType": "pc"},
|
else:
|
||||||
"query": self.OVERVIEW_QUERY,
|
raise GraphQLException("This URL does not contain a place ID")
|
||||||
}
|
|
||||||
json_payload = json.dumps(payload)
|
match = re.search(place_pattern, self.url)
|
||||||
|
if not match:
|
||||||
async with aiohttp.ClientSession() as session:
|
raise GraphQLException("Failed to parse place ID from URL")
|
||||||
async with session.post(
|
return match[1]
|
||||||
self.GRAPHQL_URL, data=json_payload, headers=self._get_request_headers()
|
|
||||||
) as response:
|
async def scrap(self):
|
||||||
if response.status == 200:
|
try:
|
||||||
return await response.json()
|
place_id = await self.parse_url()
|
||||||
else:
|
data = await self._call_get_accommodation(place_id)
|
||||||
print("실패 상태 코드:", response.status)
|
self.rawdata = data
|
||||||
raise GraphQLException(
|
fac_data = await self._get_facility_string(place_id)
|
||||||
f"Request failed with status {response.status}"
|
self.rawdata["facilities"] = fac_data
|
||||||
)
|
self.image_link_list = [
|
||||||
|
nv_image["origin"]
|
||||||
|
for nv_image in data["data"]["business"]["images"]["images"]
|
||||||
# if __name__ == "__main__":
|
]
|
||||||
# import asyncio
|
self.base_info = data["data"]["business"]["base"]
|
||||||
|
self.facility_info = fac_data
|
||||||
# url = "https://map.naver.com/p/search/%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84/place/1133638931?c=14.70,0,0,0,dh&placePath=/photo?businessCategory=pension&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5×tamp=202512191123&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5×tamp=202512191007&from=map&entry=bmp&filterType=%EC%97%85%EC%B2%B4&businessCategory=pension"
|
self.scrap_type = "GraphQL"
|
||||||
# scraper = NvMapScraper(url)
|
|
||||||
# asyncio.run(scraper.scrap())
|
except GraphQLException:
|
||||||
# print(scraper.image_link_list)
|
print("fallback")
|
||||||
# print(len(scraper.image_link_list) if scraper.image_link_list else 0)
|
self.scrap_type = "Playwright"
|
||||||
# print(scraper.base_info)
|
pass # 나중에 pw 이용한 crawling으로 fallback 추가
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
async def _call_get_accommodation(self, place_id: str) -> dict:
|
||||||
|
"""GraphQL API를 호출하여 숙소 정보를 가져옵니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
place_id: 네이버 지도 장소 ID
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
GraphQL 응답 데이터
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
GraphQLException: API 호출 실패 시
|
||||||
|
CrawlingTimeoutException: 타임아웃 발생 시
|
||||||
|
"""
|
||||||
|
payload = {
|
||||||
|
"operationName": "getAccommodation",
|
||||||
|
"variables": {"id": place_id, "deviceType": "pc"},
|
||||||
|
"query": self.OVERVIEW_QUERY,
|
||||||
|
}
|
||||||
|
json_payload = json.dumps(payload)
|
||||||
|
timeout = aiohttp.ClientTimeout(total=self.REQUEST_TIMEOUT)
|
||||||
|
|
||||||
|
try:
|
||||||
|
logger.info(f"[NvMapScraper] Requesting place_id: {place_id}")
|
||||||
|
print(f"[NvMapScraper] Requesting place_id: {place_id}")
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||||
|
async with session.post(
|
||||||
|
self.GRAPHQL_URL,
|
||||||
|
data=json_payload,
|
||||||
|
headers=self._get_request_headers()
|
||||||
|
) as response:
|
||||||
|
if response.status == 200:
|
||||||
|
logger.info(f"[NvMapScraper] SUCCESS - place_id: {place_id}")
|
||||||
|
print(f"[NvMapScraper] SUCCESS - place_id: {place_id}")
|
||||||
|
return await response.json()
|
||||||
|
|
||||||
|
# 실패 상태 코드
|
||||||
|
logger.error(f"[NvMapScraper] Failed with status {response.status} - place_id: {place_id}")
|
||||||
|
print(f"[NvMapScraper] 실패 상태 코드: {response.status}")
|
||||||
|
raise GraphQLException(
|
||||||
|
f"Request failed with status {response.status}"
|
||||||
|
)
|
||||||
|
|
||||||
|
except (TimeoutError, asyncio.TimeoutError):
|
||||||
|
logger.error(f"[NvMapScraper] Timeout - place_id: {place_id}")
|
||||||
|
print(f"[NvMapScraper] Timeout - place_id: {place_id}")
|
||||||
|
raise CrawlingTimeoutException(f"Request timed out after {self.REQUEST_TIMEOUT}s")
|
||||||
|
|
||||||
|
except aiohttp.ClientError as e:
|
||||||
|
logger.error(f"[NvMapScraper] Client error: {e}")
|
||||||
|
print(f"[NvMapScraper] Client error: {e}")
|
||||||
|
raise GraphQLException(f"Client error: {e}")
|
||||||
|
|
||||||
|
async def _get_facility_string(self, place_id: str) -> str | None:
|
||||||
|
"""숙소 페이지에서 편의시설 정보를 크롤링합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
place_id: 네이버 지도 장소 ID
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
편의시설 정보 문자열 또는 None
|
||||||
|
"""
|
||||||
|
url = f"https://pcmap.place.naver.com/accommodation/{place_id}/home"
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(url, headers=self._get_request_headers()) as response:
|
||||||
|
soup = bs4.BeautifulSoup(await response.read(), "html.parser")
|
||||||
|
c_elem = soup.find("span", "place_blind", string="편의")
|
||||||
|
if c_elem:
|
||||||
|
facilities = c_elem.parent.parent.find("div").string
|
||||||
|
return facilities
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[NvMapScraper] Failed to get facility info: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# if __name__ == "__main__":
|
||||||
|
# import asyncio
|
||||||
|
|
||||||
|
# url = "https://map.naver.com/p/search/%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84/place/1133638931?c=14.70,0,0,0,dh&placePath=/photo?businessCategory=pension&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5×tamp=202512191123&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5×tamp=202512191007&from=map&entry=bmp&filterType=%EC%97%85%EC%B2%B4&businessCategory=pension"
|
||||||
|
# scraper = NvMapScraper(url)
|
||||||
|
# asyncio.run(scraper.scrap())
|
||||||
|
# print(scraper.image_link_list)
|
||||||
|
# print(len(scraper.image_link_list) if scraper.image_link_list else 0)
|
||||||
|
# print(scraper.base_info)
|
||||||
@@ -1,64 +1,468 @@
|
|||||||
import requests
|
"""
|
||||||
from pathlib import Path
|
Azure Blob Storage 업로드 유틸리티
|
||||||
|
|
||||||
SAS_TOKEN = "sp=racwdl&st=2025-12-01T00:13:29Z&se=2026-07-31T08:28:29Z&spr=https&sv=2024-11-04&sr=c&sig=7fE2ozVBPu3Gq43%2FZDxEYdEcPLDXyNVfTf16IBasmVQ%3D"
|
Azure Blob Storage에 파일을 업로드하는 클래스를 제공합니다.
|
||||||
|
파일 경로 또는 바이트 데이터를 직접 업로드할 수 있습니다.
|
||||||
def upload_music_to_azure_blob(file_path = "스테이 머뭄_1.mp3", url = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/dev-user-idx/dev-task-idx/test_my_mp3_should_now_exist.mp3"):
|
|
||||||
access_url = f"{url}?{SAS_TOKEN}"
|
URL 경로 형식:
|
||||||
headers = {
|
- 음악: {BASE_URL}/{task_id}/song/{파일명}
|
||||||
"Content-Type": "audio/mpeg",
|
- 영상: {BASE_URL}/{task_id}/video/{파일명}
|
||||||
"x-ms-blob-type": "BlockBlob"
|
- 이미지: {BASE_URL}/{task_id}/image/{파일명}
|
||||||
}
|
|
||||||
with open(file_path, "rb") as file:
|
사용 예시:
|
||||||
response = requests.put(access_url, data=file, headers=headers)
|
from app.utils.upload_blob_as_request import AzureBlobUploader
|
||||||
if response.status_code in [200, 201]:
|
|
||||||
print(f"Success Status Code: {response.status_code}")
|
uploader = AzureBlobUploader(task_id="task-123")
|
||||||
else:
|
|
||||||
print(f"Failed Status Code: {response.status_code}")
|
# 파일 경로로 업로드
|
||||||
print(f"Response: {response.text}")
|
success = await uploader.upload_music(file_path="my_song.mp3")
|
||||||
|
success = await uploader.upload_video(file_path="my_video.mp4")
|
||||||
def upload_video_to_azure_blob(file_path = "스테이 머뭄.mp4", url = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/dev-user-idx/dev-task-idx/test_my_mp3_should_now_exist.mp4"):
|
success = await uploader.upload_image(file_path="my_image.png")
|
||||||
access_url = f"{url}?{SAS_TOKEN}"
|
|
||||||
headers = {
|
# 바이트 데이터로 직접 업로드 (media 저장 없이)
|
||||||
"Content-Type": "video/mp4",
|
success = await uploader.upload_music_bytes(audio_bytes, "my_song")
|
||||||
"x-ms-blob-type": "BlockBlob"
|
success = await uploader.upload_video_bytes(video_bytes, "my_video")
|
||||||
}
|
success = await uploader.upload_image_bytes(image_bytes, "my_image.png")
|
||||||
with open(file_path, "rb") as file:
|
|
||||||
response = requests.put(access_url, data=file, headers=headers)
|
print(uploader.public_url) # 마지막 업로드의 공개 URL
|
||||||
|
|
||||||
if response.status_code in [200, 201]:
|
성능 최적화:
|
||||||
print(f"Success Status Code: {response.status_code}")
|
- HTTP 클라이언트 재사용: 모듈 레벨의 공유 클라이언트로 커넥션 풀 재사용
|
||||||
else:
|
- 동시 업로드: 공유 클라이언트를 통해 동시 요청 처리가 개선됩니다.
|
||||||
print(f"Failed Status Code: {response.status_code}")
|
"""
|
||||||
print(f"Response: {response.text}")
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
def upload_image_to_azure_blob(file_path = "스테이 머뭄.png", url = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/dev-user-idx/dev-task-idx/test_my_mp3_should_now_exist.png"):
|
import time
|
||||||
access_url = f"{url}?{SAS_TOKEN}"
|
from pathlib import Path
|
||||||
extension = Path(file_path).suffix.lower()
|
|
||||||
content_types = {
|
import aiofiles
|
||||||
".jpg": "image/jpeg",
|
import httpx
|
||||||
".jpeg": "image/jpeg",
|
|
||||||
".png": "image/png",
|
from config import azure_blob_settings
|
||||||
".gif": "image/gif",
|
|
||||||
".webp": "image/webp",
|
# 로거 설정
|
||||||
".bmp": "image/bmp"
|
logger = logging.getLogger(__name__)
|
||||||
}
|
|
||||||
content_type = content_types.get(extension, "image/jpeg")
|
# =============================================================================
|
||||||
headers = {
|
# 모듈 레벨 공유 HTTP 클라이언트 (싱글톤 패턴)
|
||||||
"Content-Type": content_type,
|
# =============================================================================
|
||||||
"x-ms-blob-type": "BlockBlob"
|
|
||||||
}
|
# 모듈 레벨 공유 HTTP 클라이언트 (커넥션 풀 재사용)
|
||||||
with open(file_path, "rb") as file:
|
_shared_blob_client: httpx.AsyncClient | None = None
|
||||||
response = requests.put(access_url, data=file, headers=headers)
|
|
||||||
|
|
||||||
if response.status_code in [200, 201]:
|
async def get_shared_blob_client() -> httpx.AsyncClient:
|
||||||
print(f"Success Status Code: {response.status_code}")
|
"""공유 HTTP 클라이언트를 반환합니다. 없으면 생성합니다."""
|
||||||
else:
|
global _shared_blob_client
|
||||||
print(f"Failed Status Code: {response.status_code}")
|
if _shared_blob_client is None or _shared_blob_client.is_closed:
|
||||||
print(f"Response: {response.text}")
|
print("[AzureBlobUploader] Creating shared HTTP client...")
|
||||||
|
_shared_blob_client = httpx.AsyncClient(
|
||||||
|
timeout=httpx.Timeout(180.0, connect=10.0),
|
||||||
upload_video_to_azure_blob()
|
limits=httpx.Limits(max_keepalive_connections=10, max_connections=20),
|
||||||
|
)
|
||||||
upload_image_to_azure_blob()
|
print("[AzureBlobUploader] Shared HTTP client created - "
|
||||||
|
"max_connections: 20, max_keepalive: 10")
|
||||||
|
return _shared_blob_client
|
||||||
|
|
||||||
|
|
||||||
|
async def close_shared_blob_client() -> None:
|
||||||
|
"""공유 HTTP 클라이언트를 닫습니다. 앱 종료 시 호출하세요."""
|
||||||
|
global _shared_blob_client
|
||||||
|
if _shared_blob_client is not None and not _shared_blob_client.is_closed:
|
||||||
|
await _shared_blob_client.aclose()
|
||||||
|
_shared_blob_client = None
|
||||||
|
print("[AzureBlobUploader] Shared HTTP client closed")
|
||||||
|
|
||||||
|
|
||||||
|
class AzureBlobUploader:
|
||||||
|
"""Azure Blob Storage 업로드 클래스
|
||||||
|
|
||||||
|
Azure Blob Storage에 음악, 영상, 이미지 파일을 업로드합니다.
|
||||||
|
URL 형식: {BASE_URL}/{task_id}/{category}/{file_name}?{SAS_TOKEN}
|
||||||
|
|
||||||
|
카테고리별 경로:
|
||||||
|
- 음악: {task_id}/song/{file_name}
|
||||||
|
- 영상: {task_id}/video/{file_name}
|
||||||
|
- 이미지: {task_id}/image/{file_name}
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
task_id: 작업 고유 식별자
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Content-Type 매핑
|
||||||
|
IMAGE_CONTENT_TYPES = {
|
||||||
|
".jpg": "image/jpeg",
|
||||||
|
".jpeg": "image/jpeg",
|
||||||
|
".png": "image/png",
|
||||||
|
".gif": "image/gif",
|
||||||
|
".webp": "image/webp",
|
||||||
|
".bmp": "image/bmp",
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, task_id: str):
|
||||||
|
"""AzureBlobUploader 초기화
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task_id: 작업 고유 식별자
|
||||||
|
"""
|
||||||
|
self._task_id = task_id
|
||||||
|
self._base_url = azure_blob_settings.AZURE_BLOB_BASE_URL
|
||||||
|
self._sas_token = azure_blob_settings.AZURE_BLOB_SAS_TOKEN
|
||||||
|
self._last_public_url: str = ""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def task_id(self) -> str:
|
||||||
|
"""작업 고유 식별자"""
|
||||||
|
return self._task_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def public_url(self) -> str:
|
||||||
|
"""마지막 업로드의 공개 URL (SAS 토큰 제외)"""
|
||||||
|
return self._last_public_url
|
||||||
|
|
||||||
|
def _build_upload_url(self, category: str, file_name: str) -> str:
|
||||||
|
"""업로드 URL 생성 (SAS 토큰 포함)"""
|
||||||
|
# SAS 토큰 앞뒤의 ?, ', " 제거
|
||||||
|
sas_token = self._sas_token.strip("?'\"")
|
||||||
|
return (
|
||||||
|
f"{self._base_url}/{self._task_id}/{category}/{file_name}?{sas_token}"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _build_public_url(self, category: str, file_name: str) -> str:
|
||||||
|
"""공개 URL 생성 (SAS 토큰 제외)"""
|
||||||
|
return f"{self._base_url}/{self._task_id}/{category}/{file_name}"
|
||||||
|
|
||||||
|
async def _upload_bytes(
|
||||||
|
self,
|
||||||
|
file_content: bytes,
|
||||||
|
upload_url: str,
|
||||||
|
headers: dict,
|
||||||
|
timeout: float,
|
||||||
|
log_prefix: str,
|
||||||
|
) -> bool:
|
||||||
|
"""바이트 데이터를 업로드하는 공통 내부 메서드
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_content: 업로드할 바이트 데이터
|
||||||
|
upload_url: 업로드 URL
|
||||||
|
headers: HTTP 헤더
|
||||||
|
timeout: 요청 타임아웃 (초)
|
||||||
|
log_prefix: 로그 접두사
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 업로드 성공 여부
|
||||||
|
"""
|
||||||
|
size = len(file_content)
|
||||||
|
start_time = time.perf_counter()
|
||||||
|
|
||||||
|
try:
|
||||||
|
logger.info(f"[{log_prefix}] Starting upload")
|
||||||
|
print(f"[{log_prefix}] Getting shared client...")
|
||||||
|
|
||||||
|
client = await get_shared_blob_client()
|
||||||
|
client_time = time.perf_counter()
|
||||||
|
elapsed_ms = (client_time - start_time) * 1000
|
||||||
|
print(f"[{log_prefix}] Client acquired in {elapsed_ms:.1f}ms")
|
||||||
|
|
||||||
|
print(f"[{log_prefix}] Starting upload... "
|
||||||
|
f"(size: {size} bytes, timeout: {timeout}s)")
|
||||||
|
|
||||||
|
response = await asyncio.wait_for(
|
||||||
|
client.put(upload_url, content=file_content, headers=headers),
|
||||||
|
timeout=timeout,
|
||||||
|
)
|
||||||
|
upload_time = time.perf_counter()
|
||||||
|
duration_ms = (upload_time - start_time) * 1000
|
||||||
|
|
||||||
|
if response.status_code in [200, 201]:
|
||||||
|
logger.info(f"[{log_prefix}] SUCCESS - Status: {response.status_code}")
|
||||||
|
print(f"[{log_prefix}] SUCCESS - Status: {response.status_code}, "
|
||||||
|
f"Duration: {duration_ms:.1f}ms")
|
||||||
|
print(f"[{log_prefix}] Public URL: {self._last_public_url}")
|
||||||
|
return True
|
||||||
|
|
||||||
|
# 업로드 실패
|
||||||
|
logger.error(f"[{log_prefix}] FAILED - Status: {response.status_code}")
|
||||||
|
print(f"[{log_prefix}] FAILED - Status: {response.status_code}, "
|
||||||
|
f"Duration: {duration_ms:.1f}ms")
|
||||||
|
print(f"[{log_prefix}] Response: {response.text[:500]}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
elapsed = time.perf_counter() - start_time
|
||||||
|
logger.error(f"[{log_prefix}] TIMEOUT after {elapsed:.1f}s")
|
||||||
|
print(f"[{log_prefix}] TIMEOUT after {elapsed:.1f}s")
|
||||||
|
return False
|
||||||
|
|
||||||
|
except httpx.ConnectError as e:
|
||||||
|
elapsed = time.perf_counter() - start_time
|
||||||
|
logger.error(f"[{log_prefix}] CONNECT_ERROR: {e}")
|
||||||
|
print(f"[{log_prefix}] CONNECT_ERROR after {elapsed:.1f}s - "
|
||||||
|
f"{type(e).__name__}: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
except httpx.ReadError as e:
|
||||||
|
elapsed = time.perf_counter() - start_time
|
||||||
|
logger.error(f"[{log_prefix}] READ_ERROR: {e}")
|
||||||
|
print(f"[{log_prefix}] READ_ERROR after {elapsed:.1f}s - "
|
||||||
|
f"{type(e).__name__}: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
elapsed = time.perf_counter() - start_time
|
||||||
|
logger.error(f"[{log_prefix}] ERROR: {type(e).__name__}: {e}")
|
||||||
|
print(f"[{log_prefix}] ERROR after {elapsed:.1f}s - "
|
||||||
|
f"{type(e).__name__}: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def _upload_file(
|
||||||
|
self,
|
||||||
|
file_path: str,
|
||||||
|
category: str,
|
||||||
|
content_type: str,
|
||||||
|
timeout: float,
|
||||||
|
log_prefix: str,
|
||||||
|
) -> bool:
|
||||||
|
"""파일을 Azure Blob Storage에 업로드하는 내부 메서드
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_path: 업로드할 파일 경로
|
||||||
|
category: 카테고리 (song, video, image)
|
||||||
|
content_type: Content-Type 헤더 값
|
||||||
|
timeout: 요청 타임아웃 (초)
|
||||||
|
log_prefix: 로그 접두사
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 업로드 성공 여부
|
||||||
|
"""
|
||||||
|
# 파일 경로에서 파일명 추출
|
||||||
|
file_name = Path(file_path).name
|
||||||
|
|
||||||
|
upload_url = self._build_upload_url(category, file_name)
|
||||||
|
self._last_public_url = self._build_public_url(category, file_name)
|
||||||
|
print(f"[{log_prefix}] URL (without SAS): {self._last_public_url}")
|
||||||
|
|
||||||
|
headers = {"Content-Type": content_type, "x-ms-blob-type": "BlockBlob"}
|
||||||
|
|
||||||
|
async with aiofiles.open(file_path, "rb") as file:
|
||||||
|
file_content = await file.read()
|
||||||
|
|
||||||
|
return await self._upload_bytes(
|
||||||
|
file_content=file_content,
|
||||||
|
upload_url=upload_url,
|
||||||
|
headers=headers,
|
||||||
|
timeout=timeout,
|
||||||
|
log_prefix=log_prefix,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def upload_music(self, file_path: str) -> bool:
|
||||||
|
"""음악 파일을 Azure Blob Storage에 업로드합니다.
|
||||||
|
|
||||||
|
URL 경로: {task_id}/song/{파일명}
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_path: 업로드할 파일 경로
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 업로드 성공 여부
|
||||||
|
|
||||||
|
Example:
|
||||||
|
uploader = AzureBlobUploader(task_id="task-123")
|
||||||
|
success = await uploader.upload_music(file_path="my_song.mp3")
|
||||||
|
print(uploader.public_url)
|
||||||
|
"""
|
||||||
|
return await self._upload_file(
|
||||||
|
file_path=file_path,
|
||||||
|
category="song",
|
||||||
|
content_type="audio/mpeg",
|
||||||
|
timeout=120.0,
|
||||||
|
log_prefix="upload_music",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def upload_music_bytes(
|
||||||
|
self, file_content: bytes, file_name: str
|
||||||
|
) -> bool:
|
||||||
|
"""음악 바이트 데이터를 Azure Blob Storage에 직접 업로드합니다.
|
||||||
|
|
||||||
|
URL 경로: {task_id}/song/{파일명}
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_content: 업로드할 파일 바이트 데이터
|
||||||
|
file_name: 저장할 파일명 (확장자가 없으면 .mp3 추가)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 업로드 성공 여부
|
||||||
|
|
||||||
|
Example:
|
||||||
|
uploader = AzureBlobUploader(task_id="task-123")
|
||||||
|
success = await uploader.upload_music_bytes(audio_bytes, "my_song")
|
||||||
|
print(uploader.public_url)
|
||||||
|
"""
|
||||||
|
# 확장자가 없으면 .mp3 추가
|
||||||
|
if not Path(file_name).suffix:
|
||||||
|
file_name = f"{file_name}.mp3"
|
||||||
|
|
||||||
|
upload_url = self._build_upload_url("song", file_name)
|
||||||
|
self._last_public_url = self._build_public_url("song", file_name)
|
||||||
|
log_prefix = "upload_music_bytes"
|
||||||
|
print(f"[{log_prefix}] URL (without SAS): {self._last_public_url}")
|
||||||
|
|
||||||
|
headers = {"Content-Type": "audio/mpeg", "x-ms-blob-type": "BlockBlob"}
|
||||||
|
|
||||||
|
return await self._upload_bytes(
|
||||||
|
file_content=file_content,
|
||||||
|
upload_url=upload_url,
|
||||||
|
headers=headers,
|
||||||
|
timeout=120.0,
|
||||||
|
log_prefix=log_prefix,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def upload_video(self, file_path: str) -> bool:
|
||||||
|
"""영상 파일을 Azure Blob Storage에 업로드합니다.
|
||||||
|
|
||||||
|
URL 경로: {task_id}/video/{파일명}
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_path: 업로드할 파일 경로
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 업로드 성공 여부
|
||||||
|
|
||||||
|
Example:
|
||||||
|
uploader = AzureBlobUploader(task_id="task-123")
|
||||||
|
success = await uploader.upload_video(file_path="my_video.mp4")
|
||||||
|
print(uploader.public_url)
|
||||||
|
"""
|
||||||
|
return await self._upload_file(
|
||||||
|
file_path=file_path,
|
||||||
|
category="video",
|
||||||
|
content_type="video/mp4",
|
||||||
|
timeout=180.0,
|
||||||
|
log_prefix="upload_video",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def upload_video_bytes(
|
||||||
|
self, file_content: bytes, file_name: str
|
||||||
|
) -> bool:
|
||||||
|
"""영상 바이트 데이터를 Azure Blob Storage에 직접 업로드합니다.
|
||||||
|
|
||||||
|
URL 경로: {task_id}/video/{파일명}
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_content: 업로드할 파일 바이트 데이터
|
||||||
|
file_name: 저장할 파일명 (확장자가 없으면 .mp4 추가)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 업로드 성공 여부
|
||||||
|
|
||||||
|
Example:
|
||||||
|
uploader = AzureBlobUploader(task_id="task-123")
|
||||||
|
success = await uploader.upload_video_bytes(video_bytes, "my_video")
|
||||||
|
print(uploader.public_url)
|
||||||
|
"""
|
||||||
|
# 확장자가 없으면 .mp4 추가
|
||||||
|
if not Path(file_name).suffix:
|
||||||
|
file_name = f"{file_name}.mp4"
|
||||||
|
|
||||||
|
upload_url = self._build_upload_url("video", file_name)
|
||||||
|
self._last_public_url = self._build_public_url("video", file_name)
|
||||||
|
log_prefix = "upload_video_bytes"
|
||||||
|
print(f"[{log_prefix}] URL (without SAS): {self._last_public_url}")
|
||||||
|
|
||||||
|
headers = {"Content-Type": "video/mp4", "x-ms-blob-type": "BlockBlob"}
|
||||||
|
|
||||||
|
return await self._upload_bytes(
|
||||||
|
file_content=file_content,
|
||||||
|
upload_url=upload_url,
|
||||||
|
headers=headers,
|
||||||
|
timeout=180.0,
|
||||||
|
log_prefix=log_prefix,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def upload_image(self, file_path: str) -> bool:
|
||||||
|
"""이미지 파일을 Azure Blob Storage에 업로드합니다.
|
||||||
|
|
||||||
|
URL 경로: {task_id}/image/{파일명}
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_path: 업로드할 파일 경로
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 업로드 성공 여부
|
||||||
|
|
||||||
|
Example:
|
||||||
|
uploader = AzureBlobUploader(task_id="task-123")
|
||||||
|
success = await uploader.upload_image(file_path="my_image.png")
|
||||||
|
print(uploader.public_url)
|
||||||
|
"""
|
||||||
|
extension = Path(file_path).suffix.lower()
|
||||||
|
content_type = self.IMAGE_CONTENT_TYPES.get(extension, "image/jpeg")
|
||||||
|
|
||||||
|
return await self._upload_file(
|
||||||
|
file_path=file_path,
|
||||||
|
category="image",
|
||||||
|
content_type=content_type,
|
||||||
|
timeout=60.0,
|
||||||
|
log_prefix="upload_image",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def upload_image_bytes(
|
||||||
|
self, file_content: bytes, file_name: str
|
||||||
|
) -> bool:
|
||||||
|
"""이미지 바이트 데이터를 Azure Blob Storage에 직접 업로드합니다.
|
||||||
|
|
||||||
|
URL 경로: {task_id}/image/{파일명}
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_content: 업로드할 파일 바이트 데이터
|
||||||
|
file_name: 저장할 파일명
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 업로드 성공 여부
|
||||||
|
|
||||||
|
Example:
|
||||||
|
uploader = AzureBlobUploader(task_id="task-123")
|
||||||
|
with open("my_image.png", "rb") as f:
|
||||||
|
content = f.read()
|
||||||
|
success = await uploader.upload_image_bytes(content, "my_image.png")
|
||||||
|
print(uploader.public_url)
|
||||||
|
"""
|
||||||
|
extension = Path(file_name).suffix.lower()
|
||||||
|
content_type = self.IMAGE_CONTENT_TYPES.get(extension, "image/jpeg")
|
||||||
|
|
||||||
|
upload_url = self._build_upload_url("image", file_name)
|
||||||
|
self._last_public_url = self._build_public_url("image", file_name)
|
||||||
|
log_prefix = "upload_image_bytes"
|
||||||
|
print(f"[{log_prefix}] URL (without SAS): {self._last_public_url}")
|
||||||
|
|
||||||
|
headers = {"Content-Type": content_type, "x-ms-blob-type": "BlockBlob"}
|
||||||
|
|
||||||
|
return await self._upload_bytes(
|
||||||
|
file_content=file_content,
|
||||||
|
upload_url=upload_url,
|
||||||
|
headers=headers,
|
||||||
|
timeout=60.0,
|
||||||
|
log_prefix=log_prefix,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# 사용 예시:
|
||||||
|
# import asyncio
|
||||||
|
#
|
||||||
|
# async def main():
|
||||||
|
# uploader = AzureBlobUploader(task_id="task-123")
|
||||||
|
#
|
||||||
|
# # 음악 업로드 -> {BASE_URL}/task-123/song/my_song.mp3
|
||||||
|
# await uploader.upload_music("my_song.mp3")
|
||||||
|
# print(uploader.public_url)
|
||||||
|
#
|
||||||
|
# # 영상 업로드 -> {BASE_URL}/task-123/video/my_video.mp4
|
||||||
|
# await uploader.upload_video("my_video.mp4")
|
||||||
|
# print(uploader.public_url)
|
||||||
|
#
|
||||||
|
# # 이미지 업로드 -> {BASE_URL}/task-123/image/my_image.png
|
||||||
|
# await uploader.upload_image("my_image.png")
|
||||||
|
# print(uploader.public_url)
|
||||||
|
#
|
||||||
|
# asyncio.run(main())
|
||||||
@@ -1,108 +1,746 @@
|
|||||||
"""
|
"""
|
||||||
Video API Endpoints (Test)
|
Video API Router
|
||||||
|
|
||||||
프론트엔드 개발을 위한 테스트용 엔드포인트입니다.
|
이 모듈은 Creatomate API를 통한 영상 생성 관련 API 엔드포인트를 정의합니다.
|
||||||
"""
|
|
||||||
|
엔드포인트 목록:
|
||||||
from datetime import datetime, timedelta
|
- POST /video/generate/{task_id}: 영상 생성 요청 (task_id로 Project/Lyric/Song 연결)
|
||||||
from typing import Optional
|
- GET /video/status/{creatomate_render_id}: Creatomate API 영상 생성 상태 조회
|
||||||
|
- GET /video/download/{task_id}: 영상 다운로드 상태 조회 (DB polling)
|
||||||
from fastapi import APIRouter
|
- GET /videos/: 완료된 영상 목록 조회 (페이지네이션)
|
||||||
from pydantic import BaseModel, Field
|
|
||||||
|
사용 예시:
|
||||||
router = APIRouter(prefix="/video", tags=["video"])
|
from app.video.api.routers.v1.video import router
|
||||||
|
app.include_router(router, prefix="/api/v1")
|
||||||
# =============================================================================
|
"""
|
||||||
# Schemas
|
|
||||||
# =============================================================================
|
from typing import Literal
|
||||||
|
|
||||||
|
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Query
|
||||||
class VideoGenerateResponse(BaseModel):
|
from sqlalchemy import func, select
|
||||||
"""영상 생성 응답 스키마"""
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
success: bool = Field(..., description="성공 여부")
|
from app.database.session import get_session
|
||||||
task_id: str = Field(..., description="작업 고유 식별자")
|
from app.dependencies.pagination import (
|
||||||
message: str = Field(..., description="응답 메시지")
|
PaginationParams,
|
||||||
error_message: Optional[str] = Field(None, description="에러 메시지")
|
get_pagination_params,
|
||||||
|
)
|
||||||
|
from app.home.models import Image, Project
|
||||||
class VideoStatusResponse(BaseModel):
|
from app.lyric.models import Lyric
|
||||||
"""영상 상태 조회 응답 스키마"""
|
from app.song.models import Song
|
||||||
|
from app.video.models import Video
|
||||||
task_id: str = Field(..., description="작업 고유 식별자")
|
from app.video.schemas.video_schema import (
|
||||||
status: str = Field(..., description="처리 상태 (processing, completed, failed)")
|
DownloadVideoResponse,
|
||||||
video_url: Optional[str] = Field(None, description="영상 URL")
|
GenerateVideoResponse,
|
||||||
|
PollingVideoResponse,
|
||||||
|
VideoListItem,
|
||||||
class VideoItem(BaseModel):
|
VideoRenderData,
|
||||||
"""영상 아이템 스키마"""
|
)
|
||||||
|
from app.video.worker.video_task import download_and_upload_video_to_blob
|
||||||
task_id: str = Field(..., description="작업 고유 식별자")
|
from app.utils.creatomate import CreatomateService
|
||||||
video_url: str = Field(..., description="영상 URL")
|
from app.utils.pagination import PaginatedResponse
|
||||||
created_at: datetime = Field(..., description="생성 일시")
|
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/video", tags=["video"])
|
||||||
class VideoListResponse(BaseModel):
|
|
||||||
"""영상 목록 응답 스키마"""
|
|
||||||
|
@router.get(
|
||||||
videos: list[VideoItem] = Field(..., description="영상 목록")
|
"/generate/{task_id}",
|
||||||
total: int = Field(..., description="전체 개수")
|
summary="영상 생성 요청",
|
||||||
|
description="""
|
||||||
|
Creatomate API를 통해 영상 생성을 요청합니다.
|
||||||
# =============================================================================
|
|
||||||
# Test Endpoints
|
## 경로 파라미터
|
||||||
# =============================================================================
|
- **task_id**: Project/Lyric/Song/Image의 task_id (필수) - 연관된 프로젝트, 가사, 노래, 이미지를 조회하는 데 사용
|
||||||
|
|
||||||
TEST_VIDEO_URL = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/dev-user-idx/dev-task-idx/1a584e86-6a74-417d-8cff-270ef60c8646.mp4"
|
## 쿼리 파라미터
|
||||||
|
- **orientation**: 영상 방향 (horizontal: 가로형, vertical: 세로형, 기본값: vertical) - 선택
|
||||||
|
|
||||||
@router.post(
|
## 자동 조회 정보
|
||||||
"/generate/{task_id}",
|
- **image_urls**: Image 테이블에서 task_id로 조회 (img_order 순서로 정렬)
|
||||||
summary="영상 생성 요청 (테스트)",
|
- **music_url**: Song 테이블의 song_result_url 사용
|
||||||
response_model=VideoGenerateResponse,
|
- **duration**: Song 테이블의 duration 사용
|
||||||
)
|
- **lyrics**: Song 테이블의 song_prompt (가사) 사용
|
||||||
async def generate_video(task_id: str) -> VideoGenerateResponse:
|
|
||||||
"""영상 생성 요청 테스트 엔드포인트"""
|
## 반환 정보
|
||||||
return VideoGenerateResponse(
|
- **success**: 요청 성공 여부
|
||||||
success=True,
|
- **task_id**: 내부 작업 ID (Project task_id)
|
||||||
task_id=task_id,
|
- **creatomate_render_id**: Creatomate 렌더 ID (상태 조회에 사용)
|
||||||
message="영상 생성 요청 성공",
|
- **message**: 응답 메시지
|
||||||
error_message=None,
|
|
||||||
)
|
## 사용 예시
|
||||||
|
```
|
||||||
|
GET /video/generate/0694b716-dbff-7219-8000-d08cb5fce431
|
||||||
@router.get(
|
GET /video/generate/0694b716-dbff-7219-8000-d08cb5fce431?orientation=horizontal
|
||||||
"/status/{task_id}",
|
```
|
||||||
summary="영상 상태 조회 (테스트)",
|
|
||||||
response_model=VideoStatusResponse,
|
## 참고
|
||||||
)
|
- 이미지는 task_id로 Image 테이블에서 자동 조회됩니다 (img_order 순서).
|
||||||
async def get_video_status(task_id: str) -> VideoStatusResponse:
|
- 배경 음악(music_url), 영상 길이(duration), 가사(lyrics)는 task_id로 Song 테이블을 조회하여 자동으로 가져옵니다.
|
||||||
"""영상 상태 조회 테스트 엔드포인트"""
|
- 같은 task_id로 여러 Song이 있을 경우 **가장 최근 생성된 노래**를 사용합니다.
|
||||||
return VideoStatusResponse(
|
- Song의 song_result_url과 song_prompt가 있어야 영상 생성이 가능합니다.
|
||||||
task_id=task_id,
|
- creatomate_render_id를 사용하여 /status/{creatomate_render_id} 엔드포인트에서 생성 상태를 확인할 수 있습니다.
|
||||||
status="completed",
|
- Video 테이블에 데이터가 저장되며, project_id, lyric_id, song_id가 자동으로 연결됩니다.
|
||||||
video_url=TEST_VIDEO_URL,
|
""",
|
||||||
)
|
response_model=GenerateVideoResponse,
|
||||||
|
responses={
|
||||||
|
200: {"description": "영상 생성 요청 성공"},
|
||||||
@router.get(
|
400: {"description": "Song의 음악 URL, 가사(song_prompt) 또는 이미지가 없음"},
|
||||||
"s/",
|
404: {"description": "Project, Lyric, Song 또는 Image를 찾을 수 없음"},
|
||||||
summary="영상 목록 조회 (테스트)",
|
500: {"description": "영상 생성 요청 실패"},
|
||||||
response_model=VideoListResponse,
|
},
|
||||||
)
|
)
|
||||||
async def get_videos() -> VideoListResponse:
|
async def generate_video(
|
||||||
"""영상 목록 조회 테스트 엔드포인트"""
|
task_id: str,
|
||||||
now = datetime.now()
|
orientation: Literal["horizontal", "vertical"] = Query(
|
||||||
videos = [
|
default="vertical",
|
||||||
VideoItem(
|
description="영상 방향 (horizontal: 가로형, vertical: 세로형)",
|
||||||
task_id=f"test-task-id-{i:03d}",
|
),
|
||||||
video_url=TEST_VIDEO_URL,
|
) -> GenerateVideoResponse:
|
||||||
created_at=now - timedelta(hours=i),
|
"""Creatomate API를 통해 영상을 생성합니다.
|
||||||
)
|
|
||||||
for i in range(10)
|
1. task_id로 Project, Lyric, Song, Image 순차 조회
|
||||||
]
|
2. Video 테이블에 초기 데이터 저장 (status: processing)
|
||||||
|
3. Creatomate API 호출 (orientation에 따른 템플릿 자동 선택)
|
||||||
return VideoListResponse(
|
4. creatomate_render_id 업데이트 후 응답 반환
|
||||||
videos=videos,
|
|
||||||
total=len(videos),
|
Note: 이 함수는 Depends(get_session)을 사용하지 않고 명시적으로 세션을 관리합니다.
|
||||||
)
|
외부 API 호출 중 DB 커넥션이 유지되지 않도록 하여 커넥션 타임아웃 문제를 방지합니다.
|
||||||
|
|
||||||
|
중요: SQLAlchemy AsyncSession은 단일 세션에서 동시에 여러 쿼리를 실행하는 것을
|
||||||
|
지원하지 않습니다. asyncio.gather()로 병렬 쿼리를 실행하면 세션 상태 충돌이 발생합니다.
|
||||||
|
따라서 쿼리는 순차적으로 실행합니다.
|
||||||
|
"""
|
||||||
|
import time
|
||||||
|
from app.database.session import AsyncSessionLocal
|
||||||
|
|
||||||
|
request_start = time.perf_counter()
|
||||||
|
print(f"[generate_video] START - task_id: {task_id}, orientation: {orientation}")
|
||||||
|
|
||||||
|
# ==========================================================================
|
||||||
|
# 1단계: DB 조회 및 초기 데이터 저장 (세션을 명시적으로 열고 닫음)
|
||||||
|
# ==========================================================================
|
||||||
|
# 외부 API 호출 전에 필요한 데이터를 저장할 변수들
|
||||||
|
project_id: int | None = None
|
||||||
|
lyric_id: int | None = None
|
||||||
|
song_id: int | None = None
|
||||||
|
video_id: int | None = None
|
||||||
|
music_url: str | None = None
|
||||||
|
song_duration: float | None = None
|
||||||
|
lyrics: str | None = None
|
||||||
|
image_urls: list[str] = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 세션을 명시적으로 열고 DB 작업 후 바로 닫음
|
||||||
|
async with AsyncSessionLocal() as session:
|
||||||
|
# ===== 순차 쿼리 실행: Project, Lyric, Song, Image =====
|
||||||
|
# Note: AsyncSession은 동일 세션에서 병렬 쿼리를 지원하지 않음
|
||||||
|
|
||||||
|
# Project 조회
|
||||||
|
project_result = await session.execute(
|
||||||
|
select(Project)
|
||||||
|
.where(Project.task_id == task_id)
|
||||||
|
.order_by(Project.created_at.desc())
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Lyric 조회
|
||||||
|
lyric_result = await session.execute(
|
||||||
|
select(Lyric)
|
||||||
|
.where(Lyric.task_id == task_id)
|
||||||
|
.order_by(Lyric.created_at.desc())
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Song 조회
|
||||||
|
song_result = await session.execute(
|
||||||
|
select(Song)
|
||||||
|
.where(Song.task_id == task_id)
|
||||||
|
.order_by(Song.created_at.desc())
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Image 조회
|
||||||
|
image_result = await session.execute(
|
||||||
|
select(Image)
|
||||||
|
.where(Image.task_id == task_id)
|
||||||
|
.order_by(Image.img_order.asc())
|
||||||
|
)
|
||||||
|
|
||||||
|
query_time = time.perf_counter()
|
||||||
|
print(f"[generate_video] Queries completed - task_id: {task_id}, "
|
||||||
|
f"elapsed: {(query_time - request_start)*1000:.1f}ms")
|
||||||
|
|
||||||
|
# ===== 결과 처리: Project =====
|
||||||
|
project = project_result.scalar_one_or_none()
|
||||||
|
if not project:
|
||||||
|
print(f"[generate_video] Project NOT FOUND - task_id: {task_id}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f"task_id '{task_id}'에 해당하는 Project를 찾을 수 없습니다.",
|
||||||
|
)
|
||||||
|
project_id = project.id
|
||||||
|
|
||||||
|
# ===== 결과 처리: Lyric =====
|
||||||
|
lyric = lyric_result.scalar_one_or_none()
|
||||||
|
if not lyric:
|
||||||
|
print(f"[generate_video] Lyric NOT FOUND - task_id: {task_id}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f"task_id '{task_id}'에 해당하는 Lyric을 찾을 수 없습니다.",
|
||||||
|
)
|
||||||
|
lyric_id = lyric.id
|
||||||
|
|
||||||
|
# ===== 결과 처리: Song =====
|
||||||
|
song = song_result.scalar_one_or_none()
|
||||||
|
if not song:
|
||||||
|
print(f"[generate_video] Song NOT FOUND - task_id: {task_id}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f"task_id '{task_id}'에 해당하는 Song을 찾을 수 없습니다.",
|
||||||
|
)
|
||||||
|
|
||||||
|
song_id = song.id
|
||||||
|
music_url = song.song_result_url
|
||||||
|
song_duration = song.duration
|
||||||
|
lyrics = song.song_prompt
|
||||||
|
|
||||||
|
if not music_url:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail=f"Song(id={song_id})의 음악 URL이 없습니다.",
|
||||||
|
)
|
||||||
|
|
||||||
|
if not lyrics:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail=f"Song(id={song_id})의 가사(song_prompt)가 없습니다.",
|
||||||
|
)
|
||||||
|
|
||||||
|
# ===== 결과 처리: Image =====
|
||||||
|
images = image_result.scalars().all()
|
||||||
|
if not images:
|
||||||
|
print(f"[generate_video] Image NOT FOUND - task_id: {task_id}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f"task_id '{task_id}'에 해당하는 이미지를 찾을 수 없습니다.",
|
||||||
|
)
|
||||||
|
image_urls = [img.img_url for img in images]
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"[generate_video] Data loaded - task_id: {task_id}, "
|
||||||
|
f"project_id: {project_id}, lyric_id: {lyric_id}, "
|
||||||
|
f"song_id: {song_id}, images: {len(image_urls)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# ===== Video 테이블에 초기 데이터 저장 및 커밋 =====
|
||||||
|
video = Video(
|
||||||
|
project_id=project_id,
|
||||||
|
lyric_id=lyric_id,
|
||||||
|
song_id=song_id,
|
||||||
|
task_id=task_id,
|
||||||
|
creatomate_render_id=None,
|
||||||
|
status="processing",
|
||||||
|
)
|
||||||
|
session.add(video)
|
||||||
|
await session.commit()
|
||||||
|
video_id = video.id
|
||||||
|
stage1_time = time.perf_counter()
|
||||||
|
print(f"[generate_video] Video saved - task_id: {task_id}, id: {video_id}, "
|
||||||
|
f"stage1_elapsed: {(stage1_time - request_start)*1000:.1f}ms")
|
||||||
|
# 세션이 여기서 자동으로 닫힘 (async with 블록 종료)
|
||||||
|
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[generate_video] DB EXCEPTION - task_id: {task_id}, error: {e}")
|
||||||
|
return GenerateVideoResponse(
|
||||||
|
success=False,
|
||||||
|
task_id=task_id,
|
||||||
|
creatomate_render_id=None,
|
||||||
|
message="영상 생성 요청에 실패했습니다.",
|
||||||
|
error_message=str(e),
|
||||||
|
)
|
||||||
|
|
||||||
|
# ==========================================================================
|
||||||
|
# 2단계: 외부 API 호출 (세션 사용 안함 - 커넥션 풀 점유 없음)
|
||||||
|
# ==========================================================================
|
||||||
|
stage2_start = time.perf_counter()
|
||||||
|
try:
|
||||||
|
print(f"[generate_video] Stage 2 START - Creatomate API - task_id: {task_id}")
|
||||||
|
creatomate_service = CreatomateService(
|
||||||
|
orientation=orientation,
|
||||||
|
target_duration=song_duration,
|
||||||
|
)
|
||||||
|
print(f"[generate_video] Using template_id: {creatomate_service.template_id}, duration: {creatomate_service.target_duration} (song duration: {song_duration})")
|
||||||
|
|
||||||
|
# 6-1. 템플릿 조회 (비동기)
|
||||||
|
template = await creatomate_service.get_one_template_data_async(creatomate_service.template_id)
|
||||||
|
print(f"[generate_video] Template fetched - task_id: {task_id}")
|
||||||
|
|
||||||
|
# 6-2. elements에서 리소스 매핑 생성
|
||||||
|
modifications = creatomate_service.elements_connect_resource_blackbox(
|
||||||
|
elements=template["source"]["elements"],
|
||||||
|
image_url_list=image_urls,
|
||||||
|
lyric=lyrics,
|
||||||
|
music_url=music_url,
|
||||||
|
)
|
||||||
|
print(f"[generate_video] Modifications created - task_id: {task_id}")
|
||||||
|
|
||||||
|
# 6-3. elements 수정
|
||||||
|
new_elements = creatomate_service.modify_element(
|
||||||
|
template["source"]["elements"],
|
||||||
|
modifications,
|
||||||
|
)
|
||||||
|
template["source"]["elements"] = new_elements
|
||||||
|
print(f"[generate_video] Elements modified - task_id: {task_id}")
|
||||||
|
|
||||||
|
# 6-4. duration 확장
|
||||||
|
final_template = creatomate_service.extend_template_duration(
|
||||||
|
template,
|
||||||
|
creatomate_service.target_duration,
|
||||||
|
)
|
||||||
|
print(f"[generate_video] Duration extended to {creatomate_service.target_duration}s - task_id: {task_id}")
|
||||||
|
|
||||||
|
# 6-5. 커스텀 렌더링 요청 (비동기)
|
||||||
|
render_response = await creatomate_service.make_creatomate_custom_call_async(
|
||||||
|
final_template["source"],
|
||||||
|
)
|
||||||
|
print(f"[generate_video] Creatomate API response - task_id: {task_id}, response: {render_response}")
|
||||||
|
|
||||||
|
# 렌더 ID 추출
|
||||||
|
if isinstance(render_response, list) and len(render_response) > 0:
|
||||||
|
creatomate_render_id = render_response[0].get("id")
|
||||||
|
elif isinstance(render_response, dict):
|
||||||
|
creatomate_render_id = render_response.get("id")
|
||||||
|
else:
|
||||||
|
creatomate_render_id = None
|
||||||
|
|
||||||
|
stage2_time = time.perf_counter()
|
||||||
|
print(
|
||||||
|
f"[generate_video] Stage 2 DONE - task_id: {task_id}, "
|
||||||
|
f"render_id: {creatomate_render_id}, "
|
||||||
|
f"stage2_elapsed: {(stage2_time - stage2_start)*1000:.1f}ms"
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[generate_video] Creatomate API EXCEPTION - task_id: {task_id}, error: {e}")
|
||||||
|
# 외부 API 실패 시 Video 상태를 failed로 업데이트
|
||||||
|
from app.database.session import AsyncSessionLocal
|
||||||
|
async with AsyncSessionLocal() as update_session:
|
||||||
|
video_result = await update_session.execute(
|
||||||
|
select(Video).where(Video.id == video_id)
|
||||||
|
)
|
||||||
|
video_to_update = video_result.scalar_one_or_none()
|
||||||
|
if video_to_update:
|
||||||
|
video_to_update.status = "failed"
|
||||||
|
await update_session.commit()
|
||||||
|
return GenerateVideoResponse(
|
||||||
|
success=False,
|
||||||
|
task_id=task_id,
|
||||||
|
creatomate_render_id=None,
|
||||||
|
message="영상 생성 요청에 실패했습니다.",
|
||||||
|
error_message=str(e),
|
||||||
|
)
|
||||||
|
|
||||||
|
# ==========================================================================
|
||||||
|
# 3단계: creatomate_render_id 업데이트 (새 세션으로 빠르게 처리)
|
||||||
|
# ==========================================================================
|
||||||
|
stage3_start = time.perf_counter()
|
||||||
|
print(f"[generate_video] Stage 3 START - DB update - task_id: {task_id}")
|
||||||
|
try:
|
||||||
|
from app.database.session import AsyncSessionLocal
|
||||||
|
async with AsyncSessionLocal() as update_session:
|
||||||
|
video_result = await update_session.execute(
|
||||||
|
select(Video).where(Video.id == video_id)
|
||||||
|
)
|
||||||
|
video_to_update = video_result.scalar_one_or_none()
|
||||||
|
if video_to_update:
|
||||||
|
video_to_update.creatomate_render_id = creatomate_render_id
|
||||||
|
await update_session.commit()
|
||||||
|
|
||||||
|
stage3_time = time.perf_counter()
|
||||||
|
total_time = stage3_time - request_start
|
||||||
|
print(
|
||||||
|
f"[generate_video] Stage 3 DONE - task_id: {task_id}, "
|
||||||
|
f"stage3_elapsed: {(stage3_time - stage3_start)*1000:.1f}ms"
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
f"[generate_video] SUCCESS - task_id: {task_id}, "
|
||||||
|
f"render_id: {creatomate_render_id}, "
|
||||||
|
f"total_time: {total_time*1000:.1f}ms"
|
||||||
|
)
|
||||||
|
|
||||||
|
return GenerateVideoResponse(
|
||||||
|
success=True,
|
||||||
|
task_id=task_id,
|
||||||
|
creatomate_render_id=creatomate_render_id,
|
||||||
|
message="영상 생성 요청이 접수되었습니다. creatomate_render_id로 상태를 조회하세요.",
|
||||||
|
error_message=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[generate_video] Update EXCEPTION - task_id: {task_id}, error: {e}")
|
||||||
|
return GenerateVideoResponse(
|
||||||
|
success=False,
|
||||||
|
task_id=task_id,
|
||||||
|
creatomate_render_id=creatomate_render_id,
|
||||||
|
message="영상 생성은 요청되었으나 DB 업데이트에 실패했습니다.",
|
||||||
|
error_message=str(e),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/status/{creatomate_render_id}",
|
||||||
|
summary="영상 생성 상태 조회",
|
||||||
|
description="""
|
||||||
|
Creatomate API를 통해 영상 생성 작업의 상태를 조회합니다.
|
||||||
|
succeeded 상태인 경우 백그라운드에서 MP4 파일을 다운로드하고 Video 테이블을 업데이트합니다.
|
||||||
|
|
||||||
|
## 경로 파라미터
|
||||||
|
- **creatomate_render_id**: 영상 생성 시 반환된 Creatomate 렌더 ID (필수)
|
||||||
|
|
||||||
|
## 반환 정보
|
||||||
|
- **success**: 조회 성공 여부
|
||||||
|
- **status**: 작업 상태 (planned, waiting, rendering, succeeded, failed)
|
||||||
|
- **message**: 상태 메시지
|
||||||
|
- **render_data**: 렌더링 결과 데이터 (완료 시)
|
||||||
|
- **raw_response**: Creatomate API 원본 응답
|
||||||
|
|
||||||
|
## 사용 예시
|
||||||
|
```
|
||||||
|
GET /video/status/render-id-123...
|
||||||
|
```
|
||||||
|
|
||||||
|
## 상태 값
|
||||||
|
- **planned**: 예약됨
|
||||||
|
- **waiting**: 대기 중
|
||||||
|
- **transcribing**: 트랜스크립션 중
|
||||||
|
- **rendering**: 렌더링 중
|
||||||
|
- **succeeded**: 성공
|
||||||
|
- **failed**: 실패
|
||||||
|
|
||||||
|
## 참고
|
||||||
|
- succeeded 시 백그라운드에서 MP4 다운로드 및 DB 업데이트 진행
|
||||||
|
""",
|
||||||
|
response_model=PollingVideoResponse,
|
||||||
|
responses={
|
||||||
|
200: {"description": "상태 조회 성공"},
|
||||||
|
500: {"description": "상태 조회 실패"},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
async def get_video_status(
|
||||||
|
creatomate_render_id: str,
|
||||||
|
background_tasks: BackgroundTasks,
|
||||||
|
session: AsyncSession = Depends(get_session),
|
||||||
|
) -> PollingVideoResponse:
|
||||||
|
"""creatomate_render_id로 영상 생성 작업의 상태를 조회합니다.
|
||||||
|
|
||||||
|
succeeded 상태인 경우 백그라운드에서 MP4 파일을 다운로드하고
|
||||||
|
Video 테이블의 status를 completed로, result_movie_url을 업데이트합니다.
|
||||||
|
"""
|
||||||
|
print(f"[get_video_status] START - creatomate_render_id: {creatomate_render_id}")
|
||||||
|
try:
|
||||||
|
creatomate_service = CreatomateService()
|
||||||
|
result = await creatomate_service.get_render_status_async(creatomate_render_id)
|
||||||
|
print(f"[get_video_status] Creatomate API response - creatomate_render_id: {creatomate_render_id}, status: {result.get('status')}")
|
||||||
|
|
||||||
|
status = result.get("status", "unknown")
|
||||||
|
video_url = result.get("url")
|
||||||
|
|
||||||
|
# 상태별 메시지 설정
|
||||||
|
status_messages = {
|
||||||
|
"planned": "영상 생성이 예약되었습니다.",
|
||||||
|
"waiting": "영상 생성 대기 중입니다.",
|
||||||
|
"transcribing": "트랜스크립션 진행 중입니다.",
|
||||||
|
"rendering": "영상을 렌더링하고 있습니다.",
|
||||||
|
"succeeded": "영상 생성이 완료되었습니다.",
|
||||||
|
"failed": "영상 생성에 실패했습니다.",
|
||||||
|
}
|
||||||
|
message = status_messages.get(status, f"상태: {status}")
|
||||||
|
|
||||||
|
# succeeded 상태인 경우 백그라운드 태스크 실행
|
||||||
|
if status == "succeeded" and video_url:
|
||||||
|
# creatomate_render_id로 Video 조회하여 task_id 가져오기
|
||||||
|
video_result = await session.execute(
|
||||||
|
select(Video)
|
||||||
|
.where(Video.creatomate_render_id == creatomate_render_id)
|
||||||
|
.order_by(Video.created_at.desc())
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
video = video_result.scalar_one_or_none()
|
||||||
|
|
||||||
|
if video and video.status != "completed":
|
||||||
|
# 이미 완료된 경우 백그라운드 작업 중복 실행 방지
|
||||||
|
# task_id로 Project 조회하여 store_name 가져오기
|
||||||
|
project_result = await session.execute(
|
||||||
|
select(Project).where(Project.id == video.project_id)
|
||||||
|
)
|
||||||
|
project = project_result.scalar_one_or_none()
|
||||||
|
|
||||||
|
store_name = project.store_name if project else "video"
|
||||||
|
|
||||||
|
# 백그라운드 태스크로 MP4 다운로드 → Blob 업로드 → DB 업데이트 → 임시 파일 삭제
|
||||||
|
print(f"[get_video_status] Background task args - task_id: {video.task_id}, video_url: {video_url}, store_name: {store_name}")
|
||||||
|
background_tasks.add_task(
|
||||||
|
download_and_upload_video_to_blob,
|
||||||
|
task_id=video.task_id,
|
||||||
|
video_url=video_url,
|
||||||
|
store_name=store_name,
|
||||||
|
)
|
||||||
|
elif video and video.status == "completed":
|
||||||
|
print(f"[get_video_status] SKIPPED - Video already completed, creatomate_render_id: {creatomate_render_id}")
|
||||||
|
|
||||||
|
render_data = VideoRenderData(
|
||||||
|
id=result.get("id"),
|
||||||
|
status=status,
|
||||||
|
url=video_url,
|
||||||
|
snapshot_url=result.get("snapshot_url"),
|
||||||
|
)
|
||||||
|
|
||||||
|
print(f"[get_video_status] SUCCESS - creatomate_render_id: {creatomate_render_id}")
|
||||||
|
return PollingVideoResponse(
|
||||||
|
success=True,
|
||||||
|
status=status,
|
||||||
|
message=message,
|
||||||
|
render_data=render_data,
|
||||||
|
raw_response=result,
|
||||||
|
error_message=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
print(f"[get_video_status] EXCEPTION - creatomate_render_id: {creatomate_render_id}, error: {e}")
|
||||||
|
return PollingVideoResponse(
|
||||||
|
success=False,
|
||||||
|
status="error",
|
||||||
|
message="상태 조회에 실패했습니다.",
|
||||||
|
render_data=None,
|
||||||
|
raw_response=None,
|
||||||
|
error_message=f"{type(e).__name__}: {e}\n{traceback.format_exc()}",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/download/{task_id}",
|
||||||
|
summary="영상 생성 URL 조회",
|
||||||
|
description="""
|
||||||
|
task_id를 기반으로 Video 테이블의 상태를 polling하고,
|
||||||
|
completed인 경우 Project 정보와 영상 URL을 반환합니다.
|
||||||
|
|
||||||
|
## 경로 파라미터
|
||||||
|
- **task_id**: 프로젝트 task_id (필수)
|
||||||
|
|
||||||
|
## 반환 정보
|
||||||
|
- **success**: 조회 성공 여부
|
||||||
|
- **status**: 처리 상태 (processing, completed, failed)
|
||||||
|
- **message**: 응답 메시지
|
||||||
|
- **store_name**: 업체명
|
||||||
|
- **region**: 지역명
|
||||||
|
- **task_id**: 작업 고유 식별자
|
||||||
|
- **result_movie_url**: 영상 결과 URL (completed 시)
|
||||||
|
- **created_at**: 생성 일시
|
||||||
|
|
||||||
|
## 사용 예시
|
||||||
|
```
|
||||||
|
GET /video/download/019123ab-cdef-7890-abcd-ef1234567890
|
||||||
|
```
|
||||||
|
|
||||||
|
## 참고
|
||||||
|
- processing 상태인 경우 result_movie_url은 null입니다.
|
||||||
|
- completed 상태인 경우 Project 정보와 함께 result_movie_url을 반환합니다.
|
||||||
|
""",
|
||||||
|
response_model=DownloadVideoResponse,
|
||||||
|
responses={
|
||||||
|
200: {"description": "조회 성공"},
|
||||||
|
404: {"description": "Video를 찾을 수 없음"},
|
||||||
|
500: {"description": "조회 실패"},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
async def download_video(
|
||||||
|
task_id: str,
|
||||||
|
session: AsyncSession = Depends(get_session),
|
||||||
|
) -> DownloadVideoResponse:
|
||||||
|
"""task_id로 Video 상태를 polling하고 completed 시 Project 정보와 영상 URL을 반환합니다."""
|
||||||
|
print(f"[download_video] START - task_id: {task_id}")
|
||||||
|
try:
|
||||||
|
# task_id로 Video 조회 (여러 개 있을 경우 가장 최근 것 선택)
|
||||||
|
video_result = await session.execute(
|
||||||
|
select(Video)
|
||||||
|
.where(Video.task_id == task_id)
|
||||||
|
.order_by(Video.created_at.desc())
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
video = video_result.scalar_one_or_none()
|
||||||
|
|
||||||
|
if not video:
|
||||||
|
print(f"[download_video] Video NOT FOUND - task_id: {task_id}")
|
||||||
|
return DownloadVideoResponse(
|
||||||
|
success=False,
|
||||||
|
status="not_found",
|
||||||
|
message=f"task_id '{task_id}'에 해당하는 Video를 찾을 수 없습니다.",
|
||||||
|
error_message="Video not found",
|
||||||
|
)
|
||||||
|
|
||||||
|
print(f"[download_video] Video found - task_id: {task_id}, status: {video.status}")
|
||||||
|
|
||||||
|
# processing 상태인 경우
|
||||||
|
if video.status == "processing":
|
||||||
|
print(f"[download_video] PROCESSING - task_id: {task_id}")
|
||||||
|
return DownloadVideoResponse(
|
||||||
|
success=True,
|
||||||
|
status="processing",
|
||||||
|
message="영상 생성이 진행 중입니다.",
|
||||||
|
task_id=task_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# failed 상태인 경우
|
||||||
|
if video.status == "failed":
|
||||||
|
print(f"[download_video] FAILED - task_id: {task_id}")
|
||||||
|
return DownloadVideoResponse(
|
||||||
|
success=False,
|
||||||
|
status="failed",
|
||||||
|
message="영상 생성에 실패했습니다.",
|
||||||
|
task_id=task_id,
|
||||||
|
error_message="Video generation failed",
|
||||||
|
)
|
||||||
|
|
||||||
|
# completed 상태인 경우 - Project 정보 조회
|
||||||
|
project_result = await session.execute(
|
||||||
|
select(Project).where(Project.id == video.project_id)
|
||||||
|
)
|
||||||
|
project = project_result.scalar_one_or_none()
|
||||||
|
|
||||||
|
print(f"[download_video] COMPLETED - task_id: {task_id}, result_movie_url: {video.result_movie_url}")
|
||||||
|
return DownloadVideoResponse(
|
||||||
|
success=True,
|
||||||
|
status="completed",
|
||||||
|
message="영상 다운로드가 완료되었습니다.",
|
||||||
|
store_name=project.store_name if project else None,
|
||||||
|
region=project.region if project else None,
|
||||||
|
task_id=task_id,
|
||||||
|
result_movie_url=video.result_movie_url,
|
||||||
|
created_at=video.created_at,
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[download_video] EXCEPTION - task_id: {task_id}, error: {e}")
|
||||||
|
return DownloadVideoResponse(
|
||||||
|
success=False,
|
||||||
|
status="error",
|
||||||
|
message="영상 다운로드 조회에 실패했습니다.",
|
||||||
|
error_message=str(e),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"s/",
|
||||||
|
summary="생성된 영상 목록 조회",
|
||||||
|
description="""
|
||||||
|
완료된 영상 목록을 페이지네이션하여 조회합니다.
|
||||||
|
|
||||||
|
## 쿼리 파라미터
|
||||||
|
- **page**: 페이지 번호 (1부터 시작, 기본값: 1)
|
||||||
|
- **page_size**: 페이지당 데이터 수 (기본값: 10, 최대: 100)
|
||||||
|
|
||||||
|
## 반환 정보
|
||||||
|
- **items**: 영상 목록 (store_name, region, task_id, result_movie_url, created_at)
|
||||||
|
- **total**: 전체 데이터 수
|
||||||
|
- **page**: 현재 페이지
|
||||||
|
- **page_size**: 페이지당 데이터 수
|
||||||
|
- **total_pages**: 전체 페이지 수
|
||||||
|
- **has_next**: 다음 페이지 존재 여부
|
||||||
|
- **has_prev**: 이전 페이지 존재 여부
|
||||||
|
|
||||||
|
## 사용 예시
|
||||||
|
```
|
||||||
|
GET /videos/?page=1&page_size=10
|
||||||
|
```
|
||||||
|
|
||||||
|
## 참고
|
||||||
|
- status가 'completed'인 영상만 반환됩니다.
|
||||||
|
- 동일한 task_id가 있는 경우 가장 최근에 생성된 1개만 반환됩니다.
|
||||||
|
- created_at 기준 내림차순 정렬됩니다.
|
||||||
|
""",
|
||||||
|
response_model=PaginatedResponse[VideoListItem],
|
||||||
|
responses={
|
||||||
|
200: {"description": "영상 목록 조회 성공"},
|
||||||
|
500: {"description": "조회 실패"},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
async def get_videos(
|
||||||
|
session: AsyncSession = Depends(get_session),
|
||||||
|
pagination: PaginationParams = Depends(get_pagination_params),
|
||||||
|
) -> PaginatedResponse[VideoListItem]:
|
||||||
|
"""완료된 영상 목록을 페이지네이션하여 반환합니다."""
|
||||||
|
print(f"[get_videos] START - page: {pagination.page}, page_size: {pagination.page_size}")
|
||||||
|
try:
|
||||||
|
offset = (pagination.page - 1) * pagination.page_size
|
||||||
|
|
||||||
|
# 서브쿼리: task_id별 최신 Video의 id 조회 (completed 상태만)
|
||||||
|
subquery = (
|
||||||
|
select(func.max(Video.id).label("max_id"))
|
||||||
|
.where(Video.status == "completed")
|
||||||
|
.group_by(Video.task_id)
|
||||||
|
.subquery()
|
||||||
|
)
|
||||||
|
|
||||||
|
# 전체 개수 조회 (task_id별 최신 1개만)
|
||||||
|
count_query = select(func.count()).select_from(subquery)
|
||||||
|
total_result = await session.execute(count_query)
|
||||||
|
total = total_result.scalar() or 0
|
||||||
|
|
||||||
|
# 데이터 조회 (completed 상태, task_id별 최신 1개만, 최신순)
|
||||||
|
query = (
|
||||||
|
select(Video)
|
||||||
|
.where(Video.id.in_(select(subquery.c.max_id)))
|
||||||
|
.order_by(Video.created_at.desc())
|
||||||
|
.offset(offset)
|
||||||
|
.limit(pagination.page_size)
|
||||||
|
)
|
||||||
|
result = await session.execute(query)
|
||||||
|
videos = result.scalars().all()
|
||||||
|
|
||||||
|
# Project 정보 일괄 조회 (N+1 문제 해결)
|
||||||
|
project_ids = [v.project_id for v in videos if v.project_id]
|
||||||
|
projects_map: dict = {}
|
||||||
|
if project_ids:
|
||||||
|
projects_result = await session.execute(
|
||||||
|
select(Project).where(Project.id.in_(project_ids))
|
||||||
|
)
|
||||||
|
projects_map = {p.id: p for p in projects_result.scalars().all()}
|
||||||
|
|
||||||
|
# VideoListItem으로 변환
|
||||||
|
items = []
|
||||||
|
for video in videos:
|
||||||
|
project = projects_map.get(video.project_id)
|
||||||
|
|
||||||
|
item = VideoListItem(
|
||||||
|
store_name=project.store_name if project else None,
|
||||||
|
region=project.region if project else None,
|
||||||
|
task_id=video.task_id,
|
||||||
|
result_movie_url=video.result_movie_url,
|
||||||
|
created_at=video.created_at,
|
||||||
|
)
|
||||||
|
items.append(item)
|
||||||
|
|
||||||
|
response = PaginatedResponse.create(
|
||||||
|
items=items,
|
||||||
|
total=total,
|
||||||
|
page=pagination.page,
|
||||||
|
page_size=pagination.page_size,
|
||||||
|
)
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"[get_videos] SUCCESS - total: {total}, page: {pagination.page}, "
|
||||||
|
f"page_size: {pagination.page_size}, items_count: {len(items)}"
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[get_videos] EXCEPTION - error: {e}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=500,
|
||||||
|
detail=f"영상 목록 조회에 실패했습니다: {str(e)}",
|
||||||
|
)
|
||||||
@@ -1,62 +1,62 @@
|
|||||||
from sqladmin import ModelView
|
from sqladmin import ModelView
|
||||||
|
|
||||||
from app.video.models import Video
|
from app.video.models import Video
|
||||||
|
|
||||||
|
|
||||||
class VideoAdmin(ModelView, model=Video):
|
class VideoAdmin(ModelView, model=Video):
|
||||||
name = "영상"
|
name = "영상"
|
||||||
name_plural = "영상 목록"
|
name_plural = "영상 목록"
|
||||||
icon = "fa-solid fa-video"
|
icon = "fa-solid fa-video"
|
||||||
category = "영상 관리"
|
category = "영상 관리"
|
||||||
page_size = 20
|
page_size = 20
|
||||||
|
|
||||||
column_list = [
|
column_list = [
|
||||||
"id",
|
"id",
|
||||||
"project_id",
|
"project_id",
|
||||||
"lyric_id",
|
"lyric_id",
|
||||||
"song_id",
|
"song_id",
|
||||||
"task_id",
|
"task_id",
|
||||||
"status",
|
"status",
|
||||||
"created_at",
|
"created_at",
|
||||||
]
|
]
|
||||||
|
|
||||||
column_details_list = [
|
column_details_list = [
|
||||||
"id",
|
"id",
|
||||||
"project_id",
|
"project_id",
|
||||||
"lyric_id",
|
"lyric_id",
|
||||||
"song_id",
|
"song_id",
|
||||||
"task_id",
|
"task_id",
|
||||||
"status",
|
"status",
|
||||||
"result_movie_url",
|
"result_movie_url",
|
||||||
"created_at",
|
"created_at",
|
||||||
]
|
]
|
||||||
|
|
||||||
# 폼(생성/수정)에서 제외
|
# 폼(생성/수정)에서 제외
|
||||||
form_excluded_columns = ["created_at"]
|
form_excluded_columns = ["created_at"]
|
||||||
|
|
||||||
column_searchable_list = [
|
column_searchable_list = [
|
||||||
Video.task_id,
|
Video.task_id,
|
||||||
Video.status,
|
Video.status,
|
||||||
]
|
]
|
||||||
|
|
||||||
column_default_sort = (Video.created_at, True) # True: DESC (최신순)
|
column_default_sort = (Video.created_at, True) # True: DESC (최신순)
|
||||||
|
|
||||||
column_sortable_list = [
|
column_sortable_list = [
|
||||||
Video.id,
|
Video.id,
|
||||||
Video.project_id,
|
Video.project_id,
|
||||||
Video.lyric_id,
|
Video.lyric_id,
|
||||||
Video.song_id,
|
Video.song_id,
|
||||||
Video.status,
|
Video.status,
|
||||||
Video.created_at,
|
Video.created_at,
|
||||||
]
|
]
|
||||||
|
|
||||||
column_labels = {
|
column_labels = {
|
||||||
"id": "ID",
|
"id": "ID",
|
||||||
"project_id": "프로젝트 ID",
|
"project_id": "프로젝트 ID",
|
||||||
"lyric_id": "가사 ID",
|
"lyric_id": "가사 ID",
|
||||||
"song_id": "노래 ID",
|
"song_id": "노래 ID",
|
||||||
"task_id": "작업 ID",
|
"task_id": "작업 ID",
|
||||||
"status": "상태",
|
"status": "상태",
|
||||||
"result_movie_url": "영상 URL",
|
"result_movie_url": "영상 URL",
|
||||||
"created_at": "생성일시",
|
"created_at": "생성일시",
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from app.database.session import get_session
|
from app.database.session import get_session
|
||||||
|
|
||||||
SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
||||||
@@ -1,133 +1,139 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import TYPE_CHECKING, Optional
|
from typing import TYPE_CHECKING, Optional
|
||||||
|
|
||||||
from sqlalchemy import DateTime, ForeignKey, Integer, String, func
|
from sqlalchemy import DateTime, ForeignKey, Integer, String, func
|
||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
from app.database.session import Base
|
from app.database.session import Base
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from app.home.models import Project
|
from app.home.models import Project
|
||||||
from app.lyric.models import Lyric
|
from app.lyric.models import Lyric
|
||||||
from app.song.models import Song
|
from app.song.models import Song
|
||||||
|
|
||||||
|
|
||||||
class Video(Base):
|
class Video(Base):
|
||||||
"""
|
"""
|
||||||
영상 결과 테이블
|
영상 결과 테이블
|
||||||
|
|
||||||
최종 생성된 영상의 결과 URL을 저장합니다.
|
최종 생성된 영상의 결과 URL을 저장합니다.
|
||||||
Creatomate 서비스를 통해 이미지와 노래를 결합한 영상 결과입니다.
|
Creatomate 서비스를 통해 이미지와 노래를 결합한 영상 결과입니다.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
id: 고유 식별자 (자동 증가)
|
id: 고유 식별자 (자동 증가)
|
||||||
project_id: 연결된 Project의 id (외래키)
|
project_id: 연결된 Project의 id (외래키)
|
||||||
lyric_id: 연결된 Lyric의 id (외래키)
|
lyric_id: 연결된 Lyric의 id (외래키)
|
||||||
song_id: 연결된 Song의 id (외래키)
|
song_id: 연결된 Song의 id (외래키)
|
||||||
task_id: 영상 생성 작업의 고유 식별자 (UUID 형식)
|
task_id: 영상 생성 작업의 고유 식별자 (UUID 형식)
|
||||||
status: 처리 상태 (pending, processing, completed, failed 등)
|
status: 처리 상태 (pending, processing, completed, failed 등)
|
||||||
result_movie_url: 생성된 영상 URL (S3, CDN 경로)
|
result_movie_url: 생성된 영상 URL (S3, CDN 경로)
|
||||||
created_at: 생성 일시 (자동 설정)
|
created_at: 생성 일시 (자동 설정)
|
||||||
|
|
||||||
Relationships:
|
Relationships:
|
||||||
project: 연결된 Project
|
project: 연결된 Project
|
||||||
lyric: 연결된 Lyric
|
lyric: 연결된 Lyric
|
||||||
song: 연결된 Song
|
song: 연결된 Song
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__tablename__ = "video"
|
__tablename__ = "video"
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
{
|
{
|
||||||
"mysql_engine": "InnoDB",
|
"mysql_engine": "InnoDB",
|
||||||
"mysql_charset": "utf8mb4",
|
"mysql_charset": "utf8mb4",
|
||||||
"mysql_collate": "utf8mb4_unicode_ci",
|
"mysql_collate": "utf8mb4_unicode_ci",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(
|
id: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
autoincrement=True,
|
autoincrement=True,
|
||||||
comment="고유 식별자",
|
comment="고유 식별자",
|
||||||
)
|
)
|
||||||
|
|
||||||
project_id: Mapped[int] = mapped_column(
|
project_id: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
ForeignKey("project.id", ondelete="CASCADE"),
|
ForeignKey("project.id", ondelete="CASCADE"),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True,
|
index=True,
|
||||||
comment="연결된 Project의 id",
|
comment="연결된 Project의 id",
|
||||||
)
|
)
|
||||||
|
|
||||||
lyric_id: Mapped[int] = mapped_column(
|
lyric_id: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
ForeignKey("lyric.id", ondelete="CASCADE"),
|
ForeignKey("lyric.id", ondelete="CASCADE"),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True,
|
index=True,
|
||||||
comment="연결된 Lyric의 id",
|
comment="연결된 Lyric의 id",
|
||||||
)
|
)
|
||||||
|
|
||||||
song_id: Mapped[int] = mapped_column(
|
song_id: Mapped[int] = mapped_column(
|
||||||
Integer,
|
Integer,
|
||||||
ForeignKey("song.id", ondelete="CASCADE"),
|
ForeignKey("song.id", ondelete="CASCADE"),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True,
|
index=True,
|
||||||
comment="연결된 Song의 id",
|
comment="연결된 Song의 id",
|
||||||
)
|
)
|
||||||
|
|
||||||
task_id: Mapped[str] = mapped_column(
|
task_id: Mapped[str] = mapped_column(
|
||||||
String(36),
|
String(36),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True,
|
index=True,
|
||||||
comment="영상 생성 작업 고유 식별자 (UUID)",
|
comment="영상 생성 작업 고유 식별자 (UUID)",
|
||||||
)
|
)
|
||||||
|
|
||||||
status: Mapped[str] = mapped_column(
|
creatomate_render_id: Mapped[Optional[str]] = mapped_column(
|
||||||
String(50),
|
String(64),
|
||||||
nullable=False,
|
nullable=True,
|
||||||
comment="처리 상태 (processing, completed, failed)",
|
comment="Creatomate API 렌더 ID",
|
||||||
)
|
)
|
||||||
|
|
||||||
result_movie_url: Mapped[Optional[str]] = mapped_column(
|
status: Mapped[str] = mapped_column(
|
||||||
String(2048),
|
String(50),
|
||||||
nullable=True,
|
nullable=False,
|
||||||
comment="생성된 영상 URL",
|
comment="처리 상태 (processing, completed, failed)",
|
||||||
)
|
)
|
||||||
|
|
||||||
created_at: Mapped[datetime] = mapped_column(
|
result_movie_url: Mapped[Optional[str]] = mapped_column(
|
||||||
DateTime,
|
String(2048),
|
||||||
nullable=False,
|
nullable=True,
|
||||||
server_default=func.now(),
|
comment="생성된 영상 URL",
|
||||||
comment="생성 일시",
|
)
|
||||||
)
|
|
||||||
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
# Relationships
|
DateTime,
|
||||||
project: Mapped["Project"] = relationship(
|
nullable=False,
|
||||||
"Project",
|
server_default=func.now(),
|
||||||
back_populates="videos",
|
comment="생성 일시",
|
||||||
)
|
)
|
||||||
|
|
||||||
lyric: Mapped["Lyric"] = relationship(
|
# Relationships
|
||||||
"Lyric",
|
project: Mapped["Project"] = relationship(
|
||||||
back_populates="videos",
|
"Project",
|
||||||
)
|
back_populates="videos",
|
||||||
|
)
|
||||||
song: Mapped["Song"] = relationship(
|
|
||||||
"Song",
|
lyric: Mapped["Lyric"] = relationship(
|
||||||
back_populates="videos",
|
"Lyric",
|
||||||
)
|
back_populates="videos",
|
||||||
|
)
|
||||||
def __repr__(self) -> str:
|
|
||||||
def truncate(value: str | None, max_len: int = 10) -> str:
|
song: Mapped["Song"] = relationship(
|
||||||
if value is None:
|
"Song",
|
||||||
return "None"
|
back_populates="videos",
|
||||||
return (value[:max_len] + "...") if len(value) > max_len else value
|
)
|
||||||
|
|
||||||
return (
|
def __repr__(self) -> str:
|
||||||
f"<Video("
|
def truncate(value: str | None, max_len: int = 10) -> str:
|
||||||
f"id={self.id}, "
|
if value is None:
|
||||||
f"task_id='{truncate(self.task_id)}', "
|
return "None"
|
||||||
f"status='{self.status}'"
|
return (value[:max_len] + "...") if len(value) > max_len else value
|
||||||
f")>"
|
|
||||||
)
|
return (
|
||||||
|
f"<Video("
|
||||||
|
f"id={self.id}, "
|
||||||
|
f"task_id='{truncate(self.task_id)}', "
|
||||||
|
f"status='{self.status}'"
|
||||||
|
f")>"
|
||||||
|
)
|
||||||
@@ -1,91 +1,156 @@
|
|||||||
from dataclasses import dataclass, field
|
"""
|
||||||
from datetime import datetime
|
Video API Schemas
|
||||||
from typing import Dict, List
|
|
||||||
|
영상 생성 관련 Pydantic 스키마를 정의합니다.
|
||||||
from fastapi import Request
|
"""
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
@dataclass
|
from typing import Any, Dict, Literal, Optional
|
||||||
class StoreData:
|
|
||||||
id: int
|
from pydantic import BaseModel, ConfigDict, Field
|
||||||
created_at: datetime
|
|
||||||
store_name: str
|
|
||||||
store_category: str | None = None
|
# =============================================================================
|
||||||
store_region: str | None = None
|
# Response Schemas
|
||||||
store_address: str | None = None
|
# =============================================================================
|
||||||
store_phone_number: str | None = None
|
|
||||||
store_info: str | None = None
|
|
||||||
|
class GenerateVideoResponse(BaseModel):
|
||||||
|
"""영상 생성 응답 스키마
|
||||||
@dataclass
|
|
||||||
class AttributeData:
|
Usage:
|
||||||
id: int
|
GET /video/generate/{task_id}
|
||||||
attr_category: str
|
Returns the task IDs for tracking video generation.
|
||||||
attr_value: str
|
"""
|
||||||
created_at: datetime
|
|
||||||
|
model_config = ConfigDict(
|
||||||
|
json_schema_extra={
|
||||||
@dataclass
|
"example": {
|
||||||
class SongSampleData:
|
"success": True,
|
||||||
id: int
|
"task_id": "0694b716-dbff-7219-8000-d08cb5fce431",
|
||||||
ai: str
|
"creatomate_render_id": "render-id-123456",
|
||||||
ai_model: str
|
"message": "영상 생성 요청이 접수되었습니다. creatomate_render_id로 상태를 조회하세요.",
|
||||||
sample_song: str
|
"error_message": None,
|
||||||
season: str | None = None
|
}
|
||||||
num_of_people: int | None = None
|
}
|
||||||
people_category: str | None = None
|
)
|
||||||
genre: str | None = None
|
|
||||||
|
success: bool = Field(..., description="요청 성공 여부")
|
||||||
|
task_id: Optional[str] = Field(None, description="내부 작업 ID (Project task_id)")
|
||||||
@dataclass
|
creatomate_render_id: Optional[str] = Field(None, description="Creatomate 렌더 ID")
|
||||||
class PromptTemplateData:
|
message: str = Field(..., description="응답 메시지")
|
||||||
id: int
|
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시)")
|
||||||
prompt: str
|
|
||||||
description: str | None = None
|
|
||||||
|
class VideoRenderData(BaseModel):
|
||||||
|
"""Creatomate 렌더링 결과 데이터"""
|
||||||
@dataclass
|
|
||||||
class SongFormData:
|
id: Optional[str] = Field(None, description="렌더 ID")
|
||||||
store_name: str
|
status: Optional[str] = Field(None, description="렌더 상태")
|
||||||
store_id: str
|
url: Optional[str] = Field(None, description="영상 URL")
|
||||||
prompts: str
|
snapshot_url: Optional[str] = Field(None, description="스냅샷 URL")
|
||||||
attributes: Dict[str, str] = field(default_factory=dict)
|
|
||||||
attributes_str: str = ""
|
|
||||||
lyrics_ids: List[int] = field(default_factory=list)
|
class PollingVideoResponse(BaseModel):
|
||||||
llm_model: str = "gpt-4o"
|
"""영상 생성 상태 조회 응답 스키마
|
||||||
|
|
||||||
@classmethod
|
Usage:
|
||||||
async def from_form(cls, request: Request):
|
GET /video/status/{creatomate_render_id}
|
||||||
"""Request의 form 데이터로부터 dataclass 인스턴스 생성"""
|
Creatomate API 작업 상태를 조회합니다.
|
||||||
form_data = await request.form()
|
|
||||||
|
Note:
|
||||||
# 고정 필드명들
|
상태 값:
|
||||||
fixed_keys = {"store_info_name", "store_id", "llm_model", "prompts"}
|
- planned: 예약됨
|
||||||
|
- waiting: 대기 중
|
||||||
# lyrics-{id} 형태의 모든 키를 찾아서 ID 추출
|
- transcribing: 트랜스크립션 중
|
||||||
lyrics_ids = []
|
- rendering: 렌더링 중
|
||||||
attributes = {}
|
- succeeded: 성공
|
||||||
|
- failed: 실패
|
||||||
for key, value in form_data.items():
|
|
||||||
if key.startswith("lyrics-"):
|
Example Response (Success):
|
||||||
lyrics_id = key.split("-")[1]
|
{
|
||||||
lyrics_ids.append(int(lyrics_id))
|
"success": true,
|
||||||
elif key not in fixed_keys:
|
"status": "succeeded",
|
||||||
attributes[key] = value
|
"message": "영상 생성이 완료되었습니다.",
|
||||||
|
"render_data": {
|
||||||
# attributes를 문자열로 변환
|
"id": "render-id",
|
||||||
attributes_str = (
|
"status": "succeeded",
|
||||||
"\r\n\r\n".join([f"{key} : {value}" for key, value in attributes.items()])
|
"url": "https://...",
|
||||||
if attributes
|
"snapshot_url": "https://..."
|
||||||
else ""
|
},
|
||||||
)
|
"raw_response": {...},
|
||||||
|
"error_message": null
|
||||||
return cls(
|
}
|
||||||
store_name=form_data.get("store_info_name", ""),
|
"""
|
||||||
store_id=form_data.get("store_id", ""),
|
|
||||||
attributes=attributes,
|
success: bool = Field(..., description="조회 성공 여부")
|
||||||
attributes_str=attributes_str,
|
status: Optional[str] = Field(
|
||||||
lyrics_ids=lyrics_ids,
|
None, description="작업 상태 (planned, waiting, rendering, succeeded, failed)"
|
||||||
llm_model=form_data.get("llm_model", "gpt-4o"),
|
)
|
||||||
prompts=form_data.get("prompts", ""),
|
message: str = Field(..., description="상태 메시지")
|
||||||
)
|
render_data: Optional[VideoRenderData] = Field(None, description="렌더링 결과 데이터")
|
||||||
|
raw_response: Optional[Dict[str, Any]] = Field(None, description="Creatomate API 원본 응답")
|
||||||
|
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시)")
|
||||||
|
|
||||||
|
|
||||||
|
class DownloadVideoResponse(BaseModel):
|
||||||
|
"""영상 다운로드 응답 스키마
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
GET /video/download/{task_id}
|
||||||
|
Polls for video completion and returns project info with video URL.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
상태 값:
|
||||||
|
- processing: 영상 생성 진행 중 (result_movie_url은 null)
|
||||||
|
- completed: 영상 생성 완료 (result_movie_url 포함)
|
||||||
|
- failed: 영상 생성 실패
|
||||||
|
- not_found: task_id에 해당하는 Video 없음
|
||||||
|
- error: 조회 중 오류 발생
|
||||||
|
|
||||||
|
Example Response (Completed):
|
||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"status": "completed",
|
||||||
|
"message": "영상 다운로드가 완료되었습니다.",
|
||||||
|
"store_name": "스테이 머뭄",
|
||||||
|
"region": "군산",
|
||||||
|
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
||||||
|
"result_movie_url": "http://localhost:8000/media/2025-01-15/video.mp4",
|
||||||
|
"created_at": "2025-01-15T12:00:00",
|
||||||
|
"error_message": null
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
success: bool = Field(..., description="다운로드 성공 여부")
|
||||||
|
status: str = Field(..., description="처리 상태 (processing, completed, failed, not_found, error)")
|
||||||
|
message: str = Field(..., description="응답 메시지")
|
||||||
|
store_name: Optional[str] = Field(None, description="업체명")
|
||||||
|
region: Optional[str] = Field(None, description="지역명")
|
||||||
|
task_id: Optional[str] = Field(None, description="작업 고유 식별자")
|
||||||
|
result_movie_url: Optional[str] = Field(None, description="영상 결과 URL")
|
||||||
|
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
||||||
|
error_message: Optional[str] = Field(None, description="에러 메시지 (실패 시)")
|
||||||
|
|
||||||
|
|
||||||
|
class VideoListItem(BaseModel):
|
||||||
|
"""영상 목록 아이템 스키마
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
GET /videos 응답의 개별 영상 정보
|
||||||
|
|
||||||
|
Example:
|
||||||
|
{
|
||||||
|
"store_name": "스테이 머뭄",
|
||||||
|
"region": "군산",
|
||||||
|
"task_id": "019123ab-cdef-7890-abcd-ef1234567890",
|
||||||
|
"result_movie_url": "http://localhost:8000/media/2025-01-15/video.mp4",
|
||||||
|
"created_at": "2025-01-15T12:00:00"
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
store_name: Optional[str] = Field(None, description="업체명")
|
||||||
|
region: Optional[str] = Field(None, description="지역명")
|
||||||
|
task_id: str = Field(..., description="작업 고유 식별자")
|
||||||
|
result_movie_url: Optional[str] = Field(None, description="영상 결과 URL")
|
||||||
|
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
||||||
@@ -0,0 +1,333 @@
|
|||||||
|
"""
|
||||||
|
Video Background Tasks
|
||||||
|
|
||||||
|
영상 생성 관련 백그라운드 태스크를 정의합니다.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import traceback
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import aiofiles
|
||||||
|
import httpx
|
||||||
|
from sqlalchemy import select
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
|
||||||
|
from app.database.session import BackgroundSessionLocal
|
||||||
|
from app.video.models import Video
|
||||||
|
from app.utils.upload_blob_as_request import AzureBlobUploader
|
||||||
|
|
||||||
|
# 로거 설정
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# HTTP 요청 설정
|
||||||
|
REQUEST_TIMEOUT = 300.0 # 초 (영상은 용량이 크므로 5분)
|
||||||
|
|
||||||
|
|
||||||
|
async def _update_video_status(
|
||||||
|
task_id: str,
|
||||||
|
status: str,
|
||||||
|
video_url: str | None = None,
|
||||||
|
creatomate_render_id: str | None = None,
|
||||||
|
) -> bool:
|
||||||
|
"""Video 테이블의 상태를 업데이트합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task_id: 프로젝트 task_id
|
||||||
|
status: 변경할 상태 ("processing", "completed", "failed")
|
||||||
|
video_url: 영상 URL
|
||||||
|
creatomate_render_id: Creatomate render ID (선택)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 업데이트 성공 여부
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
async with BackgroundSessionLocal() as session:
|
||||||
|
if creatomate_render_id:
|
||||||
|
query_result = await session.execute(
|
||||||
|
select(Video)
|
||||||
|
.where(Video.creatomate_render_id == creatomate_render_id)
|
||||||
|
.order_by(Video.created_at.desc())
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
query_result = await session.execute(
|
||||||
|
select(Video)
|
||||||
|
.where(Video.task_id == task_id)
|
||||||
|
.order_by(Video.created_at.desc())
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
|
||||||
|
video = query_result.scalar_one_or_none()
|
||||||
|
|
||||||
|
if video:
|
||||||
|
video.status = status
|
||||||
|
if video_url is not None:
|
||||||
|
video.result_movie_url = video_url
|
||||||
|
await session.commit()
|
||||||
|
logger.info(f"[Video] Status updated - task_id: {task_id}, status: {status}")
|
||||||
|
print(f"[Video] Status updated - task_id: {task_id}, status: {status}")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
logger.warning(f"[Video] NOT FOUND in DB - task_id: {task_id}")
|
||||||
|
print(f"[Video] NOT FOUND in DB - task_id: {task_id}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
logger.error(f"[Video] DB Error while updating status - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[Video] DB Error while updating status - task_id: {task_id}, error: {e}")
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[Video] Unexpected error while updating status - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[Video] Unexpected error while updating status - task_id: {task_id}, error: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def _download_video(url: str, task_id: str) -> bytes:
|
||||||
|
"""URL에서 영상을 다운로드합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
url: 다운로드할 URL
|
||||||
|
task_id: 로그용 task_id
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bytes: 다운로드한 파일 내용
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
httpx.HTTPError: 다운로드 실패 시
|
||||||
|
"""
|
||||||
|
logger.info(f"[VideoDownload] Downloading - task_id: {task_id}")
|
||||||
|
print(f"[VideoDownload] Downloading - task_id: {task_id}")
|
||||||
|
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
response = await client.get(url, timeout=REQUEST_TIMEOUT)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
logger.info(f"[VideoDownload] SUCCESS - task_id: {task_id}, size: {len(response.content)} bytes")
|
||||||
|
print(f"[VideoDownload] SUCCESS - task_id: {task_id}, size: {len(response.content)} bytes")
|
||||||
|
return response.content
|
||||||
|
|
||||||
|
|
||||||
|
async def download_and_upload_video_to_blob(
|
||||||
|
task_id: str,
|
||||||
|
video_url: str,
|
||||||
|
store_name: str,
|
||||||
|
) -> None:
|
||||||
|
"""백그라운드에서 영상을 다운로드하고 Azure Blob Storage에 업로드한 뒤 Video 테이블을 업데이트합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task_id: 프로젝트 task_id
|
||||||
|
video_url: 다운로드할 영상 URL
|
||||||
|
store_name: 저장할 파일명에 사용할 업체명
|
||||||
|
"""
|
||||||
|
logger.info(f"[download_and_upload_video_to_blob] START - task_id: {task_id}, store_name: {store_name}")
|
||||||
|
print(f"[download_and_upload_video_to_blob] START - task_id: {task_id}, store_name: {store_name}")
|
||||||
|
temp_file_path: Path | None = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 파일명에 사용할 수 없는 문자 제거
|
||||||
|
safe_store_name = "".join(
|
||||||
|
c for c in store_name if c.isalnum() or c in (" ", "_", "-")
|
||||||
|
).strip()
|
||||||
|
safe_store_name = safe_store_name or "video"
|
||||||
|
file_name = f"{safe_store_name}.mp4"
|
||||||
|
|
||||||
|
# 임시 저장 경로 생성
|
||||||
|
temp_dir = Path("media") / "temp" / task_id
|
||||||
|
temp_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
temp_file_path = temp_dir / file_name
|
||||||
|
logger.info(f"[download_and_upload_video_to_blob] Temp directory created - path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_video_to_blob] Temp directory created - path: {temp_file_path}")
|
||||||
|
|
||||||
|
# 영상 파일 다운로드
|
||||||
|
logger.info(f"[download_and_upload_video_to_blob] Downloading video - task_id: {task_id}, url: {video_url}")
|
||||||
|
print(f"[download_and_upload_video_to_blob] Downloading video - task_id: {task_id}, url: {video_url}")
|
||||||
|
|
||||||
|
content = await _download_video(video_url, task_id)
|
||||||
|
|
||||||
|
async with aiofiles.open(str(temp_file_path), "wb") as f:
|
||||||
|
await f.write(content)
|
||||||
|
|
||||||
|
logger.info(f"[download_and_upload_video_to_blob] File downloaded - task_id: {task_id}, path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_video_to_blob] File downloaded - task_id: {task_id}, path: {temp_file_path}")
|
||||||
|
|
||||||
|
# Azure Blob Storage에 업로드
|
||||||
|
uploader = AzureBlobUploader(task_id=task_id)
|
||||||
|
upload_success = await uploader.upload_video(file_path=str(temp_file_path))
|
||||||
|
|
||||||
|
if not upload_success:
|
||||||
|
raise Exception("Azure Blob Storage 업로드 실패")
|
||||||
|
|
||||||
|
# SAS 토큰이 제외된 public_url 사용
|
||||||
|
blob_url = uploader.public_url
|
||||||
|
logger.info(f"[download_and_upload_video_to_blob] Uploaded to Blob - task_id: {task_id}, url: {blob_url}")
|
||||||
|
print(f"[download_and_upload_video_to_blob] Uploaded to Blob - task_id: {task_id}, url: {blob_url}")
|
||||||
|
|
||||||
|
# Video 테이블 업데이트
|
||||||
|
await _update_video_status(task_id, "completed", blob_url)
|
||||||
|
logger.info(f"[download_and_upload_video_to_blob] SUCCESS - task_id: {task_id}")
|
||||||
|
print(f"[download_and_upload_video_to_blob] SUCCESS - task_id: {task_id}")
|
||||||
|
|
||||||
|
except httpx.HTTPError as e:
|
||||||
|
logger.error(f"[download_and_upload_video_to_blob] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_video_to_blob] DOWNLOAD ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
await _update_video_status(task_id, "failed")
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
logger.error(f"[download_and_upload_video_to_blob] DB ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_video_to_blob] DB ERROR - task_id: {task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
await _update_video_status(task_id, "failed")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[download_and_upload_video_to_blob] EXCEPTION - task_id: {task_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_video_to_blob] EXCEPTION - task_id: {task_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
await _update_video_status(task_id, "failed")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# 임시 파일 삭제
|
||||||
|
if temp_file_path and temp_file_path.exists():
|
||||||
|
try:
|
||||||
|
temp_file_path.unlink()
|
||||||
|
logger.info(f"[download_and_upload_video_to_blob] Temp file deleted - path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_video_to_blob] Temp file deleted - path: {temp_file_path}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[download_and_upload_video_to_blob] Failed to delete temp file: {e}")
|
||||||
|
print(f"[download_and_upload_video_to_blob] Failed to delete temp file: {e}")
|
||||||
|
|
||||||
|
# 임시 디렉토리 삭제 시도
|
||||||
|
temp_dir = Path("media") / "temp" / task_id
|
||||||
|
if temp_dir.exists():
|
||||||
|
try:
|
||||||
|
temp_dir.rmdir()
|
||||||
|
except Exception:
|
||||||
|
pass # 디렉토리가 비어있지 않으면 무시
|
||||||
|
|
||||||
|
|
||||||
|
async def download_and_upload_video_by_creatomate_render_id(
|
||||||
|
creatomate_render_id: str,
|
||||||
|
video_url: str,
|
||||||
|
store_name: str,
|
||||||
|
) -> None:
|
||||||
|
"""creatomate_render_id로 Video를 조회하여 영상을 다운로드하고 Azure Blob Storage에 업로드한 뒤 Video 테이블을 업데이트합니다.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
creatomate_render_id: Creatomate API 렌더 ID
|
||||||
|
video_url: 다운로드할 영상 URL
|
||||||
|
store_name: 저장할 파일명에 사용할 업체명
|
||||||
|
"""
|
||||||
|
logger.info(f"[download_and_upload_video_by_creatomate_render_id] START - creatomate_render_id: {creatomate_render_id}, store_name: {store_name}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] START - creatomate_render_id: {creatomate_render_id}, store_name: {store_name}")
|
||||||
|
temp_file_path: Path | None = None
|
||||||
|
task_id: str | None = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
# creatomate_render_id로 Video 조회하여 task_id 가져오기
|
||||||
|
async with BackgroundSessionLocal() as session:
|
||||||
|
result = await session.execute(
|
||||||
|
select(Video)
|
||||||
|
.where(Video.creatomate_render_id == creatomate_render_id)
|
||||||
|
.order_by(Video.created_at.desc())
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
video = result.scalar_one_or_none()
|
||||||
|
|
||||||
|
if not video:
|
||||||
|
logger.warning(f"[download_and_upload_video_by_creatomate_render_id] Video NOT FOUND - creatomate_render_id: {creatomate_render_id}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] Video NOT FOUND - creatomate_render_id: {creatomate_render_id}")
|
||||||
|
return
|
||||||
|
|
||||||
|
task_id = video.task_id
|
||||||
|
logger.info(f"[download_and_upload_video_by_creatomate_render_id] Video found - creatomate_render_id: {creatomate_render_id}, task_id: {task_id}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] Video found - creatomate_render_id: {creatomate_render_id}, task_id: {task_id}")
|
||||||
|
|
||||||
|
# 파일명에 사용할 수 없는 문자 제거
|
||||||
|
safe_store_name = "".join(
|
||||||
|
c for c in store_name if c.isalnum() or c in (" ", "_", "-")
|
||||||
|
).strip()
|
||||||
|
safe_store_name = safe_store_name or "video"
|
||||||
|
file_name = f"{safe_store_name}.mp4"
|
||||||
|
|
||||||
|
# 임시 저장 경로 생성
|
||||||
|
temp_dir = Path("media") / "temp" / task_id
|
||||||
|
temp_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
temp_file_path = temp_dir / file_name
|
||||||
|
logger.info(f"[download_and_upload_video_by_creatomate_render_id] Temp directory created - path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] Temp directory created - path: {temp_file_path}")
|
||||||
|
|
||||||
|
# 영상 파일 다운로드
|
||||||
|
logger.info(f"[download_and_upload_video_by_creatomate_render_id] Downloading video - creatomate_render_id: {creatomate_render_id}, url: {video_url}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] Downloading video - creatomate_render_id: {creatomate_render_id}, url: {video_url}")
|
||||||
|
|
||||||
|
content = await _download_video(video_url, task_id)
|
||||||
|
|
||||||
|
async with aiofiles.open(str(temp_file_path), "wb") as f:
|
||||||
|
await f.write(content)
|
||||||
|
|
||||||
|
logger.info(f"[download_and_upload_video_by_creatomate_render_id] File downloaded - creatomate_render_id: {creatomate_render_id}, path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] File downloaded - creatomate_render_id: {creatomate_render_id}, path: {temp_file_path}")
|
||||||
|
|
||||||
|
# Azure Blob Storage에 업로드
|
||||||
|
uploader = AzureBlobUploader(task_id=task_id)
|
||||||
|
upload_success = await uploader.upload_video(file_path=str(temp_file_path))
|
||||||
|
|
||||||
|
if not upload_success:
|
||||||
|
raise Exception("Azure Blob Storage 업로드 실패")
|
||||||
|
|
||||||
|
# SAS 토큰이 제외된 public_url 사용
|
||||||
|
blob_url = uploader.public_url
|
||||||
|
logger.info(f"[download_and_upload_video_by_creatomate_render_id] Uploaded to Blob - creatomate_render_id: {creatomate_render_id}, url: {blob_url}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] Uploaded to Blob - creatomate_render_id: {creatomate_render_id}, url: {blob_url}")
|
||||||
|
|
||||||
|
# Video 테이블 업데이트
|
||||||
|
await _update_video_status(
|
||||||
|
task_id=task_id,
|
||||||
|
status="completed",
|
||||||
|
video_url=blob_url,
|
||||||
|
creatomate_render_id=creatomate_render_id,
|
||||||
|
)
|
||||||
|
logger.info(f"[download_and_upload_video_by_creatomate_render_id] SUCCESS - creatomate_render_id: {creatomate_render_id}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] SUCCESS - creatomate_render_id: {creatomate_render_id}")
|
||||||
|
|
||||||
|
except httpx.HTTPError as e:
|
||||||
|
logger.error(f"[download_and_upload_video_by_creatomate_render_id] DOWNLOAD ERROR - creatomate_render_id: {creatomate_render_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] DOWNLOAD ERROR - creatomate_render_id: {creatomate_render_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
if task_id:
|
||||||
|
await _update_video_status(task_id, "failed", creatomate_render_id=creatomate_render_id)
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
logger.error(f"[download_and_upload_video_by_creatomate_render_id] DB ERROR - creatomate_render_id: {creatomate_render_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] DB ERROR - creatomate_render_id: {creatomate_render_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
if task_id:
|
||||||
|
await _update_video_status(task_id, "failed", creatomate_render_id=creatomate_render_id)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[download_and_upload_video_by_creatomate_render_id] EXCEPTION - creatomate_render_id: {creatomate_render_id}, error: {e}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] EXCEPTION - creatomate_render_id: {creatomate_render_id}, error: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
if task_id:
|
||||||
|
await _update_video_status(task_id, "failed", creatomate_render_id=creatomate_render_id)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# 임시 파일 삭제
|
||||||
|
if temp_file_path and temp_file_path.exists():
|
||||||
|
try:
|
||||||
|
temp_file_path.unlink()
|
||||||
|
logger.info(f"[download_and_upload_video_by_creatomate_render_id] Temp file deleted - path: {temp_file_path}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] Temp file deleted - path: {temp_file_path}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[download_and_upload_video_by_creatomate_render_id] Failed to delete temp file: {e}")
|
||||||
|
print(f"[download_and_upload_video_by_creatomate_render_id] Failed to delete temp file: {e}")
|
||||||
|
|
||||||
|
# 임시 디렉토리 삭제 시도
|
||||||
|
if task_id:
|
||||||
|
temp_dir = Path("media") / "temp" / task_id
|
||||||
|
if temp_dir.exists():
|
||||||
|
try:
|
||||||
|
temp_dir.rmdir()
|
||||||
|
except Exception:
|
||||||
|
pass # 디렉토리가 비어있지 않으면 무시
|
||||||
@@ -1,136 +1,179 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
PROJECT_DIR = Path(__file__).resolve().parent
|
PROJECT_DIR = Path(__file__).resolve().parent
|
||||||
|
|
||||||
_base_config = SettingsConfigDict(
|
_base_config = SettingsConfigDict(
|
||||||
env_file=PROJECT_DIR / ".env",
|
env_file=PROJECT_DIR / ".env",
|
||||||
env_ignore_empty=True,
|
env_ignore_empty=True,
|
||||||
extra="ignore",
|
extra="ignore",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ProjectSettings(BaseSettings):
|
class ProjectSettings(BaseSettings):
|
||||||
PROJECT_NAME: str = Field(default="CastAD")
|
PROJECT_NAME: str = Field(default="CastAD")
|
||||||
PROJECT_DOMAIN: str = Field(default="localhost:8000")
|
PROJECT_DOMAIN: str = Field(default="localhost:8000")
|
||||||
VERSION: str = Field(default="0.1.0")
|
VERSION: str = Field(default="0.1.0")
|
||||||
DESCRIPTION: str = Field(default="FastAPI 기반 CastAD 프로젝트")
|
DESCRIPTION: str = Field(default="FastAPI 기반 CastAD 프로젝트")
|
||||||
ADMIN_BASE_URL: str = Field(default="/admin")
|
ADMIN_BASE_URL: str = Field(default="/admin")
|
||||||
DEBUG: bool = Field(default=True)
|
DEBUG: bool = Field(default=True)
|
||||||
|
|
||||||
model_config = _base_config
|
model_config = _base_config
|
||||||
|
|
||||||
|
|
||||||
class APIKeySettings(BaseSettings):
|
class APIKeySettings(BaseSettings):
|
||||||
CHATGPT_API_KEY: str = Field(default="your-chatgpt-api-key") # 기본값 추가
|
CHATGPT_API_KEY: str = Field(default="your-chatgpt-api-key") # 기본값 추가
|
||||||
SUNO_API_KEY: str = Field(default="your-suno-api-key") # Suno API 키
|
SUNO_API_KEY: str = Field(default="your-suno-api-key") # Suno API 키
|
||||||
SUNO_CALLBACK_URL: str = Field(
|
SUNO_CALLBACK_URL: str = Field(
|
||||||
default="https://example.com/api/suno/callback"
|
default="https://example.com/api/suno/callback"
|
||||||
) # Suno 콜백 URL (필수)
|
) # Suno 콜백 URL (필수)
|
||||||
CREATOMATE_API_KEY: str = Field(default="your-creatomate-api-key") # Creatomate API 키
|
CREATOMATE_API_KEY: str = Field(default="your-creatomate-api-key") # Creatomate API 키
|
||||||
|
|
||||||
model_config = _base_config
|
model_config = _base_config
|
||||||
|
|
||||||
|
|
||||||
class CORSSettings(BaseSettings):
|
class CORSSettings(BaseSettings):
|
||||||
# CORS (Cross-Origin Resource Sharing) 설정
|
# CORS (Cross-Origin Resource Sharing) 설정
|
||||||
|
|
||||||
# 요청을 허용할 출처(Origin) 목록
|
# 요청을 허용할 출처(Origin) 목록
|
||||||
# ["*"]: 모든 출처 허용 (개발 환경용, 프로덕션에서는 구체적인 도메인 지정 권장)
|
# ["*"]: 모든 출처 허용 (개발 환경용, 프로덕션에서는 구체적인 도메인 지정 권장)
|
||||||
# 예: ["https://example.com", "https://app.example.com"]
|
# 예: ["https://example.com", "https://app.example.com"]
|
||||||
CORS_ALLOW_ORIGINS: list[str] = ["*"]
|
CORS_ALLOW_ORIGINS: list[str] = ["*"]
|
||||||
|
|
||||||
# 자격 증명(쿠키, Authorization 헤더 등) 포함 요청 허용 여부
|
# 자격 증명(쿠키, Authorization 헤더 등) 포함 요청 허용 여부
|
||||||
# True: 클라이언트가 credentials: 'include'로 요청 시 쿠키/인증 정보 전송 가능
|
# True: 클라이언트가 credentials: 'include'로 요청 시 쿠키/인증 정보 전송 가능
|
||||||
# 주의: CORS_ALLOW_ORIGINS가 ["*"]일 때는 보안상 False 권장
|
# 주의: CORS_ALLOW_ORIGINS가 ["*"]일 때는 보안상 False 권장
|
||||||
CORS_ALLOW_CREDENTIALS: bool = True
|
CORS_ALLOW_CREDENTIALS: bool = True
|
||||||
|
|
||||||
# 허용할 HTTP 메서드 목록
|
# 허용할 HTTP 메서드 목록
|
||||||
# ["*"]: 모든 메서드 허용 (GET, POST, PUT, DELETE, PATCH, OPTIONS 등)
|
# ["*"]: 모든 메서드 허용 (GET, POST, PUT, DELETE, PATCH, OPTIONS 등)
|
||||||
# 구체적 지정 예: ["GET", "POST", "PUT", "DELETE"]
|
# 구체적 지정 예: ["GET", "POST", "PUT", "DELETE"]
|
||||||
CORS_ALLOW_METHODS: list[str] = ["*"]
|
CORS_ALLOW_METHODS: list[str] = ["*"]
|
||||||
|
|
||||||
# 클라이언트가 요청 시 사용할 수 있는 HTTP 헤더 목록
|
# 클라이언트가 요청 시 사용할 수 있는 HTTP 헤더 목록
|
||||||
# ["*"]: 모든 헤더 허용
|
# ["*"]: 모든 헤더 허용
|
||||||
# 구체적 지정 예: ["Content-Type", "Authorization", "X-Custom-Header"]
|
# 구체적 지정 예: ["Content-Type", "Authorization", "X-Custom-Header"]
|
||||||
CORS_ALLOW_HEADERS: list[str] = ["*"]
|
CORS_ALLOW_HEADERS: list[str] = ["*"]
|
||||||
|
|
||||||
# 브라우저의 JavaScript에서 접근 가능한 응답 헤더 목록
|
# 브라우저의 JavaScript에서 접근 가능한 응답 헤더 목록
|
||||||
# []: 기본 안전 헤더(Cache-Control, Content-Language, Content-Type,
|
# []: 기본 안전 헤더(Cache-Control, Content-Language, Content-Type,
|
||||||
# Expires, Last-Modified, Pragma)만 접근 가능
|
# Expires, Last-Modified, Pragma)만 접근 가능
|
||||||
# 추가 노출 필요 시: ["X-Total-Count", "X-Request-Id", "X-Custom-Header"]
|
# 추가 노출 필요 시: ["X-Total-Count", "X-Request-Id", "X-Custom-Header"]
|
||||||
CORS_EXPOSE_HEADERS: list[str] = []
|
CORS_EXPOSE_HEADERS: list[str] = []
|
||||||
|
|
||||||
# Preflight 요청(OPTIONS) 결과를 캐시하는 시간(초)
|
# Preflight 요청(OPTIONS) 결과를 캐시하는 시간(초)
|
||||||
# 600: 10분간 캐시 (이 시간 동안 동일 요청에 대해 preflight 생략)
|
# 600: 10분간 캐시 (이 시간 동안 동일 요청에 대해 preflight 생략)
|
||||||
# 0으로 설정 시 매번 preflight 요청 발생
|
# 0으로 설정 시 매번 preflight 요청 발생
|
||||||
CORS_MAX_AGE: int = 600
|
CORS_MAX_AGE: int = 600
|
||||||
|
|
||||||
model_config = _base_config
|
model_config = _base_config
|
||||||
|
|
||||||
|
|
||||||
class DatabaseSettings(BaseSettings):
|
class DatabaseSettings(BaseSettings):
|
||||||
# MySQL 연결 설정 (기본값: 테스트 계정 및 poc DB)
|
# MySQL 연결 설정 (기본값: 테스트 계정 및 poc DB)
|
||||||
MYSQL_HOST: str = Field(default="localhost")
|
MYSQL_HOST: str = Field(default="localhost")
|
||||||
MYSQL_PORT: int = Field(default=3306)
|
MYSQL_PORT: int = Field(default=3306)
|
||||||
MYSQL_USER: str = Field(default="test")
|
MYSQL_USER: str = Field(default="test")
|
||||||
MYSQL_PASSWORD: str = Field(default="") # 환경변수에서 로드
|
MYSQL_PASSWORD: str = Field(default="") # 환경변수에서 로드
|
||||||
MYSQL_DB: str = Field(default="poc")
|
MYSQL_DB: str = Field(default="poc")
|
||||||
|
|
||||||
# Redis 설정
|
# Redis 설정
|
||||||
REDIS_HOST: str = "localhost"
|
REDIS_HOST: str = "localhost"
|
||||||
REDIS_PORT: int = 6379
|
REDIS_PORT: int = 6379
|
||||||
|
|
||||||
model_config = _base_config
|
model_config = _base_config
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def MYSQL_URL(self) -> str:
|
def MYSQL_URL(self) -> str:
|
||||||
"""비동기 MySQL URL 생성 (asyncmy 드라이버 사용, SQLAlchemy 통합 최적화)"""
|
"""비동기 MySQL URL 생성 (asyncmy 드라이버 사용, SQLAlchemy 통합 최적화)"""
|
||||||
return f"mysql+asyncmy://{self.MYSQL_USER}:{self.MYSQL_PASSWORD}@{self.MYSQL_HOST}:{self.MYSQL_PORT}/{self.MYSQL_DB}"
|
return f"mysql+asyncmy://{self.MYSQL_USER}:{self.MYSQL_PASSWORD}@{self.MYSQL_HOST}:{self.MYSQL_PORT}/{self.MYSQL_DB}"
|
||||||
|
|
||||||
def REDIS_URL(self, db: int = 0) -> str:
|
def REDIS_URL(self, db: int = 0) -> str:
|
||||||
"""Redis URL 생성 (db 인수로 기본값 지원)"""
|
"""Redis URL 생성 (db 인수로 기본값 지원)"""
|
||||||
return f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{db}"
|
return f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{db}"
|
||||||
|
|
||||||
|
|
||||||
class SecuritySettings(BaseSettings):
|
class SecuritySettings(BaseSettings):
|
||||||
JWT_SECRET: str = "your-jwt-secret-key" # 기본값 추가 (필수 필드 안전)
|
JWT_SECRET: str = "your-jwt-secret-key" # 기본값 추가 (필수 필드 안전)
|
||||||
JWT_ALGORITHM: str = "HS256" # 기본값 추가 (필수 필드 안전)
|
JWT_ALGORITHM: str = "HS256" # 기본값 추가 (필수 필드 안전)
|
||||||
|
|
||||||
model_config = _base_config
|
model_config = _base_config
|
||||||
|
|
||||||
|
|
||||||
class NotificationSettings(BaseSettings):
|
class NotificationSettings(BaseSettings):
|
||||||
MAIL_USERNAME: str = "your-email@example.com" # 기본값 추가
|
MAIL_USERNAME: str = "your-email@example.com" # 기본값 추가
|
||||||
MAIL_PASSWORD: str = "your-email-password" # 기본값 추가
|
MAIL_PASSWORD: str = "your-email-password" # 기본값 추가
|
||||||
MAIL_FROM: str = "your-email@example.com" # 기본값 추가
|
MAIL_FROM: str = "your-email@example.com" # 기본값 추가
|
||||||
MAIL_PORT: int = 587 # 기본값 추가
|
MAIL_PORT: int = 587 # 기본값 추가
|
||||||
MAIL_SERVER: str = "smtp.gmail.com" # 기본값 추가
|
MAIL_SERVER: str = "smtp.gmail.com" # 기본값 추가
|
||||||
MAIL_FROM_NAME: str = "FastPOC App" # 기본값 추가
|
MAIL_FROM_NAME: str = "FastPOC App" # 기본값 추가
|
||||||
MAIL_STARTTLS: bool = True
|
MAIL_STARTTLS: bool = True
|
||||||
MAIL_SSL_TLS: bool = False
|
MAIL_SSL_TLS: bool = False
|
||||||
USE_CREDENTIALS: bool = True
|
USE_CREDENTIALS: bool = True
|
||||||
VALIDATE_CERTS: bool = True
|
VALIDATE_CERTS: bool = True
|
||||||
|
|
||||||
TWILIO_SID: str = "your-twilio-sid" # 기본값 추가
|
TWILIO_SID: str = "your-twilio-sid" # 기본값 추가
|
||||||
TWILIO_AUTH_TOKEN: str = "your-twilio-token" # 기본값 추가
|
TWILIO_AUTH_TOKEN: str = "your-twilio-token" # 기본값 추가
|
||||||
TWILIO_NUMBER: str = "+1234567890" # 기본값 추가
|
TWILIO_NUMBER: str = "+1234567890" # 기본값 추가
|
||||||
|
|
||||||
model_config = _base_config
|
model_config = _base_config
|
||||||
|
|
||||||
|
|
||||||
class CrawlerSettings(BaseSettings):
|
class CrawlerSettings(BaseSettings):
|
||||||
NAVER_COOKIES: str = Field(default="")
|
NAVER_COOKIES: str = Field(default="")
|
||||||
|
|
||||||
model_config = _base_config
|
model_config = _base_config
|
||||||
|
|
||||||
|
|
||||||
prj_settings = ProjectSettings()
|
class AzureBlobSettings(BaseSettings):
|
||||||
apikey_settings = APIKeySettings()
|
"""Azure Blob Storage 설정"""
|
||||||
db_settings = DatabaseSettings()
|
|
||||||
security_settings = SecuritySettings()
|
AZURE_BLOB_SAS_TOKEN: str = Field(
|
||||||
notification_settings = NotificationSettings()
|
default="",
|
||||||
cors_settings = CORSSettings()
|
description="Azure Blob Storage SAS 토큰",
|
||||||
crawler_settings = CrawlerSettings()
|
)
|
||||||
|
AZURE_BLOB_BASE_URL: str = Field(
|
||||||
|
default="https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original",
|
||||||
|
description="Azure Blob Storage 기본 URL",
|
||||||
|
)
|
||||||
|
|
||||||
|
model_config = _base_config
|
||||||
|
|
||||||
|
|
||||||
|
class CreatomateSettings(BaseSettings):
|
||||||
|
"""Creatomate 템플릿 설정"""
|
||||||
|
|
||||||
|
# 세로형 템플릿 (기본값)
|
||||||
|
TEMPLATE_ID_VERTICAL: str = Field(
|
||||||
|
default="e8c7b43f-de4b-4ba3-b8eb-5df688569193",
|
||||||
|
description="Creatomate 세로형 템플릿 ID",
|
||||||
|
)
|
||||||
|
TEMPLATE_DURATION_VERTICAL: float = Field(
|
||||||
|
default=90.0,
|
||||||
|
description="세로형 템플릿 기본 duration (초)",
|
||||||
|
)
|
||||||
|
|
||||||
|
# 가로형 템플릿
|
||||||
|
TEMPLATE_ID_HORIZONTAL: str = Field(
|
||||||
|
default="0f092a6a-f526-4ef0-9181-d4ad4426b9e7",
|
||||||
|
description="Creatomate 가로형 템플릿 ID",
|
||||||
|
)
|
||||||
|
TEMPLATE_DURATION_HORIZONTAL: float = Field(
|
||||||
|
default=30.0,
|
||||||
|
description="가로형 템플릿 기본 duration (초)",
|
||||||
|
)
|
||||||
|
|
||||||
|
model_config = _base_config
|
||||||
|
|
||||||
|
|
||||||
|
prj_settings = ProjectSettings()
|
||||||
|
apikey_settings = APIKeySettings()
|
||||||
|
db_settings = DatabaseSettings()
|
||||||
|
security_settings = SecuritySettings()
|
||||||
|
notification_settings = NotificationSettings()
|
||||||
|
cors_settings = CORSSettings()
|
||||||
|
crawler_settings = CrawlerSettings()
|
||||||
|
azure_blob_settings = AzureBlobSettings()
|
||||||
|
creatomate_settings = CreatomateSettings()
|
||||||
@@ -0,0 +1,792 @@
|
|||||||
|
# O2O-CASTAD Backend 비동기 아키텍처 및 설계 분석 보고서
|
||||||
|
|
||||||
|
> **문서 버전**: 1.0
|
||||||
|
> **작성일**: 2025-12-29
|
||||||
|
> **대상**: 개발자, 아키텍트, 코드 리뷰어
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 목차
|
||||||
|
|
||||||
|
1. [Executive Summary](#1-executive-summary)
|
||||||
|
2. [데이터베이스 세션 관리 아키텍처](#2-데이터베이스-세션-관리-아키텍처)
|
||||||
|
3. [비동기 처리 패턴](#3-비동기-처리-패턴)
|
||||||
|
4. [외부 API 통합 설계](#4-외부-api-통합-설계)
|
||||||
|
5. [백그라운드 태스크 워크플로우](#5-백그라운드-태스크-워크플로우)
|
||||||
|
6. [쿼리 최적화 전략](#6-쿼리-최적화-전략)
|
||||||
|
7. [설계 강점 분석](#7-설계-강점-분석)
|
||||||
|
8. [개선 권장 사항](#8-개선-권장-사항)
|
||||||
|
9. [아키텍처 다이어그램](#9-아키텍처-다이어그램)
|
||||||
|
10. [결론](#10-결론)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Executive Summary
|
||||||
|
|
||||||
|
### 1.1 프로젝트 개요
|
||||||
|
|
||||||
|
O2O-CASTAD Backend는 FastAPI 기반의 비동기 백엔드 서비스로, AI 기반 광고 영상 자동 생성 파이프라인을 제공합니다. 주요 외부 서비스(Creatomate, Suno, ChatGPT, Azure Blob Storage)와의 통합을 통해 가사 생성 → 노래 생성 → 영상 생성의 파이프라인을 구현합니다.
|
||||||
|
|
||||||
|
### 1.2 주요 성과
|
||||||
|
|
||||||
|
| 영역 | 개선 전 | 개선 후 | 개선율 |
|
||||||
|
|------|---------|---------|--------|
|
||||||
|
| 템플릿 API 호출 | 매번 호출 (1-2s) | 캐시 HIT (0ms) | **100% 감소** |
|
||||||
|
| HTTP 클라이언트 | 매번 생성 (50ms) | 풀 재사용 (0ms) | **100% 감소** |
|
||||||
|
| 세션 타임아웃 에러 | 빈번 | 해결 | **안정성 확보** |
|
||||||
|
|
||||||
|
### 1.3 핵심 아키텍처 결정
|
||||||
|
|
||||||
|
1. **이중 커넥션 풀 아키텍처**: 요청/백그라운드 분리
|
||||||
|
2. **명시적 세션 라이프사이클**: 외부 API 호출 전 세션 해제
|
||||||
|
3. **모듈 레벨 싱글톤**: HTTP 클라이언트 및 템플릿 캐시
|
||||||
|
4. **순차 쿼리 실행**: AsyncSession 제약으로 단일 세션 내 병렬 쿼리 불가
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 데이터베이스 세션 관리 아키텍처
|
||||||
|
|
||||||
|
### 2.1 이중 엔진 구조
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────────────────────────────────────────────────────┐
|
||||||
|
│ DATABASE LAYER │
|
||||||
|
├─────────────────────────────┬───────────────────────────────┤
|
||||||
|
│ MAIN ENGINE │ BACKGROUND ENGINE │
|
||||||
|
│ (FastAPI Requests) │ (Worker Tasks) │
|
||||||
|
├─────────────────────────────┼───────────────────────────────┤
|
||||||
|
│ pool_size: 20 │ pool_size: 10 │
|
||||||
|
│ max_overflow: 20 │ max_overflow: 10 │
|
||||||
|
│ pool_timeout: 30s │ pool_timeout: 60s │
|
||||||
|
│ Total: 최대 40 연결 │ Total: 최대 20 연결 │
|
||||||
|
├─────────────────────────────┼───────────────────────────────┤
|
||||||
|
│ AsyncSessionLocal │ BackgroundSessionLocal │
|
||||||
|
│ → Router endpoints │ → download_and_upload_* │
|
||||||
|
│ → Direct API calls │ → generate_lyric_background │
|
||||||
|
└─────────────────────────────┴───────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
**위치**: `app/database/session.py`
|
||||||
|
|
||||||
|
### 2.2 엔진 설정 상세
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 메인 엔진 (FastAPI 요청용)
|
||||||
|
engine = create_async_engine(
|
||||||
|
url=db_settings.MYSQL_URL,
|
||||||
|
pool_size=20, # 기본 풀 크기
|
||||||
|
max_overflow=20, # 추가 연결 허용
|
||||||
|
pool_timeout=30, # 연결 대기 최대 시간
|
||||||
|
pool_recycle=280, # MySQL wait_timeout 보다 짧게 설정
|
||||||
|
pool_pre_ping=True, # 연결 유효성 검사 (핵심!)
|
||||||
|
pool_reset_on_return="rollback", # 반환 시 롤백
|
||||||
|
)
|
||||||
|
|
||||||
|
# 백그라운드 엔진 (워커 태스크용)
|
||||||
|
background_engine = create_async_engine(
|
||||||
|
url=db_settings.MYSQL_URL,
|
||||||
|
pool_size=10, # 더 작은 풀
|
||||||
|
max_overflow=10,
|
||||||
|
pool_timeout=60, # 백그라운드는 대기 여유
|
||||||
|
pool_recycle=280, # MySQL wait_timeout 보다 짧게 설정
|
||||||
|
pool_pre_ping=True,
|
||||||
|
pool_reset_on_return="rollback", # 반환 시 롤백
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.3 세션 관리 패턴
|
||||||
|
|
||||||
|
#### 패턴 1: FastAPI 의존성 주입 (단순 CRUD)
|
||||||
|
|
||||||
|
```python
|
||||||
|
@router.get("/items/{id}")
|
||||||
|
async def get_item(
|
||||||
|
id: int,
|
||||||
|
session: AsyncSession = Depends(get_session),
|
||||||
|
):
|
||||||
|
result = await session.execute(select(Item).where(Item.id == id))
|
||||||
|
return result.scalar_one_or_none()
|
||||||
|
```
|
||||||
|
|
||||||
|
**적용 엔드포인트:**
|
||||||
|
- `GET /videos/` - 목록 조회
|
||||||
|
- `GET /video/download/{task_id}` - 상태 조회
|
||||||
|
- `GET /songs/` - 목록 조회
|
||||||
|
|
||||||
|
#### 패턴 2: 명시적 세션 관리 (외부 API 호출 포함)
|
||||||
|
|
||||||
|
```python
|
||||||
|
@router.get("/generate/{task_id}")
|
||||||
|
async def generate_video(task_id: str):
|
||||||
|
# 1단계: 명시적 세션 열기 → DB 작업 → 세션 닫기
|
||||||
|
async with AsyncSessionLocal() as session:
|
||||||
|
# 순차 쿼리 실행 (AsyncSession은 병렬 쿼리 미지원)
|
||||||
|
project = await session.execute(select(Project).where(...))
|
||||||
|
lyric = await session.execute(select(Lyric).where(...))
|
||||||
|
song = await session.execute(select(Song).where(...))
|
||||||
|
# 초기 데이터 저장
|
||||||
|
await session.commit()
|
||||||
|
# 세션 닫힘 (async with 블록 종료)
|
||||||
|
|
||||||
|
# 2단계: 외부 API 호출 (세션 없음 - 커넥션 점유 안함)
|
||||||
|
response = await creatomate_service.make_api_call()
|
||||||
|
|
||||||
|
# 3단계: 새 세션으로 업데이트
|
||||||
|
async with AsyncSessionLocal() as update_session:
|
||||||
|
video.render_id = response["id"]
|
||||||
|
await update_session.commit()
|
||||||
|
```
|
||||||
|
|
||||||
|
**적용 엔드포인트:**
|
||||||
|
- `GET /video/generate/{task_id}` - 영상 생성
|
||||||
|
- `GET /song/generate/{task_id}` - 노래 생성
|
||||||
|
|
||||||
|
#### 패턴 3: 백그라운드 태스크 세션
|
||||||
|
|
||||||
|
```python
|
||||||
|
async def download_and_upload_video_to_blob(task_id: str, ...):
|
||||||
|
# 백그라운드 전용 세션 팩토리 사용
|
||||||
|
async with BackgroundSessionLocal() as session:
|
||||||
|
result = await session.execute(...)
|
||||||
|
video.status = "completed"
|
||||||
|
await session.commit()
|
||||||
|
```
|
||||||
|
|
||||||
|
**적용 함수:**
|
||||||
|
- `download_and_upload_video_to_blob()`
|
||||||
|
- `download_and_upload_song_to_blob()`
|
||||||
|
- `generate_lyric_background()`
|
||||||
|
|
||||||
|
### 2.4 해결된 문제: 세션 타임아웃
|
||||||
|
|
||||||
|
**문제 상황:**
|
||||||
|
```
|
||||||
|
RuntimeError: unable to perform operation on
|
||||||
|
<TCPTransport closed=True reading=False ...>; the handler is closed
|
||||||
|
```
|
||||||
|
|
||||||
|
**원인:**
|
||||||
|
- `Depends(get_session)`으로 주입된 세션이 요청 전체 동안 유지
|
||||||
|
- 외부 API 호출 (수 초~수 분) 중 TCP 커넥션 타임아웃
|
||||||
|
- 요청 종료 시점에 이미 닫힌 커넥션 정리 시도
|
||||||
|
|
||||||
|
**해결책:**
|
||||||
|
```python
|
||||||
|
# 변경 전: 세션이 요청 전체 동안 유지
|
||||||
|
async def generate_video(session: AsyncSession = Depends(get_session)):
|
||||||
|
await session.execute(...) # DB 작업
|
||||||
|
await creatomate_api() # 외부 API (세션 유지됨 - 문제!)
|
||||||
|
await session.commit() # 타임아웃 에러 발생 가능
|
||||||
|
|
||||||
|
# 변경 후: 명시적 세션 관리
|
||||||
|
async def generate_video():
|
||||||
|
async with AsyncSessionLocal() as session:
|
||||||
|
await session.execute(...)
|
||||||
|
await session.commit()
|
||||||
|
# 세션 닫힘
|
||||||
|
|
||||||
|
await creatomate_api() # 외부 API (세션 없음 - 안전!)
|
||||||
|
|
||||||
|
async with AsyncSessionLocal() as session:
|
||||||
|
# 업데이트
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 비동기 처리 패턴
|
||||||
|
|
||||||
|
### 3.1 순차 쿼리 실행 (AsyncSession 제약)
|
||||||
|
|
||||||
|
**위치**: `app/video/api/routers/v1/video.py`
|
||||||
|
|
||||||
|
> **중요**: SQLAlchemy AsyncSession은 단일 세션에서 동시에 여러 쿼리를 실행하는 것을 지원하지 않습니다.
|
||||||
|
> `asyncio.gather()`로 병렬 쿼리를 실행하면 세션 상태 충돌이 발생합니다.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 순차 쿼리 실행: Project, Lyric, Song, Image
|
||||||
|
# Note: AsyncSession은 동일 세션에서 병렬 쿼리를 지원하지 않음
|
||||||
|
|
||||||
|
# Project 조회
|
||||||
|
project_result = await session.execute(
|
||||||
|
select(Project).where(Project.task_id == task_id)
|
||||||
|
.order_by(Project.created_at.desc()).limit(1)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Lyric 조회
|
||||||
|
lyric_result = await session.execute(
|
||||||
|
select(Lyric).where(Lyric.task_id == task_id)
|
||||||
|
.order_by(Lyric.created_at.desc()).limit(1)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Song 조회
|
||||||
|
song_result = await session.execute(
|
||||||
|
select(Song).where(Song.task_id == task_id)
|
||||||
|
.order_by(Song.created_at.desc()).limit(1)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Image 조회
|
||||||
|
image_result = await session.execute(
|
||||||
|
select(Image).where(Image.task_id == task_id)
|
||||||
|
.order_by(Image.img_order.asc())
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
**AsyncSession 병렬 쿼리 제약 사항:**
|
||||||
|
|
||||||
|
- 단일 세션 내에서 `asyncio.gather()`로 여러 쿼리 동시 실행 불가
|
||||||
|
- 세션 상태 충돌 및 예기치 않은 동작 발생 가능
|
||||||
|
- 병렬 쿼리가 필요한 경우 별도의 세션을 각각 생성해야 함
|
||||||
|
|
||||||
|
### 3.2 FastAPI BackgroundTasks 활용
|
||||||
|
|
||||||
|
```python
|
||||||
|
@router.post("/generate")
|
||||||
|
async def generate_lyric(
|
||||||
|
request_body: GenerateLyricRequest,
|
||||||
|
background_tasks: BackgroundTasks,
|
||||||
|
session: AsyncSession = Depends(get_session),
|
||||||
|
):
|
||||||
|
# 즉시 응답할 데이터 저장
|
||||||
|
lyric = Lyric(task_id=task_id, status="processing")
|
||||||
|
session.add(lyric)
|
||||||
|
await session.commit()
|
||||||
|
|
||||||
|
# 백그라운드 태스크 스케줄링
|
||||||
|
background_tasks.add_task(
|
||||||
|
generate_lyric_background,
|
||||||
|
task_id=task_id,
|
||||||
|
prompt=prompt,
|
||||||
|
language=request_body.language,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 즉시 응답 반환
|
||||||
|
return GenerateLyricResponse(success=True, task_id=task_id)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.3 비동기 컨텍스트 관리자
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 앱 라이프사이클 관리
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
# Startup
|
||||||
|
await create_db_tables()
|
||||||
|
print("Database tables created")
|
||||||
|
|
||||||
|
yield # 앱 실행
|
||||||
|
|
||||||
|
# Shutdown
|
||||||
|
await dispose_engine()
|
||||||
|
print("Database engines disposed")
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 외부 API 통합 설계
|
||||||
|
|
||||||
|
### 4.1 Creatomate 서비스
|
||||||
|
|
||||||
|
**위치**: `app/utils/creatomate.py`
|
||||||
|
|
||||||
|
#### HTTP 클라이언트 싱글톤
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 모듈 레벨 공유 클라이언트
|
||||||
|
_shared_client: httpx.AsyncClient | None = None
|
||||||
|
|
||||||
|
async def get_shared_client() -> httpx.AsyncClient:
|
||||||
|
global _shared_client
|
||||||
|
if _shared_client is None or _shared_client.is_closed:
|
||||||
|
_shared_client = httpx.AsyncClient(
|
||||||
|
timeout=httpx.Timeout(60.0, connect=10.0),
|
||||||
|
limits=httpx.Limits(
|
||||||
|
max_keepalive_connections=10,
|
||||||
|
max_connections=20,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
return _shared_client
|
||||||
|
```
|
||||||
|
|
||||||
|
**장점:**
|
||||||
|
- 커넥션 풀 재사용으로 TCP handshake 오버헤드 제거
|
||||||
|
- Keep-alive로 연결 유지
|
||||||
|
- 앱 종료 시 `close_shared_client()` 호출로 정리
|
||||||
|
|
||||||
|
#### 템플릿 캐싱
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 모듈 레벨 캐시
|
||||||
|
_template_cache: dict[str, dict] = {}
|
||||||
|
CACHE_TTL_SECONDS = 300 # 5분
|
||||||
|
|
||||||
|
async def get_one_template_data(self, template_id: str, use_cache: bool = True):
|
||||||
|
# 캐시 확인
|
||||||
|
if use_cache and template_id in _template_cache:
|
||||||
|
cached = _template_cache[template_id]
|
||||||
|
if _is_cache_valid(cached["cached_at"]):
|
||||||
|
print(f"[CreatomateService] Cache HIT - {template_id}")
|
||||||
|
return copy.deepcopy(cached["data"])
|
||||||
|
|
||||||
|
# API 호출 및 캐시 저장
|
||||||
|
data = await self._fetch_from_api(template_id)
|
||||||
|
_template_cache[template_id] = {
|
||||||
|
"data": data,
|
||||||
|
"cached_at": time.time(),
|
||||||
|
}
|
||||||
|
return copy.deepcopy(data)
|
||||||
|
```
|
||||||
|
|
||||||
|
**캐싱 전략:**
|
||||||
|
- 첫 번째 요청: API 호출 (1-2초)
|
||||||
|
- 이후 요청 (5분 내): 캐시 반환 (~0ms)
|
||||||
|
- TTL 만료 후: 자동 갱신
|
||||||
|
|
||||||
|
### 4.2 Suno 서비스
|
||||||
|
|
||||||
|
**위치**: `app/utils/suno.py`
|
||||||
|
|
||||||
|
```python
|
||||||
|
class SunoService:
|
||||||
|
async def generate_music(self, prompt: str, callback_url: str = None):
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
response = await client.post(
|
||||||
|
f"{self.base_url}/generate/music",
|
||||||
|
json={
|
||||||
|
"prompt": prompt,
|
||||||
|
"callback_url": callback_url,
|
||||||
|
},
|
||||||
|
timeout=60.0,
|
||||||
|
)
|
||||||
|
return response.json()
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.3 ChatGPT 서비스
|
||||||
|
|
||||||
|
**위치**: `app/utils/chatgpt_prompt.py`
|
||||||
|
|
||||||
|
```python
|
||||||
|
class ChatgptService:
|
||||||
|
async def generate(self, prompt: str) -> str:
|
||||||
|
# OpenAI API 호출
|
||||||
|
response = await self.client.chat.completions.create(
|
||||||
|
model="gpt-4",
|
||||||
|
messages=[{"role": "user", "content": prompt}],
|
||||||
|
)
|
||||||
|
return response.choices[0].message.content
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.4 Azure Blob Storage
|
||||||
|
|
||||||
|
**위치**: `app/utils/upload_blob_as_request.py`
|
||||||
|
|
||||||
|
```python
|
||||||
|
class AzureBlobUploader:
|
||||||
|
async def upload_video(self, file_path: str) -> bool:
|
||||||
|
# 비동기 업로드
|
||||||
|
async with aiofiles.open(file_path, "rb") as f:
|
||||||
|
content = await f.read()
|
||||||
|
# Blob 업로드 로직
|
||||||
|
return True
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 백그라운드 태스크 워크플로우
|
||||||
|
|
||||||
|
### 5.1 3단계 워크플로우 패턴
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────────────────────────────────────────────────────────┐
|
||||||
|
│ REQUEST PHASE │
|
||||||
|
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
|
||||||
|
│ │ API Request │───▶│ Save Initial │───▶│ Return Response │ │
|
||||||
|
│ │ │ │ Record │ │ (task_id) │ │
|
||||||
|
│ └──────────────┘ │ status= │ └──────────────────┘ │
|
||||||
|
│ │ "processing" │ │
|
||||||
|
│ └──────────────┘ │
|
||||||
|
└─────────────────────────────────────────────────────────────────┘
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
┌─────────────────────────────────────────────────────────────────┐
|
||||||
|
│ POLLING PHASE │
|
||||||
|
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
|
||||||
|
│ │ Client Polls │───▶│ Query │───▶│ Return Status │ │
|
||||||
|
│ │ /status/id │ │ External API │ │ + Trigger BG │ │
|
||||||
|
│ └──────────────┘ └──────────────┘ └──────────────────┘ │
|
||||||
|
└─────────────────────────────────────────────────────────────────┘
|
||||||
|
│ (if status == "succeeded")
|
||||||
|
▼
|
||||||
|
┌─────────────────────────────────────────────────────────────────┐
|
||||||
|
│ BACKGROUND COMPLETION PHASE │
|
||||||
|
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
|
||||||
|
│ │ Download │───▶│ Upload to │───▶│ Update DB │ │
|
||||||
|
│ │ Result File │ │ Azure Blob │ │ status=completed │ │
|
||||||
|
│ └──────────────┘ └──────────────┘ │ result_url=... │ │
|
||||||
|
│ └──────────────────┘ │
|
||||||
|
└─────────────────────────────────────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.2 영상 생성 플로우
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 1단계: 즉시 응답
|
||||||
|
@router.get("/video/generate/{task_id}")
|
||||||
|
async def generate_video(task_id: str):
|
||||||
|
# DB에서 필요한 데이터 조회 (병렬)
|
||||||
|
# Video 레코드 생성 (status="processing")
|
||||||
|
# Creatomate API 호출 → render_id 획득
|
||||||
|
return {"success": True, "render_id": render_id}
|
||||||
|
|
||||||
|
# 2단계: 폴링
|
||||||
|
@router.get("/video/status/{render_id}")
|
||||||
|
async def get_video_status(render_id: str, background_tasks: BackgroundTasks):
|
||||||
|
status = await creatomate_service.get_render_status(render_id)
|
||||||
|
|
||||||
|
if status == "succeeded":
|
||||||
|
# 백그라운드 태스크 트리거
|
||||||
|
background_tasks.add_task(
|
||||||
|
download_and_upload_video_to_blob,
|
||||||
|
task_id=video.task_id,
|
||||||
|
video_url=status["url"],
|
||||||
|
)
|
||||||
|
|
||||||
|
return {"status": status}
|
||||||
|
|
||||||
|
# 3단계: 백그라운드 완료
|
||||||
|
async def download_and_upload_video_to_blob(task_id: str, video_url: str):
|
||||||
|
# 임시 파일 다운로드
|
||||||
|
# Azure Blob 업로드
|
||||||
|
# DB 업데이트 (BackgroundSessionLocal 사용)
|
||||||
|
# 임시 파일 삭제
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.3 에러 처리 전략
|
||||||
|
|
||||||
|
```python
|
||||||
|
async def download_and_upload_video_to_blob(task_id: str, video_url: str):
|
||||||
|
temp_file_path: Path | None = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 다운로드 및 업로드 로직
|
||||||
|
...
|
||||||
|
|
||||||
|
async with BackgroundSessionLocal() as session:
|
||||||
|
video.status = "completed"
|
||||||
|
await session.commit()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[EXCEPTION] {task_id}: {e}")
|
||||||
|
# 실패 상태로 업데이트
|
||||||
|
async with BackgroundSessionLocal() as session:
|
||||||
|
video.status = "failed"
|
||||||
|
await session.commit()
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# 임시 파일 정리 (항상 실행)
|
||||||
|
if temp_file_path and temp_file_path.exists():
|
||||||
|
temp_file_path.unlink()
|
||||||
|
# 임시 디렉토리 정리
|
||||||
|
temp_dir.rmdir()
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 쿼리 최적화 전략
|
||||||
|
|
||||||
|
### 6.1 N+1 문제 해결
|
||||||
|
|
||||||
|
**문제 코드:**
|
||||||
|
```python
|
||||||
|
# 각 video마다 project를 개별 조회 (N+1)
|
||||||
|
for video in videos:
|
||||||
|
project = await session.execute(
|
||||||
|
select(Project).where(Project.id == video.project_id)
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
**해결 코드:**
|
||||||
|
```python
|
||||||
|
# 1. Video 목록 조회
|
||||||
|
videos = await session.execute(video_query)
|
||||||
|
video_list = videos.scalars().all()
|
||||||
|
|
||||||
|
# 2. Project ID 수집
|
||||||
|
project_ids = [v.project_id for v in video_list if v.project_id]
|
||||||
|
|
||||||
|
# 3. Project 일괄 조회 (IN 절)
|
||||||
|
projects_result = await session.execute(
|
||||||
|
select(Project).where(Project.id.in_(project_ids))
|
||||||
|
)
|
||||||
|
|
||||||
|
# 4. 딕셔너리로 매핑
|
||||||
|
projects_map = {p.id: p for p in projects_result.scalars().all()}
|
||||||
|
|
||||||
|
# 5. 조합
|
||||||
|
for video in video_list:
|
||||||
|
project = projects_map.get(video.project_id)
|
||||||
|
```
|
||||||
|
|
||||||
|
**위치**: `app/video/api/routers/v1/video.py` - `get_videos()`
|
||||||
|
|
||||||
|
### 6.2 서브쿼리를 활용한 중복 제거
|
||||||
|
|
||||||
|
```python
|
||||||
|
# task_id별 최신 Video의 id만 추출
|
||||||
|
subquery = (
|
||||||
|
select(func.max(Video.id).label("max_id"))
|
||||||
|
.where(Video.status == "completed")
|
||||||
|
.group_by(Video.task_id)
|
||||||
|
.subquery()
|
||||||
|
)
|
||||||
|
|
||||||
|
# 최신 Video만 조회
|
||||||
|
query = (
|
||||||
|
select(Video)
|
||||||
|
.where(Video.id.in_(select(subquery.c.max_id)))
|
||||||
|
.order_by(Video.created_at.desc())
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 설계 강점 분석
|
||||||
|
|
||||||
|
### 7.1 안정성 (Stability)
|
||||||
|
|
||||||
|
| 요소 | 구현 | 효과 |
|
||||||
|
|------|------|------|
|
||||||
|
| pool_pre_ping | 쿼리 전 연결 검증 | Stale 커넥션 방지 |
|
||||||
|
| pool_reset_on_return | 반환 시 롤백 | 트랜잭션 상태 초기화 |
|
||||||
|
| 이중 커넥션 풀 | 요청/백그라운드 분리 | 리소스 경합 방지 |
|
||||||
|
| Finally 블록 | 임시 파일 정리 | 리소스 누수 방지 |
|
||||||
|
|
||||||
|
### 7.2 성능 (Performance)
|
||||||
|
|
||||||
|
| 요소 | 구현 | 효과 |
|
||||||
|
|------|------|------|
|
||||||
|
| 템플릿 캐싱 | TTL 기반 메모리 캐시 | API 호출 100% 감소 |
|
||||||
|
| HTTP 클라이언트 풀 | 싱글톤 패턴 | 커넥션 재사용 |
|
||||||
|
| N+1 해결 | IN 절 배치 조회 | 쿼리 수 N→2 감소 |
|
||||||
|
|
||||||
|
### 7.3 확장성 (Scalability)
|
||||||
|
|
||||||
|
| 요소 | 구현 | 효과 |
|
||||||
|
|------|------|------|
|
||||||
|
| 명시적 세션 관리 | 외부 API 시 세션 해제 | 커넥션 풀 점유 최소화 |
|
||||||
|
| 백그라운드 태스크 | FastAPI BackgroundTasks | 논블로킹 처리 |
|
||||||
|
| 폴링 패턴 | Status endpoint | 클라이언트 주도 동기화 |
|
||||||
|
|
||||||
|
### 7.4 유지보수성 (Maintainability)
|
||||||
|
|
||||||
|
| 요소 | 구현 | 효과 |
|
||||||
|
|------|------|------|
|
||||||
|
| 구조화된 로깅 | `[function_name]` prefix | 추적 용이 |
|
||||||
|
| 타입 힌트 | Python 3.11+ 문법 | IDE 지원, 버그 감소 |
|
||||||
|
| 문서화 | Docstring, 주석 | 코드 이해도 향상 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 개선 권장 사항
|
||||||
|
|
||||||
|
### 8.1 Song 라우터 N+1 문제
|
||||||
|
|
||||||
|
**현재 상태** (`app/song/api/routers/v1/song.py`):
|
||||||
|
```python
|
||||||
|
# N+1 발생
|
||||||
|
for song in songs:
|
||||||
|
project_result = await session.execute(
|
||||||
|
select(Project).where(Project.id == song.project_id)
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
**권장 수정**:
|
||||||
|
```python
|
||||||
|
# video.py의 패턴 적용
|
||||||
|
project_ids = [s.project_id for s in songs if s.project_id]
|
||||||
|
projects_result = await session.execute(
|
||||||
|
select(Project).where(Project.id.in_(project_ids))
|
||||||
|
)
|
||||||
|
projects_map = {p.id: p for p in projects_result.scalars().all()}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 8.2 Suno 서비스 HTTP 클라이언트 풀링
|
||||||
|
|
||||||
|
**현재 상태** (`app/utils/suno.py`):
|
||||||
|
```python
|
||||||
|
# 요청마다 새 클라이언트 생성
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
**권장 수정**:
|
||||||
|
```python
|
||||||
|
# creatomate.py 패턴 적용
|
||||||
|
_suno_client: httpx.AsyncClient | None = None
|
||||||
|
|
||||||
|
async def get_suno_client() -> httpx.AsyncClient:
|
||||||
|
global _suno_client
|
||||||
|
if _suno_client is None or _suno_client.is_closed:
|
||||||
|
_suno_client = httpx.AsyncClient(
|
||||||
|
timeout=httpx.Timeout(60.0, connect=10.0),
|
||||||
|
limits=httpx.Limits(max_keepalive_connections=5, max_connections=10),
|
||||||
|
)
|
||||||
|
return _suno_client
|
||||||
|
```
|
||||||
|
|
||||||
|
### 8.3 동시성 제한
|
||||||
|
|
||||||
|
**권장 추가**:
|
||||||
|
```python
|
||||||
|
# 백그라운드 태스크 동시 실행 수 제한
|
||||||
|
BACKGROUND_TASK_SEMAPHORE = asyncio.Semaphore(5)
|
||||||
|
|
||||||
|
async def download_and_upload_video_to_blob(...):
|
||||||
|
async with BACKGROUND_TASK_SEMAPHORE:
|
||||||
|
# 기존 로직
|
||||||
|
```
|
||||||
|
|
||||||
|
### 8.4 분산 락 (선택적)
|
||||||
|
|
||||||
|
**높은 동시성 환경에서 권장**:
|
||||||
|
```python
|
||||||
|
# Redis 기반 분산 락
|
||||||
|
async def generate_video_with_lock(task_id: str):
|
||||||
|
lock_key = f"video_gen:{task_id}"
|
||||||
|
|
||||||
|
if not await redis.setnx(lock_key, "1"):
|
||||||
|
raise HTTPException(409, "Already processing")
|
||||||
|
|
||||||
|
try:
|
||||||
|
await redis.expire(lock_key, 300) # 5분 TTL
|
||||||
|
# 영상 생성 로직
|
||||||
|
finally:
|
||||||
|
await redis.delete(lock_key)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. 아키텍처 다이어그램
|
||||||
|
|
||||||
|
### 9.1 전체 요청 흐름
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────────────────────────────────────────────────────────────────┐
|
||||||
|
│ CLIENT │
|
||||||
|
└─────────────────────────────────────────────────────────────────────────┘
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
┌─────────────────────────────────────────────────────────────────────────┐
|
||||||
|
│ FASTAPI SERVER │
|
||||||
|
│ ┌─────────────────────────────────────────────────────────────────┐ │
|
||||||
|
│ │ ROUTERS │ │
|
||||||
|
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
|
||||||
|
│ │ │ /video │ │ /song │ │ /lyric │ │ /project │ │ │
|
||||||
|
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
|
||||||
|
│ └─────────────────────────────────────────────────────────────────┘ │
|
||||||
|
│ │ │
|
||||||
|
│ ┌───────────────┼───────────────┐ │
|
||||||
|
│ ▼ ▼ ▼ │
|
||||||
|
│ ┌─────────────────────┐ ┌─────────────────┐ ┌─────────────────────┐ │
|
||||||
|
│ │ MAIN ENGINE │ │ BACKGROUND │ │ EXTERNAL SERVICES │ │
|
||||||
|
│ │ (AsyncSession) │ │ ENGINE │ │ ┌───────────────┐ │ │
|
||||||
|
│ │ pool_size: 20 │ │ (BackgroundSess)│ │ │ Creatomate │ │ │
|
||||||
|
│ │ max_overflow: 20 │ │ pool_size: 10 │ │ ├───────────────┤ │ │
|
||||||
|
│ └─────────────────────┘ └─────────────────┘ │ │ Suno │ │ │
|
||||||
|
│ │ │ │ ├───────────────┤ │ │
|
||||||
|
│ ▼ ▼ │ │ ChatGPT │ │ │
|
||||||
|
│ ┌─────────────────────────────────────────┐ │ ├───────────────┤ │ │
|
||||||
|
│ │ MySQL DATABASE │ │ │ Azure Blob │ │ │
|
||||||
|
│ │ ┌────────┐ ┌────────┐ ┌────────────┐ │ │ └───────────────┘ │ │
|
||||||
|
│ │ │Project │ │ Song │ │ Video │ │ └─────────────────────┘ │
|
||||||
|
│ │ │ Lyric │ │ Image │ │ │ │ │
|
||||||
|
│ │ └────────┘ └────────┘ └────────────┘ │ │
|
||||||
|
│ └─────────────────────────────────────────┘ │
|
||||||
|
└─────────────────────────────────────────────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
### 9.2 데이터 흐름
|
||||||
|
|
||||||
|
```
|
||||||
|
[영상 생성 파이프라인]
|
||||||
|
|
||||||
|
1. 프로젝트 생성
|
||||||
|
Client ─▶ POST /project ─▶ DB(Project) ─▶ task_id
|
||||||
|
|
||||||
|
2. 이미지 업로드
|
||||||
|
Client ─▶ POST /project/image ─▶ Azure Blob ─▶ DB(Image)
|
||||||
|
|
||||||
|
3. 가사 생성
|
||||||
|
Client ─▶ POST /lyric/generate ─▶ DB(Lyric) ─▶ BackgroundTask
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
ChatGPT API
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
DB Update
|
||||||
|
|
||||||
|
4. 노래 생성
|
||||||
|
Client ─▶ GET /song/generate/{task_id} ─▶ Suno API ─▶ DB(Song)
|
||||||
|
Client ◀──── polling ─────────────────────────────────┘
|
||||||
|
|
||||||
|
5. 영상 생성
|
||||||
|
Client ─▶ GET /video/generate/{task_id}
|
||||||
|
│
|
||||||
|
├─ 순차 쿼리 ─▶ DB(Project, Lyric, Song, Image)
|
||||||
|
│
|
||||||
|
├─ Creatomate API ─▶ render_id
|
||||||
|
│
|
||||||
|
└─ DB(Video) status="processing"
|
||||||
|
|
||||||
|
Client ─▶ GET /video/status/{render_id}
|
||||||
|
│
|
||||||
|
├─ Creatomate Status Check
|
||||||
|
│
|
||||||
|
└─ if succeeded ─▶ BackgroundTask
|
||||||
|
│
|
||||||
|
├─ Download MP4
|
||||||
|
├─ Upload to Azure Blob
|
||||||
|
└─ DB Update status="completed"
|
||||||
|
|
||||||
|
6. 결과 조회
|
||||||
|
Client ─▶ GET /video/download/{task_id} ─▶ result_movie_url
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. 결론
|
||||||
|
|
||||||
|
### 10.1 현재 아키텍처 평가
|
||||||
|
|
||||||
|
O2O-CASTAD Backend는 **프로덕션 준비 수준의 비동기 아키텍처**를 갖추고 있습니다:
|
||||||
|
|
||||||
|
1. **안정성**: 이중 커넥션 풀, pool_pre_ping, 명시적 세션 관리로 런타임 에러 최소화
|
||||||
|
2. **성능**: 캐싱, HTTP 클라이언트 풀링으로 응답 시간 최적화
|
||||||
|
3. **확장성**: 백그라운드 태스크 분리, 폴링 패턴으로 부하 분산
|
||||||
|
4. **유지보수성**: 일관된 패턴, 구조화된 로깅, 타입 힌트
|
||||||
|
|
||||||
|
### 10.2 핵심 성과
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────────────────────────────────────────────────────┐
|
||||||
|
│ BEFORE → AFTER │
|
||||||
|
├─────────────────────────────────────────────────────────────┤
|
||||||
|
│ Session Timeout Errors │ Frequent → Resolved │
|
||||||
|
│ Template API Calls │ Every req → Cached (100%↓) │
|
||||||
|
│ HTTP Client Overhead │ 50ms/req → 0ms (100%↓) │
|
||||||
|
│ N+1 Query Problem │ N queries → 2 queries │
|
||||||
|
│ Connection Pool Conflicts │ Frequent → Isolated │
|
||||||
|
└─────────────────────────────────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
### 10.3 권장 다음 단계
|
||||||
|
|
||||||
|
1. **단기**: Song 라우터 N+1 문제 해결
|
||||||
|
2. **단기**: Suno 서비스 HTTP 클라이언트 풀링 적용
|
||||||
|
3. **중기**: 동시성 제한 (Semaphore) 추가
|
||||||
|
4. **장기**: Redis 캐시 레이어 도입 (템플릿 캐시 영속화)
|
||||||
|
5. **장기**: 분산 락 구현 (높은 동시성 환경 대비)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
> **문서 끝**
|
||||||
|
> 추가 질문이나 개선 제안은 개발팀에 문의하세요.
|
||||||
@@ -0,0 +1,500 @@
|
|||||||
|
# ORM 동기식 전환 보고서
|
||||||
|
|
||||||
|
## 개요
|
||||||
|
|
||||||
|
현재 프로젝트는 **SQLAlchemy 2.0+ 비동기 방식**으로 구현되어 있습니다. 이 보고서는 동기식으로 전환할 경우 필요한 코드 수정 사항을 정리합니다.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 현재 비동기 구현 현황
|
||||||
|
|
||||||
|
### 1.1 사용 중인 라이브러리
|
||||||
|
- `sqlalchemy[asyncio]>=2.0.45`
|
||||||
|
- `asyncmy>=0.2.10` (MySQL 비동기 드라이버)
|
||||||
|
- `aiomysql>=0.3.2`
|
||||||
|
|
||||||
|
### 1.2 주요 비동기 컴포넌트
|
||||||
|
| 컴포넌트 | 현재 (비동기) | 변경 후 (동기) |
|
||||||
|
|---------|-------------|--------------|
|
||||||
|
| 엔진 | `create_async_engine` | `create_engine` |
|
||||||
|
| 세션 팩토리 | `async_sessionmaker` | `sessionmaker` |
|
||||||
|
| 세션 클래스 | `AsyncSession` | `Session` |
|
||||||
|
| DB 드라이버 | `mysql+asyncmy` | `mysql+pymysql` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 파일별 수정 사항
|
||||||
|
|
||||||
|
### 2.1 pyproject.toml - 의존성 변경
|
||||||
|
|
||||||
|
**파일**: `pyproject.toml`
|
||||||
|
|
||||||
|
```diff
|
||||||
|
dependencies = [
|
||||||
|
"fastapi[standard]>=0.115.8",
|
||||||
|
- "sqlalchemy[asyncio]>=2.0.45",
|
||||||
|
+ "sqlalchemy>=2.0.45",
|
||||||
|
- "asyncmy>=0.2.10",
|
||||||
|
- "aiomysql>=0.3.2",
|
||||||
|
+ "pymysql>=1.1.0",
|
||||||
|
...
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.2 config.py - 데이터베이스 URL 변경
|
||||||
|
|
||||||
|
**파일**: `config.py` (라인 74-96)
|
||||||
|
|
||||||
|
```diff
|
||||||
|
class DatabaseSettings(BaseSettings):
|
||||||
|
@property
|
||||||
|
def MYSQL_URL(self) -> str:
|
||||||
|
- return f"mysql+asyncmy://{self.MYSQL_USER}:{self.MYSQL_PASSWORD}@{self.MYSQL_HOST}:{self.MYSQL_PORT}/{self.MYSQL_DB}"
|
||||||
|
+ return f"mysql+pymysql://{self.MYSQL_USER}:{self.MYSQL_PASSWORD}@{self.MYSQL_HOST}:{self.MYSQL_PORT}/{self.MYSQL_DB}"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.3 app/database/session.py - 세션 설정 전면 수정
|
||||||
|
|
||||||
|
**파일**: `app/database/session.py`
|
||||||
|
|
||||||
|
#### 현재 코드 (비동기)
|
||||||
|
```python
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||||
|
from typing import AsyncGenerator
|
||||||
|
|
||||||
|
engine = create_async_engine(
|
||||||
|
url=db_settings.MYSQL_URL,
|
||||||
|
echo=False,
|
||||||
|
pool_size=10,
|
||||||
|
max_overflow=10,
|
||||||
|
pool_timeout=5,
|
||||||
|
pool_recycle=3600,
|
||||||
|
pool_pre_ping=True,
|
||||||
|
pool_reset_on_return="rollback",
|
||||||
|
)
|
||||||
|
|
||||||
|
AsyncSessionLocal = async_sessionmaker(
|
||||||
|
bind=engine,
|
||||||
|
class_=AsyncSession,
|
||||||
|
expire_on_commit=False,
|
||||||
|
autoflush=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
||||||
|
async with AsyncSessionLocal() as session:
|
||||||
|
try:
|
||||||
|
yield session
|
||||||
|
except Exception as e:
|
||||||
|
await session.rollback()
|
||||||
|
raise e
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 변경 후 코드 (동기)
|
||||||
|
```python
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import Session, sessionmaker
|
||||||
|
from typing import Generator
|
||||||
|
|
||||||
|
engine = create_engine(
|
||||||
|
url=db_settings.MYSQL_URL,
|
||||||
|
echo=False,
|
||||||
|
pool_size=10,
|
||||||
|
max_overflow=10,
|
||||||
|
pool_timeout=5,
|
||||||
|
pool_recycle=3600,
|
||||||
|
pool_pre_ping=True,
|
||||||
|
pool_reset_on_return="rollback",
|
||||||
|
)
|
||||||
|
|
||||||
|
SessionLocal = sessionmaker(
|
||||||
|
bind=engine,
|
||||||
|
class_=Session,
|
||||||
|
expire_on_commit=False,
|
||||||
|
autoflush=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_session() -> Generator[Session, None, None]:
|
||||||
|
with SessionLocal() as session:
|
||||||
|
try:
|
||||||
|
yield session
|
||||||
|
except Exception as e:
|
||||||
|
session.rollback()
|
||||||
|
raise e
|
||||||
|
```
|
||||||
|
|
||||||
|
#### get_worker_session 함수 변경
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- from contextlib import asynccontextmanager
|
||||||
|
+ from contextlib import contextmanager
|
||||||
|
|
||||||
|
- @asynccontextmanager
|
||||||
|
- async def get_worker_session() -> AsyncGenerator[AsyncSession, None]:
|
||||||
|
- worker_engine = create_async_engine(
|
||||||
|
+ @contextmanager
|
||||||
|
+ def get_worker_session() -> Generator[Session, None, None]:
|
||||||
|
+ worker_engine = create_engine(
|
||||||
|
url=db_settings.MYSQL_URL,
|
||||||
|
poolclass=NullPool,
|
||||||
|
)
|
||||||
|
- session_factory = async_sessionmaker(
|
||||||
|
- bind=worker_engine,
|
||||||
|
- class_=AsyncSession,
|
||||||
|
+ session_factory = sessionmaker(
|
||||||
|
+ bind=worker_engine,
|
||||||
|
+ class_=Session,
|
||||||
|
expire_on_commit=False,
|
||||||
|
autoflush=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
- async with session_factory() as session:
|
||||||
|
+ with session_factory() as session:
|
||||||
|
try:
|
||||||
|
yield session
|
||||||
|
finally:
|
||||||
|
- await session.close()
|
||||||
|
- await worker_engine.dispose()
|
||||||
|
+ session.close()
|
||||||
|
+ worker_engine.dispose()
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.4 app/*/dependencies.py - 타입 힌트 변경
|
||||||
|
|
||||||
|
**파일**: `app/song/dependencies.py`, `app/lyric/dependencies.py`, `app/video/dependencies.py`
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
+ from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
- SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
||||||
|
+ SessionDep = Annotated[Session, Depends(get_session)]
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.5 라우터 파일들 - async/await 제거
|
||||||
|
|
||||||
|
**영향받는 파일**:
|
||||||
|
- `app/home/api/routers/v1/home.py`
|
||||||
|
- `app/lyric/api/routers/v1/lyric.py`
|
||||||
|
- `app/song/api/routers/v1/song.py`
|
||||||
|
- `app/video/api/routers/v1/video.py`
|
||||||
|
|
||||||
|
#### 예시: lyric.py (라인 70-90)
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- async def get_lyric_by_task_id(
|
||||||
|
+ def get_lyric_by_task_id(
|
||||||
|
task_id: str,
|
||||||
|
- session: AsyncSession = Depends(get_session),
|
||||||
|
+ session: Session = Depends(get_session),
|
||||||
|
):
|
||||||
|
- result = await session.execute(select(Lyric).where(Lyric.task_id == task_id))
|
||||||
|
+ result = session.execute(select(Lyric).where(Lyric.task_id == task_id))
|
||||||
|
lyric = result.scalar_one_or_none()
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 예시: CRUD 작업 (라인 218-260)
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- async def create_project(
|
||||||
|
+ def create_project(
|
||||||
|
request_body: ProjectCreateRequest,
|
||||||
|
- session: AsyncSession = Depends(get_session),
|
||||||
|
+ session: Session = Depends(get_session),
|
||||||
|
):
|
||||||
|
project = Project(
|
||||||
|
store_name=request_body.customer_name,
|
||||||
|
region=request_body.region,
|
||||||
|
task_id=task_id,
|
||||||
|
)
|
||||||
|
session.add(project)
|
||||||
|
- await session.commit()
|
||||||
|
- await session.refresh(project)
|
||||||
|
+ session.commit()
|
||||||
|
+ session.refresh(project)
|
||||||
|
return project
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 예시: 플러시 작업 (home.py 라인 340-350)
|
||||||
|
|
||||||
|
```diff
|
||||||
|
session.add(image)
|
||||||
|
- await session.flush()
|
||||||
|
+ session.flush()
|
||||||
|
result = image.id
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.6 서비스 파일들 - Raw SQL 쿼리 변경
|
||||||
|
|
||||||
|
**영향받는 파일**:
|
||||||
|
- `app/lyric/services/lyrics.py`
|
||||||
|
- `app/song/services/song.py`
|
||||||
|
- `app/video/services/video.py`
|
||||||
|
|
||||||
|
#### 예시: lyrics.py (라인 20-30)
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- async def get_store_default_info(conn: AsyncConnection):
|
||||||
|
+ def get_store_default_info(conn: Connection):
|
||||||
|
query = """SELECT * FROM store_default_info;"""
|
||||||
|
- result = await conn.execute(text(query))
|
||||||
|
+ result = conn.execute(text(query))
|
||||||
|
return result.fetchall()
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 예시: INSERT 쿼리 (라인 360-400)
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- async def insert_song_result(conn: AsyncConnection, params: dict):
|
||||||
|
+ def insert_song_result(conn: Connection, params: dict):
|
||||||
|
insert_query = """INSERT INTO song_results_all (...) VALUES (...)"""
|
||||||
|
- await conn.execute(text(insert_query), params)
|
||||||
|
- await conn.commit()
|
||||||
|
+ conn.execute(text(insert_query), params)
|
||||||
|
+ conn.commit()
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.7 app/home/services/base.py - BaseService 클래스
|
||||||
|
|
||||||
|
**파일**: `app/home/services/base.py`
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
+ from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
class BaseService:
|
||||||
|
- def __init__(self, model, session: AsyncSession):
|
||||||
|
+ def __init__(self, model, session: Session):
|
||||||
|
self.model = model
|
||||||
|
self.session = session
|
||||||
|
|
||||||
|
- async def _get(self, id: UUID):
|
||||||
|
- return await self.session.get(self.model, id)
|
||||||
|
+ def _get(self, id: UUID):
|
||||||
|
+ return self.session.get(self.model, id)
|
||||||
|
|
||||||
|
- async def _add(self, entity):
|
||||||
|
+ def _add(self, entity):
|
||||||
|
self.session.add(entity)
|
||||||
|
- await self.session.commit()
|
||||||
|
- await self.session.refresh(entity)
|
||||||
|
+ self.session.commit()
|
||||||
|
+ self.session.refresh(entity)
|
||||||
|
return entity
|
||||||
|
|
||||||
|
- async def _update(self, entity):
|
||||||
|
- return await self._add(entity)
|
||||||
|
+ def _update(self, entity):
|
||||||
|
+ return self._add(entity)
|
||||||
|
|
||||||
|
- async def _delete(self, entity):
|
||||||
|
- await self.session.delete(entity)
|
||||||
|
+ def _delete(self, entity):
|
||||||
|
+ self.session.delete(entity)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 모델 파일 - 변경 불필요
|
||||||
|
|
||||||
|
다음 모델 파일들은 **변경이 필요 없습니다**:
|
||||||
|
- `app/home/models.py`
|
||||||
|
- `app/lyric/models.py`
|
||||||
|
- `app/song/models.py`
|
||||||
|
- `app/video/models.py`
|
||||||
|
|
||||||
|
모델 정의 자체는 비동기/동기와 무관하게 동일합니다. `Mapped`, `mapped_column`, `relationship` 등은 그대로 사용 가능합니다.
|
||||||
|
|
||||||
|
단, **관계 로딩 전략**에서 `lazy="selectin"` 설정은 동기 환경에서도 작동하지만, 필요에 따라 `lazy="joined"` 또는 `lazy="subquery"`로 변경할 수 있습니다.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 수정 패턴 요약
|
||||||
|
|
||||||
|
### 4.1 Import 변경 패턴
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# 엔진/세션 관련
|
||||||
|
- from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||||
|
+ from sqlalchemy import create_engine
|
||||||
|
+ from sqlalchemy.orm import Session, sessionmaker
|
||||||
|
|
||||||
|
# 타입 힌트
|
||||||
|
- from typing import AsyncGenerator
|
||||||
|
+ from typing import Generator
|
||||||
|
|
||||||
|
# 컨텍스트 매니저
|
||||||
|
- from contextlib import asynccontextmanager
|
||||||
|
+ from contextlib import contextmanager
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.2 함수 정의 변경 패턴
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- async def function_name(...):
|
||||||
|
+ def function_name(...):
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.3 await 제거 패턴
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- result = await session.execute(query)
|
||||||
|
+ result = session.execute(query)
|
||||||
|
|
||||||
|
- await session.commit()
|
||||||
|
+ session.commit()
|
||||||
|
|
||||||
|
- await session.refresh(obj)
|
||||||
|
+ session.refresh(obj)
|
||||||
|
|
||||||
|
- await session.flush()
|
||||||
|
+ session.flush()
|
||||||
|
|
||||||
|
- await session.rollback()
|
||||||
|
+ session.rollback()
|
||||||
|
|
||||||
|
- await session.close()
|
||||||
|
+ session.close()
|
||||||
|
|
||||||
|
- await engine.dispose()
|
||||||
|
+ engine.dispose()
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.4 컨텍스트 매니저 변경 패턴
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- async with SessionLocal() as session:
|
||||||
|
+ with SessionLocal() as session:
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 영향받는 파일 목록
|
||||||
|
|
||||||
|
### 5.1 반드시 수정해야 하는 파일
|
||||||
|
|
||||||
|
| 파일 | 수정 범위 |
|
||||||
|
|-----|---------|
|
||||||
|
| `pyproject.toml` | 의존성 변경 |
|
||||||
|
| `config.py` | DB URL 변경 |
|
||||||
|
| `app/database/session.py` | 전면 수정 |
|
||||||
|
| `app/database/session-prod.py` | 전면 수정 |
|
||||||
|
| `app/home/api/routers/v1/home.py` | async/await 제거 |
|
||||||
|
| `app/lyric/api/routers/v1/lyric.py` | async/await 제거 |
|
||||||
|
| `app/song/api/routers/v1/song.py` | async/await 제거 |
|
||||||
|
| `app/video/api/routers/v1/video.py` | async/await 제거 |
|
||||||
|
| `app/lyric/services/lyrics.py` | async/await 제거 |
|
||||||
|
| `app/song/services/song.py` | async/await 제거 |
|
||||||
|
| `app/video/services/video.py` | async/await 제거 |
|
||||||
|
| `app/home/services/base.py` | async/await 제거 |
|
||||||
|
| `app/song/dependencies.py` | 타입 힌트 변경 |
|
||||||
|
| `app/lyric/dependencies.py` | 타입 힌트 변경 |
|
||||||
|
| `app/video/dependencies.py` | 타입 힌트 변경 |
|
||||||
|
| `app/dependencies/database.py` | 타입 힌트 변경 |
|
||||||
|
|
||||||
|
### 5.2 수정 불필요한 파일
|
||||||
|
|
||||||
|
| 파일 | 이유 |
|
||||||
|
|-----|-----|
|
||||||
|
| `app/home/models.py` | 모델 정의는 동기/비동기 무관 |
|
||||||
|
| `app/lyric/models.py` | 모델 정의는 동기/비동기 무관 |
|
||||||
|
| `app/song/models.py` | 모델 정의는 동기/비동기 무관 |
|
||||||
|
| `app/video/models.py` | 모델 정의는 동기/비동기 무관 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 주의사항
|
||||||
|
|
||||||
|
### 6.1 FastAPI와의 호환성
|
||||||
|
|
||||||
|
FastAPI는 동기 엔드포인트도 지원합니다. 동기 함수는 스레드 풀에서 실행됩니다:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 동기 엔드포인트 - FastAPI가 자동으로 스레드풀에서 실행
|
||||||
|
@router.get("/items/{item_id}")
|
||||||
|
def get_item(item_id: int, session: Session = Depends(get_session)):
|
||||||
|
return session.get(Item, item_id)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6.2 성능 고려사항
|
||||||
|
|
||||||
|
동기식으로 전환 시 고려할 점:
|
||||||
|
- **동시성 감소**: 비동기 I/O의 이점 상실
|
||||||
|
- **스레드 풀 의존**: 동시 요청이 많을 경우 스레드 풀 크기 조정 필요
|
||||||
|
- **블로킹 I/O**: DB 쿼리 중 다른 요청 처리 불가
|
||||||
|
|
||||||
|
### 6.3 백그라운드 작업
|
||||||
|
|
||||||
|
현재 `get_worker_session()`으로 별도 이벤트 루프에서 실행되는 백그라운드 작업이 있습니다. 동기식 전환 시 스레드 기반 백그라운드 작업으로 변경해야 합니다:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
|
||||||
|
executor = ThreadPoolExecutor(max_workers=4)
|
||||||
|
|
||||||
|
def background_task():
|
||||||
|
with get_worker_session() as session:
|
||||||
|
# 작업 수행
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 실행
|
||||||
|
executor.submit(background_task)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 마이그레이션 단계
|
||||||
|
|
||||||
|
### Step 1: 의존성 변경
|
||||||
|
1. `pyproject.toml` 수정
|
||||||
|
2. `pip install pymysql` 또는 `uv sync` 실행
|
||||||
|
|
||||||
|
### Step 2: 설정 파일 수정
|
||||||
|
1. `config.py`의 DB URL 변경
|
||||||
|
2. `app/database/session.py` 전면 수정
|
||||||
|
|
||||||
|
### Step 3: 라우터 수정
|
||||||
|
1. 각 라우터 파일의 `async def` → `def` 변경
|
||||||
|
2. 모든 `await` 키워드 제거
|
||||||
|
3. `AsyncSession` → `Session` 타입 힌트 변경
|
||||||
|
|
||||||
|
### Step 4: 서비스 수정
|
||||||
|
1. 서비스 파일들의 async/await 제거
|
||||||
|
2. Raw SQL 쿼리 함수들 수정
|
||||||
|
|
||||||
|
### Step 5: 의존성 수정
|
||||||
|
1. `dependencies.py` 파일들의 타입 힌트 변경
|
||||||
|
|
||||||
|
### Step 6: 테스트
|
||||||
|
1. 모든 엔드포인트 기능 테스트
|
||||||
|
2. 성능 테스트 (동시 요청 처리 확인)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 결론
|
||||||
|
|
||||||
|
비동기에서 동기로 전환은 기술적으로 가능하지만, 다음을 고려해야 합니다:
|
||||||
|
|
||||||
|
**장점**:
|
||||||
|
- 코드 복잡도 감소 (async/await 제거)
|
||||||
|
- 디버깅 용이
|
||||||
|
- 레거시 라이브러리와의 호환성 향상
|
||||||
|
|
||||||
|
**단점**:
|
||||||
|
- 동시성 처리 능력 감소
|
||||||
|
- I/O 바운드 작업에서 성능 저하 가능
|
||||||
|
- FastAPI의 비동기 장점 미활용
|
||||||
|
|
||||||
|
현재 프로젝트가 FastAPI 기반이고 I/O 작업(DB, 외부 API 호출)이 많다면, **비동기 유지를 권장**합니다. 동기 전환은 특별한 요구사항(레거시 통합, 팀 역량 등)이 있을 때만 고려하시기 바랍니다.
|
||||||
@@ -0,0 +1,297 @@
|
|||||||
|
# 비동기 처리 문제 분석 보고서
|
||||||
|
|
||||||
|
## 요약
|
||||||
|
|
||||||
|
전반적으로 이 프로젝트는 현대적인 비동기 아키텍처를 잘 구현하고 있습니다. 그러나 몇 가지 잠재적인 문제점과 개선 가능한 부분이 발견되었습니다.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 심각도 높음 - 즉시 개선 권장
|
||||||
|
|
||||||
|
### 1.1 N+1 쿼리 문제 (video.py:596-612)
|
||||||
|
|
||||||
|
```python
|
||||||
|
# get_videos() 엔드포인트에서
|
||||||
|
for video in videos:
|
||||||
|
# 매 video마다 별도의 DB 쿼리 실행 - N+1 문제!
|
||||||
|
project_result = await session.execute(
|
||||||
|
select(Project).where(Project.id == video.project_id)
|
||||||
|
)
|
||||||
|
project = project_result.scalar_one_or_none()
|
||||||
|
```
|
||||||
|
|
||||||
|
**문제점**: 비디오 목록 조회 시 각 비디오마다 별도의 Project 쿼리가 발생합니다. 10개 비디오 조회 시 11번의 DB 쿼리가 실행됩니다.
|
||||||
|
|
||||||
|
**개선 방안**:
|
||||||
|
```python
|
||||||
|
# selectinload를 사용한 eager loading
|
||||||
|
from sqlalchemy.orm import selectinload
|
||||||
|
|
||||||
|
query = (
|
||||||
|
select(Video)
|
||||||
|
.options(selectinload(Video.project)) # relationship 필요
|
||||||
|
.where(Video.id.in_(select(subquery.c.max_id)))
|
||||||
|
.order_by(Video.created_at.desc())
|
||||||
|
.offset(offset)
|
||||||
|
.limit(pagination.page_size)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 또는 한 번에 project_ids 수집 후 일괄 조회
|
||||||
|
project_ids = [v.project_id for v in videos]
|
||||||
|
projects_result = await session.execute(
|
||||||
|
select(Project).where(Project.id.in_(project_ids))
|
||||||
|
)
|
||||||
|
projects_map = {p.id: p for p in projects_result.scalars().all()}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 1.2 가사 생성 API의 블로킹 문제 (lyric.py:274-276)
|
||||||
|
|
||||||
|
```python
|
||||||
|
# ChatGPT API 호출이 완료될 때까지 HTTP 응답이 블로킹됨
|
||||||
|
print(f"[generate_lyric] ChatGPT generation started - task_id: {task_id}")
|
||||||
|
result = await service.generate(prompt=prompt) # 수 초~수십 초 소요
|
||||||
|
print(f"[generate_lyric] ChatGPT generation completed - task_id: {task_id}")
|
||||||
|
```
|
||||||
|
|
||||||
|
**문제점**:
|
||||||
|
- ChatGPT API 응답이 5-30초 이상 걸릴 수 있음
|
||||||
|
- 이 시간 동안 클라이언트 연결이 유지되어야 함
|
||||||
|
- 다수 동시 요청 시 worker 스레드 고갈 가능성
|
||||||
|
|
||||||
|
**개선 방안 (BackgroundTask 패턴)**:
|
||||||
|
```python
|
||||||
|
@router.post("/generate")
|
||||||
|
async def generate_lyric(
|
||||||
|
request_body: GenerateLyricRequest,
|
||||||
|
background_tasks: BackgroundTasks,
|
||||||
|
session: AsyncSession = Depends(get_session),
|
||||||
|
) -> GenerateLyricResponse:
|
||||||
|
# DB에 processing 상태로 저장
|
||||||
|
lyric = Lyric(status="processing", ...)
|
||||||
|
session.add(lyric)
|
||||||
|
await session.commit()
|
||||||
|
|
||||||
|
# 백그라운드에서 ChatGPT 호출
|
||||||
|
background_tasks.add_task(
|
||||||
|
generate_lyric_background,
|
||||||
|
task_id=task_id,
|
||||||
|
prompt=prompt,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 즉시 응답 반환
|
||||||
|
return GenerateLyricResponse(
|
||||||
|
success=True,
|
||||||
|
task_id=task_id,
|
||||||
|
message="가사 생성이 시작되었습니다. /status/{task_id}로 상태를 확인하세요.",
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 1.3 Creatomate 서비스의 동기/비동기 메서드 혼재 (creatomate.py)
|
||||||
|
|
||||||
|
**문제점**: 동기 메서드가 여전히 존재하여 실수로 async 컨텍스트에서 호출될 수 있습니다.
|
||||||
|
|
||||||
|
| 동기 메서드 | 비동기 메서드 |
|
||||||
|
|------------|--------------|
|
||||||
|
| `get_all_templates_data()` | 없음 |
|
||||||
|
| `get_one_template_data()` | `get_one_template_data_async()` |
|
||||||
|
| `make_creatomate_call()` | 없음 |
|
||||||
|
| `make_creatomate_custom_call()` | `make_creatomate_custom_call_async()` |
|
||||||
|
| `get_render_status()` | `get_render_status_async()` |
|
||||||
|
|
||||||
|
**개선 방안**:
|
||||||
|
```python
|
||||||
|
# 모든 HTTP 호출 메서드를 async로 통일
|
||||||
|
async def get_all_templates_data(self) -> dict:
|
||||||
|
url = f"{self.BASE_URL}/v1/templates"
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
response = await client.get(url, headers=self.headers, timeout=30.0)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
# 동기 버전 제거 또는 deprecated 표시
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 심각도 중간 - 개선 권장
|
||||||
|
|
||||||
|
### 2.1 백그라운드 태스크에서 매번 엔진 생성 (session.py:82-127)
|
||||||
|
|
||||||
|
```python
|
||||||
|
@asynccontextmanager
|
||||||
|
async def get_worker_session() -> AsyncGenerator[AsyncSession, None]:
|
||||||
|
# 매 호출마다 새 엔진 생성 - 오버헤드 발생
|
||||||
|
worker_engine = create_async_engine(
|
||||||
|
url=db_settings.MYSQL_URL,
|
||||||
|
poolclass=NullPool,
|
||||||
|
...
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
**문제점**: 백그라운드 태스크가 빈번하게 호출되면 엔진 생성/소멸 오버헤드가 증가합니다.
|
||||||
|
|
||||||
|
**개선 방안**:
|
||||||
|
```python
|
||||||
|
# 모듈 레벨에서 워커 전용 엔진 생성
|
||||||
|
_worker_engine = create_async_engine(
|
||||||
|
url=db_settings.MYSQL_URL,
|
||||||
|
poolclass=NullPool,
|
||||||
|
)
|
||||||
|
_WorkerSessionLocal = async_sessionmaker(bind=_worker_engine, ...)
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def get_worker_session() -> AsyncGenerator[AsyncSession, None]:
|
||||||
|
async with _WorkerSessionLocal() as session:
|
||||||
|
try:
|
||||||
|
yield session
|
||||||
|
except Exception as e:
|
||||||
|
await session.rollback()
|
||||||
|
raise e
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.2 대용량 파일 다운로드 시 메모리 사용 (video_task.py:49-54)
|
||||||
|
|
||||||
|
```python
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
response = await client.get(video_url, timeout=180.0)
|
||||||
|
response.raise_for_status()
|
||||||
|
# 전체 파일을 메모리에 로드 - 대용량 영상 시 문제
|
||||||
|
async with aiofiles.open(str(temp_file_path), "wb") as f:
|
||||||
|
await f.write(response.content)
|
||||||
|
```
|
||||||
|
|
||||||
|
**문제점**: 수백 MB 크기의 영상 파일을 한 번에 메모리에 로드합니다.
|
||||||
|
|
||||||
|
**개선 방안 - 스트리밍 다운로드**:
|
||||||
|
```python
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
async with client.stream("GET", video_url, timeout=180.0) as response:
|
||||||
|
response.raise_for_status()
|
||||||
|
async with aiofiles.open(str(temp_file_path), "wb") as f:
|
||||||
|
async for chunk in response.aiter_bytes(chunk_size=8192):
|
||||||
|
await f.write(chunk)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.3 httpx.AsyncClient 반복 생성
|
||||||
|
|
||||||
|
여러 곳에서 `async with httpx.AsyncClient() as client:`를 사용하여 매번 새 클라이언트를 생성합니다.
|
||||||
|
|
||||||
|
**개선 방안 - 재사용 가능한 클라이언트**:
|
||||||
|
```python
|
||||||
|
# app/utils/http_client.py
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
_client: httpx.AsyncClient | None = None
|
||||||
|
|
||||||
|
async def get_http_client() -> httpx.AsyncClient:
|
||||||
|
global _client
|
||||||
|
if _client is None:
|
||||||
|
_client = httpx.AsyncClient(timeout=30.0)
|
||||||
|
return _client
|
||||||
|
|
||||||
|
async def close_http_client():
|
||||||
|
global _client
|
||||||
|
if _client:
|
||||||
|
await _client.aclose()
|
||||||
|
_client = None
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 심각도 낮음 - 선택적 개선
|
||||||
|
|
||||||
|
### 3.1 generate_video 엔드포인트의 다중 DB 조회 (video.py:109-191)
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 4개의 개별 쿼리가 순차적으로 실행됨
|
||||||
|
project_result = await session.execute(select(Project).where(...))
|
||||||
|
lyric_result = await session.execute(select(Lyric).where(...))
|
||||||
|
song_result = await session.execute(select(Song).where(...))
|
||||||
|
image_result = await session.execute(select(Image).where(...))
|
||||||
|
```
|
||||||
|
|
||||||
|
**개선 방안 - 병렬 쿼리 실행**:
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
project_task = session.execute(select(Project).where(Project.task_id == task_id))
|
||||||
|
lyric_task = session.execute(select(Lyric).where(Lyric.task_id == task_id))
|
||||||
|
song_task = session.execute(
|
||||||
|
select(Song).where(Song.task_id == task_id).order_by(Song.created_at.desc()).limit(1)
|
||||||
|
)
|
||||||
|
image_task = session.execute(
|
||||||
|
select(Image).where(Image.task_id == task_id).order_by(Image.img_order.asc())
|
||||||
|
)
|
||||||
|
|
||||||
|
project_result, lyric_result, song_result, image_result = await asyncio.gather(
|
||||||
|
project_task, lyric_task, song_task, image_task
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3.2 템플릿 조회 캐싱 미적용
|
||||||
|
|
||||||
|
`get_one_template_data_async()`가 매번 Creatomate API를 호출합니다.
|
||||||
|
|
||||||
|
**개선 방안 - 간단한 메모리 캐싱**:
|
||||||
|
```python
|
||||||
|
from functools import lru_cache
|
||||||
|
from cachetools import TTLCache
|
||||||
|
|
||||||
|
_template_cache = TTLCache(maxsize=100, ttl=3600) # 1시간 캐시
|
||||||
|
|
||||||
|
async def get_one_template_data_async(self, template_id: str) -> dict:
|
||||||
|
if template_id in _template_cache:
|
||||||
|
return _template_cache[template_id]
|
||||||
|
|
||||||
|
url = f"{self.BASE_URL}/v1/templates/{template_id}"
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
response = await client.get(url, headers=self.headers, timeout=30.0)
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
_template_cache[template_id] = data
|
||||||
|
return data
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 긍정적인 부분 (잘 구현된 패턴)
|
||||||
|
|
||||||
|
| 항목 | 상태 | 설명 |
|
||||||
|
|------|------|------|
|
||||||
|
| SQLAlchemy AsyncSession | O | `asyncmy` 드라이버와 `AsyncSessionLocal` 사용 |
|
||||||
|
| 파일 I/O | O | `aiofiles` 사용으로 비동기 파일 처리 |
|
||||||
|
| HTTP 클라이언트 | O | `httpx.AsyncClient` 사용 |
|
||||||
|
| OpenAI API | O | `AsyncOpenAI` 클라이언트 사용 |
|
||||||
|
| 백그라운드 태스크 | O | FastAPI `BackgroundTasks` 적절히 사용 |
|
||||||
|
| 세션 관리 | O | 메인/워커 세션 분리로 이벤트 루프 충돌 방지 |
|
||||||
|
| 연결 풀 설정 | O | `pool_size`, `pool_recycle`, `pool_pre_ping` 적절히 설정 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 우선순위별 개선 권장 사항
|
||||||
|
|
||||||
|
| 우선순위 | 항목 | 예상 효과 |
|
||||||
|
|----------|------|----------|
|
||||||
|
| **1** | N+1 쿼리 문제 해결 | DB 부하 감소, 응답 속도 개선 |
|
||||||
|
| **2** | 가사 생성 백그라운드 처리 | 동시 요청 처리 능력 향상 |
|
||||||
|
| **3** | Creatomate 동기 메서드 제거 | 실수로 인한 블로킹 방지 |
|
||||||
|
| **4** | 대용량 파일 스트리밍 다운로드 | 메모리 사용량 감소 |
|
||||||
|
| **5** | 워커 세션 엔진 재사용 | 오버헤드 감소 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 분석 일자
|
||||||
|
|
||||||
|
2024-12-29
|
||||||
@@ -1,82 +1,82 @@
|
|||||||
-- input_history 테이블
|
-- input_history 테이블
|
||||||
CREATE TABLE input_history (
|
CREATE TABLE input_history (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
customer_name VARCHAR(255) NOT NULL,
|
customer_name VARCHAR(255) NOT NULL,
|
||||||
region VARCHAR(100) NOT NULL,
|
region VARCHAR(100) NOT NULL,
|
||||||
task_id CHAR(36) NOT NULL,
|
task_id CHAR(36) NOT NULL,
|
||||||
detail_region_info TEXT,
|
detail_region_info TEXT,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
|
|
||||||
-- upload_img_url 테이블
|
-- upload_img_url 테이블
|
||||||
CREATE TABLE upload_img_url (
|
CREATE TABLE upload_img_url (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
task_id CHAR(36) NOT NULL,
|
task_id CHAR(36) NOT NULL,
|
||||||
img_uid INT NOT NULL,
|
img_uid INT NOT NULL,
|
||||||
img_url VARCHAR(2048) NOT NULL,
|
img_url VARCHAR(2048) NOT NULL,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (task_id) REFERENCES input_history(task_id) ON DELETE CASCADE
|
FOREIGN KEY (task_id) REFERENCES input_history(task_id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
-- lyrics 테이블
|
-- lyrics 테이블
|
||||||
CREATE TABLE lyrics (
|
CREATE TABLE lyrics (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
input_history_id INT NOT NULL,
|
input_history_id INT NOT NULL,
|
||||||
task_id CHAR(36) NOT NULL,
|
task_id CHAR(36) NOT NULL,
|
||||||
status VARCHAR(50) NOT NULL,
|
status VARCHAR(50) NOT NULL,
|
||||||
lyrics_prompt TEXT NOT NULL,
|
lyrics_prompt TEXT NOT NULL,
|
||||||
lyrics_result LONGTEXT NOT NULL,
|
lyrics_result LONGTEXT NOT NULL,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE
|
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
-- song 테이블
|
-- song 테이블
|
||||||
CREATE TABLE song (
|
CREATE TABLE song (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
input_history_id INT NOT NULL,
|
input_history_id INT NOT NULL,
|
||||||
lyrics_id INT NOT NULL,
|
lyrics_id INT NOT NULL,
|
||||||
task_id CHAR(36) NOT NULL,
|
task_id CHAR(36) NOT NULL,
|
||||||
status VARCHAR(50) NOT NULL,
|
status VARCHAR(50) NOT NULL,
|
||||||
song_prompt TEXT NOT NULL,
|
song_prompt TEXT NOT NULL,
|
||||||
song_result_url_1 VARCHAR(2048),
|
song_result_url_1 VARCHAR(2048),
|
||||||
song_result_url_2 VARCHAR(2048),
|
song_result_url_2 VARCHAR(2048),
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE,
|
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE,
|
||||||
FOREIGN KEY (lyrics_id) REFERENCES lyrics(id) ON DELETE CASCADE
|
FOREIGN KEY (lyrics_id) REFERENCES lyrics(id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
-- creatomate_result_url 테이블
|
-- creatomate_result_url 테이블
|
||||||
CREATE TABLE creatomate_result_url (
|
CREATE TABLE creatomate_result_url (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
input_history_id INT NOT NULL,
|
input_history_id INT NOT NULL,
|
||||||
song_id INT NOT NULL,
|
song_id INT NOT NULL,
|
||||||
task_id CHAR(36) NOT NULL,
|
task_id CHAR(36) NOT NULL,
|
||||||
status VARCHAR(50) NOT NULL,
|
status VARCHAR(50) NOT NULL,
|
||||||
result_movie_url VARCHAR(2048),
|
result_movie_url VARCHAR(2048),
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE,
|
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE,
|
||||||
FOREIGN KEY (song_id) REFERENCES song(id) ON DELETE CASCADE
|
FOREIGN KEY (song_id) REFERENCES song(id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
-- ===== 인덱스 추가 (쿼리 성능 최적화) =====
|
-- ===== 인덱스 추가 (쿼리 성능 최적화) =====
|
||||||
|
|
||||||
-- input_history
|
-- input_history
|
||||||
CREATE INDEX idx_input_history_task_id ON input_history(task_id);
|
CREATE INDEX idx_input_history_task_id ON input_history(task_id);
|
||||||
|
|
||||||
-- upload_img_url (task_id 인덱스 + 복합 인덱스)
|
-- upload_img_url (task_id 인덱스 + 복합 인덱스)
|
||||||
CREATE INDEX idx_upload_img_url_task_id ON upload_img_url(task_id);
|
CREATE INDEX idx_upload_img_url_task_id ON upload_img_url(task_id);
|
||||||
CREATE INDEX idx_upload_img_url_task_id_img_uid ON upload_img_url(task_id, img_uid);
|
CREATE INDEX idx_upload_img_url_task_id_img_uid ON upload_img_url(task_id, img_uid);
|
||||||
|
|
||||||
-- lyrics (input_history_id + task_id 인덱스)
|
-- lyrics (input_history_id + task_id 인덱스)
|
||||||
CREATE INDEX idx_lyrics_input_history_id ON lyrics(input_history_id);
|
CREATE INDEX idx_lyrics_input_history_id ON lyrics(input_history_id);
|
||||||
CREATE INDEX idx_lyrics_task_id ON lyrics(task_id);
|
CREATE INDEX idx_lyrics_task_id ON lyrics(task_id);
|
||||||
|
|
||||||
-- song (input_history_id + lyrics_id + task_id 인덱스)
|
-- song (input_history_id + lyrics_id + task_id 인덱스)
|
||||||
CREATE INDEX idx_song_input_history_id ON song(input_history_id);
|
CREATE INDEX idx_song_input_history_id ON song(input_history_id);
|
||||||
CREATE INDEX idx_song_lyrics_id ON song(lyrics_id);
|
CREATE INDEX idx_song_lyrics_id ON song(lyrics_id);
|
||||||
CREATE INDEX idx_song_task_id ON song(task_id);
|
CREATE INDEX idx_song_task_id ON song(task_id);
|
||||||
|
|
||||||
-- creatomate_result_url (input_history_id + song_id + task_id 인덱스)
|
-- creatomate_result_url (input_history_id + song_id + task_id 인덱스)
|
||||||
CREATE INDEX idx_creatomate_input_history_id ON creatomate_result_url(input_history_id);
|
CREATE INDEX idx_creatomate_input_history_id ON creatomate_result_url(input_history_id);
|
||||||
CREATE INDEX idx_creatomate_song_id ON creatomate_result_url(song_id);
|
CREATE INDEX idx_creatomate_song_id ON creatomate_result_url(song_id);
|
||||||
CREATE INDEX idx_creatomate_task_id ON creatomate_result_url(task_id);
|
CREATE INDEX idx_creatomate_task_id ON creatomate_result_url(task_id);
|
||||||
@@ -1,83 +1,83 @@
|
|||||||
-- input_history 테이블
|
-- input_history 테이블
|
||||||
CREATE TABLE input_history (
|
CREATE TABLE input_history (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
customer_name VARCHAR(255) NOT NULL,
|
customer_name VARCHAR(255) NOT NULL,
|
||||||
region VARCHAR(100) NOT NULL,
|
region VARCHAR(100) NOT NULL,
|
||||||
task_id CHAR(36) NOT NULL UNIQUE, -- 유니크 UUID
|
task_id CHAR(36) NOT NULL UNIQUE, -- 유니크 UUID
|
||||||
detail_region_info TEXT,
|
detail_region_info TEXT,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
|
|
||||||
-- upload_img_url 테이블
|
-- upload_img_url 테이블
|
||||||
CREATE TABLE upload_img_url (
|
CREATE TABLE upload_img_url (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
task_id CHAR(36) NOT NULL, -- input_history와 연결
|
task_id CHAR(36) NOT NULL, -- input_history와 연결
|
||||||
img_uid INT NOT NULL,
|
img_uid INT NOT NULL,
|
||||||
img_url VARCHAR(2048) NOT NULL,
|
img_url VARCHAR(2048) NOT NULL,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
UNIQUE KEY unique_img_task_image (task_id, img_uid),
|
UNIQUE KEY unique_img_task_image (task_id, img_uid),
|
||||||
FOREIGN KEY (task_id) REFERENCES input_history(task_id) ON DELETE CASCADE
|
FOREIGN KEY (task_id) REFERENCES input_history(task_id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
-- lyrics 테이블
|
-- lyrics 테이블
|
||||||
CREATE TABLE lyrics (
|
CREATE TABLE lyrics (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
input_history_id INT NOT NULL,
|
input_history_id INT NOT NULL,
|
||||||
task_id CHAR(36) NOT NULL UNIQUE,
|
task_id CHAR(36) NOT NULL UNIQUE,
|
||||||
status VARCHAR(50) NOT NULL,
|
status VARCHAR(50) NOT NULL,
|
||||||
lyrics_prompt TEXT NOT NULL,
|
lyrics_prompt TEXT NOT NULL,
|
||||||
lyrics_result LONGTEXT NOT NULL,
|
lyrics_result LONGTEXT NOT NULL,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE
|
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
-- song 테이블
|
-- song 테이블
|
||||||
CREATE TABLE song (
|
CREATE TABLE song (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
input_history_id INT NOT NULL,
|
input_history_id INT NOT NULL,
|
||||||
lyrics_id INT NOT NULL,
|
lyrics_id INT NOT NULL,
|
||||||
task_id CHAR(36) NOT NULL UNIQUE,
|
task_id CHAR(36) NOT NULL UNIQUE,
|
||||||
status VARCHAR(50) NOT NULL,
|
status VARCHAR(50) NOT NULL,
|
||||||
song_prompt TEXT NOT NULL,
|
song_prompt TEXT NOT NULL,
|
||||||
song_result_url_1 VARCHAR(2048),
|
song_result_url_1 VARCHAR(2048),
|
||||||
song_result_url_2 VARCHAR(2048),
|
song_result_url_2 VARCHAR(2048),
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE,
|
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE,
|
||||||
FOREIGN KEY (lyrics_id) REFERENCES lyrics(id) ON DELETE CASCADE
|
FOREIGN KEY (lyrics_id) REFERENCES lyrics(id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
-- creatomate_result_url 테이블
|
-- creatomate_result_url 테이블
|
||||||
CREATE TABLE creatomate_result_url (
|
CREATE TABLE creatomate_result_url (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
input_history_id INT NOT NULL,
|
input_history_id INT NOT NULL,
|
||||||
song_id INT NOT NULL,
|
song_id INT NOT NULL,
|
||||||
task_id CHAR(36) NOT NULL UNIQUE,
|
task_id CHAR(36) NOT NULL UNIQUE,
|
||||||
status VARCHAR(50) NOT NULL,
|
status VARCHAR(50) NOT NULL,
|
||||||
result_movie_url VARCHAR(2048),
|
result_movie_url VARCHAR(2048),
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE,
|
FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE,
|
||||||
FOREIGN KEY (song_id) REFERENCES song(id) ON DELETE CASCADE
|
FOREIGN KEY (song_id) REFERENCES song(id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
-- ===== 인덱스 추가 (쿼리 성능 최적화) =====
|
-- ===== 인덱스 추가 (쿼리 성능 최적화) =====
|
||||||
|
|
||||||
-- input_history
|
-- input_history
|
||||||
CREATE INDEX idx_input_history_task_id ON input_history(task_id);
|
CREATE INDEX idx_input_history_task_id ON input_history(task_id);
|
||||||
|
|
||||||
-- upload_img_url (task_id 인덱스 + 복합 인덱스)
|
-- upload_img_url (task_id 인덱스 + 복합 인덱스)
|
||||||
CREATE INDEX idx_upload_img_url_task_id ON upload_img_url(task_id);
|
CREATE INDEX idx_upload_img_url_task_id ON upload_img_url(task_id);
|
||||||
CREATE INDEX idx_upload_img_url_task_id_img_uid ON upload_img_url(task_id, img_uid);
|
CREATE INDEX idx_upload_img_url_task_id_img_uid ON upload_img_url(task_id, img_uid);
|
||||||
|
|
||||||
-- lyrics (input_history_id + task_id 인덱스)
|
-- lyrics (input_history_id + task_id 인덱스)
|
||||||
CREATE INDEX idx_lyrics_input_history_id ON lyrics(input_history_id);
|
CREATE INDEX idx_lyrics_input_history_id ON lyrics(input_history_id);
|
||||||
CREATE INDEX idx_lyrics_task_id ON lyrics(task_id);
|
CREATE INDEX idx_lyrics_task_id ON lyrics(task_id);
|
||||||
|
|
||||||
-- song (input_history_id + lyrics_id + task_id 인덱스)
|
-- song (input_history_id + lyrics_id + task_id 인덱스)
|
||||||
CREATE INDEX idx_song_input_history_id ON song(input_history_id);
|
CREATE INDEX idx_song_input_history_id ON song(input_history_id);
|
||||||
CREATE INDEX idx_song_lyrics_id ON song(lyrics_id);
|
CREATE INDEX idx_song_lyrics_id ON song(lyrics_id);
|
||||||
CREATE INDEX idx_song_task_id ON song(task_id);
|
CREATE INDEX idx_song_task_id ON song(task_id);
|
||||||
|
|
||||||
-- creatomate_result_url (input_history_id + song_id + task_id 인덱스)
|
-- creatomate_result_url (input_history_id + song_id + task_id 인덱스)
|
||||||
CREATE INDEX idx_creatomate_input_history_id ON creatomate_result_url(input_history_id);
|
CREATE INDEX idx_creatomate_input_history_id ON creatomate_result_url(input_history_id);
|
||||||
CREATE INDEX idx_creatomate_song_id ON creatomate_result_url(song_id);
|
CREATE INDEX idx_creatomate_song_id ON creatomate_result_url(song_id);
|
||||||
CREATE INDEX idx_creatomate_task_id ON creatomate_result_url(task_id);
|
CREATE INDEX idx_creatomate_task_id ON creatomate_result_url(task_id);
|
||||||
@@ -1,382 +1,382 @@
|
|||||||
# Pydantic ConfigDict 사용 매뉴얼
|
# Pydantic ConfigDict 사용 매뉴얼
|
||||||
|
|
||||||
## 개요
|
## 개요
|
||||||
|
|
||||||
Pydantic v2에서 `ConfigDict`는 모델의 유효성 검사, 직렬화, JSON 스키마 생성 등의 동작을 제어하는 설정을 정의하는 TypedDict입니다.
|
Pydantic v2에서 `ConfigDict`는 모델의 유효성 검사, 직렬화, JSON 스키마 생성 등의 동작을 제어하는 설정을 정의하는 TypedDict입니다.
|
||||||
|
|
||||||
> Pydantic v1의 `class Config`는 더 이상 권장되지 않으며, `ConfigDict`를 사용해야 합니다.
|
> Pydantic v1의 `class Config`는 더 이상 권장되지 않으며, `ConfigDict`를 사용해야 합니다.
|
||||||
|
|
||||||
## 기본 사용법
|
## 기본 사용법
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from pydantic import BaseModel, ConfigDict
|
from pydantic import BaseModel, ConfigDict
|
||||||
|
|
||||||
class MyModel(BaseModel):
|
class MyModel(BaseModel):
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
str_strip_whitespace=True,
|
str_strip_whitespace=True,
|
||||||
strict=True
|
strict=True
|
||||||
)
|
)
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
age: int
|
age: int
|
||||||
```
|
```
|
||||||
|
|
||||||
## 설정 옵션 전체 목록
|
## 설정 옵션 전체 목록
|
||||||
|
|
||||||
### 문자열 처리
|
### 문자열 처리
|
||||||
|
|
||||||
| 옵션 | 타입 | 기본값 | 설명 |
|
| 옵션 | 타입 | 기본값 | 설명 |
|
||||||
|------|------|--------|------|
|
|------|------|--------|------|
|
||||||
| `str_to_lower` | `bool` | `False` | 문자열을 소문자로 변환 |
|
| `str_to_lower` | `bool` | `False` | 문자열을 소문자로 변환 |
|
||||||
| `str_to_upper` | `bool` | `False` | 문자열을 대문자로 변환 |
|
| `str_to_upper` | `bool` | `False` | 문자열을 대문자로 변환 |
|
||||||
| `str_strip_whitespace` | `bool` | `False` | 문자열 앞뒤 공백 제거 |
|
| `str_strip_whitespace` | `bool` | `False` | 문자열 앞뒤 공백 제거 |
|
||||||
| `str_min_length` | `int \| None` | `None` | 문자열 최소 길이 |
|
| `str_min_length` | `int \| None` | `None` | 문자열 최소 길이 |
|
||||||
| `str_max_length` | `int \| None` | `None` | 문자열 최대 길이 |
|
| `str_max_length` | `int \| None` | `None` | 문자열 최대 길이 |
|
||||||
|
|
||||||
**예시:**
|
**예시:**
|
||||||
```python
|
```python
|
||||||
class UserInput(BaseModel):
|
class UserInput(BaseModel):
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
str_strip_whitespace=True,
|
str_strip_whitespace=True,
|
||||||
str_to_lower=True,
|
str_to_lower=True,
|
||||||
str_min_length=1,
|
str_min_length=1,
|
||||||
str_max_length=100
|
str_max_length=100
|
||||||
)
|
)
|
||||||
|
|
||||||
username: str
|
username: str
|
||||||
|
|
||||||
user = UserInput(username=" HELLO ")
|
user = UserInput(username=" HELLO ")
|
||||||
print(user.username) # "hello"
|
print(user.username) # "hello"
|
||||||
```
|
```
|
||||||
|
|
||||||
### 유효성 검사
|
### 유효성 검사
|
||||||
|
|
||||||
| 옵션 | 타입 | 기본값 | 설명 |
|
| 옵션 | 타입 | 기본값 | 설명 |
|
||||||
|------|------|--------|------|
|
|------|------|--------|------|
|
||||||
| `strict` | `bool` | `False` | 엄격한 타입 검사 활성화 (타입 강제 변환 비활성화) |
|
| `strict` | `bool` | `False` | 엄격한 타입 검사 활성화 (타입 강제 변환 비활성화) |
|
||||||
| `validate_assignment` | `bool` | `False` | 속성 할당 시 유효성 검사 수행 |
|
| `validate_assignment` | `bool` | `False` | 속성 할당 시 유효성 검사 수행 |
|
||||||
| `validate_default` | `bool` | `False` | 기본값도 유효성 검사 수행 |
|
| `validate_default` | `bool` | `False` | 기본값도 유효성 검사 수행 |
|
||||||
| `validate_return` | `bool` | `False` | 반환값 유효성 검사 |
|
| `validate_return` | `bool` | `False` | 반환값 유효성 검사 |
|
||||||
| `revalidate_instances` | `Literal['always', 'never', 'subclass-instances']` | `'never'` | 모델 인스턴스 재검증 시점 |
|
| `revalidate_instances` | `Literal['always', 'never', 'subclass-instances']` | `'never'` | 모델 인스턴스 재검증 시점 |
|
||||||
| `arbitrary_types_allowed` | `bool` | `False` | Pydantic이 지원하지 않는 타입 허용 |
|
| `arbitrary_types_allowed` | `bool` | `False` | Pydantic이 지원하지 않는 타입 허용 |
|
||||||
|
|
||||||
**예시 - strict 모드:**
|
**예시 - strict 모드:**
|
||||||
```python
|
```python
|
||||||
class StrictModel(BaseModel):
|
class StrictModel(BaseModel):
|
||||||
model_config = ConfigDict(strict=True)
|
model_config = ConfigDict(strict=True)
|
||||||
|
|
||||||
count: int
|
count: int
|
||||||
|
|
||||||
# strict=False (기본값): "123" -> 123 자동 변환
|
# strict=False (기본값): "123" -> 123 자동 변환
|
||||||
# strict=True: "123" 입력 시 ValidationError 발생
|
# strict=True: "123" 입력 시 ValidationError 발생
|
||||||
```
|
```
|
||||||
|
|
||||||
**예시 - validate_assignment:**
|
**예시 - validate_assignment:**
|
||||||
```python
|
```python
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
model_config = ConfigDict(validate_assignment=True)
|
model_config = ConfigDict(validate_assignment=True)
|
||||||
|
|
||||||
age: int
|
age: int
|
||||||
|
|
||||||
user = User(age=25)
|
user = User(age=25)
|
||||||
user.age = "invalid" # ValidationError 발생
|
user.age = "invalid" # ValidationError 발생
|
||||||
```
|
```
|
||||||
|
|
||||||
### Extra 필드 처리
|
### Extra 필드 처리
|
||||||
|
|
||||||
| 옵션 | 타입 | 기본값 | 설명 |
|
| 옵션 | 타입 | 기본값 | 설명 |
|
||||||
|------|------|--------|------|
|
|------|------|--------|------|
|
||||||
| `extra` | `'allow' \| 'ignore' \| 'forbid'` | `'ignore'` | 추가 필드 처리 방식 |
|
| `extra` | `'allow' \| 'ignore' \| 'forbid'` | `'ignore'` | 추가 필드 처리 방식 |
|
||||||
|
|
||||||
**값 설명:**
|
**값 설명:**
|
||||||
- `'ignore'`: 추가 필드 무시 (기본값)
|
- `'ignore'`: 추가 필드 무시 (기본값)
|
||||||
- `'allow'`: 추가 필드 허용, `__pydantic_extra__`에 저장
|
- `'allow'`: 추가 필드 허용, `__pydantic_extra__`에 저장
|
||||||
- `'forbid'`: 추가 필드 입력 시 에러 발생
|
- `'forbid'`: 추가 필드 입력 시 에러 발생
|
||||||
|
|
||||||
**예시:**
|
**예시:**
|
||||||
```python
|
```python
|
||||||
class AllowExtra(BaseModel):
|
class AllowExtra(BaseModel):
|
||||||
model_config = ConfigDict(extra='allow')
|
model_config = ConfigDict(extra='allow')
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
data = AllowExtra(name="John", unknown_field="value")
|
data = AllowExtra(name="John", unknown_field="value")
|
||||||
print(data.__pydantic_extra__) # {'unknown_field': 'value'}
|
print(data.__pydantic_extra__) # {'unknown_field': 'value'}
|
||||||
|
|
||||||
class ForbidExtra(BaseModel):
|
class ForbidExtra(BaseModel):
|
||||||
model_config = ConfigDict(extra='forbid')
|
model_config = ConfigDict(extra='forbid')
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
ForbidExtra(name="John", unknown="value") # ValidationError 발생
|
ForbidExtra(name="John", unknown="value") # ValidationError 발생
|
||||||
```
|
```
|
||||||
|
|
||||||
### 불변성 (Immutability)
|
### 불변성 (Immutability)
|
||||||
|
|
||||||
| 옵션 | 타입 | 기본값 | 설명 |
|
| 옵션 | 타입 | 기본값 | 설명 |
|
||||||
|------|------|--------|------|
|
|------|------|--------|------|
|
||||||
| `frozen` | `bool` | `False` | 모델을 불변(immutable)으로 만듦, `__hash__()` 구현 |
|
| `frozen` | `bool` | `False` | 모델을 불변(immutable)으로 만듦, `__hash__()` 구현 |
|
||||||
|
|
||||||
**예시:**
|
**예시:**
|
||||||
```python
|
```python
|
||||||
class ImmutableUser(BaseModel):
|
class ImmutableUser(BaseModel):
|
||||||
model_config = ConfigDict(frozen=True)
|
model_config = ConfigDict(frozen=True)
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
age: int
|
age: int
|
||||||
|
|
||||||
user = ImmutableUser(name="John", age=30)
|
user = ImmutableUser(name="John", age=30)
|
||||||
user.age = 31 # 에러 발생: Instance is frozen
|
user.age = 31 # 에러 발생: Instance is frozen
|
||||||
|
|
||||||
# frozen=True이면 해시 가능
|
# frozen=True이면 해시 가능
|
||||||
users_set = {user} # 정상 작동
|
users_set = {user} # 정상 작동
|
||||||
```
|
```
|
||||||
|
|
||||||
### 별칭 (Alias) 설정
|
### 별칭 (Alias) 설정
|
||||||
|
|
||||||
| 옵션 | 타입 | 기본값 | 설명 |
|
| 옵션 | 타입 | 기본값 | 설명 |
|
||||||
|------|------|--------|------|
|
|------|------|--------|------|
|
||||||
| `populate_by_name` | `bool` | `False` | 필드명과 별칭 모두로 값 설정 허용 (deprecated) |
|
| `populate_by_name` | `bool` | `False` | 필드명과 별칭 모두로 값 설정 허용 (deprecated) |
|
||||||
| `validate_by_alias` | `bool` | `True` | 별칭으로 필드 값 설정 허용 |
|
| `validate_by_alias` | `bool` | `True` | 별칭으로 필드 값 설정 허용 |
|
||||||
| `validate_by_name` | `bool` | `False` | 별칭이 있어도 필드명으로 값 설정 허용 |
|
| `validate_by_name` | `bool` | `False` | 별칭이 있어도 필드명으로 값 설정 허용 |
|
||||||
| `serialize_by_alias` | `bool` | `False` | 직렬화 시 별칭 사용 |
|
| `serialize_by_alias` | `bool` | `False` | 직렬화 시 별칭 사용 |
|
||||||
| `alias_generator` | `Callable[[str], str] \| None` | `None` | 별칭 자동 생성 함수 |
|
| `alias_generator` | `Callable[[str], str] \| None` | `None` | 별칭 자동 생성 함수 |
|
||||||
| `loc_by_alias` | `bool` | `True` | 에러 위치에 별칭 사용 |
|
| `loc_by_alias` | `bool` | `True` | 에러 위치에 별칭 사용 |
|
||||||
|
|
||||||
**예시:**
|
**예시:**
|
||||||
```python
|
```python
|
||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
|
|
||||||
class APIResponse(BaseModel):
|
class APIResponse(BaseModel):
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
validate_by_alias=True,
|
validate_by_alias=True,
|
||||||
validate_by_name=True,
|
validate_by_name=True,
|
||||||
serialize_by_alias=True
|
serialize_by_alias=True
|
||||||
)
|
)
|
||||||
|
|
||||||
user_name: str = Field(alias="userName")
|
user_name: str = Field(alias="userName")
|
||||||
|
|
||||||
# 둘 다 가능
|
# 둘 다 가능
|
||||||
response1 = APIResponse(userName="John")
|
response1 = APIResponse(userName="John")
|
||||||
response2 = APIResponse(user_name="John")
|
response2 = APIResponse(user_name="John")
|
||||||
|
|
||||||
print(response1.model_dump(by_alias=True)) # {"userName": "John"}
|
print(response1.model_dump(by_alias=True)) # {"userName": "John"}
|
||||||
```
|
```
|
||||||
|
|
||||||
**예시 - alias_generator:**
|
**예시 - alias_generator:**
|
||||||
```python
|
```python
|
||||||
def to_camel(name: str) -> str:
|
def to_camel(name: str) -> str:
|
||||||
parts = name.split('_')
|
parts = name.split('_')
|
||||||
return parts[0] + ''.join(word.capitalize() for word in parts[1:])
|
return parts[0] + ''.join(word.capitalize() for word in parts[1:])
|
||||||
|
|
||||||
class CamelModel(BaseModel):
|
class CamelModel(BaseModel):
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
alias_generator=to_camel,
|
alias_generator=to_camel,
|
||||||
serialize_by_alias=True
|
serialize_by_alias=True
|
||||||
)
|
)
|
||||||
|
|
||||||
first_name: str
|
first_name: str
|
||||||
last_name: str
|
last_name: str
|
||||||
|
|
||||||
data = CamelModel(firstName="John", lastName="Doe")
|
data = CamelModel(firstName="John", lastName="Doe")
|
||||||
print(data.model_dump(by_alias=True))
|
print(data.model_dump(by_alias=True))
|
||||||
# {"firstName": "John", "lastName": "Doe"}
|
# {"firstName": "John", "lastName": "Doe"}
|
||||||
```
|
```
|
||||||
|
|
||||||
### JSON 스키마
|
### JSON 스키마
|
||||||
|
|
||||||
| 옵션 | 타입 | 기본값 | 설명 |
|
| 옵션 | 타입 | 기본값 | 설명 |
|
||||||
|------|------|--------|------|
|
|------|------|--------|------|
|
||||||
| `title` | `str \| None` | `None` | JSON 스키마 타이틀 |
|
| `title` | `str \| None` | `None` | JSON 스키마 타이틀 |
|
||||||
| `json_schema_extra` | `dict \| Callable \| None` | `None` | JSON 스키마에 추가할 정보 |
|
| `json_schema_extra` | `dict \| Callable \| None` | `None` | JSON 스키마에 추가할 정보 |
|
||||||
| `json_schema_serialization_defaults_required` | `bool` | `False` | 직렬화 스키마에서 기본값이 있는 필드도 required로 표시 |
|
| `json_schema_serialization_defaults_required` | `bool` | `False` | 직렬화 스키마에서 기본값이 있는 필드도 required로 표시 |
|
||||||
| `json_schema_mode_override` | `Literal['validation', 'serialization', None]` | `None` | JSON 스키마 모드 강제 지정 |
|
| `json_schema_mode_override` | `Literal['validation', 'serialization', None]` | `None` | JSON 스키마 모드 강제 지정 |
|
||||||
|
|
||||||
**예시 - json_schema_extra:**
|
**예시 - json_schema_extra:**
|
||||||
```python
|
```python
|
||||||
class Product(BaseModel):
|
class Product(BaseModel):
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
title="상품 정보",
|
title="상품 정보",
|
||||||
json_schema_extra={
|
json_schema_extra={
|
||||||
"example": {
|
"example": {
|
||||||
"name": "노트북",
|
"name": "노트북",
|
||||||
"price": 1500000
|
"price": 1500000
|
||||||
},
|
},
|
||||||
"description": "상품 데이터를 나타내는 모델"
|
"description": "상품 데이터를 나타내는 모델"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
price: int
|
price: int
|
||||||
|
|
||||||
# OpenAPI/Swagger 문서에 예시가 표시됨
|
# OpenAPI/Swagger 문서에 예시가 표시됨
|
||||||
```
|
```
|
||||||
|
|
||||||
**예시 - Callable json_schema_extra:**
|
**예시 - Callable json_schema_extra:**
|
||||||
```python
|
```python
|
||||||
def add_examples(schema: dict) -> dict:
|
def add_examples(schema: dict) -> dict:
|
||||||
schema["examples"] = [
|
schema["examples"] = [
|
||||||
{"name": "예시1", "value": 100},
|
{"name": "예시1", "value": 100},
|
||||||
{"name": "예시2", "value": 200}
|
{"name": "예시2", "value": 200}
|
||||||
]
|
]
|
||||||
return schema
|
return schema
|
||||||
|
|
||||||
class DynamicSchema(BaseModel):
|
class DynamicSchema(BaseModel):
|
||||||
model_config = ConfigDict(json_schema_extra=add_examples)
|
model_config = ConfigDict(json_schema_extra=add_examples)
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
value: int
|
value: int
|
||||||
```
|
```
|
||||||
|
|
||||||
### ORM/속성 모드
|
### ORM/속성 모드
|
||||||
|
|
||||||
| 옵션 | 타입 | 기본값 | 설명 |
|
| 옵션 | 타입 | 기본값 | 설명 |
|
||||||
|------|------|--------|------|
|
|------|------|--------|------|
|
||||||
| `from_attributes` | `bool` | `False` | 객체 속성에서 모델 생성 허용 (SQLAlchemy 등) |
|
| `from_attributes` | `bool` | `False` | 객체 속성에서 모델 생성 허용 (SQLAlchemy 등) |
|
||||||
|
|
||||||
**예시:**
|
**예시:**
|
||||||
```python
|
```python
|
||||||
class UserORM:
|
class UserORM:
|
||||||
def __init__(self, name: str, age: int):
|
def __init__(self, name: str, age: int):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.age = age
|
self.age = age
|
||||||
|
|
||||||
class UserModel(BaseModel):
|
class UserModel(BaseModel):
|
||||||
model_config = ConfigDict(from_attributes=True)
|
model_config = ConfigDict(from_attributes=True)
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
age: int
|
age: int
|
||||||
|
|
||||||
orm_user = UserORM(name="John", age=30)
|
orm_user = UserORM(name="John", age=30)
|
||||||
pydantic_user = UserModel.model_validate(orm_user)
|
pydantic_user = UserModel.model_validate(orm_user)
|
||||||
print(pydantic_user) # name='John' age=30
|
print(pydantic_user) # name='John' age=30
|
||||||
```
|
```
|
||||||
|
|
||||||
### Enum 처리
|
### Enum 처리
|
||||||
|
|
||||||
| 옵션 | 타입 | 기본값 | 설명 |
|
| 옵션 | 타입 | 기본값 | 설명 |
|
||||||
|------|------|--------|------|
|
|------|------|--------|------|
|
||||||
| `use_enum_values` | `bool` | `False` | Enum 대신 값(value)으로 저장 |
|
| `use_enum_values` | `bool` | `False` | Enum 대신 값(value)으로 저장 |
|
||||||
|
|
||||||
**예시:**
|
**예시:**
|
||||||
```python
|
```python
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ACTIVE = "active"
|
ACTIVE = "active"
|
||||||
INACTIVE = "inactive"
|
INACTIVE = "inactive"
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
model_config = ConfigDict(use_enum_values=True)
|
model_config = ConfigDict(use_enum_values=True)
|
||||||
|
|
||||||
status: Status
|
status: Status
|
||||||
|
|
||||||
user = User(status=Status.ACTIVE)
|
user = User(status=Status.ACTIVE)
|
||||||
print(user.status) # "active" (문자열)
|
print(user.status) # "active" (문자열)
|
||||||
print(type(user.status)) # <class 'str'>
|
print(type(user.status)) # <class 'str'>
|
||||||
|
|
||||||
# use_enum_values=False (기본값)이면
|
# use_enum_values=False (기본값)이면
|
||||||
# user.status는 Status.ACTIVE (Enum 객체)
|
# user.status는 Status.ACTIVE (Enum 객체)
|
||||||
```
|
```
|
||||||
|
|
||||||
### 직렬화 설정
|
### 직렬화 설정
|
||||||
|
|
||||||
| 옵션 | 타입 | 기본값 | 설명 |
|
| 옵션 | 타입 | 기본값 | 설명 |
|
||||||
|------|------|--------|------|
|
|------|------|--------|------|
|
||||||
| `ser_json_timedelta` | `'iso8601' \| 'float'` | `'iso8601'` | timedelta JSON 직렬화 형식 |
|
| `ser_json_timedelta` | `'iso8601' \| 'float'` | `'iso8601'` | timedelta JSON 직렬화 형식 |
|
||||||
| `ser_json_bytes` | `'utf8' \| 'base64' \| 'hex'` | `'utf8'` | bytes JSON 직렬화 인코딩 |
|
| `ser_json_bytes` | `'utf8' \| 'base64' \| 'hex'` | `'utf8'` | bytes JSON 직렬화 인코딩 |
|
||||||
| `ser_json_inf_nan` | `'null' \| 'constants' \| 'strings'` | `'null'` | 무한대/NaN JSON 직렬화 형식 |
|
| `ser_json_inf_nan` | `'null' \| 'constants' \| 'strings'` | `'null'` | 무한대/NaN JSON 직렬화 형식 |
|
||||||
|
|
||||||
### 숫자/Float 설정
|
### 숫자/Float 설정
|
||||||
|
|
||||||
| 옵션 | 타입 | 기본값 | 설명 |
|
| 옵션 | 타입 | 기본값 | 설명 |
|
||||||
|------|------|--------|------|
|
|------|------|--------|------|
|
||||||
| `allow_inf_nan` | `bool` | `True` | float에서 무한대/NaN 허용 |
|
| `allow_inf_nan` | `bool` | `True` | float에서 무한대/NaN 허용 |
|
||||||
| `coerce_numbers_to_str` | `bool` | `False` | 숫자를 문자열로 강제 변환 허용 |
|
| `coerce_numbers_to_str` | `bool` | `False` | 숫자를 문자열로 강제 변환 허용 |
|
||||||
|
|
||||||
### 기타 설정
|
### 기타 설정
|
||||||
|
|
||||||
| 옵션 | 타입 | 기본값 | 설명 |
|
| 옵션 | 타입 | 기본값 | 설명 |
|
||||||
|------|------|--------|------|
|
|------|------|--------|------|
|
||||||
| `protected_namespaces` | `tuple[str, ...]` | `('model_',)` | 보호할 필드명 접두사 |
|
| `protected_namespaces` | `tuple[str, ...]` | `('model_',)` | 보호할 필드명 접두사 |
|
||||||
| `hide_input_in_errors` | `bool` | `False` | 에러 메시지에서 입력값 숨김 |
|
| `hide_input_in_errors` | `bool` | `False` | 에러 메시지에서 입력값 숨김 |
|
||||||
| `defer_build` | `bool` | `False` | validator/serializer 빌드 지연 |
|
| `defer_build` | `bool` | `False` | validator/serializer 빌드 지연 |
|
||||||
| `use_attribute_docstrings` | `bool` | `False` | 속성 docstring을 필드 설명으로 사용 |
|
| `use_attribute_docstrings` | `bool` | `False` | 속성 docstring을 필드 설명으로 사용 |
|
||||||
| `regex_engine` | `'rust-regex' \| 'python-re'` | `'rust-regex'` | 정규식 엔진 선택 |
|
| `regex_engine` | `'rust-regex' \| 'python-re'` | `'rust-regex'` | 정규식 엔진 선택 |
|
||||||
| `validation_error_cause` | `bool` | `False` | Python 예외를 에러 원인에 포함 |
|
| `validation_error_cause` | `bool` | `False` | Python 예외를 에러 원인에 포함 |
|
||||||
|
|
||||||
## 설정 상속
|
## 설정 상속
|
||||||
|
|
||||||
자식 모델은 부모 모델의 `model_config`를 상속받습니다.
|
자식 모델은 부모 모델의 `model_config`를 상속받습니다.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class ParentModel(BaseModel):
|
class ParentModel(BaseModel):
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
str_strip_whitespace=True,
|
str_strip_whitespace=True,
|
||||||
extra='allow'
|
extra='allow'
|
||||||
)
|
)
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
class ChildModel(ParentModel):
|
class ChildModel(ParentModel):
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
frozen=True # 부모 설정 + frozen=True
|
frozen=True # 부모 설정 + frozen=True
|
||||||
)
|
)
|
||||||
|
|
||||||
age: int
|
age: int
|
||||||
|
|
||||||
# ChildModel은 str_strip_whitespace=True, extra='allow', frozen=True
|
# ChildModel은 str_strip_whitespace=True, extra='allow', frozen=True
|
||||||
```
|
```
|
||||||
|
|
||||||
## FastAPI와 함께 사용
|
## FastAPI와 함께 사용
|
||||||
|
|
||||||
FastAPI에서 요청/응답 스키마로 사용할 때 특히 유용합니다.
|
FastAPI에서 요청/응답 스키마로 사용할 때 특히 유용합니다.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from pydantic import BaseModel, ConfigDict, Field
|
from pydantic import BaseModel, ConfigDict, Field
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
class CreateUserRequest(BaseModel):
|
class CreateUserRequest(BaseModel):
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
str_strip_whitespace=True,
|
str_strip_whitespace=True,
|
||||||
json_schema_extra={
|
json_schema_extra={
|
||||||
"example": {
|
"example": {
|
||||||
"username": "johndoe",
|
"username": "johndoe",
|
||||||
"email": "john@example.com"
|
"email": "john@example.com"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
username: str = Field(..., min_length=3, max_length=50)
|
username: str = Field(..., min_length=3, max_length=50)
|
||||||
email: str
|
email: str
|
||||||
|
|
||||||
class UserResponse(BaseModel):
|
class UserResponse(BaseModel):
|
||||||
model_config = ConfigDict(
|
model_config = ConfigDict(
|
||||||
from_attributes=True, # ORM 객체에서 변환 가능
|
from_attributes=True, # ORM 객체에서 변환 가능
|
||||||
serialize_by_alias=True
|
serialize_by_alias=True
|
||||||
)
|
)
|
||||||
|
|
||||||
id: int
|
id: int
|
||||||
user_name: str = Field(alias="userName")
|
user_name: str = Field(alias="userName")
|
||||||
|
|
||||||
@app.post("/users", response_model=UserResponse)
|
@app.post("/users", response_model=UserResponse)
|
||||||
async def create_user(user: CreateUserRequest):
|
async def create_user(user: CreateUserRequest):
|
||||||
# user.username은 자동으로 공백이 제거됨
|
# user.username은 자동으로 공백이 제거됨
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
## 주의사항
|
## 주의사항
|
||||||
|
|
||||||
1. **v1에서 마이그레이션**: `class Config`는 deprecated입니다. `model_config = ConfigDict(...)`를 사용하세요.
|
1. **v1에서 마이그레이션**: `class Config`는 deprecated입니다. `model_config = ConfigDict(...)`를 사용하세요.
|
||||||
|
|
||||||
2. **populate_by_name은 deprecated**: `validate_by_alias`와 `validate_by_name`을 함께 사용하세요.
|
2. **populate_by_name은 deprecated**: `validate_by_alias`와 `validate_by_name`을 함께 사용하세요.
|
||||||
|
|
||||||
3. **json_encoders는 deprecated**: 커스텀 직렬화가 필요하면 `@field_serializer` 데코레이터를 사용하세요.
|
3. **json_encoders는 deprecated**: 커스텀 직렬화가 필요하면 `@field_serializer` 데코레이터를 사용하세요.
|
||||||
|
|
||||||
## 참고 자료
|
## 참고 자료
|
||||||
|
|
||||||
- [Pydantic Configuration API 공식 문서](https://docs.pydantic.dev/latest/api/config/)
|
- [Pydantic Configuration API 공식 문서](https://docs.pydantic.dev/latest/api/config/)
|
||||||
- [Pydantic Models 개념](https://docs.pydantic.dev/latest/concepts/models/)
|
- [Pydantic Models 개념](https://docs.pydantic.dev/latest/concepts/models/)
|
||||||
- [Pydantic Migration Guide](https://docs.pydantic.dev/latest/migration/)
|
- [Pydantic Migration Guide](https://docs.pydantic.dev/latest/migration/)
|
||||||
@@ -1,158 +1,158 @@
|
|||||||
from app.core.database import Base
|
from app.core.database import Base
|
||||||
from sqlalchemy import (
|
from sqlalchemy import (
|
||||||
Boolean,
|
Boolean,
|
||||||
DateTime,
|
DateTime,
|
||||||
Enum,
|
Enum,
|
||||||
ForeignKey,
|
ForeignKey,
|
||||||
Index,
|
Index,
|
||||||
Integer,
|
Integer,
|
||||||
PrimaryKeyConstraint,
|
PrimaryKeyConstraint,
|
||||||
String,
|
String,
|
||||||
func,
|
func,
|
||||||
)
|
)
|
||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
from starlette.authentication import BaseUser
|
from starlette.authentication import BaseUser
|
||||||
|
|
||||||
|
|
||||||
class User(Base, BaseUser):
|
class User(Base, BaseUser):
|
||||||
__tablename__ = "users"
|
__tablename__ = "users"
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(
|
id: Mapped[int] = mapped_column(
|
||||||
Integer, primary_key=True, nullable=False, autoincrement=True
|
Integer, primary_key=True, nullable=False, autoincrement=True
|
||||||
)
|
)
|
||||||
username: Mapped[str] = mapped_column(
|
username: Mapped[str] = mapped_column(
|
||||||
String(255), unique=True, nullable=False, index=True
|
String(255), unique=True, nullable=False, index=True
|
||||||
)
|
)
|
||||||
email: Mapped[str] = mapped_column(
|
email: Mapped[str] = mapped_column(
|
||||||
String(255), unique=True, nullable=False, index=True
|
String(255), unique=True, nullable=False, index=True
|
||||||
)
|
)
|
||||||
hashed_password: Mapped[str] = mapped_column(String(60), nullable=False)
|
hashed_password: Mapped[str] = mapped_column(String(60), nullable=False)
|
||||||
# age_level 컬럼을 Enum으로 정의
|
# age_level 컬럼을 Enum으로 정의
|
||||||
age_level_choices = ["10", "20", "30", "40", "50", "60", "70", "80"]
|
age_level_choices = ["10", "20", "30", "40", "50", "60", "70", "80"]
|
||||||
age_level: Mapped[str] = mapped_column(
|
age_level: Mapped[str] = mapped_column(
|
||||||
Enum(*age_level_choices, name="age_level_enum"),
|
Enum(*age_level_choices, name="age_level_enum"),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
default="10",
|
default="10",
|
||||||
)
|
)
|
||||||
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
||||||
created_at: Mapped[DateTime] = mapped_column(DateTime, server_default=func.now())
|
created_at: Mapped[DateTime] = mapped_column(DateTime, server_default=func.now())
|
||||||
|
|
||||||
# One-to-many relationship with Post (DynamicMapped + lazy="dynamic")
|
# One-to-many relationship with Post (DynamicMapped + lazy="dynamic")
|
||||||
posts_user: Mapped[list["Post"]] = relationship("Post", back_populates="user_posts")
|
posts_user: Mapped[list["Post"]] = relationship("Post", back_populates="user_posts")
|
||||||
|
|
||||||
# # Many-to-many relationship with Group
|
# # Many-to-many relationship with Group
|
||||||
# user_groups: DynamicMapped["UserGroupAssociation"] = relationship(
|
# user_groups: DynamicMapped["UserGroupAssociation"] = relationship(
|
||||||
# "UserGroupAssociation", back_populates="user", lazy="dynamic"
|
# "UserGroupAssociation", back_populates="user", lazy="dynamic"
|
||||||
# )
|
# )
|
||||||
# n:m 관계 (Group) – 최적의 lazy 옵션: selectin
|
# n:m 관계 (Group) – 최적의 lazy 옵션: selectin
|
||||||
group_user: Mapped[list["Group"]] = relationship(
|
group_user: Mapped[list["Group"]] = relationship(
|
||||||
"Group",
|
"Group",
|
||||||
secondary="user_group_association",
|
secondary="user_group_association",
|
||||||
back_populates="user_group",
|
back_populates="user_group",
|
||||||
lazy="selectin",
|
lazy="selectin",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"id={self.id}, username={self.username}"
|
return f"id={self.id}, username={self.username}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_authenticated(self) -> bool:
|
def is_authenticated(self) -> bool:
|
||||||
return self.is_active
|
return self.is_active
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def display_name(self) -> str:
|
def display_name(self) -> str:
|
||||||
return self.username
|
return self.username
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def identity(self) -> str:
|
def identity(self) -> str:
|
||||||
return self.username
|
return self.username
|
||||||
|
|
||||||
|
|
||||||
# 1:N Relationship - Posts
|
# 1:N Relationship - Posts
|
||||||
class Post(Base):
|
class Post(Base):
|
||||||
__tablename__ = "posts"
|
__tablename__ = "posts"
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||||
user_id: Mapped[int] = mapped_column(
|
user_id: Mapped[int] = mapped_column(
|
||||||
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
|
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
|
||||||
)
|
)
|
||||||
title: Mapped[str] = mapped_column(String(255), nullable=False)
|
title: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||||
content: Mapped[str] = mapped_column(String(10000), nullable=False)
|
content: Mapped[str] = mapped_column(String(10000), nullable=False)
|
||||||
is_published: Mapped[bool] = mapped_column(Boolean, default=False)
|
is_published: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||||
created_at: Mapped[DateTime] = mapped_column(DateTime, server_default=func.now())
|
created_at: Mapped[DateTime] = mapped_column(DateTime, server_default=func.now())
|
||||||
updated_at: Mapped[DateTime] = mapped_column(
|
updated_at: Mapped[DateTime] = mapped_column(
|
||||||
DateTime, server_default=func.now(), onupdate=func.now()
|
DateTime, server_default=func.now(), onupdate=func.now()
|
||||||
)
|
)
|
||||||
# tags: Mapped[dict] = mapped_column(MutableDict.as_mutable(JSONB), default=[]) // sqlite 지원 안함
|
# tags: Mapped[dict] = mapped_column(MutableDict.as_mutable(JSONB), default=[]) // sqlite 지원 안함
|
||||||
view_count: Mapped[int] = mapped_column(Integer, default=0)
|
view_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||||
|
|
||||||
# Many-to-one relationship with User (using dynamic loading)
|
# Many-to-one relationship with User (using dynamic loading)
|
||||||
user_posts: Mapped["User"] = relationship("User", back_populates="posts_user")
|
user_posts: Mapped["User"] = relationship("User", back_populates="posts_user")
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"Post(id={self.id}, user_id={self.user_id}, title={self.title})"
|
return f"Post(id={self.id}, user_id={self.user_id}, title={self.title})"
|
||||||
|
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
Index("idx_posts_user_id", "user_id"),
|
Index("idx_posts_user_id", "user_id"),
|
||||||
Index("idx_posts_created_at", "created_at"),
|
Index("idx_posts_created_at", "created_at"),
|
||||||
Index(
|
Index(
|
||||||
"idx_posts_user_id_created_at", "user_id", "created_at"
|
"idx_posts_user_id_created_at", "user_id", "created_at"
|
||||||
), # Composite index
|
), # Composite index
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# N:M Relationship - Users and Groups
|
# N:M Relationship - Users and Groups
|
||||||
# Association table for many-to-many relationship
|
# Association table for many-to-many relationship
|
||||||
# N:M Association Table (중간 테이블)
|
# N:M Association Table (중간 테이블)
|
||||||
class UserGroupAssociation(Base):
|
class UserGroupAssociation(Base):
|
||||||
__tablename__ = "user_group_association"
|
__tablename__ = "user_group_association"
|
||||||
|
|
||||||
user_id: Mapped[int] = mapped_column(
|
user_id: Mapped[int] = mapped_column(
|
||||||
Integer, ForeignKey("users.id", ondelete="CASCADE"), primary_key=True
|
Integer, ForeignKey("users.id", ondelete="CASCADE"), primary_key=True
|
||||||
)
|
)
|
||||||
group_id: Mapped[int] = mapped_column(
|
group_id: Mapped[int] = mapped_column(
|
||||||
Integer, ForeignKey("groups.id", ondelete="CASCADE"), primary_key=True
|
Integer, ForeignKey("groups.id", ondelete="CASCADE"), primary_key=True
|
||||||
)
|
)
|
||||||
|
|
||||||
# # 관계 정의
|
# # 관계 정의
|
||||||
# user: Mapped["User"] = relationship("User", back_populates="user_groups")
|
# user: Mapped["User"] = relationship("User", back_populates="user_groups")
|
||||||
# group: Mapped["Group"] = relationship("Group", back_populates="group_users")
|
# group: Mapped["Group"] = relationship("Group", back_populates="group_users")
|
||||||
# # 복합 기본 키 설정
|
# # 복합 기본 키 설정
|
||||||
|
|
||||||
# 기본 키 설정을 위한 __table_args__ 추가
|
# 기본 키 설정을 위한 __table_args__ 추가
|
||||||
__table_args__ = (PrimaryKeyConstraint("user_id", "group_id"),)
|
__table_args__ = (PrimaryKeyConstraint("user_id", "group_id"),)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"UserGroupAssociation(user_id={self.user_id}, group_id={self.group_id})"
|
return f"UserGroupAssociation(user_id={self.user_id}, group_id={self.group_id})"
|
||||||
|
|
||||||
|
|
||||||
# Group 테이블
|
# Group 테이블
|
||||||
class Group(Base):
|
class Group(Base):
|
||||||
__tablename__ = "groups"
|
__tablename__ = "groups"
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||||
name: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
|
name: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
|
||||||
description: Mapped[str] = mapped_column(String(1000))
|
description: Mapped[str] = mapped_column(String(1000))
|
||||||
is_public: Mapped[bool] = mapped_column(Boolean, default=True)
|
is_public: Mapped[bool] = mapped_column(Boolean, default=True)
|
||||||
created_at: Mapped[DateTime] = mapped_column(DateTime, server_default=func.now())
|
created_at: Mapped[DateTime] = mapped_column(DateTime, server_default=func.now())
|
||||||
updated_at: Mapped[DateTime] = mapped_column(
|
updated_at: Mapped[DateTime] = mapped_column(
|
||||||
DateTime, server_default=func.now(), onupdate=func.now()
|
DateTime, server_default=func.now(), onupdate=func.now()
|
||||||
)
|
)
|
||||||
|
|
||||||
user_group: Mapped[list["User"]] = relationship(
|
user_group: Mapped[list["User"]] = relationship(
|
||||||
"User",
|
"User",
|
||||||
secondary="user_group_association",
|
secondary="user_group_association",
|
||||||
back_populates="group_user",
|
back_populates="group_user",
|
||||||
lazy="selectin",
|
lazy="selectin",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Group을 만든 사용자와 관계 (일반적인 1:N 관계)
|
# Group을 만든 사용자와 관계 (일반적인 1:N 관계)
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"Group(id={self.id}, name={self.name})"
|
return f"Group(id={self.id}, name={self.name})"
|
||||||
|
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
Index("idx_groups_name", "name"),
|
Index("idx_groups_name", "name"),
|
||||||
Index("idx_groups_is_public", "is_public"),
|
Index("idx_groups_is_public", "is_public"),
|
||||||
Index("idx_groups_created_at", "created_at"),
|
Index("idx_groups_created_at", "created_at"),
|
||||||
Index("idx_groups_composite", "is_public", "created_at"),
|
Index("idx_groups_composite", "is_public", "created_at"),
|
||||||
)
|
)
|
||||||
|
After Width: | Height: | Size: 191 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 152 KiB |
|
After Width: | Height: | Size: 191 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 152 KiB |
|
After Width: | Height: | Size: 191 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 152 KiB |
|
After Width: | Height: | Size: 191 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 152 KiB |
|
After Width: | Height: | Size: 191 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 152 KiB |
|
After Width: | Height: | Size: 191 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 152 KiB |
@@ -1,54 +1,54 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from scalar_fastapi import get_scalar_api_reference
|
from scalar_fastapi import get_scalar_api_reference
|
||||||
|
|
||||||
from app.admin_manager import init_admin
|
from app.admin_manager import init_admin
|
||||||
from app.core.common import lifespan
|
from app.core.common import lifespan
|
||||||
from app.database.session import engine
|
from app.database.session import engine
|
||||||
from app.home.api.routers.v1.home import router as home_router
|
from app.home.api.routers.v1.home import router as home_router
|
||||||
from app.lyric.api.routers.v1.lyric import router as lyric_router
|
from app.lyric.api.routers.v1.lyric import router as lyric_router
|
||||||
from app.song.api.routers.v1.song import router as song_router
|
from app.song.api.routers.v1.song import router as song_router
|
||||||
from app.video.api.routers.v1.video import router as video_router
|
from app.video.api.routers.v1.video import router as video_router
|
||||||
from app.utils.cors import CustomCORSMiddleware
|
from app.utils.cors import CustomCORSMiddleware
|
||||||
from config import prj_settings
|
from config import prj_settings
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title=prj_settings.PROJECT_NAME,
|
title=prj_settings.PROJECT_NAME,
|
||||||
version=prj_settings.VERSION,
|
version=prj_settings.VERSION,
|
||||||
description=prj_settings.DESCRIPTION,
|
description=prj_settings.DESCRIPTION,
|
||||||
lifespan=lifespan,
|
lifespan=lifespan,
|
||||||
docs_url=None, # 기본 Swagger UI 비활성화
|
docs_url=None, # 기본 Swagger UI 비활성화
|
||||||
redoc_url=None, # 기본 ReDoc 비활성화
|
redoc_url=None, # 기본 ReDoc 비활성화
|
||||||
)
|
)
|
||||||
|
|
||||||
init_admin(app, engine)
|
init_admin(app, engine)
|
||||||
|
|
||||||
custom_cors_middleware = CustomCORSMiddleware(app)
|
custom_cors_middleware = CustomCORSMiddleware(app)
|
||||||
custom_cors_middleware.configure_cors()
|
custom_cors_middleware.configure_cors()
|
||||||
|
|
||||||
app.mount("/static", StaticFiles(directory="static"), name="static")
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
||||||
app.mount("/media", StaticFiles(directory="media"), name="media")
|
app.mount("/media", StaticFiles(directory="media"), name="media")
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=["*"],
|
allow_origins=["*"],
|
||||||
allow_methods=["*"],
|
allow_methods=["*"],
|
||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
allow_credentials=True,
|
allow_credentials=True,
|
||||||
max_age=-1,
|
max_age=-1,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/docs", include_in_schema=False)
|
@app.get("/docs", include_in_schema=False)
|
||||||
def get_scalar_docs():
|
def get_scalar_docs():
|
||||||
return get_scalar_api_reference(
|
return get_scalar_api_reference(
|
||||||
openapi_url=app.openapi_url,
|
openapi_url=app.openapi_url,
|
||||||
title="Scalar API",
|
title="Scalar API",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
app.include_router(home_router)
|
app.include_router(home_router)
|
||||||
app.include_router(lyric_router) # Lyric API 라우터 추가
|
app.include_router(lyric_router) # Lyric API 라우터 추가
|
||||||
app.include_router(song_router) # Song API 라우터 추가
|
app.include_router(song_router) # Song API 라우터 추가
|
||||||
app.include_router(video_router) # Video API 라우터 추가
|
app.include_router(video_router) # Video API 라우터 추가
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
import aiohttp
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
class BlobUploader():
|
||||||
|
root_url = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/"
|
||||||
|
image_content_types = {
|
||||||
|
".jpg": "image/jpeg",
|
||||||
|
".jpeg": "image/jpeg",
|
||||||
|
".png": "image/png",
|
||||||
|
".gif": "image/gif",
|
||||||
|
".webp": "image/webp",
|
||||||
|
".bmp": "image/bmp"
|
||||||
|
}
|
||||||
|
# SAS TOKEN 입력으로 저장
|
||||||
|
def __init__(self, sas_token):
|
||||||
|
self.sas_token = sas_token
|
||||||
|
|
||||||
|
# 음악 파일 업로드 (user_idx/task_idx/music_file_name 경로에 저장), 해당 업로드 링크 출력 / 실패 시 None
|
||||||
|
async def upload_music_to_azure_blob(self, file_data : bytes, user_idx : str, task_idx : str, music_file_name : str) -> str:
|
||||||
|
url = f"{self.root_url}/{user_idx}/{task_idx}/{music_file_name}"
|
||||||
|
access_url = f"{url}?{self.sas_token}"
|
||||||
|
headers = {
|
||||||
|
"Content-Type": "audio/mpeg",
|
||||||
|
"x-ms-blob-type": "BlockBlob"
|
||||||
|
}
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.put(access_url, data=file_data, headers=headers) as resp:
|
||||||
|
if resp.status in [200, 201]:
|
||||||
|
return url
|
||||||
|
else:
|
||||||
|
text = await resp.text()
|
||||||
|
print(f"Failed Status Code: {resp.status}")
|
||||||
|
print(f"Response: {text}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# 영상 파일 업로드 (user_idx/task_idx/video_file_name 경로에 저장), 해당 업로드 링크 출력 / 실패 시 None
|
||||||
|
async def upload_video_to_azure_blob(self, file_data : bytes, user_idx : str, task_idx : str, video_file_name : str) -> str:
|
||||||
|
url = f"{self.root_url}/{user_idx}/{task_idx}/{video_file_name}"
|
||||||
|
access_url = f"{url}?{self.sas_token}"
|
||||||
|
headers = {
|
||||||
|
"Content-Type": "video/mp4",
|
||||||
|
"x-ms-blob-type": "BlockBlob"
|
||||||
|
}
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.put(access_url, data=file_data, headers=headers) as resp:
|
||||||
|
if resp.status in [200, 201]:
|
||||||
|
return url
|
||||||
|
else:
|
||||||
|
text = await resp.text()
|
||||||
|
print(f"Failed Status Code: {resp.status}")
|
||||||
|
print(f"Response: {text}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# 이미지 파일 업로드 (user_idx/task_idx/image_file_name 경로에 저장), 해당 업로드 링크 출력 / 실패 시 None
|
||||||
|
async def upload_image_to_azure_blob(self, file_data : bytes, user_idx : str, task_idx : str, image_file_name : str) -> str:
|
||||||
|
url = f"{self.root_url}/{user_idx}/{task_idx}/{image_file_name}"
|
||||||
|
access_url = f"{url}?{self.sas_token}"
|
||||||
|
extension = Path(image_file_name).suffix.lower()
|
||||||
|
content_type = self.image_content_types.get(extension, "image/jpeg")
|
||||||
|
headers = {
|
||||||
|
"Content-Type": content_type,
|
||||||
|
"x-ms-blob-type": "BlockBlob"
|
||||||
|
}
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.put(access_url, data=file_data, headers=headers) as resp:
|
||||||
|
if resp.status in [200, 201]:
|
||||||
|
return url
|
||||||
|
else:
|
||||||
|
text = await resp.text()
|
||||||
|
print(f"Failed Status Code: {resp.status}")
|
||||||
|
print(f"Response: {text}")
|
||||||
|
return None
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import asyncio
|
||||||
|
from nvMapScraper import nvMapScraper
|
||||||
|
from nvMapPwScraper import nvMapPwScraper
|
||||||
|
|
||||||
|
async def main_function():
|
||||||
|
await nvMapPwScraper.initiate_scraper()
|
||||||
|
selected = {'title': '<b>스테이</b>,<b>머뭄</b>',
|
||||||
|
'link': 'https://www.instagram.com/staymeomoom',
|
||||||
|
'category': '숙박>펜션',
|
||||||
|
'description': '',
|
||||||
|
'telephone': '',
|
||||||
|
'address': '전북특별자치도 군산시 신흥동 63-18',
|
||||||
|
'roadAddress': '전북특별자치도 군산시 절골길 18',
|
||||||
|
'mapx': '1267061254',
|
||||||
|
'mapy': '359864175',
|
||||||
|
'lng': 126.7061254,
|
||||||
|
'lat': 35.9864175}
|
||||||
|
|
||||||
|
async with nvMapPwScraper() as pw_scraper:
|
||||||
|
new_url = await pw_scraper.get_place_id_url(selected)
|
||||||
|
|
||||||
|
print(new_url)
|
||||||
|
nv_scraper = nvMapScraper(new_url) # 이후 동일한 플로우
|
||||||
|
await nv_scraper.scrap()
|
||||||
|
print(nv_scraper.rawdata)
|
||||||
|
return
|
||||||
|
|
||||||
|
print("running main_funtion..")
|
||||||
|
asyncio.run(main_function())
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
import asyncio
|
||||||
|
from playwright.async_api import async_playwright
|
||||||
|
from urllib import parse
|
||||||
|
|
||||||
|
class nvMapPwScraper():
|
||||||
|
# cls vars
|
||||||
|
is_ready = False
|
||||||
|
_playwright = None
|
||||||
|
_browser = None
|
||||||
|
_context = None
|
||||||
|
_win_width = 1280
|
||||||
|
_win_height = 720
|
||||||
|
_max_retry = 30 # place id timeout threshold seconds
|
||||||
|
|
||||||
|
# instance var
|
||||||
|
page = None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def default_context_builder(cls):
|
||||||
|
context_builder_dict = {}
|
||||||
|
context_builder_dict['viewport'] = {
|
||||||
|
'width' : cls._win_width,
|
||||||
|
'height' : cls._win_height
|
||||||
|
}
|
||||||
|
context_builder_dict['screen'] = {
|
||||||
|
'width' : cls._win_width,
|
||||||
|
'height' : cls._win_height
|
||||||
|
}
|
||||||
|
context_builder_dict['user_agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
|
||||||
|
context_builder_dict['locale'] = 'ko-KR'
|
||||||
|
context_builder_dict['timezone_id']='Asia/Seoul'
|
||||||
|
|
||||||
|
return context_builder_dict
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def initiate_scraper(cls):
|
||||||
|
if not cls._playwright:
|
||||||
|
cls._playwright = await async_playwright().start()
|
||||||
|
if not cls._browser:
|
||||||
|
cls._browser = await cls._playwright.chromium.launch(headless=True)
|
||||||
|
if not cls._context:
|
||||||
|
cls._context = await cls._browser.new_context(**cls.default_context_builder())
|
||||||
|
cls.is_ready = True
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
if not self.is_ready:
|
||||||
|
raise Exception("nvMapScraper is not initiated")
|
||||||
|
|
||||||
|
async def __aenter__(self):
|
||||||
|
await self.create_page()
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, exc_type, exc, tb):
|
||||||
|
await self.page.close()
|
||||||
|
|
||||||
|
async def create_page(self):
|
||||||
|
self.page = await self._context.new_page()
|
||||||
|
await self.page.add_init_script(
|
||||||
|
'''const defaultGetter = Object.getOwnPropertyDescriptor(
|
||||||
|
Navigator.prototype,
|
||||||
|
"webdriver"
|
||||||
|
).get;
|
||||||
|
defaultGetter.apply(navigator);
|
||||||
|
defaultGetter.toString();
|
||||||
|
Object.defineProperty(Navigator.prototype, "webdriver", {
|
||||||
|
set: undefined,
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true,
|
||||||
|
get: new Proxy(defaultGetter, {
|
||||||
|
apply: (target, thisArg, args) => {
|
||||||
|
Reflect.apply(target, thisArg, args);
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const patchedGetter = Object.getOwnPropertyDescriptor(
|
||||||
|
Navigator.prototype,
|
||||||
|
"webdriver"
|
||||||
|
).get;
|
||||||
|
patchedGetter.apply(navigator);
|
||||||
|
patchedGetter.toString();''')
|
||||||
|
|
||||||
|
await self.page.set_extra_http_headers({
|
||||||
|
'sec-ch-ua': '\"Not?A_Brand\";v=\"99\", \"Chromium\";v=\"130\"'
|
||||||
|
})
|
||||||
|
await self.page.goto("http://google.com")
|
||||||
|
|
||||||
|
async def goto_url(self, url, wait_until="domcontentloaded", timeout=20000):
|
||||||
|
page = self.page
|
||||||
|
await page.goto(url, wait_until=wait_until, timeout=timeout)
|
||||||
|
|
||||||
|
async def get_place_id_url(self, selected):
|
||||||
|
|
||||||
|
title = selected['title'].replace("<b>", "").replace("</b>", "")
|
||||||
|
address = selected.get('roadAddress', selected['address']).replace("<b>", "").replace("</b>", "")
|
||||||
|
encoded_query = parse.quote(f"{address} {title}")
|
||||||
|
url = f"https://map.naver.com/p/search/{encoded_query}"
|
||||||
|
|
||||||
|
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
|
||||||
|
|
||||||
|
if "/place/" in self.page.url:
|
||||||
|
return self.page.url
|
||||||
|
|
||||||
|
url = self.page.url.replace("?","?isCorrectAnswer=true&")
|
||||||
|
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
|
||||||
|
|
||||||
|
if "/place/" in self.page.url:
|
||||||
|
return self.page.url
|
||||||
|
|
||||||
|
if (count == self._max_retry / 2):
|
||||||
|
raise Exception("Failed to identify place id. loading timeout")
|
||||||
|
else:
|
||||||
|
raise Exception("Failed to identify place id. item is ambiguous")
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
import re
|
||||||
|
import aiohttp
|
||||||
|
import json
|
||||||
|
import asyncio
|
||||||
|
import bs4
|
||||||
|
|
||||||
|
PLACE_PATTERN = r"/place/(\d+)"
|
||||||
|
GRAPHQL_URL = "https://pcmap-api.place.naver.com/graphql"
|
||||||
|
NAVER_COOKIES="NAC=mQ7mBownbQf4A; NNB=TQPII6AKDBFGQ; PLACE_LANGUAGE=ko; NACT=1; nid_inf=1431570813; NID_AUT=k2T7FraXOdIMRCHzEZIFtHQup+I7b87M5fd7+p65AXZTdGB/gelRmW8s/Q4oDxm8; tooltipDisplayed=true; SRT30=1762660151; NID_SES=AAAB1Lpy3y3hGzuPbJpJl8vvFx18C+HXXuZEFou/YPgocHe7k2/5MpFlgE48X1JF7c7IPoU2khZKkkuLx+tsvWAzOf0TnG/G8RrBGeawnSluSJcKcTdKKRJ4cygKc/OabVxoc3TNZJWxer3vFtXBoXkDS5querVNS6wvcMhA/p4vkPKOeepwKLR+1IJERlQJWZw4q29IdAysrbBNn3Akf9mDA5eTYvMDLYyRkToRh10TVMW/yhyNQeMXlIdnR8U1ZCNqe/9ErYdos5gQDstswEJQQA0T2cHFGJOtmlYMPlnhWado5w521iZXGJyKcA9ZawizM/i5nK5xNYtPGS3cvImUYl6B5ulIipUJSqpj8v2XstK0TZlOGxHToXaVDrCNmSfCA9vFYbTb6xJHB2JRAT3Jik/z6QgLjJLBWRnsucMDqldxoiEDAUHEhY3pjgZ89quR3c3hwAuTlI9hBn5I3e5VQR0Y/GxoS9mIkMF8pJmcGneqnE0BNIt91RN6Se5rDM69B+JWppBXtSir1JGuXADaRLLMP8VlxJX949iH0UYTKWKsrD4OgNNK5aUx24nAH494WPknBMlx4fCMIeWzy7K3sEZkNUn/+A+eHraqIFfbGpveSCNM+8EqEjMgA+YRgg3eig==; _naver_usersession_=Kkgzim/64JicPJzgkIIvqQ==; page_uid=jesTPsqVWUZssE4qJeossssssD0-011300; SRT5=1762662010; BUC=z5Fu3sAYtFwpbRDrrDFYdn4AgK5hNkOqX-DdaLU7VJM="
|
||||||
|
|
||||||
|
OVERVIEW_QUERY = '''
|
||||||
|
query getAccommodation($id: String!, $deviceType: String) {
|
||||||
|
business: placeDetail(input: {id: $id, isNx: true, deviceType: $deviceType}) {
|
||||||
|
base {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
category
|
||||||
|
roadAddress
|
||||||
|
address
|
||||||
|
phone
|
||||||
|
virtualPhone
|
||||||
|
microReviews
|
||||||
|
conveniences
|
||||||
|
visitorReviewsTotal
|
||||||
|
}
|
||||||
|
images { images { origin url } }
|
||||||
|
cpImages(source: [ugcImage]) { images { origin url } }
|
||||||
|
}
|
||||||
|
}'''
|
||||||
|
|
||||||
|
REQUEST_HEADERS = {
|
||||||
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
|
||||||
|
"Referer": "https://map.naver.com/",
|
||||||
|
"Origin": "https://map.naver.com",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Cookie": NAVER_COOKIES
|
||||||
|
}
|
||||||
|
|
||||||
|
class GraphQLException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class nvMapScraper():
|
||||||
|
url : str = None
|
||||||
|
scrap_type : str = None
|
||||||
|
rawdata : dict = None
|
||||||
|
image_link_list : list[str] = None
|
||||||
|
base_info : dict = None
|
||||||
|
|
||||||
|
|
||||||
|
def __init__(self, url):
|
||||||
|
self.url = url
|
||||||
|
|
||||||
|
async def parse_url(self):
|
||||||
|
if 'place' not in self.url:
|
||||||
|
if 'naver.me' in self.url:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(self.url) as response:
|
||||||
|
self.url = str(response.url)
|
||||||
|
else:
|
||||||
|
raise GraphQLException("this shorten url not have place id")
|
||||||
|
try:
|
||||||
|
place_id = re.search(PLACE_PATTERN, self.url)[1]
|
||||||
|
except Exception as E:
|
||||||
|
raise GraphQLException("Cannot find place id")
|
||||||
|
|
||||||
|
return place_id
|
||||||
|
|
||||||
|
async def scrap(self):
|
||||||
|
try:
|
||||||
|
place_id = await self.parse_url()
|
||||||
|
data = await self.call_get_accomodation(place_id)
|
||||||
|
self.rawdata = data
|
||||||
|
fac_data = await self.get_facility_string(place_id)
|
||||||
|
self.rawdata['facilities'] = fac_data
|
||||||
|
self.image_link_list = [nv_image['origin'] for nv_image in data['data']['business']['images']['images']]
|
||||||
|
self.base_info = data['data']['business']['base']
|
||||||
|
self.facility_info = fac_data
|
||||||
|
self.scrap_type = "GraphQL"
|
||||||
|
|
||||||
|
except GraphQLException as G:
|
||||||
|
print (G)
|
||||||
|
print("fallback")
|
||||||
|
self.scrap_type = "Playwright"
|
||||||
|
pass # 나중에 pw 이용한 crawling으로 fallback 추가
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
async def call_get_accomodation(self, place_id):
|
||||||
|
payload = {
|
||||||
|
"operationName" : "getAccommodation",
|
||||||
|
"variables": { "id": place_id, "deviceType": "pc" },
|
||||||
|
"query": OVERVIEW_QUERY,
|
||||||
|
}
|
||||||
|
json_payload = json.dumps(payload)
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.post(GRAPHQL_URL, data=json_payload, headers=REQUEST_HEADERS) as response:
|
||||||
|
response.encoding = 'utf-8'
|
||||||
|
if response.status == 200: # 요청 성공
|
||||||
|
return await response.json() # await 주의
|
||||||
|
else: # 요청 실패
|
||||||
|
print('실패 상태 코드:', response.status)
|
||||||
|
print(response.text)
|
||||||
|
raise Exception()
|
||||||
|
|
||||||
|
async def get_facility_string(self, place_id):
|
||||||
|
url = f"https://pcmap.place.naver.com/accommodation/{place_id}/home"
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(url, headers=REQUEST_HEADERS) as response:
|
||||||
|
soup = bs4.BeautifulSoup(await response.read(), 'html.parser')
|
||||||
|
c_elem = soup.find('span', 'place_blind', string='편의')
|
||||||
|
facilities = c_elem.parent.parent.find('div').string
|
||||||
|
return facilities
|
||||||
|
|
||||||
|
# url = "https://naver.me/IgJGCCic"
|
||||||
|
# scraper = nvMapScraper(url)
|
||||||
|
# asyncio.run(scraper.scrap())
|
||||||
|
# print(scraper.image_link_list)
|
||||||
|
# print(len(scraper.image_link_list))
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import asyncio
|
||||||
|
from nvMapScraper import nvMapScraper
|
||||||
|
from nvMapPwScraper import nvMapPwScraper
|
||||||
|
|
||||||
|
async def main_function():
|
||||||
|
await nvMapPwScraper.initiate_scraper()
|
||||||
|
selected = {'title': '<b>스테이</b>,<b>머뭄</b>',
|
||||||
|
'link': 'https://www.instagram.com/staymeomoom',
|
||||||
|
'category': '숙박>펜션',
|
||||||
|
'description': '',
|
||||||
|
'telephone': '',
|
||||||
|
'address': '전북특별자치도 군산시 신흥동 63-18',
|
||||||
|
'roadAddress': '전북특별자치도 군산시 절골길 18',
|
||||||
|
'mapx': '1267061254',
|
||||||
|
'mapy': '359864175',
|
||||||
|
'lng': 126.7061254,
|
||||||
|
'lat': 35.9864175}
|
||||||
|
|
||||||
|
async with nvMapPwScraper() as pw_scraper:
|
||||||
|
new_url = await pw_scraper.get_place_id_url(selected)
|
||||||
|
|
||||||
|
print(new_url)
|
||||||
|
nv_scraper = nvMapScraper(new_url) # 이후 동일한 플로우
|
||||||
|
await nv_scraper.scrap()
|
||||||
|
print(nv_scraper.rawdata)
|
||||||
|
return
|
||||||
|
|
||||||
|
print("running main_funtion..")
|
||||||
|
asyncio.run(main_function())
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
import asyncio
|
||||||
|
from playwright.async_api import async_playwright
|
||||||
|
from urllib import parse
|
||||||
|
|
||||||
|
class nvMapPwScraper():
|
||||||
|
# cls vars
|
||||||
|
is_ready = False
|
||||||
|
_playwright = None
|
||||||
|
_browser = None
|
||||||
|
_context = None
|
||||||
|
_win_width = 1280
|
||||||
|
_win_height = 720
|
||||||
|
_max_retry = 30 # place id timeout threshold seconds
|
||||||
|
|
||||||
|
# instance var
|
||||||
|
page = None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def default_context_builder(cls):
|
||||||
|
context_builder_dict = {}
|
||||||
|
context_builder_dict['viewport'] = {
|
||||||
|
'width' : cls._win_width,
|
||||||
|
'height' : cls._win_height
|
||||||
|
}
|
||||||
|
context_builder_dict['screen'] = {
|
||||||
|
'width' : cls._win_width,
|
||||||
|
'height' : cls._win_height
|
||||||
|
}
|
||||||
|
context_builder_dict['user_agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
|
||||||
|
context_builder_dict['locale'] = 'ko-KR'
|
||||||
|
context_builder_dict['timezone_id']='Asia/Seoul'
|
||||||
|
|
||||||
|
return context_builder_dict
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def initiate_scraper(cls):
|
||||||
|
if not cls._playwright:
|
||||||
|
cls._playwright = await async_playwright().start()
|
||||||
|
if not cls._browser:
|
||||||
|
cls._browser = await cls._playwright.chromium.launch(headless=True)
|
||||||
|
if not cls._context:
|
||||||
|
cls._context = await cls._browser.new_context(**cls.default_context_builder())
|
||||||
|
cls.is_ready = True
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
if not self.is_ready:
|
||||||
|
raise Exception("nvMapScraper is not initiated")
|
||||||
|
|
||||||
|
async def __aenter__(self):
|
||||||
|
await self.create_page()
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, exc_type, exc, tb):
|
||||||
|
await self.page.close()
|
||||||
|
|
||||||
|
async def create_page(self):
|
||||||
|
self.page = await self._context.new_page()
|
||||||
|
await self.page.add_init_script(
|
||||||
|
'''const defaultGetter = Object.getOwnPropertyDescriptor(
|
||||||
|
Navigator.prototype,
|
||||||
|
"webdriver"
|
||||||
|
).get;
|
||||||
|
defaultGetter.apply(navigator);
|
||||||
|
defaultGetter.toString();
|
||||||
|
Object.defineProperty(Navigator.prototype, "webdriver", {
|
||||||
|
set: undefined,
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true,
|
||||||
|
get: new Proxy(defaultGetter, {
|
||||||
|
apply: (target, thisArg, args) => {
|
||||||
|
Reflect.apply(target, thisArg, args);
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const patchedGetter = Object.getOwnPropertyDescriptor(
|
||||||
|
Navigator.prototype,
|
||||||
|
"webdriver"
|
||||||
|
).get;
|
||||||
|
patchedGetter.apply(navigator);
|
||||||
|
patchedGetter.toString();''')
|
||||||
|
|
||||||
|
await self.page.set_extra_http_headers({
|
||||||
|
'sec-ch-ua': '\"Not?A_Brand\";v=\"99\", \"Chromium\";v=\"130\"'
|
||||||
|
})
|
||||||
|
await self.page.goto("http://google.com")
|
||||||
|
|
||||||
|
async def goto_url(self, url, wait_until="domcontentloaded", timeout=20000):
|
||||||
|
page = self.page
|
||||||
|
await page.goto(url, wait_until=wait_until, timeout=timeout)
|
||||||
|
|
||||||
|
async def get_place_id_url(self, selected):
|
||||||
|
|
||||||
|
title = selected['title'].replace("<b>", "").replace("</b>", "")
|
||||||
|
address = selected.get('roadAddress', selected['address']).replace("<b>", "").replace("</b>", "")
|
||||||
|
encoded_query = parse.quote(f"{address} {title}")
|
||||||
|
url = f"https://map.naver.com/p/search/{encoded_query}"
|
||||||
|
|
||||||
|
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
|
||||||
|
|
||||||
|
if "/place/" in self.page.url:
|
||||||
|
return self.page.url
|
||||||
|
|
||||||
|
url = self.page.url.replace("?","?isCorrectAnswer=true&")
|
||||||
|
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
|
||||||
|
|
||||||
|
if "/place/" in self.page.url:
|
||||||
|
return self.page.url
|
||||||
|
|
||||||
|
if (count == self._max_retry / 2):
|
||||||
|
raise Exception("Failed to identify place id. loading timeout")
|
||||||
|
else:
|
||||||
|
raise Exception("Failed to identify place id. item is ambiguous")
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
import asyncio
|
|
||||||
import json
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import json
|
||||||
|
import asyncio
|
||||||
|
import bs4
|
||||||
|
|
||||||
|
PLACE_PATTERN = r"/place/(\d+)"
|
||||||
GRAPHQL_URL = "https://pcmap-api.place.naver.com/graphql"
|
GRAPHQL_URL = "https://pcmap-api.place.naver.com/graphql"
|
||||||
NAVER_COOKIES = "NAC=mQ7mBownbQf4A; NNB=TQPII6AKDBFGQ; PLACE_LANGUAGE=ko; NACT=1; nid_inf=1431570813; NID_AUT=k2T7FraXOdIMRCHzEZIFtHQup+I7b87M5fd7+p65AXZTdGB/gelRmW8s/Q4oDxm8; tooltipDisplayed=true; SRT30=1762660151; NID_SES=AAAB1Lpy3y3hGzuPbJpJl8vvFx18C+HXXuZEFou/YPgocHe7k2/5MpFlgE48X1JF7c7IPoU2khZKkkuLx+tsvWAzOf0TnG/G8RrBGeawnSluSJcKcTdKKRJ4cygKc/OabVxoc3TNZJWxer3vFtXBoXkDS5querVNS6wvcMhA/p4vkPKOeepwKLR+1IJERlQJWZw4q29IdAysrbBNn3Akf9mDA5eTYvMDLYyRkToRh10TVMW/yhyNQeMXlIdnR8U1ZCNqe/9ErYdos5gQDstswEJQQA0T2cHFGJOtmlYMPlnhWado5w521iZXGJyKcA9ZawizM/i5nK5xNYtPGS3cvImUYl6B5ulIipUJSqpj8v2XstK0TZlOGxHToXaVDrCNmSfCA9vFYbTb6xJHB2JRAT3Jik/z6QgLjJLBWRnsucMDqldxoiEDAUHEhY3pjgZ89quR3c3hwAuTlI9hBn5I3e5VQR0Y/GxoS9mIkMF8pJmcGneqnE0BNIt91RN6Se5rDM69B+JWppBXtSir1JGuXADaRLLMP8VlxJX949iH0UYTKWKsrD4OgNNK5aUx24nAH494WPknBMlx4fCMIeWzy7K3sEZkNUn/+A+eHraqIFfbGpveSCNM+8EqEjMgA+YRgg3eig==; _naver_usersession_=Kkgzim/64JicPJzgkIIvqQ==; page_uid=jesTPsqVWUZssE4qJeossssssD0-011300; SRT5=1762662010; BUC=z5Fu3sAYtFwpbRDrrDFYdn4AgK5hNkOqX-DdaLU7VJM="
|
NAVER_COOKIES="NAC=mQ7mBownbQf4A; NNB=TQPII6AKDBFGQ; PLACE_LANGUAGE=ko; NACT=1; nid_inf=1431570813; NID_AUT=k2T7FraXOdIMRCHzEZIFtHQup+I7b87M5fd7+p65AXZTdGB/gelRmW8s/Q4oDxm8; tooltipDisplayed=true; SRT30=1762660151; NID_SES=AAAB1Lpy3y3hGzuPbJpJl8vvFx18C+HXXuZEFou/YPgocHe7k2/5MpFlgE48X1JF7c7IPoU2khZKkkuLx+tsvWAzOf0TnG/G8RrBGeawnSluSJcKcTdKKRJ4cygKc/OabVxoc3TNZJWxer3vFtXBoXkDS5querVNS6wvcMhA/p4vkPKOeepwKLR+1IJERlQJWZw4q29IdAysrbBNn3Akf9mDA5eTYvMDLYyRkToRh10TVMW/yhyNQeMXlIdnR8U1ZCNqe/9ErYdos5gQDstswEJQQA0T2cHFGJOtmlYMPlnhWado5w521iZXGJyKcA9ZawizM/i5nK5xNYtPGS3cvImUYl6B5ulIipUJSqpj8v2XstK0TZlOGxHToXaVDrCNmSfCA9vFYbTb6xJHB2JRAT3Jik/z6QgLjJLBWRnsucMDqldxoiEDAUHEhY3pjgZ89quR3c3hwAuTlI9hBn5I3e5VQR0Y/GxoS9mIkMF8pJmcGneqnE0BNIt91RN6Se5rDM69B+JWppBXtSir1JGuXADaRLLMP8VlxJX949iH0UYTKWKsrD4OgNNK5aUx24nAH494WPknBMlx4fCMIeWzy7K3sEZkNUn/+A+eHraqIFfbGpveSCNM+8EqEjMgA+YRgg3eig==; _naver_usersession_=Kkgzim/64JicPJzgkIIvqQ==; page_uid=jesTPsqVWUZssE4qJeossssssD0-011300; SRT5=1762662010; BUC=z5Fu3sAYtFwpbRDrrDFYdn4AgK5hNkOqX-DdaLU7VJM="
|
||||||
|
|
||||||
OVERVIEW_QUERY = """
|
OVERVIEW_QUERY = '''
|
||||||
query getAccommodation($id: String!, $deviceType: String) {
|
query getAccommodation($id: String!, $deviceType: String) {
|
||||||
business: placeDetail(input: {id: $id, isNx: true, deviceType: $deviceType}) {
|
business: placeDetail(input: {id: $id, isNx: true, deviceType: $deviceType}) {
|
||||||
base {
|
base {
|
||||||
@@ -25,81 +26,94 @@ query getAccommodation($id: String!, $deviceType: String) {
|
|||||||
images { images { origin url } }
|
images { images { origin url } }
|
||||||
cpImages(source: [ugcImage]) { images { origin url } }
|
cpImages(source: [ugcImage]) { images { origin url } }
|
||||||
}
|
}
|
||||||
}"""
|
}'''
|
||||||
|
|
||||||
REQUEST_HEADERS = {
|
REQUEST_HEADERS = {
|
||||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
|
||||||
"Referer": "https://map.naver.com/",
|
"Referer": "https://map.naver.com/",
|
||||||
"Origin": "https://map.naver.com",
|
"Origin": "https://map.naver.com",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Cookie": NAVER_COOKIES,
|
"Cookie": NAVER_COOKIES
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class GraphQLException(Exception):
|
class GraphQLException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class nvMapScraper():
|
||||||
class nvMapScraper:
|
url : str = None
|
||||||
url: str = None
|
scrap_type : str = None
|
||||||
scrap_type: str = None
|
rawdata : dict = None
|
||||||
rawdata: dict = None
|
image_link_list : list[str] = None
|
||||||
image_link_list: list[str] = None
|
base_info : dict = None
|
||||||
base_info: dict = None
|
|
||||||
|
|
||||||
def __init__(self, url):
|
def __init__(self, url):
|
||||||
self.url = url
|
self.url = url
|
||||||
|
|
||||||
def parse_url(self):
|
async def parse_url(self):
|
||||||
place_pattern = r"/place/(\d+)"
|
if 'place' not in self.url:
|
||||||
|
if 'naver.me' in self.url:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(self.url) as response:
|
||||||
|
self.url = str(response.url)
|
||||||
|
else:
|
||||||
|
raise GraphQLException("this shorten url not have place id")
|
||||||
try:
|
try:
|
||||||
place_id = re.search(place_pattern, self.url)[1]
|
place_id = re.search(PLACE_PATTERN, self.url)[1]
|
||||||
except:
|
except Exception as E:
|
||||||
raise GraphQLException()
|
raise GraphQLException("Cannot find place id")
|
||||||
|
|
||||||
return place_id
|
return place_id
|
||||||
|
|
||||||
async def scrap(self):
|
async def scrap(self):
|
||||||
try:
|
try:
|
||||||
place_id = self.parse_url()
|
place_id = await self.parse_url()
|
||||||
data = await self.call_get_accomodation(place_id)
|
data = await self.call_get_accomodation(place_id)
|
||||||
self.rawdata = data
|
self.rawdata = data
|
||||||
self.image_link_list = [
|
fac_data = await self.get_facility_string(place_id)
|
||||||
nv_image["origin"]
|
self.rawdata['facilities'] = fac_data
|
||||||
for nv_image in data["data"]["business"]["images"]["images"]
|
self.image_link_list = [nv_image['origin'] for nv_image in data['data']['business']['images']['images']]
|
||||||
]
|
self.base_info = data['data']['business']['base']
|
||||||
self.base_info = data["data"]["business"]["base"]
|
self.facility_info = fac_data
|
||||||
self.scrap_type = "GraphQL"
|
self.scrap_type = "GraphQL"
|
||||||
|
|
||||||
except GraphQLException:
|
except GraphQLException as G:
|
||||||
|
print (G)
|
||||||
print("fallback")
|
print("fallback")
|
||||||
self.scrap_type = "Playwright"
|
self.scrap_type = "Playwright"
|
||||||
pass # 나중에 pw 이용한 crawling으로 fallback 추가
|
pass # 나중에 pw 이용한 crawling으로 fallback 추가
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
async def call_get_accomodation(self, place_id):
|
async def call_get_accomodation(self, place_id):
|
||||||
payload = {
|
payload = {
|
||||||
"operationName": "getAccommodation",
|
"operationName" : "getAccommodation",
|
||||||
"variables": {"id": place_id, "deviceType": "pc"},
|
"variables": { "id": place_id, "deviceType": "pc" },
|
||||||
"query": OVERVIEW_QUERY,
|
"query": OVERVIEW_QUERY,
|
||||||
}
|
}
|
||||||
json_payload = json.dumps(payload)
|
json_payload = json.dumps(payload)
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.post(
|
async with session.post(GRAPHQL_URL, data=json_payload, headers=REQUEST_HEADERS) as response:
|
||||||
GRAPHQL_URL, data=json_payload, headers=REQUEST_HEADERS
|
response.encoding = 'utf-8'
|
||||||
) as response:
|
if response.status == 200: # 요청 성공
|
||||||
response.encoding = "utf-8"
|
return await response.json() # await 주의
|
||||||
if response.status == 200: # 요청 성공
|
else: # 요청 실패
|
||||||
return await response.json() # await 주의
|
print('실패 상태 코드:', response.status)
|
||||||
else: # 요청 실패
|
print(response.text)
|
||||||
print("실패 상태 코드:", response.status)
|
|
||||||
raise Exception()
|
raise Exception()
|
||||||
|
|
||||||
|
async def get_facility_string(self, place_id):
|
||||||
url = "https://map.naver.com/p/search/%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84/place/1133638931?c=14.70,0,0,0,dh&placePath=/photo?businessCategory=pension&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5×tamp=202512191123&fromPanelNum=2&locale=ko&searchText=%EC%8A%A4%ED%85%8C%EC%9D%B4%EB%A8%B8%EB%AD%84&svcName=map_pcv5×tamp=202512191007&from=map&entry=bmp&filterType=%EC%97%85%EC%B2%B4&businessCategory=pension"
|
url = f"https://pcmap.place.naver.com/accommodation/{place_id}/home"
|
||||||
scraper = nvMapScraper(url)
|
async with aiohttp.ClientSession() as session:
|
||||||
asyncio.run(scraper.scrap())
|
async with session.get(url, headers=REQUEST_HEADERS) as response:
|
||||||
print(scraper.image_link_list)
|
soup = bs4.BeautifulSoup(await response.read(), 'html.parser')
|
||||||
print(len(scraper.image_link_list))
|
c_elem = soup.find('span', 'place_blind', string='편의')
|
||||||
print(scraper.base_info)
|
facilities = c_elem.parent.parent.find('div').string
|
||||||
|
return facilities
|
||||||
|
|
||||||
|
# url = "https://naver.me/IgJGCCic"
|
||||||
|
# scraper = nvMapScraper(url)
|
||||||
|
# asyncio.run(scraper.scrap())
|
||||||
|
# print(scraper.image_link_list)
|
||||||
|
# print(len(scraper.image_link_list))
|
||||||
@@ -1,226 +1,226 @@
|
|||||||
import copy
|
import copy
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
CREATOMATE_API_KEY = "df9e4382d7e84fe790bf8a2168152be195d5a3568524ceb66ed989a2dea809f7d3065d6803b2e3dd9d02b5e5ec1c9823"
|
CREATOMATE_API_KEY = "df9e4382d7e84fe790bf8a2168152be195d5a3568524ceb66ed989a2dea809f7d3065d6803b2e3dd9d02b5e5ec1c9823"
|
||||||
# ACCOUNT_URL = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/"
|
# ACCOUNT_URL = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/"
|
||||||
# Creatomate 템플릿 정보 전부 가져오기
|
# Creatomate 템플릿 정보 전부 가져오기
|
||||||
|
|
||||||
|
|
||||||
class Creatomate:
|
class Creatomate:
|
||||||
base_url: str = "https://api.creatomate.com"
|
base_url: str = "https://api.creatomate.com"
|
||||||
|
|
||||||
def __init__(self, api_key):
|
def __init__(self, api_key):
|
||||||
self.api_key = api_key
|
self.api_key = api_key
|
||||||
|
|
||||||
def get_all_templates_data(self) -> dict:
|
def get_all_templates_data(self) -> dict:
|
||||||
url = Creatomate.base_url + "/v1/templates"
|
url = Creatomate.base_url + "/v1/templates"
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Bearer {self.api_key}",
|
"Authorization": f"Bearer {self.api_key}",
|
||||||
}
|
}
|
||||||
response = requests.get(url, headers=headers)
|
response = requests.get(url, headers=headers)
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
# Creatomate 템플릿 ID 로부터 해당 템플릿 정보 가져오기
|
# Creatomate 템플릿 ID 로부터 해당 템플릿 정보 가져오기
|
||||||
def get_one_template_data(self, template_id: str) -> dict:
|
def get_one_template_data(self, template_id: str) -> dict:
|
||||||
url = Creatomate.base_url + f"/v1/templates/{template_id}"
|
url = Creatomate.base_url + f"/v1/templates/{template_id}"
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Bearer {self.api_key}",
|
"Authorization": f"Bearer {self.api_key}",
|
||||||
}
|
}
|
||||||
response = requests.get(url, headers=headers)
|
response = requests.get(url, headers=headers)
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
# 템플릿 정보 파싱하여 리소스 이름 추출하기
|
# 템플릿 정보 파싱하여 리소스 이름 추출하기
|
||||||
def parse_template_component_name(self, template_source: dict) -> dict:
|
def parse_template_component_name(self, template_source: dict) -> dict:
|
||||||
def recursive_parse_component(element: dict) -> dict:
|
def recursive_parse_component(element: dict) -> dict:
|
||||||
if "name" in element:
|
if "name" in element:
|
||||||
result_element_name_type = {element["name"]: element["type"]}
|
result_element_name_type = {element["name"]: element["type"]}
|
||||||
else:
|
else:
|
||||||
result_element_name_type = {}
|
result_element_name_type = {}
|
||||||
|
|
||||||
if element["type"] == "composition":
|
if element["type"] == "composition":
|
||||||
minor_component_list = [
|
minor_component_list = [
|
||||||
recursive_parse_component(minor) for minor in element["elements"]
|
recursive_parse_component(minor) for minor in element["elements"]
|
||||||
]
|
]
|
||||||
for minor_component in minor_component_list: ## WARNING : Same name component should shroud other component. be aware
|
for minor_component in minor_component_list: ## WARNING : Same name component should shroud other component. be aware
|
||||||
result_element_name_type.update(minor_component)
|
result_element_name_type.update(minor_component)
|
||||||
|
|
||||||
return result_element_name_type
|
return result_element_name_type
|
||||||
|
|
||||||
result = {}
|
result = {}
|
||||||
for result_element_dict in [
|
for result_element_dict in [
|
||||||
recursive_parse_component(component) for component in template_source
|
recursive_parse_component(component) for component in template_source
|
||||||
]:
|
]:
|
||||||
result.update(result_element_dict)
|
result.update(result_element_dict)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# 템플릿 정보 이미지/가사/음악 리소스와 매핑하기
|
# 템플릿 정보 이미지/가사/음악 리소스와 매핑하기
|
||||||
# 이미지는 순차적으로 집어넣기
|
# 이미지는 순차적으로 집어넣기
|
||||||
# 가사는 개행마다 한 텍스트 삽입
|
# 가사는 개행마다 한 텍스트 삽입
|
||||||
# Template에 audio-music 항목이 있어야 함. (추가된 템플릿 Cafe뿐임)
|
# Template에 audio-music 항목이 있어야 함. (추가된 템플릿 Cafe뿐임)
|
||||||
def template_connect_resource_blackbox(
|
def template_connect_resource_blackbox(
|
||||||
self, template_id: str, image_url_list: list[str], lyric: str, music_url: str
|
self, template_id: str, image_url_list: list[str], lyric: str, music_url: str
|
||||||
) -> dict:
|
) -> dict:
|
||||||
template_data = self.get_one_template_data(template_id)
|
template_data = self.get_one_template_data(template_id)
|
||||||
template_component_data = self.parse_template_component_name(
|
template_component_data = self.parse_template_component_name(
|
||||||
template_data["source"]["elements"]
|
template_data["source"]["elements"]
|
||||||
)
|
)
|
||||||
|
|
||||||
lyric.replace("\r", "")
|
lyric.replace("\r", "")
|
||||||
lyric_splited = lyric.split("\n")
|
lyric_splited = lyric.split("\n")
|
||||||
modifications = {}
|
modifications = {}
|
||||||
for idx, (template_component_name, template_type) in enumerate(
|
for idx, (template_component_name, template_type) in enumerate(
|
||||||
template_component_data.items()
|
template_component_data.items()
|
||||||
):
|
):
|
||||||
match template_type:
|
match template_type:
|
||||||
case "image":
|
case "image":
|
||||||
modifications[template_component_name] = image_url_list[
|
modifications[template_component_name] = image_url_list[
|
||||||
idx % len(image_url_list)
|
idx % len(image_url_list)
|
||||||
]
|
]
|
||||||
case "text":
|
case "text":
|
||||||
modifications[template_component_name] = lyric_splited[
|
modifications[template_component_name] = lyric_splited[
|
||||||
idx % len(lyric_splited)
|
idx % len(lyric_splited)
|
||||||
]
|
]
|
||||||
|
|
||||||
modifications["audio-music"] = music_url
|
modifications["audio-music"] = music_url
|
||||||
|
|
||||||
return modifications
|
return modifications
|
||||||
|
|
||||||
def elements_connect_resource_blackbox(
|
def elements_connect_resource_blackbox(
|
||||||
self, elements: list, image_url_list: list[str], lyric: str, music_url: str
|
self, elements: list, image_url_list: list[str], lyric: str, music_url: str
|
||||||
) -> dict:
|
) -> dict:
|
||||||
template_component_data = self.parse_template_component_name(elements)
|
template_component_data = self.parse_template_component_name(elements)
|
||||||
|
|
||||||
lyric.replace("\r", "")
|
lyric.replace("\r", "")
|
||||||
lyric_splited = lyric.split("\n")
|
lyric_splited = lyric.split("\n")
|
||||||
modifications = {}
|
modifications = {}
|
||||||
for idx, (template_component_name, template_type) in enumerate(
|
for idx, (template_component_name, template_type) in enumerate(
|
||||||
template_component_data.items()
|
template_component_data.items()
|
||||||
):
|
):
|
||||||
match template_type:
|
match template_type:
|
||||||
case "image":
|
case "image":
|
||||||
modifications[template_component_name] = image_url_list[
|
modifications[template_component_name] = image_url_list[
|
||||||
idx % len(image_url_list)
|
idx % len(image_url_list)
|
||||||
]
|
]
|
||||||
case "text":
|
case "text":
|
||||||
modifications[template_component_name] = lyric_splited[
|
modifications[template_component_name] = lyric_splited[
|
||||||
idx % len(lyric_splited)
|
idx % len(lyric_splited)
|
||||||
]
|
]
|
||||||
|
|
||||||
modifications["audio-music"] = music_url
|
modifications["audio-music"] = music_url
|
||||||
|
|
||||||
return modifications
|
return modifications
|
||||||
|
|
||||||
def modify_element(self, elements: list, modification: dict):
|
def modify_element(self, elements: list, modification: dict):
|
||||||
def recursive_modify(element: dict) -> dict:
|
def recursive_modify(element: dict) -> dict:
|
||||||
if "name" in element:
|
if "name" in element:
|
||||||
match element["type"]:
|
match element["type"]:
|
||||||
case "image":
|
case "image":
|
||||||
element["source"] = modification[element["name"]]
|
element["source"] = modification[element["name"]]
|
||||||
case "audio":
|
case "audio":
|
||||||
element["source"] = modification.get(element["name"], "")
|
element["source"] = modification.get(element["name"], "")
|
||||||
case "video":
|
case "video":
|
||||||
element["source"] = modification[element["name"]]
|
element["source"] = modification[element["name"]]
|
||||||
case "text":
|
case "text":
|
||||||
element["source"] = modification.get(element["name"], "")
|
element["source"] = modification.get(element["name"], "")
|
||||||
case "composition":
|
case "composition":
|
||||||
for minor in element["elements"]:
|
for minor in element["elements"]:
|
||||||
recursive_modify(minor)
|
recursive_modify(minor)
|
||||||
|
|
||||||
for minor in elements:
|
for minor in elements:
|
||||||
recursive_modify(minor)
|
recursive_modify(minor)
|
||||||
|
|
||||||
return elements
|
return elements
|
||||||
|
|
||||||
# Creatomate에 생성 요청
|
# Creatomate에 생성 요청
|
||||||
# response에 요청 정보 있으니 풀링 필요
|
# response에 요청 정보 있으니 풀링 필요
|
||||||
def make_creatomate_call(self, template_id: str, modifications: dict):
|
def make_creatomate_call(self, template_id: str, modifications: dict):
|
||||||
url = Creatomate.base_url + "/v2/renders"
|
url = Creatomate.base_url + "/v2/renders"
|
||||||
|
|
||||||
data = {"template_id": template_id, "modifications": modifications}
|
data = {"template_id": template_id, "modifications": modifications}
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Bearer {self.api_key}",
|
"Authorization": f"Bearer {self.api_key}",
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post(url, json=data, headers=headers)
|
response = requests.post(url, json=data, headers=headers)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
# Creatomate에 생성 요청 without template
|
# Creatomate에 생성 요청 without template
|
||||||
# response에 요청 정보 있으니 풀링 필요
|
# response에 요청 정보 있으니 풀링 필요
|
||||||
def make_creatomate_custom_call(self, source: str):
|
def make_creatomate_custom_call(self, source: str):
|
||||||
url = Creatomate.base_url + "/v2/renders"
|
url = Creatomate.base_url + "/v2/renders"
|
||||||
data = source
|
data = source
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Bearer {self.api_key}",
|
"Authorization": f"Bearer {self.api_key}",
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post(url, json=data, headers=headers)
|
response = requests.post(url, json=data, headers=headers)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def calc_scene_duration(self, template: dict):
|
def calc_scene_duration(self, template: dict):
|
||||||
total_template_duration = 0
|
total_template_duration = 0
|
||||||
for elem in template["source"]["elements"]:
|
for elem in template["source"]["elements"]:
|
||||||
try:
|
try:
|
||||||
if elem["type"] == "audio":
|
if elem["type"] == "audio":
|
||||||
continue
|
continue
|
||||||
total_template_duration += elem["duration"]
|
total_template_duration += elem["duration"]
|
||||||
if "animations" not in elem:
|
if "animations" not in elem:
|
||||||
continue
|
continue
|
||||||
for animation in elem["animations"]:
|
for animation in elem["animations"]:
|
||||||
assert animation["time"] == 0 # 0이 아닌 경우 확인 필요
|
assert animation["time"] == 0 # 0이 아닌 경우 확인 필요
|
||||||
if animation["transition"]:
|
if animation["transition"]:
|
||||||
total_template_duration -= animation["duration"]
|
total_template_duration -= animation["duration"]
|
||||||
except:
|
except:
|
||||||
print(elem)
|
print(elem)
|
||||||
return total_template_duration
|
return total_template_duration
|
||||||
|
|
||||||
def extend_template_duration(self, template: dict, target_duration: float):
|
def extend_template_duration(self, template: dict, target_duration: float):
|
||||||
template["duration"] = target_duration
|
template["duration"] = target_duration
|
||||||
total_template_duration = self.calc_scene_duration(template)
|
total_template_duration = self.calc_scene_duration(template)
|
||||||
extend_rate = target_duration / total_template_duration
|
extend_rate = target_duration / total_template_duration
|
||||||
new_template = copy.deepcopy(template)
|
new_template = copy.deepcopy(template)
|
||||||
for elem in new_template["source"]["elements"]:
|
for elem in new_template["source"]["elements"]:
|
||||||
try:
|
try:
|
||||||
if elem["type"] == "audio":
|
if elem["type"] == "audio":
|
||||||
continue
|
continue
|
||||||
elem["duration"] = elem["duration"] * extend_rate
|
elem["duration"] = elem["duration"] * extend_rate
|
||||||
if "animations" not in elem:
|
if "animations" not in elem:
|
||||||
continue
|
continue
|
||||||
for animation in elem["animations"]:
|
for animation in elem["animations"]:
|
||||||
assert animation["time"] == 0 # 0이 아닌 경우 확인 필요
|
assert animation["time"] == 0 # 0이 아닌 경우 확인 필요
|
||||||
animation["duration"] = animation["duration"] * extend_rate
|
animation["duration"] = animation["duration"] * extend_rate
|
||||||
except:
|
except:
|
||||||
print(elem)
|
print(elem)
|
||||||
return new_template
|
return new_template
|
||||||
|
|
||||||
|
|
||||||
# Azure사용한 legacy 코드 원본
|
# Azure사용한 legacy 코드 원본
|
||||||
# def template_connect_resource_blackbox(template_id, user_idx, task_idx):
|
# def template_connect_resource_blackbox(template_id, user_idx, task_idx):
|
||||||
# secret_client = get_keyvault_client()
|
# secret_client = get_keyvault_client()
|
||||||
# account_url = secret_client.get_secret(BLOB_ACCOUNT_URL_KEY).value
|
# account_url = secret_client.get_secret(BLOB_ACCOUNT_URL_KEY).value
|
||||||
# media_folder_path = f"{user_idx}/{task_idx}"
|
# media_folder_path = f"{user_idx}/{task_idx}"
|
||||||
# lyric_path = f"{media_folder_path}/lyric.txt"
|
# lyric_path = f"{media_folder_path}/lyric.txt"
|
||||||
# lyric = az_storage.az_storage_read_ado2_media(lyric_path).readall().decode('UTF-8')
|
# lyric = az_storage.az_storage_read_ado2_media(lyric_path).readall().decode('UTF-8')
|
||||||
# media_list = az_storage.az_storage_get_ado2_media_list(media_folder_path)
|
# media_list = az_storage.az_storage_get_ado2_media_list(media_folder_path)
|
||||||
# image_list = [media.name for media in media_list if '/crawling-images/' in media.name]
|
# image_list = [media.name for media in media_list if '/crawling-images/' in media.name]
|
||||||
# template_data = get_one_template_data(template_id)
|
# template_data = get_one_template_data(template_id)
|
||||||
# template_component_data = parse_template_component_name(template_data['source']['elements'])
|
# template_component_data = parse_template_component_name(template_data['source']['elements'])
|
||||||
# lyric.replace("\r", "")
|
# lyric.replace("\r", "")
|
||||||
# lyric_splited = lyric.split("\n")
|
# lyric_splited = lyric.split("\n")
|
||||||
# modifications = {}
|
# modifications = {}
|
||||||
# for idx, (template_component_name, template_type) in enumerate(template_component_data.items()):
|
# for idx, (template_component_name, template_type) in enumerate(template_component_data.items()):
|
||||||
# match template_type:
|
# match template_type:
|
||||||
# case 'image':
|
# case 'image':
|
||||||
# modifications[template_component_name] = f"{account_url}/{BLOB_CONTAINER_NAME}/{image_list[idx % len(image_list)]}"
|
# modifications[template_component_name] = f"{account_url}/{BLOB_CONTAINER_NAME}/{image_list[idx % len(image_list)]}"
|
||||||
# case 'text':
|
# case 'text':
|
||||||
# modifications[template_component_name] = lyric_splited[idx % len(lyric_splited)]
|
# modifications[template_component_name] = lyric_splited[idx % len(lyric_splited)]
|
||||||
|
|
||||||
# modifications["audio-music"] = f"{account_url}/{BLOB_CONTAINER_NAME}/{BLOB_MEDIA_FOLDER}/{media_folder_path}/music_mureka.mp3"
|
# modifications["audio-music"] = f"{account_url}/{BLOB_CONTAINER_NAME}/{BLOB_MEDIA_FOLDER}/{media_folder_path}/music_mureka.mp3"
|
||||||
# print(modifications)
|
# print(modifications)
|
||||||
|
|
||||||
# return modifications
|
# return modifications
|
||||||