크레아토 완료

This commit is contained in:
bluebamus committed 2025-12-26 17:20:36 +09:00
1 parent 586dd5ccc9
commit 52520d770b
6 files changed
+362 -60

No files matched your search

+78 -2
View File
@@ -25,10 +25,15 @@ response = creatomate.make_creatomate_call(template_id, modifications)
"""
import copy
from typing import Literal
import httpx
from config import apikey_settings
from config import apikey_settings, creatomate_settings
# Orientation 타입 정의
OrientationType = Literal["horizontal", "vertical"]
class CreatomateService:
@@ -36,13 +41,40 @@ class CreatomateService:
BASE_URL = "https://api.creatomate.com"
def __init__(self, api_key: str | None = None):
# 템플릿 설정 (config에서 가져옴)
TEMPLATE_CONFIG = {
"horizontal": {
"template_id": creatomate_settings.TEMPLATE_ID_HORIZONTAL,
"duration": creatomate_settings.TEMPLATE_DURATION_HORIZONTAL,
},
"vertical": {
"template_id": creatomate_settings.TEMPLATE_ID_VERTICAL,
"duration": creatomate_settings.TEMPLATE_DURATION_VERTICAL,
},
}
def __init__(
self,
api_key: str | None = None,
orientation: OrientationType = "vertical",
target_duration: float | None = None,
):
"""
Args:
api_key: Creatomate API 키 (Bearer token으로 사용)
None일 경우 config에서 자동으로 가져옴
orientation: 영상 방향 ("horizontal" 또는 "vertical", 기본값: "vertical")
target_duration: 목표 영상 길이 (초)
None일 경우 orientation에 해당하는 기본값 사용
"""
self.api_key = api_key or apikey_settings.CREATOMATE_API_KEY
self.orientation = orientation
# orientation에 따른 템플릿 설정 가져오기
config = self.TEMPLATE_CONFIG.get(orientation, self.TEMPLATE_CONFIG["vertical"])
self.template_id = config["template_id"]
self.target_duration = target_duration if target_duration is not None else config["duration"]
self.headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}",
@@ -62,6 +94,14 @@ class CreatomateService:
response.raise_for_status()
return response.json()
async def get_one_template_data_async(self, template_id: str) -> dict:
"""특정 템플릿 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()
return response.json()
def parse_template_component_name(self, template_source: list) -> dict:
"""템플릿 정보를 파싱하여 리소스 이름을 추출합니다."""
@@ -209,6 +249,18 @@ class CreatomateService:
response.raise_for_status()
return response.json()
async def make_creatomate_custom_call_async(self, source: dict):
"""템플릿 없이 Creatomate에 비동기로 커스텀 렌더링 요청을 보냅니다.
Note:
response에 요청 정보가 있으니 폴링 필요
"""
url = f"{self.BASE_URL}/v2/renders"
async with httpx.AsyncClient() as client:
response = await client.post(url, json=source, headers=self.headers, timeout=60.0)
response.raise_for_status()
return response.json()
def get_render_status(self, render_id: str) -> dict:
"""렌더링 작업의 상태를 조회합니다.
@@ -232,6 +284,30 @@ class CreatomateService:
response.raise_for_status()
return response.json()
async def get_render_status_async(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}"
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=self.headers, timeout=30.0)
response.raise_for_status()
return response.json()
def calc_scene_duration(self, template: dict) -> float:
"""템플릿의 전체 장면 duration을 계산합니다."""
total_template_duration = 0.0