{
  "name": "IPTV_insert_DB",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        -256,
        96
      ],
      "id": "7276a376-9455-4f44-bb03-91c94645a91c",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "jsCode": "const today = new Date();\n\n// \u2795 8 \u5c0f\u6642\nconst todayPlus8 = new Date(today.getTime() + 8 * 60 * 60 * 1000);\n\n// \u660e\u5929\nconst tomorrow = new Date();\ntomorrow.setDate(todayPlus8.getDate() + 4);\n\n// \u683c\u5f0f\u5316 yyyy-mm-dd\nfunction formatYYYYMMDD(date) {\n  return date.toISOString().split(\"T\")[0];\n}\n\n// \u683c\u5f0f\u5316 dd/mm/yyyy\n\n// \u56db\u500b\u7d50\u679c\nconst result = {\n  today_ymd: formatYYYYMMDD(todayPlus8),       // 2025-09-17 (UTC+8 \u5f8c\u7684\u65e5\u671f)\n  tomorrow_ymd: formatYYYYMMDD(tomorrow),      // 2025-09-18\n};\n\n// \u56de\u50b3\u7d66 n8n\nreturn [{ json: result }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -1040,
        112
      ],
      "id": "223a23ef-07af-4c38-b77c-ac1c7d4e0ec2",
      "name": "Extract time"
    },
    {
      "parameters": {
        "jsCode": "// \u6bcf\u7d44 20 \u7b46\nconst batchSize = 20;\n\n// \u5047\u8a2d\u4e0a\u6e38\u8f38\u51fa\u5728 $json.output \u6216 $json.events\nconst events = $input.first().json.data;\n\nconst batches = [];\nfor (let i = 0; i < events.length; i += batchSize) {\n  batches.push({\n    json: {\n      events: events.slice(i, i + batchSize)\n    }\n  });\n}\n\nreturn batches;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -448,
        96
      ],
      "id": "7c63f2d5-1c25-4920-872f-a4c190f0a04a",
      "name": "Split match"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://eventcollectingservice.kl.starixuat.com/IPTV/events",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "events",
              "value": "={{ $json.events }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        176,
        176
      ],
      "id": "889069d4-8180-4cf6-9a6b-c8628646bd35",
      "name": "Update into DB",
      "retryOnFail": true,
      "maxTries": 2,
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://10.18.200.198:8000/download-iptv",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "start_date",
              "value": "={{ $json.today_ymd }}"
            },
            {
              "name": "end_date",
              "value": "={{ $json.tomorrow_ymd }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -864,
        112
      ],
      "id": "992fed86-a9ea-4728-bd21-3aa86c4645f4",
      "name": "Crawling",
      "retryOnFail": false,
      "executeOnce": false,
      "alwaysOutputData": true,
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nfor (const item of $input.all()) {\n  item.json.myNewField = 1;\n}\n\nreturn $input.all();"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        0,
        -16
      ],
      "id": "393edf08-1f37-494a-a5c6-8c823b867944",
      "name": "End"
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {}
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -1264,
        112
      ],
      "id": "9baff658-9da8-4671-b458-843af856835d",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "9b88a3de-2373-478f-a0b0-2f7691c55635",
              "leftValue": "={{ $json.success }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -688,
        112
      ],
      "id": "6e3de625-fa71-4cc7-aff6-d5f8dedd5041",
      "name": "If"
    },
    {
      "parameters": {
        "url": "http://10.18.200.198:8000/login",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -672,
        352
      ],
      "id": "7d91e74d-9d36-4c1b-8593-e1658b1b1780",
      "name": "login",
      "retryOnFail": true,
      "maxTries": 5,
      "waitBetweenTries": 5000
    }
  ],
  "connections": {
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "End",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Update into DB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract time": {
      "main": [
        [
          {
            "node": "Crawling",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split match": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update into DB": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Crawling": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Extract time",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Split match",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "login",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "login": {
      "main": [
        [
          {
            "node": "Crawling",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "1de66743-b7c6-44e6-935f-95e46b49e55e",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "E1ib9Y7V7XyK6BXa",
  "tags": []
}