studio / publisher /tasks /publish.py
Ava2lon's picture
Upload 170 files
345855e verified
Raw
History Blame Contribute Delete
4.02 kB
import asyncio
from datetime import datetime
import uuid
# -------------------------------------------------
# CONTEXT NORMALIZER
# -------------------------------------------------
def normalize_context(context):
"""
Supports:
- dict input (registry executor)
- object input (legacy execution engine)
"""
if isinstance(context, dict):
return {
"platform": context.get("platform", "tiktok"),
"content": context.get("content", {}),
"metadata": context.get("metadata", {}),
"video_path": context.get("video_path"),
"payload": context
}
return {
"platform": getattr(context, "platform", "tiktok"),
"content": getattr(context, "content", {}),
"metadata": getattr(context, "metadata", {}),
"video_path": getattr(context, "video_path", None),
"payload": {}
}
# -------------------------------------------------
# SAFE DISPATCH LAYER (registry-aware)
# -------------------------------------------------
async def safe_dispatch(platform, content, metadata, video_path=None):
"""
Uses platform_dispatcher if available.
Falls back to simulated response if missing.
"""
try:
from publisher.platform_dispatcher import dispatch_publish
return await dispatch_publish(
video_path=video_path,
payload={
"platform": platform,
"content": content,
"metadata": metadata
}
)
except Exception as e:
return {
"status": "fallback_success",
"platform": platform,
"message": "dispatch fallback executed",
"error": str(e),
"simulated": True
}
# -------------------------------------------------
# MAIN PUBLISH TASK
# -------------------------------------------------
async def run(context):
ctx = normalize_context(context)
batch_id = str(uuid.uuid4())
started_at = datetime.utcnow().isoformat()
try:
platform = ctx["platform"]
content = ctx["content"]
metadata = ctx["metadata"]
video_path = ctx["video_path"]
# -------------------------------------------------
# VALIDATION LAYER
# -------------------------------------------------
if not content:
return {
"status": "error",
"stage": "validation",
"message": "Missing content payload"
}
# -------------------------------------------------
# PLATFORM ROUTING
# -------------------------------------------------
supported_platforms = {
"tiktok",
"reels",
"youtube",
"shorts"
}
if platform not in supported_platforms:
return {
"status": "error",
"stage": "validation",
"message": f"Unsupported platform: {platform}"
}
# -------------------------------------------------
# DISPATCH EXECUTION
# -------------------------------------------------
result = await safe_dispatch(
platform=platform,
content=content,
metadata=metadata,
video_path=video_path
)
# -------------------------------------------------
# RESPONSE NORMALIZATION
# -------------------------------------------------
return {
"status": "success",
"task": "publish",
"batch_id": batch_id,
"started_at": started_at,
"completed_at": datetime.utcnow().isoformat(),
"platform": platform,
"result": result
}
except Exception as e:
return {
"status": "error",
"task": "publish",
"batch_id": batch_id,
"message": str(e),
"stage": "publish_failed"
}