CRAN_Status_Badge

Overview

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!).

Installation

CRAN version 2.1.0 (recommended)

install.packages('spotifyr')

Development version

devtools::install_github('charlie86/spotifyr')

Authentication

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()

Authorization code flow

For certain functions and applications, you’ll need to log in as a Spotify user. To do this, your Spotify Developer application needs to have a callback url. You can set this to whatever you want that will work with your application, but a good default option is http://localhost:1410/ (see image below). For more information on authorization, visit the offical Spotify Developer Guide.

Usage

What was The Beatles’ favorite key?

key_mode n
D major 24
G major 21
A major 13
F major 12
C major 11

Get your most recently played tracks

track.name artist.name track.album.name played_at
Finally We Are No One múm Finally We Are No One 2019-03-19 19:59:23
I Can’t Feel My Hand Any More, It’s Alright, Sleep Still múm Finally We Are No One 2019-03-19 19:54:15
Faraway Swimmingpool múm Finally We Are No One 2019-03-19 19:48:35
Now There’s That Fear Again múm Finally We Are No One 2019-03-19 19:45:38
K/Half Noise múm Finally We Are No One 2019-03-19 19:41:42

Find your all time favorite artists

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, oxford indie, permanent wave, rock
Flying Lotus alternative hip hop, chillwave, electronic, escape room, experimental hip hop, glitch, glitch hop, hip hop, indietronica, intelligent dance music, wonky
Onra alternative hip hop, chillhop, trip hop, wonky
Teebs abstract beats, bass music, chillwave, wonky
Aphex Twin ambient, electronic, fourth world, intelligent dance music, trip hop

Find your favorite tracks at the moment

name artist.name album.name
Illegal Smile John Prine John Prine
Shattered Dreams Earl Sweatshirt Some Rap Songs
December 24 Earl Sweatshirt Some Rap Songs
Cold Summers Earl Sweatshirt Some Rap Songs
Are You a Glacier? Salami Rose Joe Louis Zlaty Sauce Nephew

What’s the most joyful Joy Division song?

My favorite audio feature has to be “valence,” a measure of musical positivity.

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…

Sentify: A Shiny app

This app, powered by spotifyr, allows you to visualize the energy and valence (musical positivity) of all of Spotify’s artists and playlists.