| track_name | valence |
|---|---|
| Shine On Top | 0.964 |
| Come With Me | 0.931 |
| Brand New | 0.914 |
| 24 / 7 / 365 | 0.904 |
| It Only Takes Two | 0.858 |
| Let Me Know | 0.844 |
| Malibu Interlude | 0.836 |
| Good Day | 0.815 |
| On Time | 0.808 |
| Yours | 0.799 |
Spotify API Interfacing - Class Turn-In
Background and Possible Uses
Spotify is an audio streaming platform that hosts both music and podcasts. From a data perspective, they are a gold mine for anyone interested in music analysis, or really anyone who just wants to screw around with a massive dataset. You can do a plethora of things through this API, including, but not limited to:
Retrieving data from your favourite artist, album or show. (what I’ll be doing here)
Searching for Spotify content.
Controlling and interacting with the playback, play and resume, Seek to a position or retrieve your queue.
Managing your personal library, by creating a new playlist and adding your favorite tracks to it.
Personally, I think that’s pretty cool. You can control your playback through this API! You may not be a hit at parties for knowing how to modulate your playback via an API, but it’s still a cool thing to be able to do.
Setting Up
To set up the use of the Spotify API, you will have to take a trip to https://developer.spotify.com/dashboard, which will allow you to create an app to interface with the API. The steps to do so are as follows:
Log into the dashboard using your Spotify account
Select “Create App” and fill out the necessary fields.
- Under “Redirect URI”, you can really do any http://localhost URI. I used http://localhost:2024
Select “Web API” where the form asks what kind of API you are going to use
Agree to the TOS and save the app.
When in your dashboard, click on “Settings” in the top right to find your Client ID and Client secret. Both will need to be on hand to interface with the API.
After you have created your app and have your Client ID and secret at the ready, then you will need to use the following code in R:
library(tidyverse)
library(spotifyr) # Necessary to interact with the Spotify API.
library(knitr) # Prettier table outputs
Sys.setenv(SPOTIFY_CLIENT_ID = "YOUR_CLIENT_ID") # Include your own ID in the quotes
Sys.setenv(SPOTIFY_CLIENT_SECRET = "YOUR_CLIENT_SECRET") # Include your own secret in the quotes
access_token <- get_spotify_access_token()After this setup, you will be ready to interface with the Spotify API.
Usage Examples
You can use this API to get information about your favorite artists. Personally, I like Surfaces. Many of their songs are more upbeat and joyful, which Spotify actually has a variable for: valence. Using this variable, a very simple example of how we can use this API is by finding the most upbeat songs Surfaces makes. The following code would produce this:
surfaces <- get_artist_audio_features('surfaces')
surfaces %>%
arrange(desc(valence)) %>%
select(track_name, valence) %>%
head(10) %>%
kable()Something that’s nice about this is that there are songs I haven’t heard yet! Might add them to my playlist.