Spotify

As an avid music listener, I love learning about what artists and songs I’ve been listening to and exploring new music. The Spotify Wrapped campaign that started a few years ago is such a great marketing tool, because it gives a recap of what you listened to all year, almost like a box score from a football game. It’s fascinating to revisit your most played songs, artists, and genres.

Spotify API

Fortunately for us music nuts, we don’t have to wait until December every year to get a recap of what we’ve been listening to. Spotify’s Application Programming Interface (API) allows us to interact with our listening data through R. With a number of packages and functions, you can pull data to look at musical characteristics as well as look at your personal listening history to learn about what you like, what you’ve been listening to recently, or what songs you like to binge.

This tutorial will walk you through how to set up an account with Spotify’s API and how to use R to explore your music and listening.

Spotify Developer Authentication

The first thing you have to do is set up an account with Spotify’s developer site to establish a connection. If you already have a Spotify account, all it takes is logging in with your normal login. Once you’ve logged in, click the “Create an App” and enter the required information.

This will take you to your Client ID and Client Secret, which you will need to establish the connection with R. Once you have your ID and secret, we can move into R to begin the process. First, make sure to install the Spotifyr package using install.packages(spotifyr).

Now you can enter your ID and secret to authenticate your credentials. To do this, use system environment variables, and enter your specific ID and secret inside the single quotes. You will also use the access_token command to pull your access token into R. The code should look like the following:

Sys.setenv(SPOTIFY_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxx')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxx')

access_token <- get_spotify_access_token()

The last step of authorization is to setup a callback url. In your developer dashboard, click the “Edit Settings” button and then enter your url under “Redirect URL”. I used a simple default in http://localhost:1410/ .

Analyzing the Data

Now that you are an authorized user, you can begin to use R to pull data from Spotify’s API. The packages you will need include the tidyverse, spotifyr, and lubridate.

What was The Rolling Stones’ Favorite Key

One of the aspects of Spotify data is data regarding the music from each artist in its library. This includes things like musical key and tempo. Using this data, we can find out the most popular keys used by artists. Let’s use The Rolling Stones as an example.

First we have to make an API call to pull data for The Rolling Stones into R. Create a new variable in the environment using the get_artist_audio_features() function below:

stones <- get_artist_audio_features('the rolling stones')

Now that the data is in R, we can use the dplyr functions to explore. Let’s find out which keys The Rolling Stones’ used most in their songs. The code should look something like the code below:

stones %>% 
  count(key_mode, sort = TRUE) %>% 
  head(5)

According to the table that is generated, C major is the most common key used by The Rolling Stones, followed by G major, A major, D major, and E major.

Explore your own listening

Recent Songs

The Spotify API can also be used to pull personal listening data. Spotifyr has plenty of functions to pull data about songs, albums, and artists that you listen to. Let’s use the get_my_recently_played() function to look at the songs that I have most recently listened to. We’ll limit it at 5 songs and we’ll request the track, artist, and album name, as well as when the track was played. The code should look something like this:

get_my_recently_played(limit = 5) %>% 
  mutate(
    artist.name = map_chr(track.artists, function(x) x$name[1]),
    played_at = as_datetime(played_at)
  ) %>% 
  select(
    all_of(c("track.name", "artist.name", "track.album.name", "played_at"))
  ) %>% 
  table()

This returned my most recent songs, which included three songs by the Wombats, one song by COIN, and one song by Milky Chance.

All-Time Artists

You can also use R to look at your all time most listened to artists on your Spotify account. This is done with the get_my_top_artists_or_tracks() function. By specifying that we want to look at artists and limiting to 5, we can look at the most listened to artists in the data. This code looks like this:

get_my_top_artists_or_tracks(type = 'artists', 
                             time_range = 'long_term', 
                             limit = 5) %>% 
  select(.data$name, .data$genres) %>% 
  rowwise %>% 
  mutate(genres = paste(.data$genres, collapse = ', ')) %>% 
  ungroup %>% 
  table()

After running this in my account, my most listened to artists are Childish Gambino, Cody Jinks, Felly, Kanye West, and mike.

Go Explore

Now that you are an authenticated user and have a basic understanding of Spotify’s API and its connection to R, try it out for yourself and learn about your own listening habits. There are so many cool features to explore Spotify’s library and your own music tastes. Enjoy!