DEV Community

Mustapha Turki
Mustapha Turki

Posted on

Soundcloud podcasts

If you happen to have a list of subscriptions you love on Soundcloud, you might enjoy this story to never miss great music.
The idea came in when I wanted to curate music playlists on a blog, but somehow wanted to automate things a little. Having a registered app on Soundcloud, I used their apis to get the newest tracks. The final result is a playlist looking like this:

Let's see how you can do the same with your preferences.


Get subscriptions

First you need to call the SC API to get your followings data. You do this by hitting the url at /me/followings.

async function getFollowings() {
  let followings = []
  async function build (href) {
    const options = href || {
      url: `/me/followings`,
      baseURL: SC_BASE_URL,
      method: 'GET',
      params: {
        oauth_token: TOKEN,
        limit: 200,
        linked_partitioning: 1
      }
    }

    const response = await axios(options)

    followings = followings.concat(...response.data.collection)

    process.stdout.write('.')

    if (response.data.next_href) {
      await build(response.data.next_href)
    }

    return null
  }

  await build()

  return followings
}

Get tracks

You then loop through the previously fetched followings and get their tracks accordingly. You will have a list of tracks starting from a particular date.

async function getTracks (id) {
  const fromDate = new Date()
  fromDate.setDate(fromDate.getDate() - 7)

  const response = await axios({
    url: `/users/${id}/tracks`,
    baseURL: SC_BASE_URL,
    method: 'GET',
    params: {
      'oauth_token': SC_API_TOKEN,
      'created_at[from]': fromDate,
    }
  })

  process.stdout.write('.')

  return response.data
}

Plugging them together

You can again filter the tracks to only keep those with a particular duration (in this case, long music podcasts or episodes).

getFollowings()
  .then((users) => users.map(u => u.id))
  .then((ids) => {
    console.log('Followings count', ids.length)
    return Promise.all(ids.map((id) => {
      return getTracks(id)
    }))
  }).then((tracks) => {
    const merged = [].concat(...tracks)
    return _.uniqBy(merged, 'id')
      .filter(t => t.duration > 40 * 60 * 1000 
        && t.playback_count > 1000
        && t.comment_count > 10
      )
      .filter(t => helpers.filterVideosByTitle(t.title))
  })

And that's it. If you are interested in discovering more, please visit my blog at https://stolenbeats.com and don't hestitate to get in touch.

Top comments (0)