Spotify Wrapped Weekly

AI Generated Black Spotify Logo

I had been meaning to learn how to use the Spotify API for a while so I embarked on creating my very own Weekly Spotify Wrapped.

Here's a summary of what I did:
  • I used my CLIENT_ID and CLIENT_SECRET to get an access token with Authorization Code grant type. To avoid having to repeatedly authorize the application, I also use a refresh token.
  • I used token to grab recently played songs that have been played in the current week. Spotify currently only stores the last 50 recently played songs, meaning that using 'next' cursor, or a while loop with 'after' or 'before' would not lead to any more songs.
    • To make my request cleaner I have used field filtering:
      url = ""
      timestamp_ms = int(starting_date.timestamp()) * 1000
      query = f"?limit={limit}&after={timestamp_ms}"
      query += "&,,,played_at"
      query_url = url + query
    • I have implemented Caching to make the Dashboard more responsive and faster to load. In particular, I am using the Flask filesystem Cache, which is a bit of an overkill given that we only have access to $50$ songs. I have set a timeout of a day.
    • Wrote a decorator to handle API errors when GETting data more elegantly.
    • The function group_songs_by_artists now stores artist ids and their genres into a file for already requested artists. This means that as time goes on, this app will require fewer requests to extract the artist genres, since these are not accessible from recently_played.
  • I have then used this data and Chart.js to produce the following three visualizations:
    1. Pie Chart showing which artists I have listened to this week.
    2. Bar Chart showing at what time of the day I have mostly listened to songs.
    3. Bar Chart showing the genres of the songs.

My aim in the future will be to implement a mood ring, by inferring the mood from songs either based on lyrics or metrics such as tempo and cadence. A redacted copy of the code is available in my GitHub Repository.

Another plan for the future is to create a cronjob that pulls recently played songs at the end of each day, and stores them in a database or file system. This way one can create a rolling-window of recently played songs and actually show data for the whole week.

Mauro Camara Escudero
Statistical Machine Learning Ph.D.

My research interests include approximate manifold sampling and generative models.