checkforalump.com

Unlocking Your Spotify Activity with R and “spotifyr”

Written on

Chapter 1: Introduction to Spotify Data Analysis

In today's digital age, music consumption has shifted dramatically. As someone who fondly remembers browsing CD stores for the latest albums, I recognize how my music ritual transformed over the years. At just 15, finding that perfect album and experiencing the thrill of hearing the first track was an essential part of my life. Music is, arguably, one of the defining aspects of humanity that connects us across galaxies.

“After silence, that which comes nearest to expressing the inexpressible is music.”

  • Aldous Huxley.

Fast forward 17 years, and the landscape has changed. The joy of visiting record stores has been replaced by the convenience of streaming services, with Spotify leading the charge. Recent statistics indicate that 35% of global music streaming subscribers are on Spotify, significantly outpacing competitors like Apple Music and Amazon.

Given this backdrop, I was intrigued to explore how Spotify allows users to download their personal data, including playback history and search logs, in JSON format.

Visualizing Spotify Listening Habits

Chapter 2: Obtaining Your Spotify Data

Spotify conveniently offers a "year in review" email each December, summarizing your listening habits over the year. However, digging deeper into your data can uncover insights that Spotify doesn't readily provide.

Downloading Spotify Data Steps

After submitting your request, you’ll receive a verification email from Spotify for security purposes. Confirm this email to proceed.

Confirmation Email from Spotify

Once confirmed, Spotify will notify you that your data will be compiled and sent within 30 days. Patience is key here. In my case, I received my download link within three days.

Notification Email for Data Download

When you finally get the email with the download link, click “Download,” and you will be redirected to the privacy settings page. After re-entering your password for security, the download of “my_spotify_data.zip” will start.

Downloading My Spotify Data

Congratulations! You've now obtained your Spotify data. Let's delve into analyzing your listening history.

Chapter 3: Analyzing Your Listening History

Upon extracting the downloaded ZIP file, locate the “My Data” folder, which contains various JSON files. The one of particular interest is “StreamingHistory0.json,” which holds your playback history.

Contents of My Spotify Data

If you've listened to a lot of music, you may find multiple files, as Spotify splits histories larger than 1.5 MB. Keep in mind that Spotify only retains data for the past year, so while it would be fascinating to access a more extensive history, we will focus on the last year's data for now.

Now, let’s create a new R script and start by loading the necessary libraries for data manipulation and visualization.

# Required Libraries

library(jsonlite)

library(lubridate)

library(gghighlight)

library(spotifyr)

library(tidyverse)

library(knitr)

library(ggplot2)

# Reading the Streaming History JSON

streamHistory <- fromJSON("StreamingHistory0.json", flatten = TRUE)

Your playback history contains essential variables: “endTime,” “artistName,” “trackName,” and “msPlayed.”

Now, you can analyze when you listened to music the most.

Section 3.1: When Did You Listen to Music the Most?

Using your streaming data, you can plot your listening activity over time. This analysis can reveal trends in your music consumption, such as the impact of working from home during the pandemic.

# Adding Date and Timing

mySpotify <- streamHistory %>%

as_tibble() %>%

mutate_at("endTime", ymd_hm) %>%

mutate(endTime = endTime - hours(6)) %>%

mutate(date = floor_date(endTime, "day"), seconds = msPlayed / 1000, minutes = seconds / 60)

# Weekly Listening Activity

streamingHours <- mySpotify %>%

filter(date >= "2020-01-01") %>%

group_by(date) %>%

group_by(date = floor_date(date, "week")) %>%

summarize(hours = sum(minutes) / 60) %>%

arrange(date) %>%

ggplot(aes(x = date, y = hours)) +

geom_col(aes(fill = hours)) +

scale_fill_gradient(low = "yellow", high = "red") +

labs(x= "Date", y= "Listening Hours") +

ggtitle("When Did I Listen to Music the Most?", "Weekly Listening Activity")

streamingHours

The resulting plot will showcase your listening patterns over time, highlighting significant changes in habits.

Chapter 4: Further Analysis of Listening Habits

From tracking your overall listening habits to analyzing the most popular artists, there’s much to explore. For example, you can identify which artists you listened to the most or the specific days and times when you are most active.

You can also visualize your engagement with particular artists over time, utilizing the “gghighlight” library for emphasis.

# Artist-Specific Listening Activity

hoursArtist <- mySpotify %>%

group_by(artistName, date = floor_date(date, "month")) %>%

summarize(hours = sum(minutes) / 60) %>%

ggplot(aes(x = date, y = hours, group = artistName)) +

labs(x= "Date", y= "Listening Hours") +

ggtitle("Artist-Specific Listening Activity", "e.g. Alton Ellis and Jarabe de Palo") +

geom_line() +

gghighlight(artistName == "Alton Ellis" || artistName == "Jarabe De Palo")

hoursArtist

This approach can reveal how your listening habits correlate with events in your life or the music scene, providing a deeper understanding of your preferences.

Chapter 5: Exploring the spotifyr Package

Let's shift our focus to the “spotifyr” package, which allows you to connect with Spotify's API and access a wealth of information about tracks, artists, and albums.

Image

After creating your app, add the callback URL “http://localhost:1410/” in the app settings.

# Connecting to the Spotify API

Sys.setenv(SPOTIFY_CLIENT_ID = 'YOUR_CLIENT_ID')

Sys.setenv(SPOTIFY_CLIENT_SECRET = 'YOUR_CLIENT_SECRET')

get_spotify_authorization_code()

By executing this function, you’ll be prompted to authorize your application. Once authenticated, you can leverage the spotifyr package to gather even more data about your musical tastes.

Chapter 6: Conclusion

You can uncover fascinating insights about your musical preferences using R and the spotifyr package. From tracking your listening history to identifying your favorite artists, the possibilities are endless. I hope you find this exploration as enjoyable and enlightening as I did.

Thank you for taking the time to read this article! May your data analysis journey be as exciting as the music you love.