devtools::install_github('charlie86/spotifyr')
library('spotifyr')
library('tidyverse')
library('knitr')Spotify API
Spotify API
spotifyr
Overview
The R package spotifyr allows us to obtain information from Spotify’s Web API. This enables us to pull information about songs and artists, as well as user listening behaviors. Using this, you can obtain information about your own Spotify information!
Installation
Begin by installing and loading the necessary packages. Here, some other packages can be loaded that will be useful in the examples
Create Developer Account
To access Spotify’s Web API, you must first create a Developer account. This can be done here: https://developer.spotify.com/.
You will then login to the Spotify account you would like to obtain information from. After logging in, create a new app. Under “Redirect URI”, you can use: http://localhost:1410/. When asked which API/SDKs you are using, select “Web API”.
After completing these steps, go to settings. Here you will find you Client ID and Client Secret, which are needed to authenticate.
Authentication
After obtaining your Client ID and Client Secret, you can use these to pull your access token into R. This can be done using the code below (replacing the values between ’ ’ with your information):
client_id <- Sys.setenv(SPOTIFY_CLIENT_ID = ‘xxxxxxxxxxxxxxx’)
client_secret <- Sys.setenv(SPOTIFY_CLIENT_SECRET = ‘xxxxxxxxxxxxxxx’)
access_token <- get_spotify_access_token()
API Usage Examples
One example of what exactly can be done with this is pulling data about specific artists or songs. Below, the provided code shows what keys my favorite band, Rascal Flatts, uses most often.
What is Rascal Flatts’ favorite key?
library(spotifyr)
rascal_flatts <- get_artist_audio_features('rascal flatts')library(tidyverse)
library(knitr)
rascal_flatts <- get_artist_audio_features('rascal flatts')
rascal_flatts %>%
count(key_mode, sort = TRUE) %>%
head(5) %>%
kable()| key_mode | n |
|---|---|
| D major | 38 |
| E major | 38 |
| C major | 32 |
| G major | 27 |
| G# major | 21 |
The results show that the most used key by Rascal Flatts is D major!
The spotifyr package contains many functions that can be used to view data about specific Spotify users. Using this API, you can even create your own “year-in-review” dataset.