SpotifyR
If you want to follow along…
login and create a project to get your client ID and client secret: https://developer.spotify.com/dashboard/login
Run this code:
library(spotifyr)
Sys.setenv(SPOTIFY_CLIENT_ID = ’ ‘) Sys.setenv(SPOTIFY_CLIENT_SECRET =’ ’)
access_token <- get_spotify_access_token(client_id = Sys.getenv(‘SPOTIFY_CLIENT_ID’), client_secret = Sys.getenv(‘SPOTIFY_CLIENT_SECRET’))
Test run:
get_artist_audio_features(“the beatles”)
Basic Setup
Sys.setenv(SPOTIFY_CLIENT_ID = 'ec55060487114a1093d5bd213cec9bab')
Sys.setenv(SPOTIFY_CLIENT_SECRET = '62e0b5abbf3a4ab28a48fb1cd77767be')
library(spotifyr)
access_token <- get_spotify_access_token(client_id = Sys.getenv('SPOTIFY_CLIENT_ID'), client_secret = Sys.getenv('SPOTIFY_CLIENT_SECRET'))Testing some commands
billie <- get_artist_audio_features(artist = "billie eilish")
billie %>%
filter(danceability >= .5, energy >= .3) %>%
select(track_name, danceability, energy, external_urls.spotify)## track_name danceability energy
## 1 bad guy 0.701 0.425
## 2 you should see me in a crown 0.678 0.533
## 3 all the good girls go to hell 0.726 0.444
## 4 wish you were gay 0.853 0.351
## 5 my strange addiction 0.939 0.305
## 6 bury a friend 0.905 0.389
## 7 ilomilo 0.855 0.423
## external_urls.spotify
## 1 https://open.spotify.com/track/2Fxmhks0bxGSBdJ92vM42m
## 2 https://open.spotify.com/track/3XF5xLJHOQQRbWya6hBp7d
## 3 https://open.spotify.com/track/6IRdLKIyS4p7XNiP8r6rsx
## 4 https://open.spotify.com/track/3Fj47GNK2kUF0uaEDgXLaD
## 5 https://open.spotify.com/track/3Tc57t9l2O8FwQZtQOvPXK
## 6 https://open.spotify.com/track/4SSnFejRGlZikf02HLewEF
## 7 https://open.spotify.com/track/7qEKqBCD2vE5vIBsrUitpD
TOP <- get_artist_albums(id = "3YQKmKGau1PzlVlkL1iodx",
include_groups = c("album"))
TOP %>%
filter(album_group == "album") %>%
select(name, release_date, total_tracks, external_urls.spotify) %>%
dedupe_album_names(album_name_col = "name", album_release_year_col = "release_date")## name release_date total_tracks
## 1 Trench 2018-10-05 14
## 2 Blurryface 2015-05-15 14
## 3 Spotify Sessions 2013-06-18 7
## 4 Vessel 2013-01-08 12
## 5 Vessel 2013-01-08 16
## 6 Vessel 2013-01-08 14
## 7 Vessel 2013-01-08 12
## 8 Vessel (with Bonus Tracks) 2013-01-08 16
## 9 Twenty One Pilots 2009-12-29 14
## external_urls.spotify
## 1 https://open.spotify.com/album/621cXqrTSSJi1WqDMSLmbL
## 2 https://open.spotify.com/album/3cQO7jp5S9qLBoIVtbkSM1
## 3 https://open.spotify.com/album/4RTM2aJ9TN4xhk3Qy1DhTV
## 4 https://open.spotify.com/album/213bMr2NI3VDcBLWpkKFxG
## 5 https://open.spotify.com/album/3fo0F5z0ikwpsuv0OSiKPu
## 6 https://open.spotify.com/album/3f57KlLAHuV19bNpn21rvV
## 7 https://open.spotify.com/album/2r2r78NE05YjyHyVbVgqFn
## 8 https://open.spotify.com/album/14onjcwGXJFQnTmmCnTUfQ
## 9 https://open.spotify.com/album/1Fexc96V24RL17Ko9VXUOM
get_genre_artists(genre = "rock") %>%
filter(popularity >= 90) %>%
select(name, followers.total) %>%
arrange(desc(followers.total))## $q
## [1] "genre:\"rock\""
##
## $type
## [1] "artist"
##
## $market
## NULL
##
## $limit
## [1] 20
##
## $offset
## [1] 0
##
## $access_token
## [1] "BQBWCY9thbdWOnr2xX84N_Lu2lFmynVFzIaw7AnXGobc5GDQbfkEwPAgh_3j-nB-VQ-c9IkQuilDvNDz6PE"
## # A tibble: 4 x 2
## name followers.total
## <chr> <int>
## 1 Imagine Dragons 33160729
## 2 Queen 32804621
## 3 Maroon 5 29872621
## 4 The Beatles 18944023
Comparing two similar artists
killers <- get_artist_audio_features(artist = "The Killers")
greenday <- get_artist_audio_features(artist = "Green Day")
kg <- killers %>%
bind_rows(greenday) %>%
select(artist_name, danceability, energy, loudness, speechiness, liveness, tempo, instrumentalness, acousticness)
kg_clusts <-
kg %>%
select(-artist_name) %>%
scale() %>%
kmeans(centers = 3) %>%
fitted("classes")pcagraphkg <- PCA(kg %>% select(-artist_name, -kg_clusts), graph = FALSE)
plot(pcagraphkg, choix = "var")kgclusters <-
kg %>%
mutate(cluster = factor(kg_clusts))
kgplot <-
kgclusters %>%
ggplot(aes(x = danceability, y = energy, name = artist_name)) +
geom_point(aes(color = cluster))
kgplotlattice::cloud(danceability ~ energy * liveness,
data = kg,
screen = list(x = -90,
y = 90,
z = 0),
groups = kgclusters,
auto.key = list(
space = "top",
title = "Green Day vs. The Killers",
cex.title = 1,
columns = 2),
par.settings = latticeExtra::custom.theme(
symbol = viridis::viridis(3),
bg = "gray90", fg = "gray20", pch = 19
))Comparing two very different artists
timmcgraw <- get_artist_audio_features(artist = "Tim McGraw")
lilwayne <- get_artist_audio_features(artist = "Lil Wayne")
tl <- timmcgraw %>%
bind_rows(lilwayne) %>%
select(artist_name, danceability, energy, loudness, speechiness, liveness, tempo, instrumentalness, acousticness)
tl_clusts <-
tl %>%
select(-artist_name) %>%
scale() %>%
kmeans(centers = 3) %>%
fitted("classes")pcagraphtl <- PCA(tl %>% select(-artist_name, -tl_clusts), graph = FALSE)
plot(pcagraphtl, choix = "var")tlclusters <-
tl %>%
mutate(cluster = factor(tl_clusts))
tlplot <-
tlclusters %>%
ggplot(aes(x = danceability, y = energy, name = artist_name)) +
geom_point(aes(color = cluster))
tlplotlattice::cloud(danceability ~ energy * liveness,
data = tl,
screen = list(x = -90,
y = 90,
z = 0),
groups = tlclusters,
auto.key = list(
space = "top",
title = "Tim McGraw vs. Lil Wayne",
cex.title = 1,
columns = 2),
par.settings = latticeExtra::custom.theme(
symbol = viridis::viridis(3),
bg = "gray90", fg = "gray20", pch = 19
))