spotifyr is a wrapper for pulling track audio features and other information from Spotify’s Web API in bulk. By automatically batching API requests, it allows you to enter an artist’s name and retrieve their entire discography in seconds, along with Spotify’s audio features and track/album popularity metrics. You can also pull song and playlist information for a given Spotify User (including yourself!).
Development version (recommended)
devtools::install_github('charlie86/spotifyr')
CRAN version 1.0.0 (Note: this is somewhat outdated, as it takes extra time to submit and pass CRAN checks)
install.packages('spotifyr')
First, set up a Dev account with Spotify to access their Web API
here.
This will give you your Client ID
and Client Secret
. Once you have
those, you can pull your access token into R with
get_spotify_access_token()
.
The easiest way to authenticate is to set your credentials to the System
Environment variables SPOTIFY_CLIENT_ID
and SPOTIFY_CLIENT_SECRET
.
The default arguments to get_spotify_access_token()
(and all other
functions in this package) will refer to those. Alternatively, you can
set them manually and make sure to explicitly refer to your access token
in each subsequent function call.
Sys.setenv(SPOTIFY_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxx')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxx')
access_token <- get_spotify_access_token()
library(spotifyr)
beatles <- get_artist_audio_features('the beatles')
library(tidyverse)
library(knitr)
beatles %>%
count(key_mode, sort = TRUE) %>%
head(5) %>%
kable()
key_mode | n |
---|---|
D major | 184 |
G major | 113 |
A major | 76 |
C major | 76 |
A minor | 72 |
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
get_my_recently_played(limit = 5) %>%
mutate(artist.name = map_chr(track.artists, function(x) x$name[1]),
played_at = as_datetime(played_at)) %>%
select(track.name, artist.name, track.album.name, played_at) %>%
kable()
track.name | artist.name | track.album.name | played_at |
---|---|---|---|
Plains | Deerhunter | Why Hasn’t Everything Already Disappeared? | 2019-03-02 23:59:35 |
In the Aeroplane Over the Sea | Neutral Milk Hotel | In the Aeroplane Over the Sea | 2019-03-02 23:57:22 |
Harness Your Hopes - b-side | Pavement | Brighten The Corners: Nicene Creedence Ed. | 2019-03-02 23:54:00 |
Paranoid Android | Radiohead | OK Computer | 2019-03-02 23:50:10 |
1979 - Remastered 2012 | The Smashing Pumpkins | Mellon Collie And The Infinite Sadness (Deluxe Edition) | 2019-03-02 23:47:02 |
get_my_top_artists_or_tracks(type = 'artists', time_range = 'long_term', limit = 5) %>%
select(name, genres) %>%
rowwise %>%
mutate(genres = paste(genres, collapse = ', ')) %>%
ungroup %>%
kable()
name | genres |
---|---|
Radiohead | alternative rock, art rock, melancholia, modern rock, permanent wave, rock |
Onra | alternative hip hop, chillhop, trip hop, wonky |
Flying Lotus | alternative hip hop, chillwave, electronic, experimental hip hop, glitch, glitch hop, hip hop, indietronica, intelligent dance music, wonky |
Teebs | abstract beats, bass music, chillwave, indietronica, wonky |
Aphex Twin | ambient, electronic, intelligent dance music, trip hop |
get_my_top_artists_or_tracks(type = 'tracks', time_range = 'short_term', limit = 5) %>%
mutate(artist.name = map_chr(artists, function(x) x$name[1])) %>%
select(name, artist.name, album.name) %>%
kable()
name | artist.name | album.name |
---|---|---|
Illegal Smile | John Prine | John Prine |
Spanish Pipedream | John Prine | John Prine |
The Bends | Earl Sweatshirt | Some Rap Songs |
Shattered Dreams | Earl Sweatshirt | Some Rap Songs |
Nowhere2go | Earl Sweatshirt | Some Rap Songs |
My favorite audio feature has to be “valence,” a measure of musical positivity.
joy <- get_artist_audio_features('joy division')
joy %>%
arrange(-valence) %>%
select(track_name, valence) %>%
head(5) %>%
kable()
track_name | valence |
---|---|
Passover - 2007 Remaster | 0.941 |
Colony - 2007 Remaster | 0.808 |
Atrocity Exhibition - 2007 Remaster | 0.787 |
A Means to an End - 2007 Remaster | 0.752 |
Interzone - 2007 Remastered Version | 0.746 |
Now if only there was some way to plot joy…
library(ggjoy)
#> Loading required package: ggridges
#>
#> Attaching package: 'ggridges'
#> The following object is masked from 'package:ggplot2':
#>
#> scale_discrete_manual
#> The ggjoy package has been deprecated. Please switch over to the
#> ggridges package, which provides the same functionality. Porting
#> guidelines can be found here:
#> https://github.com/clauswilke/ggjoy/blob/master/README.md
ggplot(joy, aes(x = valence, y = album_name)) +
geom_joy() +
theme_joy() +
ggtitle("Joyplot of Joy Division's joy distributions", subtitle = "Based on valence pulled from Spotify's Web API with spotifyr")
#> Picking joint bandwidth of 0.106
This app, powered by spotifyr, allows you to visualize the energy and valence (musical positivity) of all of Spotify’s artists and playlists.
The coolest thing about making this package has definitely been seeing all the awesome stuff other people have done with it. Here are a few examples:
Exploring the Spotify API with R: A tutorial for beginners, by a beginner, Mia Smith
Sentiment analysis of musical taste: a cross-European comparison, Paul Elvers
Blue Christmas: A data-driven search for the most depressing Christmas song, Caitlin Hudon
KendRick LamaR, David K. Laing
Vilken är Kents mest deprimerande låt? (What is Kent’s most depressing song?), Filip Wästberg
Чёрное зеркало Arcade Fire (Black Mirror Arcade Fire), TheSociety
Sente-se triste quando ouve “Amar pelos dois”? Não é o único (Do you feel sad when you hear “Love for both?” You’re not alone), Rui Barros, Rádio Renascença
Using Data to Find the Angriest Death Grips Song, Evan Oppenheimer
Hierarchical clustering of David Bowie records, Alyssa Goldberg
tayloR, Simran Vatsa
Long Distance Calling: Data Science meets Post-Rock…, Sebastian Kuhn