Skip to content

womeimingzi11/spotifyr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spotifyr

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

Development version (recommended)

devtools::install_github('charlie86/spotifyr')

The development version now imports the geniusR package from Josiah Parry, which you can install with:

devtools::install_github('JosiahParry/geniusR')

(Note that this is separate from the geniusr package currently on CRAN)

CRAN version 1.0.0 (Note: this is somewhat outdated, as it takes extra time to submit and pass CRAN checks)

install.packages('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()

Usage

What was The Beatles' favorite key?

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 70
A major 65
C major 61
G major 61
E major 60

Get your most recently played tracks

get_my_recently_played(limit = 5) %>% 
    select(track_name, artist_name, album_name, played_at_utc) %>% 
    kable()
#> Auto-refreshing stale OAuth token.
track_name artist_name album_name played_at_utc
Rubu letherette Last Night On The Planet 2018-07-31 14:06:43
dartgun Samiyam Animals Have Feelings 2018-07-31 14:02:48
Dark Sea Onra Chinoiseries 2018-07-31 14:00:18
54 Cymru Beats Aphex Twin Drukqs 2018-07-31 13:58:29
A Paw In My Face The Field From Here We Go Sublime 2018-07-31 13:49:06

Find your all time favorite artists

get_my_top_artists(time_range = 'long_term', limit = 5) %>% 
    select(artist_name, artist_genres) %>% 
    kable()
artist_name artist_genres
Radiohead c("alternative rock", "art rock", "melancholia", "modern rock", "permanent wave", "rock")
Onra c("alternative hip hop", "chillhop", "trip hop", "wonky")
Flying Lotus c("alternative hip hop", "chillwave", "electronic", "glitch", "glitch hop", "hip hop", "indie r&b", "indietronica", "intelligent dance music", "wonky")
Teebs c("abstract beats", "bass music", "chillwave", "indietronica", "wonky")
Four Tet c("alternative dance", "chamber psych", "electronic", "indietronica", "intelligent dance music", "microhouse", "new rave", "nu jazz", "trip hop")

Find your favorite tracks at the moment

get_my_top_tracks(time_range = 'short_term', limit = 5) %>% 
    select(track_name, artist_name, album_name) %>% 
    kable()
track_name artist_name album_name
Fellin' Myself Mac Dre Ronald Dregan For President 2004: Dreganomics
Daydreaming Radiohead A Moon Shaped Pool
Not My Job Mac Dre The Genie Of The Lamp
Myxomatosis Radiohead Hail To the Thief
Not My Job Mac Dre The Genie Of The Lamp

What's the most joyful Joy Division song?

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
These Days 0.949
Passover - 2007 Remastered Version 0.941
Colony - 2007 Remastered Version 0.808
Atrocity Exhibition - 2007 Remastered Version 0.787
Wilderness 0.775
Now if only there was some way to plot joy...

Joyplot of the emotional rollercoasters that are Joy Division's albums

library(ggjoy)
#> Loading required package: ggridges
#> 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.112

Danceability of Thom Yorke

library(magick)
library(lubridate)

tots <- map_df(c('radiohead', 'thom yorke', 'atoms for peace'), get_artist_audio_features)
non_studio_albums <- c('OK Computer OKNOTOK 1997 2017', 'TKOL RMX 1234567', 'In Rainbows Disk 2', 
                       'Com Lag: 2+2=5', 'I Might Be Wrong', 'The Eraser Rmxs')

tots <- filter(tots, !album_name %in% non_studio_albums)

album_names_label <- tots %>% 
    arrange(album_release_date) %>% 
    mutate(label = str_glue('{album_name} ({year(album_release_year)})')) %>% 
    pull(label) %>% 
    unique

plot_df <- tots %>% 
    select(track_name, album_name, danceability, album_release_date) %>% 
    gather(metric, value, -c(track_name, album_name, album_release_date))

p <- ggplot(plot_df, aes(x = value, y = album_release_date)) + 
    geom_density_ridges(size = .1) +
    theme_ridges(center_axis_labels = TRUE, grid = FALSE, font_size = 6) +
    theme(plot.title = element_text(face = 'bold', size = 14, hjust = 1.25),
          plot.subtitle = element_text(size = 10, hjust = 1.1)) +
    ggtitle('Have we reached peak Thom Yorke danceability?', 'Song danceability by album - Radiohead, Thom Yorke, and Atoms for Peace') +
    labs(x = 'Song danceability', y = '') +
    scale_x_continuous(breaks = c(0,.25,.5,.75,1)) +
    scale_y_discrete(labels = album_names_label)

ggsave(p, filename = 'img/danceplot.png', width = 5, height = 3)
#> Picking joint bandwidth of 0.0714
background <- image_read('img/danceplot.png')
logo_raw <- image_read('img/thom_dance.gif')
frames <- lapply(1:length(logo_raw), function(frame) {
    hjust <- 200+(100*frame)
    image_composite(background, logo_raw[frame], offset = str_glue('+{hjust}+400'))
})

image_animate(image_join(frames), fps = 5, loop = 0)

Parallelization

By default, get_artist_audio_features(), get_artist_albums(), get_album_tracks(), get_playlist_tracks(), and get_user_playlists() will run in parallel using the furrr package. To turn this feature off, set parallelize = FALSE. You can also adjust the evaluation strategy by setting future_plan, which accepts a string matching one of the strategies implemented in future::plan() (defaults to "multiprocess").

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.

Dope stuff other people have done with spotifyr

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:

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, Renascença

Hierarchical clustering of David Bowie records - Alyssa Goldberg

tayloR - Simran Vatsa

About

R wrapper for Spotify's Web API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • R 100.0%