Sound effects

Creating song lists with Spotify API

Let the music play on

The Spotify Developer API is a treasure trove of statistical information on just about any song one can think of and this page is a quick walk through on how to use it to retrieve playlist and song data. It is relatively easy to use and I employed it to curate song lists to form the baseline for data analysis of lyrics. The code is in Python and can be found here: sound_effects.

vinyl
Let the music play on!

Get your credentials on

The first step in gaining access to Spotify is to open a developer account. During signup, you will be asked for a website address suggesting that this is a requirement. In reality, this does not appear to be enforced and the system accepted 'localhost' instead of an actual web application returned needed credentials, namely 'clientId' and 'clientSecret'. These are the keys to completing the first step of the authentification process.

Authentication

Spotify uses a two step process authentication. Phase one involves turning over the developer account credentials along with conversion between string and bytes which in turn produces a "token" as part of the response from the API. This token gives the developer the "keys to the kingdom" for one hour after which time access expires and one must go through the process again. For the purposes of this project, every time the script runs, a new token is generated and submitted rather than setting it up on a timer to execute after expiration.

spotify api
Authentication

JSON Response

The JSON response is substantial. To get an idea of what might be helpful and avoid wading through hundreds of lines of data for each query return, the documentation provides an opportunity to explore formats here.

In my case, I was interested in obtaining playlists that I could in turn loop through to pull individual song titles, and the code is set up to access 50 playlists at a time. The script returned over 2,000 song titles for "Country Romance" and 934 for "Hip Hop Romance" without duplicates in a CSV.

json response
JSON response snippet

The next step

The "sister" project continues what might be a natural progression for music lovers and retrieves lyrics from the Genius Developer API for the purpose of natural language processing and the repository for this code is here: cupid_shuffle.

Thanks for reading this far, and I hope this helps with using the API. Please feel free to clone or fork the repository and make it your own. If I can be of assistance, please do not hesitate to reach out. I can be contacted through the link in the footer.