# library(tidyverse)
# library(jsonlite)
# library(magrittr)
# library(spotifyr)
# library(httpuv)
# library(knitr)
# library(readr)Assignment 5 - API Data (Spotify)
Assignment 5 - API Data (Spotify)
1. Explain the service you intend to demonstrate and why someone would want to use it.
The service I will be demonstrating is the spotifyr package. This package pulls information from Spotify's Web API. You are able to explore artist’s discography data and streaming metrics by searching their name. You also can pull data from Spotify users and can look at your own playlist and song information.
2. Walkthrough the steps for setting up the API both in R and on the service’s website (if necessary.)
Visit Spotify’s Developer website:
https://developer.spotify.com/Sign into your personal Spotify account in order to to access their Web API
Click create app and fill out the following:
App Name
App Description
Website (I used
https://www.xavier.edu/)Redirect URL (I used this default option
http://localhost:1410/)Which API/SDKs are you planning to use (I did not click any)
Agree to their terms
You now have access to a client ID and client secret, now we can start using R to access the API
First, upload all of the packages you may need. Be sure to include the spotifyr package!
- Next, in order to authenticate, set your credentials by inserting the Client ID and Client Secret you were granted into the system environment variables:
# id <- 'XXXXXXXXX'
# secret <- 'XXXXXXXXX'
# Sys.setenv(SPOTIFY_CLIENT_ID = id)
# Sys.setenv(SPOTIFY_CLIENT_SECRET = secret)- In order to get access to your access token, use the function from the spotifyr package:
# access_token <- get_spotify_access_token()
# auth <- get_spotify_authorization_code(client_id = Sys.getenv("SPOTIFY_CLIENT_ID"),
# client_secret = Sys.getenv("SPOTIFY_CLIENT_SECRET"),
# scope = scopes()[c(7,8,14,15)])3. Give a brief demonstration of at least one of the features by making a call to the API.
One of the awesome features of this package is getting access to artists discography, streaming metrics, and Spotify’s audio features. To access an artist’s Spotify track audio features, I used the ‘get_artist_audio_features’ feature. I used this to look at Taylor Swift’s track audio features.
# tswift <- get_artist_audio_features('taylor swift')I am using this feature to test the liveliness trait. This trait tests the presence of an audience in the recording. A value above 0.8 provides strong likelihood that the track is live.
# tay_live_tracks <- tswift %>%
# arrange(desc(liveness)) %>%
# select(track_name, liveness) %>%
# head(10)
# print(tay_live_tracks)After looking at these results, I can tell that the liveliness trait is very good at telling whether or not the track has a live audience. You can tell that the ‘Better than Revenge’ crowd was exceptionally into the performance.