Running playlist builder ft. DJ Spotify recs

Jerry Brown
Nerd For Tech
Published in
4 min readFeb 22, 2021

--

My latest project is a single page web application, written in Object Oriented Javascript and HTML/CSS for the frontend, Rails for the backend API, and AJAX interactions between the two.

The app is an evolution of my first project at Flatiron, which was a pure Ruby CLI program that hooked up to the Spotify API to produce song rec(ommendation)s based on a target running cadence (step count) and music genre. For context, runners typically want to maintain a high step count during their runs (even the chill ones!) as a higher foot turnover can naturally improve running form which lowers the chance of injury.

Have you ever found yourself running faster when listening to an upbeat song? One of the easiest ways to change your running cadence is to listen to songs with a tempo that correlates to your steps — just run to the beat!

The Spotify API has a really cool recommendations endpoint that receives seed parameters like artists, genres, tracks, as well as things data points like min_popularity, target_danceability, and you guessed it: target_tempo measured in beats per minute, a direct correlation to running cadence.

My first implementation of the command-line playlist builder took one seed genre and target cadence as the seed parameters. During some spot testing of the CLI build, I found that the Spotify recommendations occasionally produced tracks that were way off the target tempo. Therefore, this time around I wanted to ensure more seed params were provided in the hopes that it would expand the pool of matching tracks and return a list of 20 recommendations that very closely matched the desired tempo.

I stuck with Genre as the main seed parameter as opposed to artist or tracks mainly because 1) when I’m personally making a running playlist, I usually want to explore new music which genres allows for and 2) the recommendation pool will probably be bigger if five genres are provided as opposed to five artists. Therefore, after inputting the target cadence (song BPM), the user is prompted to select between 1–5 seed genres that they like to run to:

Capturing cadence and genres

Adding the genre photos was another feature evolution I mapped out for this web-app. After finding out category icons are available through the get a category endpoint, I immediately created a request to use the ids provided from the seed genres endpoint to retrieve their respective icons, which unfortunately aren’t provided with with the seed genres request.

As a side note which I reviewed a previous blog post, not all of Spotify’s genres can be used as recommendation seeds, which is why they separate their “seed genres” from their “get all categories” endpoint. Another important point here is that, for a reason that I am still trying to figure out, the id string’s provided in the seed genres request do not match the id’s provided when you get all categories. So not only is a separate call to the API required to retrieve icons for seed genres, but you’ll then need to iterate over the seed genre id strings so that they’re formatted to match the category id for the icon request. My implementation is as follows:

  1. Request all seed genres which returns an array of id strings:

2. Format the strings to match category ids — I opted to make my life easier and simply remove the dashes from multi-word which ensures matches for all single word ids and most multi-word ids, but misses unique genres like “R&B” (id translates to “rnb”).

3. Request the maximum of 50 categories to maximize match count

4. Filter categories to keep only those that can be used as seeds (sorry R&B fans..)

From there, grabbing the genre images url to render on the frontend was straightforward and absolutely makes the playlist building experience better. I’m still not exactly sure why Spotify gives different ids or what the difference is between a ‘genre’ and a ‘category’, but it seems like developers might often want more data points to supplement seed genres, and I hope an update is made soon.

Please let me know if I’m missing something or if you’ve found an easier way to do this! And you can check out my project, Cadence Tunes v2 on Github here.

--

--