Spotify Data from APIs

Spotify is a digital music, podcast, and video service that gives you access to millions of songs and other content from creators all over the world.

R and API Walkthrough

First I installed and loaded 6 different packages that are necessary for me to get a Spotify API. These six packages are:

library(spotifyr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(httpuv)
library(knitr)
library(lubridate)
library(purrr)

Web Tutorial

First, visit the website: https://developer.spotify.com/documentation/web-api. Once you arrive log into your Spotify account.

Second, go to your dashboard and click on “Create App” (you may have to verify your email first)

Third, once you click on “Create App” give your app a name and a description. After go down to Redirect URL, this can be anything but it has to be filled out. I just used the default that Spotify gave me. After you have a redirect URL keep scrolling down and select the box that says “Web API”. Then you can save.

Lastly, find the settings button on the top right corner, and take note of your client id and client secret.

R Code

To set up your API on R use the following R code. (Type in your own ID and Secret that you got on the Spotify website.

#Sys.setenv(SPOTIFY_CLIENT_ID ='')
#Sys.setenv(SPOTIFY_CLIENT_SECRET = '')
#access_token <- get_spotify_access_token('')

Example

I wanted to find out what key Jimmy Buffet has his songs in so I ran the following code:

jimmybuffet <- get_artist_audio_features('jimmy buffet')
jimmybuffet %>%
  count(key_mode,sort = TRUE)
##    key_mode   n
## 1   D major 204
## 2   G major 166
## 3   E major  95
## 4   A major  92
## 5   C major  91
## 6   F major  41
## 7   B major  19
## 8   B minor  12
## 9   A minor  11
## 10 A# major  10
## 11 F# minor  10
## 12 A# minor   7
## 13  E minor   7
## 14 D# major   6
## 15 F# major   6
## 16 G# major   6
## 17 C# minor   5
## 18  F minor   5
## 19 C# major   4
## 20  D minor   4
## 21  G minor   4
## 22  C minor   3
## 23 G# minor   2
## 24 D# minor   1

According to the results: Jimmy Buffet plays 204 notes in D major and 166 notes in G major. Those were the two most popular.