Accessing and Using Spotify API

Ever wanted your Spotify Wrapped in the middle of the year? Well you’re in luck! This is a tutotial for how to access your Spotify data and data on all artists that is stored by Spotify.

Step One

You are going to start by going to https://developer.spotify.com/dashboard/login and setting up an account and linking it to your current Spotify Account. Then you create an app and open it. Once you do this you should see a page that has your client ID and client secret. You’ll need both of these in a second so keep that page open.

Step Two

Now you can open R and install and download the Spotifyr package. You may have to use “devtools::install_github(‘charlie86/spotifyr’)” in order to download the package. Then you will initialize your authentication with the following commands:

Sys.setenv(SPOTIFY_CLIENT_ID = ‘PASTE CLIENT ID’) Sys.setenv(SPOTIFY_CLIENT_SECRET = ‘PASTE CLIENT SECRET’)

This is where you’ll paste your ID and Secret.

Step Three

Now that you are “logged in” and authenticated you can begin to use calls and analyze your data or artist data. I will show an example of what you can do for each.

One of my favorite bands is Panic! at the Disco. Based on my mood I want to listen to a mostly happy or mostly sad album. So based on major vs minor key, I will make bar charts for each album.

patd <- get_artist_audio_features('panic! at the disco')

patd %>% 
  select(mode_name, album_name) %>% 
  filter(album_name != "Live in Chicago", album_name != "All My Friends We're Glorious: Death of a Bachelor Tour Live",
         album_name != "Vices & Virtues (Deluxe Edition)") %>% 
  group_by(mode_name) %>% 
  ggplot(aes(x = mode_name))+
  geom_bar(fill = c("yellow","blue","yellow","blue","yellow","blue","yellow",
                    "blue","yellow","blue","yellow","blue","yellow","blue"))+
  facet_wrap(~album_name)+
  labs(title = "Which Panic Albums are Happy/Sad?", subtitle = "Based on Key",
       x = "Key Type")

As you can see if I want a mostly sad album I should listen to A Fever You Can’t Sweat Out, but if I want a happy album, I should listen to Pretty Odd.

Step Four

Now you can also do things like analyze your playlists and affect your Spotify playback in real time. Unfortunately, you need a constantly changing access key or authorization code to do it, so I can’t show the results in this tutorial but here’s some code that is cool.

I listen to a lot of music and the artists tend to vary in popularity. So what if I get asked to play some music on a car ride and I want to play more popular music that I still enjoy? One thing that this API allows me to do it affect my Spotify queue in real time. This code will organize my last 50 songs listened to by popularity and queue them up in that order.

recents<- get_my_recently_played(limit = 50)

byPopularity<- recents %>% arrange(desc(track.popularity))

toggle_my_shuffle(state = FALSE, authorization = get_spotify_authorization_code) start_my_playback(uris = byPopularity$track.uri, authorization = get_spotify_authorization_code)