Spotify

I chose to take a dive into Spotify’s Web API. It functions similar to what was covered in class, but uses a different third-party wrapper library named “spotifyr” and allows you to examine not only public data, but also user-specific and private data such as top songs, artists, and more.

Initial Setup

We can first begin by loading the initial helper libraries using:

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(dplyr)
library(httpuv)

Utilizing Spotify’s API is fairly simple, and only requires one package on top of the defaults. This can be installed and loaded with:

library(spotifyr)

After installing this package, you have to log in and create a project on Spotify’s API Site to receive a client id and secret key, using the following:

Sys.setenv(SPOTIFY_CLIENT_ID = '7d869385b48d421fb693628e01b81a65')
Sys.setenv(SPOTIFY_CLIENT_SECRET = '8d77b121c9e04bf583cc16c7f47f22a0')

access_token <- get_spotify_access_token()

After establishing the key, the public API will become visible. If you wish to access user-centrist or private data, you must add an OAuth callback link to the Spotify project (http://localhost:1410/) and authenticate the account you wish to use.

There are a lot of features / things to look at, ranging from aggregated global lists to individual song features (sentiment, notes used, genre, etc). Here’s an example question and response that represents how easy it is to use:

Q1: Do I listen to more singles, or album songs?

my_top_twenty = get_my_top_artists_or_tracks(type= 'tracks', limit = 50)
my_top_twenty %>%
  ggplot(aes(x = album.album_type)) +
  geom_bar(color="black") +
  theme_minimal()