Spotify

As a lover of music and Spotify, learning about Spotify’s API was incredibly interesting. Most users including myself love seeing their listening statistics every year in their Spotify Wrapped and has gained a lot of traction for the company over the past few years especially on social media. Users often share some of their Spotify Wrapped on Instagram or other platforms. Along with that Artists also get their very own Spotify Wrapped that shows them how many minutes their listeners spent listening to their tracks & how many countries their music was played in. It is intriguing to go back and see if your predictions of your top songs, genres, or artists were true or not.

Spotify’s API

Some benefits that can come from Spotify’s API allow users to show popular trends within the music industry and listeners habits when using the platform. When looking at trends in the music industry as a whole, this would be beneficial to people studying sociology, anthropology, or analysts that work for various record labels. People studying sociology and anthropology could use Spotify’s API to study how music has developed and changed throughout different time periods. As for the record labels, they could find this helpful to see how well their artists are really doing and how they could possibly improve streams based on what chords or genres are the most popular.

Spotify’s API allows users to create their very own Spotify Wrapped, that can be accessed at any point in time and this way you don’t have to wait til the beginning of December to see your stats! Spotify’s API has numerous packages that let you interact with your own listening data within R Studio. Below is a tutorial on how to set up a developer account and a few examples of some of the functions you can use to dive deeper into the music you enjoy listening to.

Authentication Process

To start off, you can create your developer account with this link,
(https://developer.spotify.com/). If you already have a spotify account I recommend using that so you can access your own listening data. After doing this You will be given your client ID and the client secret. This is a random string that is will allow you to access Spotify’s API within R Studio. This will populate on the Spotify Developer page, so you can copy & paste the code chunk below into R and enter in your personal credentials. After doing so, you can access the Spotify token.

Sys.setenv(SPOTIFY_CLIENT_ID = 'bc8ede1e81f44809805b4fff310f18be')
Sys.setenv(SPOTIFY_CLIENT_SECRET = '5200724170f24961a2f49aa5a7206ea7')

access_token <- get_spotify_access_token()

The Last step of the authentication process is to set up your personal call back url. You can find this within the the “Edit Settings” tab on your developer dashboard. Within the “Edit Settings” tab you have the option to create your own call back url or use the basic pre made one, (http://localhost:1410/.) I chose to use pre made one for this tutorial. Once this is done, you are ready to start exploring your listening habits, who your favorite artists are in a certain month and plenty of other interesting statistics! Enjoy!

Analzying the Data & Usage

To start off, we must load in all of the necessary packages. Certain functions within Spotify’s API can be done with certain packages within R, so it is crucial to load in any packages you may need at the start.

library(spotifyr)
library(knitr)
library(tidyverse)
library(lubridate)

Below are a few examples of different packages and functions within the Spotifyr package.

To start off, lets look at one of Joel’s favorite bands, blink-182.

What is blink-182’s favorite key to write a song in? Here we are counting the various key’s blink-182’s songs are written in and limiting it to the top 5 keys.

blink182 <- get_artist_audio_features('blink-182')

blink182 %>% 
  count(key_mode, sort = TRUE) %>% 
  head(5) %>% 
  kable()
key_mode n
C major 88
A major 78
D major 69
B major 62
G major 47

Here we can see that blink-182’s most popular key to write and perform a song in is C major with 88 songs.

Next, I wanted to look at some of my recently played songs. This is a feature within the Spotify API that in more focused on the users data rather than the Spotify library as a whole. Here we are pulling 10 recently played tracks. We are selecting the track name, the artist, the name of the album and the time the song was played at.

newly_played <- 
  data.frame(get_my_recently_played(limit = 10) %>% 
  mutate(artist.name = map_chr(track.artists, function(x) x$name[1]),
         played_at = as_datetime(played_at)) %>% 
  select(track.name, artist.name, track.album.name, played_at))

kable(newly_played)
track.name artist.name track.album.name played_at
I Love This Part The Wrecks I Love This Part 2022-03-31 17:39:17
make up sex (feat. blackbear) Machine Gun Kelly mainstream sellout 2022-03-31 17:36:13
Westcoast Collective Dominic Fike Don’t Forget About Me, Demos 2022-03-31 17:34:04
Sleeping with Roses Chelsea Cutler Sleeping with Roses 2022-03-31 17:32:16
Skin Dijon Skin 2022-03-31 17:30:56
i-70 Jeremy Zucker CRUSHER 2022-03-31 17:27:05
the lifeboat’s empty! Chelsea Cutler the lifeboat’s empty! 2022-03-31 17:22:20
When I Close My Eyes Chelsea Cutler When I Close My Eyes 2022-03-31 17:19:26
MEMORIES! 347aidan TROUBLED MEMORIES! 2022-03-31 17:16:34
In My Head (feat. Travis Barker) 24kGoldn In My Head (feat. Travis Barker) 2022-03-31 17:04:26

Here we can see a list of my 10 recently played songs. You can see some repeated artists, like how I listened to 3 Chelsea Cutler Songs. This is just a quick snapshot into some of my listening habits. You can adjust this to look at more or less than 10 songs at one time. You could then possibly take it a step further and look at your recently played over the last few months as well.

Lastly, to save this data you just pulled from the Spotify API, you can use this “write.csv()” function to save the data to your computer.

write.csv(newly_played, file = "Spotify Homework 5 Data.CSV")

The End

And that’s a little tutorial of how to use Spotify’s API. Feel free to continue to explore your listening habits or your friends before you get to see your own Spotify Wrapped! Enjoy!