Spotify API

Author

Thomas Bonnici

Introduction

Spotify is a popular music platform that is used by millions of people to stream their favorite artists and music tracks. Spotify is my API that I will be using in this document. I will have the ability to do things like pull an entire track list from any artist in the spotify database.

R Installation

First, we need to install the spotify packages. After it’s installed you can run the second “library command” line to read it in from your library.

library(spotifyr)
library(knitr)
library(tidyverse)

How to Obtain a Spotify API (Steps Continued)

  1. Next, you have to go to Spotify’s website to create a developer account (Link Here).
  2. After the link is clicked, you need to log in with your spotify account.
  3. Next, click the blue “create app” button.
  4. There will be four fill in boxes that pop up on your screen: App Name, App Description, Website and Redirect URI. The website one isn’t required, but the other three need to be filled in. For the URI, I used this link http://localhost:1410/ (This is the Spotify default URI).
  5. Once you put the information in, you need to make sure the “Web API” check box is checked at the bottom of the screen, as well as the terms agreement check box.
  6. Lastly, hit the blue save button at the bottom.

This will bring you to your new home page for the newly created app. On this page is where you can find your Client ID and Client Secret.

  1. You can navigate to that by hitting the white “settings” button in the top right.
  2. Next, at the top left make sure the word “Basic Information” is underlined.
  3. The Client ID will automatically show on screen. To view the Client Secret, hit the View Client Secret underlined button. Once you see those IDs, you can copy them in the code chunk below:
Sys.setenv(SPOTIFY_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxx')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxx')

By running the code, you will obtain your access token! By inserting your Client ID and Client Secret, this function will output the access token. This access_token will appear in your global environment.

Getting Access Due to Spotify’s Restrictions

In this section, we need to tell spotify

  1. First, to allow us to be able to run some of the code, I need to do an override with the API. The capabilities of the API have been restricted over the last few years, and require this code to override it:
spotifyr::scopes()
 [1] "ugc-image-upload"            "user-read-playback-state"   
 [3] "user-modify-playback-state"  "user-read-currently-playing"
 [5] "app-remote-control"          "streaming"                  
 [7] "playlist-read-private"       "playlist-read-collaborative"
 [9] "playlist-modify-private"     "playlist-modify-public"     
[11] "user-follow-modify"          "user-follow-read"           
[13] "user-read-playback-position" "user-top-read"              
[15] "user-read-recently-played"   "user-library-modify"        
[17] "user-library-read"           "user-read-email"            
[19] "user-read-private"           "user-soa-link"              
[21] "user-soa-unlink"             "soa-manage-entitlements"    
[23] "soa-manage-partner"          "soa-create-partner"         
  1. The next step is to request the scopes that we want. In this case, I wanted to look at playlist items as well as user read items (like songs). So, I am telling Spotify I only need access to these four data points. Here’s what the code looks like:
requested_scopes <- 
  scopes()[c(7,8,14,15)]
  1. After that, we need to override the API scopes requested. It looks like this:
auth <- 
  get_spotify_authorization_code(client_id = Sys.getenv("SPOTIFY_CLIENT_ID"),
                                 client_secret = Sys.getenv("SPOTIFY_CLIENT_SECRET"),
                                 scope = requested_scopes)

Now that this is complete, we are free to analyze anything out of what we requested!

Usage

In this section, I will explore Spotify’s analytical capabilities. I am going to see who my top artists are as of March 25th (similar to the spotify wrapped capabilities).

Here’s the code to retrieve my favorite artists:

get_my_top_artists_or_tracks(type = 'artists', time_range = 'long_term', limit = 5, authorization = auth) %>% 
    select(name, genres) %>% 
    rowwise %>% 
    mutate(genres = paste(genres, collapse = ', ')) %>% 
    ungroup
name genres
Morgan Wallen contemporary country
Drake canadian hip hop, canadian pop, hip hop, pop rap, rap
Logic conscious hip hop, hip hop, pop rap, rap
Kendrick Lamar conscious hip hop, hip hop, rap, west coast rap
Kanye West chicago rap, hip hop, rap