feat: 인스타 릴스 — 릴스 mp4 저장·공개 주소(/v1/reel)·REELS 게시 API, 관리자 relay, 소개 릴스 HyperFrames 프로젝트·배경 5장, 인스타 올리기 nginx 타임아웃 600s
This commit is contained in:
1 parent
70fe14ae39
commit
7469e58a89
64 files changed
+928
-11
No files matched your search
+38
-9
@@ -17,6 +17,9 @@ MAX_ITEMS = 20
|
||||
# 캐러셀 컨테이너가 FINISHED 가 될 때까지 (문서: 1분에 한 번, 5분까지)
|
||||
STATUS_TRIES = 10
|
||||
STATUS_WAIT_SEC = 6.0
|
||||
# 릴스는 인스타가 영상을 변환하느라 더 오래 걸린다 (5초 간격 5분)
|
||||
VIDEO_STATUS_TRIES = 60
|
||||
VIDEO_STATUS_WAIT_SEC = 5.0
|
||||
|
||||
|
||||
class InstagramError(Exception):
|
||||
@@ -65,15 +68,7 @@ async def publish_carousel(image_urls: list[str], caption: str, client: Optional
|
||||
container = (await _post(client, f"{user}/media", {"image_url": image_urls[0], "caption": caption}))["id"]
|
||||
else:
|
||||
container = (await _post(client, f"{user}/media", {"media_type": "CAROUSEL", "children": ",".join(children), "caption": caption}))["id"]
|
||||
for _ in range(STATUS_TRIES):
|
||||
status = await _status(client, container)
|
||||
if status == "FINISHED":
|
||||
break
|
||||
if status in ("ERROR", "EXPIRED"):
|
||||
raise InstagramError(f"컨테이너 상태 {status}")
|
||||
await asyncio.sleep(wait_sec)
|
||||
else:
|
||||
raise InstagramError("컨테이너가 제때 준비되지 않았다")
|
||||
await _wait_finished(client, container, STATUS_TRIES, wait_sec)
|
||||
media_id = (await _post(client, f"{user}/media_publish", {"creation_id": container}))["id"]
|
||||
LOG.info(f"[instagram] published media {media_id} ({len(image_urls)} images)")
|
||||
return media_id
|
||||
@@ -82,3 +77,37 @@ async def publish_carousel(image_urls: list[str], caption: str, client: Optional
|
||||
finally:
|
||||
if own:
|
||||
await client.aclose()
|
||||
|
||||
|
||||
async def _wait_finished(client: httpx.AsyncClient, container: str, tries: int, wait_sec: float) -> None:
|
||||
for _ in range(tries):
|
||||
status = await _status(client, container)
|
||||
if status == "FINISHED":
|
||||
return
|
||||
if status in ("ERROR", "EXPIRED"):
|
||||
raise InstagramError(f"컨테이너 상태 {status}")
|
||||
await asyncio.sleep(wait_sec)
|
||||
raise InstagramError("컨테이너가 제때 준비되지 않았다")
|
||||
|
||||
|
||||
async def publish_reel(video_url: str, caption: str, cover_url: Optional[str] = None, client: Optional[httpx.AsyncClient] = None, wait_sec: float = VIDEO_STATUS_WAIT_SEC) -> str:
|
||||
"""공개 mp4 주소를 릴스로 올리고(피드에도 보이게) 인스타 미디어 id 를 돌려준다."""
|
||||
if not configured():
|
||||
raise InstagramError("INSTAGRAM_ACCESS_TOKEN / INSTAGRAM_USER_ID 가 비어 있다")
|
||||
user = settings.instagram_user_id.strip()
|
||||
own = client is None
|
||||
client = client or httpx.AsyncClient(timeout=TIMEOUT_SEC)
|
||||
try:
|
||||
data = {"media_type": "REELS", "video_url": video_url, "caption": caption, "share_to_feed": "true"}
|
||||
if cover_url:
|
||||
data["cover_url"] = cover_url
|
||||
container = (await _post(client, f"{user}/media", data))["id"]
|
||||
await _wait_finished(client, container, VIDEO_STATUS_TRIES, wait_sec)
|
||||
media_id = (await _post(client, f"{user}/media_publish", {"creation_id": container}))["id"]
|
||||
LOG.info(f"[instagram] published reel {media_id}")
|
||||
return media_id
|
||||
except httpx.HTTPError as e:
|
||||
raise InstagramError(f"{type(e).__name__}") from e
|
||||
finally:
|
||||
if own:
|
||||
await client.aclose()
|
||||
Reference in new issue
Block a user