The other day a friend of mine requested recommendations for songs that were both sad but upbeat, a tall order. After wracking my brain and coming up with pretty much nothing I thought, "Wouldn't it be nice if you could just specify specifically what kind of songs you want and have Spotify generate a playlist?" Well if you know me you know I went ahead and built a little app that does just that! So, have you ever wanted to generate a playlist based on specific attributes like happiness, danciness, or acousticness? Or perhaps you'd be interested in seeing what your top Spotify tracks say about what you like to listen to? Well with this Spotify Playlist Generator you can do both of those things!
With everything I post on this blog, I want to share a bit of the detail that went into this project. The first thing you'll need to do if you want to make your own app is to create a new app in the Spotify Developer platform. From there you will be able to get your client id and secret to be used in your requests as well as set your redirect URLs. Once you're set up the process is as follows:
Spotify offers a couple of ways to authenticate users but I went with the Authorization code flow. You will need to create a link using your client id, a redirect URL, and the specific scopes you require (for this project I'm using "user-read-private", "user-read-email", "user-top-read", "playlist-modify-public", and "playlist-modify-private"). If the user allows your app you will receive a code that you can then use with your secret to get a token for the user. You'll use this token in all subsequent requests for user data.
Once you have a token you can make a request to get the user's top tracks. In order to learn about the characteristics of these tracks however you'll need to get the features for each track. Thankfully there is a single endpoint you can use to get the features of multiple tracks but you will need to pass in the track ID. Parse through the top track response to grab the IDs and then you can make the request for features and then tie the results back to the original list of top tracks.
Now that you've got the top tracks and their features, you can analyze the distribution of some of the top attributes like valence, danceability, and acousticness. I've used ApexCharts.js to build out each mini histogram and Ion.RangeSlider to create a slider for the range below each one. With the slider, you can add a callback that will help keep track of what values have been selected.
Now that the user has selected a set of ranges for the attributes and features they are interested in it's time to generate the playlist. I've decided to use genre as the "seed" but Spotify allows you to use genre, song, or artist as the seed for getting a recommendation. I have a drop-down with all the genres Spotify offers and in combination with the min and max attribute ranges you can get a recommendation for songs that match.
Finally, now that you have a list of songs you need to actually create the playlist and then add those songs to the playlist. Here again, you'll want to parse through the response from the recommendation and grab the IDs that you will then pass to your newly created playlist. Make sure you capture the returned ID for the playlist you created so you can use it to add the tracks. Also, you will get the URL for the playlist as a part of the response when you create it and you can use that in the output to the user once the songs have been added.
And that's it! I haven't gone into the nitty-gritty details for this one but hopefully, you are able to understand the process. Feel free to try out the Spotify Playlist Generator for yourself!
Sign up to never miss a post!
Sign up to never miss a post!
Want me to cover a specific topic? Let me know!
Leave a comment