{
  "name": "Freelanly Video Posting",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 30
            }
          ]
        }
      },
      "id": "trigger",
      "name": "Every 30 min",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://freelanly.com/api/cron/post-video-to-social",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "<redacted-credential>"
            }
          ]
        },
        "options": {}
      },
      "id": "get-post",
      "name": "Get Video Post",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.hasPost }}",
              "value2": true
            }
          ]
        }
      },
      "id": "if-has-post",
      "name": "Has Post?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://198.12.73.168:3123/api/short-video",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ scenes: $json.body.scenes, config: $json.body.config }) }}",
        "options": {}
      },
      "id": "create-video",
      "name": "Create Video",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        200
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "videoId",
              "value": "={{ $json.videoId }}"
            },
            {
              "name": "queueItemId",
              "value": "={{ $('Get Video Post').item.json.body.queueItemId }}"
            },
            {
              "name": "jobUrl",
              "value": "={{ $('Get Video Post').item.json.body.jobUrl }}"
            },
            {
              "name": "socialCaption",
              "value": "={{ $('Get Video Post').item.json.body.socialCaption }}"
            }
          ],
          "number": [
            {
              "name": "retryCount",
              "value": 0
            }
          ]
        },
        "options": {}
      },
      "id": "set-data",
      "name": "Set Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        880,
        200
      ]
    },
    {
      "parameters": {
        "amount": 2,
        "unit": "minutes"
      },
      "id": "wait",
      "name": "Wait 2 min",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1100,
        200
      ]
    },
    {
      "parameters": {
        "method": "HEAD",
        "url": "=http://198.12.73.168:3123/api/short-video/{{ $json.videoId }}",
        "options": {
          "response": {
            "response": {
              "neverError": true
            }
          }
        }
      },
      "id": "check-video",
      "name": "Check Video Ready",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        200
      ]
    },
    {
      "parameters": {
        "jsCode": "const statusCode = $input.first().json.statusCode || 404;\nconst data = $('Set Data').item.json;\nconst retryItem = $('Retry').item;\nconst retryCount = retryItem ? retryItem.json.retryCount : data.retryCount;\n\nreturn [{\n  json: {\n    isReady: statusCode === 200,\n    videoId: data.videoId,\n    queueItemId: data.queueItemId,\n    jobUrl: data.jobUrl,\n    socialCaption: data.socialCaption,\n    retryCount: retryCount\n  }\n}];"
      },
      "id": "process-status",
      "name": "Process Status",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1540,
        200
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.isReady }}",
              "value2": true
            }
          ]
        }
      },
      "id": "if-ready",
      "name": "Video Ready?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1760,
        200
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=http://198.12.73.168:3123/api/short-video/{{ $json.videoId }}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        }
      },
      "id": "download",
      "name": "Download Video",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1980,
        100
      ]
    },
    {
      "parameters": {
        "chatId": "@PCFTI",
        "text": "={{ $('Process Status').item.json.socialCaption }}\n\nMore jobs: {{ $('Process Status').item.json.jobUrl }}",
        "additionalFields": {
          "appendAttribution": false
        },
        "operation": "sendVideo"
      },
      "id": "telegram",
      "name": "Post to Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2200,
        0
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "authentication": "communityManagement",
        "postAs": "organization",
        "organization": "53520294",
        "text": "={{ $('Process Status').item.json.socialCaption }}\n\nApply: {{ $('Process Status').item.json.jobUrl }}\n\nMore remote jobs at freelanly.com",
        "additionalFields": {}
      },
      "id": "linkedin",
      "name": "Post to LinkedIn",
      "type": "n8n-nodes-base.linkedIn",
      "typeVersion": 1,
      "position": [
        2200,
        200
      ],
      "credentials": {
        "linkedInCommunityManagementOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "https://freelanly.com/api/cron/post-video-to-social",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "<redacted-credential>"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ queueItemId: $('Process Status').item.json.queueItemId, status: 'posted', videoId: $('Process Status').item.json.videoId, videoUrl: 'http://198.12.73.168:3123/api/short-video/' + $('Process Status').item.json.videoId }) }}",
        "options": {}
      },
      "id": "mark-posted",
      "name": "Mark as Posted",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2420,
        100
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.retryCount }}",
              "value2": 10,
              "operation": "smaller"
            }
          ]
        }
      },
      "id": "if-can-retry",
      "name": "Can Retry?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1980,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "videoId",
              "value": "={{ $json.videoId }}"
            },
            {
              "name": "queueItemId",
              "value": "={{ $json.queueItemId }}"
            },
            {
              "name": "jobUrl",
              "value": "={{ $json.jobUrl }}"
            },
            {
              "name": "socialCaption",
              "value": "={{ $json.socialCaption }}"
            }
          ],
          "number": [
            {
              "name": "retryCount",
              "value": "={{ $json.retryCount + 1 }}"
            }
          ]
        },
        "options": {}
      },
      "id": "retry",
      "name": "Retry",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        2200,
        300
      ]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "https://freelanly.com/api/cron/post-video-to-social",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "<redacted-credential>"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ queueItemId: $json.queueItemId, status: 'failed', error: 'Video generation timed out after 20 minutes' }) }}",
        "options": {}
      },
      "id": "mark-failed",
      "name": "Mark as Failed",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2200,
        480
      ]
    },
    {
      "parameters": {},
      "id": "no-post",
      "name": "No Post Available",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        660,
        400
      ]
    },
    {
      "parameters": {},
      "id": "done",
      "name": "Done",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        2640,
        100
      ]
    }
  ],
  "connections": {
    "Every 30 min": {
      "main": [
        [
          {
            "node": "Get Video Post",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Video Post": {
      "main": [
        [
          {
            "node": "Has Post?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Post?": {
      "main": [
        [
          {
            "node": "Create Video",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Post Available",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Video": {
      "main": [
        [
          {
            "node": "Set Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Data": {
      "main": [
        [
          {
            "node": "Wait 2 min",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 2 min": {
      "main": [
        [
          {
            "node": "Check Video Ready",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Video Ready": {
      "main": [
        [
          {
            "node": "Process Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Status": {
      "main": [
        [
          {
            "node": "Video Ready?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Video Ready?": {
      "main": [
        [
          {
            "node": "Download Video",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Can Retry?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Video": {
      "main": [
        [
          {
            "node": "Post to Telegram",
            "type": "main",
            "index": 0
          },
          {
            "node": "Post to LinkedIn",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post to Telegram": {
      "main": [
        [
          {
            "node": "Mark as Posted",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mark as Posted": {
      "main": [
        [
          {
            "node": "Done",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Can Retry?": {
      "main": [
        [
          {
            "node": "Retry",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mark as Failed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Retry": {
      "main": [
        [
          {
            "node": "Wait 2 min",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}