/mood-of-music

Using the Spotify API to determine the mood of a user's playlist

Primary LanguageJupyter Notebook

Mood Of Music

As the range of the variety of music grows over time, research has shown how an individual's music preferences correlate to their mood. To fully utilize this correlation, our project aims to call from the Spotify API to analyze a user's overall mood for a year, specfically the year 2020: the start of the COVID-19 Pandemic.

At the end of every year, Spotify provides the user a playlist called "Spotify Wrapped", which consists of the user's top songs from the past year. The Spotify API also provides audio features for each track, such as "danceability","liveness","speechiness",etc. Among these features, our team uses the "valence" metric, which represents the musical postiviness conveyed by a track.

According to Spotify, "Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sounds more negative (e.g. sad, depressed, angry). Therefore, through analyzing the valence values across a user's "Spotify Wrapped" from years 2019 to 2022, our project will observe whether a significant change of happiness is apparent during the periods before and after COVID-19.

Required Software

To run the software, you will need the following:

  • numpy
  • pandas
  • matplotlib
  • spotipy
  • google_spreadsheet
  • requests
  • validators
  • google-api-python-client
  • google-auth-httplib2
  • google-auth-oauthlib
  • (To use the Spotify API) Spotify Web API Access Token
  • (To access the spreadsheet) Google OAuth 2.0 Token

Library Installation

  • To install all of the necessary libraries, make sure you're running a version of Python that is 3.7 or above
  • Ensure that your file directory is at the root of the repository. Then, look for a file called requirements.txt. Afterwards, run the following:
pip install -r requirements.txt
  • Now you have all the libraries to run our code!

Spotify Web API

To access the Spotify Web API, you will need an Access Token:

Once you have your token, create a file called spotify_key.py and enter your details into the following format:

SPOTIPY_CLIENT_ID = "abcdefghijklmop"

SPOTIPY_CLIENT_SECRET = "12345678956789"

SPOTIPY_REDIRECT_URI = "http://localhost/"

Save this file in the root of the repository and you should be set. MAKE SURE NOT TO SHARE THIS WITH ANYONE


Google OAuth 2.0 Token

Since "Spotify Wrapped" Playlists are private playlists, our project sent out a survey for people to share their playlists into a public format.

These results are written on to a Google Sheets spreadsheet, which our code directly calls data from.

To access the Google Spreadsheet, you will need a Google OAuth 2.0 Token:

That should be all. You're all set!

If you have any other questions, please contact one of the people above.

Bonus

We've included a special function called email_results.py, which sends the valence results of their playlists to those who submitted their email on the survey. Please do not run this function! We have already emailed the results of the survey . We only ask you to appreciate the beauty of the code :).