"Wait until finished" after "Split in Batches" and Merge the Batches (For Spotify Workflow)

Hello,

Right now I’m trying to build a Spotify workflow where I get a playlist, separate the albums from that playlist, remove the duplicate albums and post the results to a Wordpress Blog.

There is a problem when I extract the albums with “Spotify error response [429]: API rate limit exceeded”. This problem can be fixed by using a SplitinBatches Node, like i did in the example below.

{
  "nodes": [
    {
      "parameters": {
        "resource": "playlist",
        "operation": "getTracks",
        "id": "spotify:playlist:0qmA0zvRAv2Yo2D1pNFZFg",
        "returnAll": true
      },
      "name": "Spotify",
      "type": "n8n-nodes-base.spotify",
      "typeVersion": 1,
      "position": [
        140,
        10
      ],
      "credentials": {
        "spotifyOAuth2Api": "N8N"
      }
    },
    {
      "parameters": {
        "resource": "album",
        "id": "={{$json[\"track\"][\"album\"][\"uri\"]}}"
      },
      "name": "Spotify1",
      "type": "n8n-nodes-base.spotify",
      "typeVersion": 1,
      "position": [
        480,
        10
      ],
      "credentials": {
        "spotifyOAuth2Api": "N8N"
      }
    },
    {
      "parameters": {
        "options": {
          "reset": false
        }
      },
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        370,
        260
      ]
    },
    {
      "parameters": {
        "functionCode": "const waitTimeSeconds = 3;\n\nreturn new Promise((resolve) => {\n  setTimeout(() => {\n    resolve(items);\n  }, waitTimeSeconds * 1000);\n});\n\n"
      },
      "name": "Wait",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        540,
        280
      ]
    }
  ],
  "connections": {
    "Spotify": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spotify1": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Spotify1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Perfect would be a solution where:

  1. There is some sort of “wait until finished” function where the workflow wait until all albums are grabbed.

  2. The batches that were produced by the SplitinBatches Node should be merged. For that I already tried the solution from this page: Merging items after "split in batches" - #2 by jan but it’s not working for me and seems to be something different.

At the end of the workflow should be this for removing duplicates and posting to Wordpress:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const seen = new Set();\n\nreturn items.filter(i => {\n  if (seen.has(i.json.uri)) return false;\n  seen.add(i.json.uri);\n  return true;\n});\n"
      },
      "name": "Remove Duplicates",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1100,
        280
      ]
    },
    {
      "parameters": {
        "title": "= {{$node[\"Remove Duplicates\"].json[\"artists\"][0][\"name\"]}} - {{$node[\"Remove Duplicates\"].json[\"name\"]}}",
        "additionalFields": {
          "content": "=<img src=\"{{$node[\"Remove Duplicates\"].json[\"images\"][0][\"url\"]}}\">\n<br>\n<b>{{$node[\"Remove Duplicates\"].json[\"artists\"][0][\"name\"]}} | {{$node[\"Remove Duplicates\"].json[\"name\"]}}</b>\nRelease Date: {{$node[\"Remove Duplicates\"].json[\"release_date\"]}} | Label: {{$node[\"Remove Duplicates\"].json[\"label\"]}} | {{$node[\"Remove Duplicates\"].json[\"album_type\"]}}\n<br>\n<iframe src=\"https://open.spotify.com/embed/album/{{$node[\"Remove Duplicates\"].json[\"id\"]}}\" width=\"640\" height=\"380\" frameborder=\"0\" allowtransparency=\"true\" allow=\"encrypted-media\"></iframe>",
          "status": "publish",
          "categories": [
            2
          ]
        }
      },
      "name": "Wordpress",
      "type": "n8n-nodes-base.wordpress",
      "typeVersion": 1,
      "position": [
        1350,
        280
      ],
      "credentials": {
        "wordpressApi": "wordpress"
      }
    }
  ],
  "connections": {
    "Remove Duplicates": {
      "main": [
        [
          {
            "node": "Wordpress",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

It works perfectly for very small playlists where i don’t hit the API limit and don’t need SplitinBatches. But with SplitinBatches it gets complicated.

I’m happy to hear your thoughts and solutions for that. Thanks in advance.

Actually, does it look for me like the topic you mentioned above already provides exactly what you want and need.

The IF-Node will exit the loop as soon as there is nothing to process any more, and still do your wait to avoid the rate-limit. The Function-Node after it, will merge all the data from the loop, and after it can you do your remove the duplicates and the post to WordPress.

Hey @ottic!

Were you able to find a solution? If you need more help do let us know :slight_smile:

Thanks for getting back. Yes, I found a solution just be putting this IF-Node as last item in the loop. Works fine.

{
  "nodes": [
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$node[\"SplitInBatches\"].context[\"noItemsLeft\"]}}"
            }
          ]
        }
      },
      "name": "IF Loop is Finished",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        310,
        100
      ]
    }
  ],
  "connections": {}
}
2 Likes