library(spotifyr)
library(tidyverse)
library(knitr)Spotifyr Tutorial
Spotifyr
The spotifyr package allows users to retrieve information about artists, songs, and playlists, including the many variables Spotify uses to catalogue them. You can also interact with your own account by retrieving your own listening data, much like the annual Spotify wrapped. Spotifyr even has functionality to manipulate playback by pausing and changing the volume on your listening device.
Client Authentication
Before using the spotifyr package in R, you’ll need to log in to the Spotify developer website with your normal Spotify account.
Once you’ve logged in, create a new app, and select Web API.
After creating the app, you will be returned to your dashboard. Open the app, and navigate to settings on the right to find your Client ID and Client Secret.
Package Installation
Use the install.packages and library commands to install and load the spotifyr package
Logging In
Spotifyr uses default values for many of its commands, and we can set that default to our authorization credentials using the Set Environment command. Alternatively, you can still set your credentials as unique variables but will need to reference them manually with some commands.
Sys.setenv(SPOTIFY_CLIENT_ID = client_id)
Sys.setenv(SPOTIFY_CLIENT_SECRET = client_secret)
access_token <- get_spotify_access_token()Retrieving Data
Using spotifyr’s built-in commands, we can now retrieve information about artists and tracks. The get_artist_audio_features command can create a data frame of an artist’s entire discography and all the unseen variables Spotify uses to categorize and recommend music.
san_fermin <- get_artist_audio_features('san fermin')You can also use the retrieved data to perform further analysis of your favorite artists
san_fermin %>%
count(mode_name) %>%
kable()| mode_name | n |
|---|---|
| major | 71 |
| minor | 30 |
You could even use spotifyr to find new music with get_recommendations and get_related_artists. get_recommendations can utilize up to 5 seeds to determine what songs to put in your new playlist; these can be artists, tracks, or genres.
Artist and track IDs can be retrieved using spotifyr, or copied from the URL of their respective webpages. Like in this link:
https://open.spotify.com/track/6hJQ4kbfV3WsI805UcYx9B
All characters after /track/ or /artist/ are the track or artist ID.
recommendations <- get_recommendations(limit = 20,
seed_artist = '7fSnislKgW9Mz0YIqWQmGt',
seed_tracks = '6hJQ4kbfV3WsI805UcYx9B')