Like many music enthusiasts, the most used app on my phone by far is Spotify. One of my favorite features is their daily or weekly curated playlists based on your listening tastes. Spotify users can get as many as six curated ‘Daily Mixes’ of 50 songs, as well as a ‘Discover Weekly’ of 30 songs updated every Monday. That’s more than 2k songs a Spotify user will be recommended in a given week. Assuming an everage of 3 minutes per song, even a dedicated user would find themselves spending more than 15 hours a day to listen to all of that content. That…wouldn’t be healthy.
But Spotify’s recommendations are good! And I always feel like I’m losing something when these curated playlists expire before I can enjoy all or even most of the songs they contain. Or at least I did, until I found a way around it.
In this articule, I’m going to take you through Spotify’s API and how you can solve this problem with some beginner to intermediate Python skills.
Introduction to Spotify’s API
Spotify has made several public APIs for developers to interact with their application. Some of the marketed use cases are exploring Spotify’s music catalogue, queuing songs, and creating playlists.
You can credential yourself using this
documentation guide. I’d walk you through it myself but I don’t work for Spotify and I want to get to the interesting stuff.
In the remainder of this article I will be talking leveraging
Spotipy, an open source library for python developers to access Spotify’s Web API.
NOTE: At the time of writing, Spotipy’s active version was 2.22.1, later versions may not have all of the same functionality available.
Creating a Playlist
The first step will be one of the easier things you can do with this library which is create a new playlist. We’ll use this playlist to hold all of the songs that get recommended to me permanently (or until I decide what to do with them). For this you can use the user_playlist_create() function. You will need to pass in a few parameters like name and whether you want it to be public and collaborative.
Accessing Curated Playlists
The next step will be to pull a curated playlist and get a list of the tracks in that playlist. For this we will use Spotipy’s search() function which takes as input the name of the entity and the table you are searching. The result will return in a JSON format so you’ll have to unpack it to get to the details.
Adding Tracks to Playlist
The final step will be to add the tracks to your newly created playlist. For this we will loop the Track IDs from the curated playlist into an initiatilized list that we will then add to our created playlist using Spotipy’s playlist_add_items() function. Feel free to use my method below: