SoundCloud Scraper

FREEMIUM
By DataFanatic | Updated 16日前 | Music
Popularity

9.7 / 10

Latency

885ms

Service Level

100%

Health Check

100%

Back to All Tutorials (3)

How to Use Endpoints with `offset`

Overview

Before using any endpoints that have an optional field offset, please read and run the code examples below. Taking the endpoint /v1/user/playlists as an example, the code examples demonstrate how to get all playlists of a user. They may help you better understand how to work with pagination and avoid pitfalls.

If there is no code example in your programming languages, variable names and comments still help.

Node.js Code Example (with axios)

// A third-party HTTP client library.
let axios = require('axios')

// Libraries help you manipulate files.
let fs = require('node:fs')
let path = require('node:path')

// Replace this key with yours.
let apiKey = '<YOUR-API-KEY>'

let paramUser = 'https://soundcloud.com/atlantic-records-uk'
let resultFileName = 'playlists.json'

main()

async function main() {

  // Scrapes playlists.
  let playlists = await getUserAllPlaylists(paramUser, 20)

  // Saves the playlists to a JSON file.
  await fs.promises.writeFile(path.join(__dirname, resultFileName), JSON.stringify(playlists, null, 2))

}

/**
 * Scrapes all of a user's playlists.
 * @param {string} user User URL or ID.
 * @param {number} [limit] The max number of playlists per page.
 * @param {number} [maxPages] The max number of pages that will be scraped. Avoids unexpected infinite loop.
 */
async function getUserAllPlaylists(user, limit = 50, maxPages = 20) {

  // Sets query parameters for the first page.
  let params = { user: user, limit: limit }

  let playlists = []

  for (let i = 0; i < maxPages; ++i) {

    console.log(`Fetching page ${i}`)
    console.log(`Params: ${JSON.stringify(params)}`)

    let res
    try {
      res = await axios.request({
        method: 'GET',
        url: 'https://soundcloud-scraper.p.rapidapi.com/v1/user/playlists',
        params: params,
        headers: {
          'X-RapidAPI-Key': apiKey,
          'X-RapidAPI-Host': 'soundcloud-scraper.p.rapidapi.com',
        },
      })
    } catch (err) {

      // For the HTTP library `axios`, an error will be thrown
      // if the response status falls out of the range of 2xx
      // or the request was made but no response was received.
      // Learn more: https://axios-http.com/docs/handling_errors.
      console.log(err)

      break
    }

    // Note that the number of items on a page may be less than `limit`,
    // even if the page is not the last page.
    // (SoundCloud's backend seems to simply discard some of the items fetched from the database.)
    let page = res.data.playlists
    console.log(`Item count: ${page.items.length}\n`)

    // Appends items of the current page.
    playlists.push(...page.items)

    // `nextOffset` is the only field that determines whether there are more items.
    if (page.nextOffset === null) { // No more playlists.
      break
    }

    // Resets the query parameters for the next page.
    params = { user: user, offset: page.nextOffset, limit: limit }

  }

  return playlists

}