비동기 적용

This commit is contained in:
bluebamus committed 2025-12-29 16:48:01 +09:00
1 parent 95d90dcb50
commit 153b9f0ca4
6 files changed
+534 -161

No files matched your search

+60 -62
View File
@@ -13,14 +13,14 @@ creatomate = CreatomateService()
# 또는 명시적으로 API 키 전달
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)
```
"""
@@ -37,7 +37,10 @@ OrientationType = Literal["horizontal", "vertical"]
class CreatomateService:
"""Creatomate API를 통한 영상 생성 서비스"""
"""Creatomate API를 통한 영상 생성 서비스
모든 HTTP 호출 메서드는 비동기(async)로 구현되어 있습니다.
"""
BASE_URL = "https://api.creatomate.com"
@@ -71,37 +74,43 @@ class CreatomateService:
self.orientation = orientation
# orientation에 따른 템플릿 설정 가져오기
config = self.TEMPLATE_CONFIG.get(orientation, self.TEMPLATE_CONFIG["vertical"])
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.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}",
}
def get_all_templates_data(self) -> dict:
async def get_all_templates_data(self) -> dict:
"""모든 템플릿 정보를 조회합니다."""
url = f"{self.BASE_URL}/v1/templates"
response = httpx.get(url, headers=self.headers, timeout=30.0)
response.raise_for_status()
return response.json()
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 get_one_template_data(self, template_id: str) -> dict:
async def get_one_template_data(self, template_id: str) -> dict:
"""특정 템플릿 ID로 템플릿 정보를 조회합니다."""
url = f"{self.BASE_URL}/v1/templates/{template_id}"
response = httpx.get(url, headers=self.headers, timeout=30.0)
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()
# 하위 호환성을 위한 별칭 (deprecated)
async def get_one_template_data_async(self, template_id: str) -> dict:
"""특정 템플릿 ID로 템플릿 정보를 조회합니다.
Deprecated: get_one_template_data()를 사용하세요.
"""
return await self.get_one_template_data(template_id)
def parse_template_component_name(self, template_source: list) -> dict:
"""템플릿 정보를 파싱하여 리소스 이름을 추출합니다."""
@@ -129,7 +138,7 @@ class CreatomateService:
return result
def template_connect_resource_blackbox(
async def template_connect_resource_blackbox(
self,
template_id: str,
image_url_list: list[str],
@@ -143,7 +152,7 @@ class CreatomateService:
- 가사는 개행마다 한 텍스트 삽입
- Template에 audio-music 항목이 있어야 함
"""
template_data = self.get_one_template_data(template_id)
template_data = await self.get_one_template_data(template_id)
template_component_data = self.parse_template_component_name(
template_data["source"]["elements"]
)
@@ -223,7 +232,9 @@ class CreatomateService:
return elements
def make_creatomate_call(self, template_id: str, modifications: dict):
async def make_creatomate_call(
self, template_id: str, modifications: dict
) -> dict:
"""Creatomate에 렌더링 요청을 보냅니다.
Note:
@@ -234,58 +245,37 @@ class CreatomateService:
"template_id": template_id,
"modifications": modifications,
}
response = httpx.post(url, json=data, headers=self.headers, timeout=60.0)
response.raise_for_status()
return response.json()
async with httpx.AsyncClient() as client:
response = await client.post(
url, json=data, headers=self.headers, timeout=60.0
)
response.raise_for_status()
return response.json()
def make_creatomate_custom_call(self, source: dict):
async def make_creatomate_custom_call(self, source: dict) -> dict:
"""템플릿 없이 Creatomate에 커스텀 렌더링 요청을 보냅니다.
Note:
response에 요청 정보가 있으니 폴링 필요
"""
url = f"{self.BASE_URL}/v2/renders"
response = httpx.post(url, json=source, headers=self.headers, timeout=60.0)
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 = 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:
"""렌더링 작업의 상태를 조회합니다.
# 하위 호환성을 위한 별칭 (deprecated)
async def make_creatomate_custom_call_async(self, source: dict) -> dict:
"""템플릿 없이 Creatomate에 커스텀 렌더링 요청을 보냅니다.
Args:
render_id: Creatomate 렌더 ID
Returns:
렌더링 상태 정보
Note:
상태 값:
- planned: 예약됨
- waiting: 대기 중
- transcribing: 트랜스크립션 중
- rendering: 렌더링 중
- succeeded: 성공
- failed: 실패
Deprecated: make_creatomate_custom_call()을 사용하세요.
"""
url = f"{self.BASE_URL}/v1/renders/{render_id}"
response = httpx.get(url, headers=self.headers, timeout=30.0)
response.raise_for_status()
return response.json()
return await self.make_creatomate_custom_call(source)
async def get_render_status_async(self, render_id: str) -> dict:
"""렌더링 작업의 상태를 비동기로 조회합니다.
async def get_render_status(self, render_id: str) -> dict:
"""렌더링 작업의 상태를 조회합니다.
Args:
render_id: Creatomate 렌더 ID
@@ -308,6 +298,14 @@ class CreatomateService:
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