{
  "name": "03-Multi-Channel-Uploader",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 18 * * 1,3,5"
            }
          ]
        }
      },
      "name": "Schedule - Mon/Wed/Fri 6PM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "command": "cd /app && python -c \"from scripts.utils.database import Database; db = Database(); videos = db._get_connection().__enter__().cursor().execute('SELECT v.id, s.language FROM videos v JOIN scripts s ON v.script_id = s.id WHERE v.status = \\'ready\\' AND v.id NOT IN (SELECT video_id FROM uploads) ORDER BY v.created_at ASC LIMIT 2').fetchall(); import json; print(json.dumps([dict(r) for r in videos]))\""
      },
      "name": "Get Ready Videos",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Parse videos list\nconst output = items[0].json.stdout;\nconst videos = JSON.parse(output);\n\nreturn videos.map(v => ({\n  json: {\n    video_id: v.id,\n    language: v.language,\n    channel: v.language === 'pt-BR' ? 'portuguese' : 'english'\n  }\n}));"
      },
      "name": "Parse Videos",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "Split Items",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "command": "cd /app && python scripts/06_upload/youtube_uploader.py --video-id {{ $json.video_id }} --channel {{ $json.channel }} --privacy public"
      },
      "name": "Upload to YouTube",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Extract YouTube video ID\nconst output = items[0].json.stdout;\nconst match = output.match(/YouTube Video ID: ([\\w-]+)/);\nconst youtubeId = match ? match[1] : 'unknown';\n\nreturn [{\n  json: {\n    video_id: items[0].json.video_id,\n    channel: items[0].json.channel,\n    youtube_id: youtubeId,\n    url: `https://www.youtube.com/watch?v=${youtubeId}`,\n    uploaded_at: new Date().toISOString()\n  }\n}];"
      },
      "name": "Parse Upload Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    },
    {
      "parameters": {
        "amount": 300
      },
      "name": "Wait 5 Minutes",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        1450,
        300
      ]
    },
    {
      "parameters": {},
      "name": "Loop Back",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1650,
        300
      ]
    },
    {
      "parameters": {
        "mode": "combine",
        "combineBy": "combineAll"
      },
      "name": "Collect Results",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2,
      "position": [
        1050,
        500
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "summary",
              "value": "={{ $json.length }} videos uploaded successfully"
            },
            {
              "name": "timestamp",
              "value": "={{ $now }}"
            }
          ]
        }
      },
      "name": "Upload Summary",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1250,
        500
      ]
    }
  ],
  "connections": {
    "Schedule - Mon/Wed/Fri 6PM": {
      "main": [
        [
          {
            "node": "Get Ready Videos",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Ready Videos": {
      "main": [
        [
          {
            "node": "Parse Videos",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Videos": {
      "main": [
        [
          {
            "node": "Split Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Items": {
      "main": [
        [
          {
            "node": "Upload to YouTube",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Collect Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload to YouTube": {
      "main": [
        [
          {
            "node": "Parse Upload Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Upload Result": {
      "main": [
        [
          {
            "node": "Wait 5 Minutes",
            "type": "main",
            "index": 0
          },
          {
            "node": "Collect Results",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Wait 5 Minutes": {
      "main": [
        [
          {
            "node": "Loop Back",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Back": {
      "main": [
        [
          {
            "node": "Split Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Collect Results": {
      "main": [
        [
          {
            "node": "Upload Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "updatedAt": "2025-01-01T00:00:00.000Z",
  "versionId": "1"
}