{
  "name": "YouTube2Post Test Workflow",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "youtube-test",
        "responseMode": "lastNode",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        }
      },
      "id": "webhook_trigger",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1.1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Validate and extract YouTube video ID\nconst input = $input.item.json.body || $input.item.json;\nconst url = input.youtube_url || input.url;\n\nif (!url) {\n  return {\n    error: true,\n    message: 'YouTube URL is required',\n    timestamp: new Date().toISOString()\n  };\n}\n\n// Extract video ID from various YouTube URL formats\nconst patterns = [\n  /(?:youtube\\.com\\/watch\\?v=|youtu\\.be\\/|youtube\\.com\\/shorts\\/)([\\w-]{11})/,\n  /youtube\\.com\\/embed\\/([\\w-]{11})/,\n  /youtube\\.com\\/v\\/([\\w-]{11})/\n];\n\nlet videoId = null;\nfor (const pattern of patterns) {\n  const match = url.match(pattern);\n  if (match) {\n    videoId = match[1];\n    break;\n  }\n}\n\nif (!videoId) {\n  return {\n    error: true,\n    message: 'Invalid YouTube URL format',\n    url: url,\n    timestamp: new Date().toISOString()\n  };\n}\n\n// Return validated data\nreturn {\n  error: false,\n  videoId: videoId,\n  originalUrl: url,\n  language: input.language || 'zh-CN',\n  requestId: Math.random().toString(36).substr(2, 9),\n  timestamp: new Date().toISOString(),\n  processStep: 'validation_complete'\n};"
      },
      "id": "validate_url",
      "name": "Validate URL",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.error }}",
              "value2": true
            }
          ]
        }
      },
      "id": "check_error",
      "name": "Check Error",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Return error response\nconst errorData = $input.item.json;\n\nreturn {\n  success: false,\n  error: {\n    message: errorData.message,\n    url: errorData.url || 'not provided',\n    timestamp: errorData.timestamp\n  },\n  statusCode: 400\n};"
      },
      "id": "format_error",
      "name": "Format Error Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        850,
        400
      ]
    },
    {
      "parameters": {
        "jsCode": "// Process YouTube video information\nconst data = $input.item.json;\n\n// Simulate video metadata (in real scenario, would call YouTube API)\nconst videoMetadata = {\n  videoId: data.videoId,\n  title: `Test Video - ${data.videoId}`,\n  description: 'This is a simulated video description for testing purposes.',\n  duration: Math.floor(Math.random() * 600) + 60, // Random duration between 1-11 minutes\n  views: Math.floor(Math.random() * 100000),\n  likes: Math.floor(Math.random() * 5000),\n  publishedAt: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString(),\n  channelTitle: 'Test Channel',\n  thumbnails: {\n    default: `https://img.youtube.com/vi/${data.videoId}/default.jpg`,\n    medium: `https://img.youtube.com/vi/${data.videoId}/mqdefault.jpg`,\n    high: `https://img.youtube.com/vi/${data.videoId}/hqdefault.jpg`\n  }\n};\n\n// Generate article content based on video\nconst articleContent = {\n  title: `${videoMetadata.title} - \u6df1\u5ea6\u89e3\u6790`,\n  summary: `\u672c\u6587\u57fa\u4e8eYouTube\u89c6\u9891\"${videoMetadata.title}\"\u751f\u6210\uff0c\u5305\u542b\u89c6\u9891\u7684\u6838\u5fc3\u5185\u5bb9\u548c\u8981\u70b9\u5206\u6790\u3002`,\n  sections: [\n    {\n      heading: '\u89c6\u9891\u6982\u8ff0',\n      content: videoMetadata.description\n    },\n    {\n      heading: '\u5173\u952e\u8981\u70b9',\n      content: [\n        '\u8981\u70b91: \u89c6\u9891\u5185\u5bb9\u7684\u6838\u5fc3\u89c2\u70b9',\n        '\u8981\u70b92: \u91cd\u8981\u7684\u6280\u672f\u7ec6\u8282',\n        '\u8981\u70b93: \u5b9e\u8df5\u5e94\u7528\u5efa\u8bae'\n      ]\n    },\n    {\n      heading: '\u89c6\u9891\u4fe1\u606f',\n      content: {\n        '\u89c6\u9891ID': videoMetadata.videoId,\n        '\u53d1\u5e03\u65f6\u95f4': videoMetadata.publishedAt,\n        '\u89c6\u9891\u65f6\u957f': `${Math.floor(videoMetadata.duration / 60)}\u5206${videoMetadata.duration % 60}\u79d2`,\n        '\u89c2\u770b\u6b21\u6570': videoMetadata.views.toLocaleString(),\n        '\u70b9\u8d5e\u6570': videoMetadata.likes.toLocaleString()\n      }\n    }\n  ],\n  metadata: videoMetadata,\n  generatedAt: new Date().toISOString(),\n  language: data.language\n};\n\nreturn {\n  success: true,\n  requestId: data.requestId,\n  videoInfo: videoMetadata,\n  article: articleContent,\n  processSteps: [\n    'url_validated',\n    'video_metadata_fetched',\n    'content_analyzed',\n    'article_generated'\n  ],\n  timestamp: new Date().toISOString()\n};"
      },
      "id": "process_video",
      "name": "Process Video",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        850,
        200
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json) }}",
        "options": {}
      },
      "id": "respond_webhook",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "webhook_trigger": {
      "main": [
        [
          {
            "node": "validate_url",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "validate_url": {
      "main": [
        [
          {
            "node": "check_error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "check_error": {
      "main": [
        [
          {
            "node": "process_video",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "format_error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "process_video": {
      "main": [
        [
          {
            "node": "respond_webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "format_error": {
      "main": [
        [
          {
            "node": "respond_webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}