This is a general tutorial on how to access and use Spotify API. In this tutorial we will we looking at my own spotify account and seeing my listening history over the past year. I will be showing my top artists and songs that I have listened to the most in the past year. I will also demonstrate how to analyze an artists discography when looking at different aspects like feeling from the song and danceability. ## Required Pacages First we will have to install some packages to make the analysis of the listening data
library(httpuv)
library(tidyverse)
library(spotifyr)
library(knitr)
library(dplyr)
library(ggplot2)
To access the spotify API you have to go to the website Spotify for Developers (developer.spotify.com). You can create an account or use your own spotify account for the information. After creating an account, you will create an app and enter in the proper information in order for the app to work. After creating the app, you can click on the settings and your API ID will be there. There is also a link to click that will show you the Client Secret ID. Both of these are necessary to have access to the API. The code to enter to gain access is below.
##Sys.setenv(SPOTIFY_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxx')
##Sys.setenv(SPOTIFY_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxx')
##access_token <- get_spotify_access_token()
The access token allows you to have have the ablity to spotifys data. When getting your own data the code is a little different. This is used for when you are running queries to get your top artists of the year or songs of the year, you are sending a request to the Spotify account to use that data. Once you run this code, there will be a prompt for you to login to your own spotify account
After entering in all the required information from Spotify and getting authentication into your account you are ready to start getting data. I will start by showing my top artists of the last year. Below is the code that I entered. This code is sending a request to spotify for my data.
topartist <- get_my_top_artists_or_tracks(type = "artists", limit = 10, offset = 0, time_range = "long_term",authorization = get_spotify_authorization_code())
topartist %>%
select(name, popularity) %>%
kable()
| name | popularity |
|---|---|
| Kanye West | 91 |
| Tyler, The Creator | 89 |
| Drake | 97 |
| Mac Miller | 84 |
| Lil Uzi Vert | 88 |
| A$AP Rocky | 83 |
| Chance the Rapper | 75 |
| JAY-Z | 85 |
| Travis Scott | 90 |
| Kid Cudi | 81 |
In the command, I have to define what type of data that I want, and I put the limit to 10 and choose a time range for longterm (one year). The table lists my top ten artists of the year and their popularity.
I wrote the same command with the change of type to Tracks. It displays a table of my top songs of the year
toptracks <- get_my_top_artists_or_tracks(type = "tracks", limit = 10, offset = 0,
time_range = "long_term")
toptracks %>%
select(name, popularity) %>%
kable()
| name | popularity |
|---|---|
| 5AM | 48 |
| Can I Kick It? | 71 |
| Fashion Killa | 72 |
| JoHn Muir | 55 |
| Tell Em | 80 |
| Mind Playing Tricks on Me | 67 |
| Excuse Me | 62 |
| Under The Sun (with J. Cole & Lute feat. DaBaby) | 74 |
| Right Above It | 70 |
| Memphis, Pt. 2 (feat. NLE Choppa and A$AP Ferg) | 6 |
The table shows my top songs and the popularity of each of them. I wanted to look at this to see the variety that I have in my music taste in the last year. I was able to see that some of the songs that I listen to are not as popular as the others.
I decided that I wanted to look further into one of my favorite Artists. I choose Chance the Rapper because he was my second favorite artist of the year but he is always one of my top favorite. I wanted to see how his music has changed over the years and if each album had a different valence. I ran a command that allowed me to have access to Chance the Rapper’s discography and it gave me many different variables of the songs. I also cleaned the data from duplicate albums.
Valence is a term that is used in the Spotify API that measures the feeling that someone has after listening to the song. The scale is from 0-1, songs with higher valence (.6-.9) are happy, cheerful, and positive and songs with lower valence (.5 and lower) are sad or depressed. In this table we are going to look at songs that have the highest Valence and compare their danceability to them.
CleanChance %>%
select(track_name, danceability, valence) %>%
arrange(desc(valence)) %>%
head(10) %>%
kable()
| track_name | danceability | valence |
|---|---|---|
| NaNa | 0.594 | 0.891 |
| Pusha Man | 0.768 | 0.883 |
| Family For | 0.498 | 0.832 |
| The Big Day | 0.654 | 0.822 |
| Favorite Song | 0.725 | 0.811 |
| Nostalgia | 0.612 | 0.810 |
| Juice | 0.759 | 0.804 |
| No Problem (feat. Lil Wayne & 2 Chainz) | 0.652 | 0.788 |
| Long Time | 0.392 | 0.773 |
| Ballin Flossin | 0.926 | 0.766 |
In the top 10 songs with the highest valence, we can see that it varies for each song but some of them are close to eachother. I also created a scatter plot to see more songs and how they compare with danibility.
In the graph the green dots mean that the song has a high Valence and the red is a low Valence. On the right of the plot we can see that their is a similar amount of songs that have a high danceability. I found it interesting that there were so many songs with low valence that are danceabilty because I originally thought that a happier song would lead to a higher danceability.
I wanted to look at each album’s average danceability to find which one is made for dance. I did this by getting the average of each album. I displayed them in a table to get a good comparison.
| album_name | danceability |
|---|---|
| The Big Day | 0.7313636 |
| Merry Christmas Lil Mama: The Gift That Keeps On Giving | 0.6803529 |
| Acid Rap | 0.6455714 |
| 10 Day | 0.5934286 |
| Coloring Book | 0.5895714 |
I found that The Big Day was the most danceable album out of his discography.
The graph gives a better look at the danceability of each album.