#Spotify API Tutorial#

This is brief tutorial for how to set up the spotify API and run some basic analysis within it. I chose to use this API because I wanted to gain access to some information on different songs. In particular, I will be doing a brief analysis on Taylor Swift to demonstrate the various unique features spotify uses to generate cool recommendations for their listeners.

This service could be particularly interesting to someone who wants to learn more about music and the trends in popular songs or even just to gain a better understanding of their own listening behaviors.

#Required Packages#

For this tutorial, you will need to install the package spotifyr from Cran. This package allows you to pull data using the Spotify API with a few functions such as “get_artist_audio_features”. Additionally, you may want to consider using the tidyverse when running your analysis.

library(spotifyr)
library(tidyverse)

#Creating an Account and Authentiation#

Before going and authenticating the API you must make a developer account on Spotify. Use this link to get the developer website: https://developer.spotify.com/

Note: You can do this by using your current spotify login also. Note: You must agree with the spotify developer terms of service to get a developer account.

After creating the developer account, make an “APP” and tell spotify what you intend to do. Once this is completed you will have access to the access to the Client ID and Client Secret ID numbers you see below. Once you get these numbers you will be able to return to R.

Note:If you hit rotate on the client secret number you will need to reenter it into your R code for the data API calls to work. , you must download the spotifyr package for the following commands to work.

Sys.setenv(SPOTIFY_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxx')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'xxxxxxxxxxxxxxxx')

access_token <- get_spotify_access_token()

Analysis

taylorswift <- get_artist_audio_features('Taylor Swift')

This API call grabs all the songs that Taylor Swift was featured on! If Taylor Swift gets on a new song and it is released to spotify then one could rerun this command and get a new data frame with the new songs! This command can also be used to get personal listening information like playlists etc.If you wanted to look up another artist you would just change the artist name from ‘taylor swift’ to the name you would like to view.

How many songs did Taylor work on in each Major?

taylorswift %>% 
  count(key_mode, sort = TRUE) %>% 
  head(5) %>% 
  kable()
key_mode n
G major 224
F major 156
C major 151
E major 138
D major 135

#What is the average tempo in a taylor swift song###

mean(taylorswift$tempo)
## [1] 120.877

I found that on average tempo to be around 120! This seems very fast. I expected Taylor Swifts music to be more slow but this is because I only listen to her country music.

A Scatter Plot of the songs danceability and energy

taylorswift %>% 
  ggplot(aes(danceability,energy, color= explicit))+
  geom_point()+
  xlab("Danceability")+
  ylab("Energy")+
  ggtitle("A Scatter of Danceability by Energy")

I see that there is no correlation between explicit songs that Taylor Swift is featured on being more danceable or energetic. I would expect to see some correlation if more artists were factored into this graph.