How to use Spotipy’s API to pull data from Spotify
Tags: Spotify, Data Science, Data Journalism, Python, Data Analysis

For my final project at General Assembly, I’ve chosen to do my project (which I call, “predicting the hits”) on music using Billboard Hot-100 data. The idea of my project is that given lyrics and audio features of a track, can we accurately predict if a new song will make it to the Billboard Hot-100 top 10 list?

As I mentioned in my last post, I’ve already collected 20 years worth of billboard data which will be so much fun to sift through in finding trends. However, this section of my blog is on how we go about getting the audio features which is where Spotipy’s API comes in. This API allows you to access data from Spotify whether it is a track, artist, playlist or even a podcast episode. So let’s get into how you use it!

#Step 1: Get Client Secret and Client ID information.

In order to get this information, you need to create a developer account on Spotify’s developer site. The instructions are pretty clear on how to get the so just follow sign up and it’ll be straight forward from there. You need both the client and secret information to use Spotipy. After you’ve obtained these keys you can then do the following:

Import Spotipy and the authentication code like below and set the client and secret to a variable.

For privacy purposes, I’ve kept my client and secret info a SECRET and so should you. From here, you can access anything you want. Feel free to search through Spotify’s Console to see which endpoints (parameters) you can query .

#Step 2: Write code to pull Hot 100 data from Spotify.

Spotipy has pretty succinct documentation that can help you find out how to navigate Spotify data. For my project, I found out that there’s a Billboard-Hot 100 list on Spotify! So here’s how I was able to pull that info: So let’s say you’re only looking to access Billboard Hot 100 data like me, there’s no instance of being able to review what the previous weeks data is. This presented a bit of a challenge for this project.

Note: Spotify has a limit of 100 results for a specific playlist.

In the code above, I figured I would’ve been able to use a while loop to access past week’s chart data so I created a start and stop date variable. Even though the date column data type is listed as a string, you can’t iterate through it because it isn’t something that Spotify’s API permits. So my next idea, although tedious, will be to include this data manually to my actual Spotify account. Feel free to use the code above and modify it to your liking.

#Step 3: Pull Audio Features.

So as I described above, I’ll only be able to pull 100 audio features max for this current week’s Billboard data. I’ll still go back to create a playlist on my own Spotify that can include more than 100 songs. Continuing from the previous code above, I’ve saved the audio features to a variable and since it is a list, it can be iterated through.

So, this is a brief synopsis of how to collect playlist data from Spotipy. Again, my supposedly “easy” way of collecting more than 100 track ids and features back fired so I may have to go the manual route on this one. I may write another post to show you how I was able to overcome this hurdle. Stay tuned.

Until next time, thanks for reading!