| import asyncio |
| from datetime import datetime |
| import uuid |
|
|
|
|
| |
| |
| |
|
|
| 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": {} |
| } |
|
|
|
|
| |
| |
| |
|
|
| 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 |
| } |
|
|
|
|
| |
| |
| |
|
|
| 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"] |
|
|
| |
| |
| |
|
|
| if not content: |
| return { |
| "status": "error", |
| "stage": "validation", |
| "message": "Missing content payload" |
| } |
|
|
| |
| |
| |
|
|
| supported_platforms = { |
| "tiktok", |
| "reels", |
| "youtube", |
| "shorts" |
| } |
|
|
| if platform not in supported_platforms: |
| return { |
| "status": "error", |
| "stage": "validation", |
| "message": f"Unsupported platform: {platform}" |
| } |
|
|
| |
| |
| |
|
|
| result = await safe_dispatch( |
| platform=platform, |
| content=content, |
| metadata=metadata, |
| video_path=video_path |
| ) |
|
|
| |
| |
| |
|
|
| 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" |
| } |