Automate the Process of Adding Your User’s Saved Tracks by Genre to Playlists on Spotify
By: Cristóbal Veas
Tags: Python, Editors Pick, Music, Spotify, Data

Stop Adding Tracks to Your Playlists on Spotify One by One!!.

Photo by Puria Berenji on Unsplash

I consider myself a music lover, I usually spend most of the time in a day listening to music while I’m doing routine activities. As a consequence of this, I keep playing many different playlists, and when I like a new track I instantly saved it into my User’s Library on Spotify. But the main problem of doing this is that when the year is ending I always finish having a lot of music on my User’s Library, which instead of this, I could save all this new music on different playlists depending on the music genre of each track.

That’s why I decided to create a script on Python that automates all this process, so instead of wasting time in saving song by song on my playlists, the script will execute this process saving the tracks by Genre on my different Playlists on Spotify.

In this article, I will explain how works this script and the main concepts to understand how to use it. So let’s go-to action!!.

You can download all the code and files on my GitHub Repository.

1. Spotify Credentials

To manipulate your User’s Library and Playlist you have to use the Spotify API. First, you need to access this page, click on the dashboard section, log in with your Spotify account, and create a new app. Complete the information required and then you will obtain your Client Id and Client Secret to use the API. You will need the following values to use all the tools on Spotify required for this process:

Client Id : To obtain, create a new app on Spotify for Developers page.

: To obtain, create a new app on Spotify for Developers page. Client Secret : To obtain, create a new app on Spotify for Developers page.

: To obtain, create a new app on Spotify for Developers page. Username : To obtain, go to your profile on Spotify Desktop App, click on share and copy Spotify URI.

: To obtain, go to your profile on Spotify Desktop App, click on share and copy Spotify URI. Redirect URL: Specify a URL to redirect when the Spotify API approves your credentials. I usually use the localhost. “http://localhost:8888/callback”

Once you have all these values, you need to save them into the variables CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, and USERNAME located on the file utils.py.

You will notice the file genre-analysis.ipynb have a function called credentials with some variables, these variables are:

sp : Authentification to access the data and features of tracks on Spotify.

: Authentification to access the data and features of tracks on Spotify. spt: Authentification to access your personal library on Spotify.

Authentification to access your personal library on Spotify. spp: Authentification to manipulate your personal playlists on Spotify.

2. Downloading the Data of Saved Tracks

The second part of this article consists of downloading the following features of your user’s saved tracks:

id : The Spotify’s id of the track.

: The Spotify’s id of the track. name: The title of the track.

The title of the track. album: The album’s title of the track.

The album’s title of the track. artist: The artist’s name of the track.

These features will use when you need to obtain the music genres of the tracks. Then you will obtain all these features executing the functions get_saved_song_ids and get_features_saved_songs. In my case, I obtained the 223 tracks that I saved on my User’s Library displaying in the following DataFrame:

DataFrame with Tracks Saved on Spotify (Photo by Author)

2.1. Bonus: Downloading Tracks from Playlists

I expanded the scope of the function get_saved_song_ids to include an option to download the track ids of any playlists. If you want to use it you just need to aggregate the parameters defined below:

playlist_id : the Id of the playlist you want to download. To obtain open Spotify Desktop App, search your playlist, click on share and copy Spotify URI. You have to remove the part in bold of the character as follows: [spotify:playlist:] 3u1KRDahOx5ZNKWXBVAAVd

: the Id of the playlist you want to download. To obtain open Spotify Desktop App, search your playlist, click on share and copy Spotify URI. You have to remove the part in bold of the character as follows: 3u1KRDahOx5ZNKWXBVAAVd is_playlist: You should specify “True” so the function will understand you are passing information about a playlist instead of User’s Saved Tracks.

3. Finding the Music Genres of Saved Tracks:

The Spotify API doesn’t have any feature related to tag the music genre for each track. Instead of this, the API tags music genres for artists and albums. That’s why we need to store all the information above for each track.

So I created a function called finding_music_genre_on_spotify and you need to pass as a parameter, the data frame, and the required authentification. It’s important to mention that the function takes a bit of time to finish its process. The function will create a new column called “genre” with all the music genres associated with the album or artist for each track. I obtained the next information:

DataFrame with Tracks and Music Genres on Spotify (Photo by Author)

Finally, to prevent any kind of null values for music genres, I created a code that uses the music genres based on the Data Frame for tracks that have the same artist or album.

3.1. Bonus: Finding Music Genres on Wikipedia

In case you couldn’t find all the music genres for your tracks using the function above, and you still having null values, I created another function called find_music_genre_on_wikipedia. The function does the same process as the other function but searching for information on Wikipedia and comparing the results with the music genres on the file musicgenresdata.txt, to extract the words related to the specific music genre for each track.

4. Adding The Saved Tracks to your Playlists!!

Finally, with all the music genres found, you can start adding your tracks to your target playlists. In my case, I created 4 playlists, so I had to obtain the Spotify URI for these 4 playlists. The playlists are:

Metal Playlist.

Rock Playlist.

Pop Playlist.

Chill Playlist.

With the function add_music_to_playlist_genre, you can add all the tracks you want by the genre of those tracks. This function has the following parameters:

df : The data frame with all the information of tracks.

: The data frame with all the information of tracks. username : The Spotify URI of your username (defined on Spotify Credentials Section of this article).

: The Spotify URI of your username (defined on Spotify Credentials Section of this article). genre : the genre of tracks you want to add.

: the genre of tracks you want to add. playlist_id : the Spotify URI of your target playlist.

: the Spotify URI of your target playlist. spp: The authentification for playlists manipulation (defined on Spotify Credentials Section of this article).

It’s important to mention 2 requirements when adding tracks on playlists:

You must to “ make public ” your target playlist (go to your playlist, clicking on share, and then on make public).

” your target playlist (go to your playlist, clicking on share, and then on make public). The add_music_to_playlist_genre returns a new data frame with the removal of the information for the tracks you added to the playlist. The reason is to prevent duplicated tracks on different playlists because some tracks have more than 1 music genre.

In my case, I added all the Tracks with Music Genre “Metalcore” to the Metal Playlist. I also Added “Art Rock” tracks to Rock Playlist and “Ambient” to Chill Playlist.

I recommend you to save the data frame after adding some tracks to different playlists, so in case you have a lot of tracks on your User’s Library, you won't have duplicate songs each time you decided to run the script!.

I hope this article could help you to automate your Spotify Desktop App and also to expand your knowledge and seeking for new ideas to keep automating tasks using Python and Data Analysis tools.

If you want to listen to the Rock Playlist that I created using this script, you can access it by clicking the link below.

Other of My Articles:

Gain Access to Expert View — Subscribe to DDI Intel