Spotify API Directions and Demonstration
Spotify Explanation and API Uses
Spotify is a music ware house that digitally stores anything from music to podcast. It has an extensive amount of data available to the public with an easily available API for use. This could be used in many different ways including, but not limited to the following:
Artists tracking their songs and what data may be fueling a song’s success or failure
Producers looking to sign new artists that match their currently contracted artists or evaluating artists they currently have a contract with
Listeners that are interested in the type of music they listen to and want to summarize their Spotify use
Anyone that likes playing with data will find a home here as well
A longer and more descriptive list of the data available can be found here: https://count.co/report/kGJ3skS05g5?frame=gDqunwVZxVp
API Set-up
Spotify
A personal API can easily be set up at https://developer.spotify.com/documentation/web-api (This page also gives a detailed description to echo my words).
Steps:
Log in to your Spotify account in the top right corner
Click on “Dashboard” or go to https://developer.spotify.com/dashboard
Click on “Create app” in the top right
Enter the App name of your choosing (Example: “BAIS APP”)
Enter your App description (Example: “Spotify App for BAIS Class”)
Enter in a Redirect URI for user after authentication (Example given: “http://localhost:3000” - Does not need to be a specific working URI)
Check the box labeled Web API under “Which API/SDKs are you planning to use?”
Check “I understand and agree with Spotify’s Developer Terms of Service and Design Guidelines”
Save
Click on settings in the top right to view Client ID and press “View client secret” for your client secret
R
To use this API in R you just need a few packages and a few lines of code. They are as follows:
# Load packages
library(tidyverse) # All the tidy things
library(spotifyr) # Needed for Spotify
library(httpuv) # needed for some potential uses of spotifyr
#set system value
Sys.setenv(SPOTIFY_CLIENT_ID = 'YOUR_CLIENT_ID') Sys.setenv(SPOTIFY_CLIENT_SECRET = 'YOUR_CLIENT SECRET')
access_toekn <- get_spotify_access_token()
Now you are ready to use the Spotify API and access any of your personal data as well as any data generated for the public.
Demonstration
The following shows an example of calling all songs from the artist “Green Day”. That data is then simply used in order to count the amount of songs corresponding to the key of the song and ranking their ten fastest songs, by tempo.
green_day <- get_artist_audio_features('green day')
# Green Day's songs by key
green_day %>%
count(key_mode, sort = TRUE)
# Green Day's top 10 fastest tempo songs
green_day %>%
arrange(-tempo) %>%
select(track_name, tempo) %>%
head(10)
You can also call a whole playlist, whether it is a local playlist or public. The code for this is as follows.
# play list ID for the top-50 global songs
# This id can be found as the last portion of the html https:open.spotify.com/playlist/37i9dQZEVXbMDoHDwVN2tF
`playlistglobal50_playlist_id <- “37i9dQZEVXbMDoHDwVN2tF”
top_50_global_playlist <- get_playlist_tracks( global50_playlist_id, fields = NULL, limit = 50, authorization = get_spotify_access_token(), include_meta_info = FALSE)`
------
These are simple uses of the Spotify API, but this gives a good glimpse into many possibilities.