Personal ProjectPython · Spotify APICompleted

Passive Study Spotify

A Python automation script that builds optimised Spotify playlists for deep focus and passive study sessions. Uses the Spotify Web API to filter tracks by audio features — tempo, energy, and instrumentalness — and writes the results directly to your Spotify library.

Python 3SpotipySpotify Web APIOAuth 2.0JSON ConfigPlaylist Automation

200+

Tracks Processed

3

Playlist Profiles

5

Audio Features Used

1

Script, Zero Ads

The Problem

Spotify's algorithmic playlists — Focus, Chill, Deep Work — are hit or miss. They include tracks with vocals, jarring tempo changes, or dynamics that break concentration. Building study playlists manually is tedious, and saved playlists go stale.

The goal was a script that could programmatically curate tracks from your Liked Songs or a seed playlist, filter by audio characteristics, and write the result to a fresh Spotify playlist — automatically.

How It Works

🎵

Audio Feature Filtering

Filters tracks by Spotify's audio analysis data: instrumentalness > 0.7, energy range 0.3–0.6, tempo 60–120 BPM. Configurable per profile.

📋

Profile System

Three built-in profiles: Deep Focus (strict filtering), Background Study (relaxed), and Lo-Fi Mode (tempo-bias). JSON config for custom profiles.

🔄

Auto Playlist Refresh

Generates a new playlist each run with a datestamp. Keeps your library clean — no stale tracks accumulating in one bloated list.

🔐

OAuth 2.0 Auth

Uses Spotipy's PKCE flow for secure authentication. Tokens cached locally — no re-auth on repeat runs.

🎯

Seed Flexibility

Works from Liked Songs, any playlist, or a mix of artists. Sampling logic avoids overrepresenting a single artist.

⚙️

CLI Control

Profile selection, playlist size, and source via CLI args. No GUI needed — runs from a scheduled task or cron job.

Technical Notes

Spotify Audio Features API

The Spotify Web API returns per-track audio analysis including acousticness, danceability, energy, instrumentalness, liveness, loudness, speechiness, tempo, and valence. The script batches feature requests (max 100 tracks per call) to stay within rate limits.

Spotipy Library

Spotipy handles the OAuth dance and wraps the REST API. Scope required: user-library-read playlist-modify-private playlist-modify-public.

Profile Config (JSON)

{
  "deep_focus": {
    "instrumentalness_min": 0.7,
    "energy_min": 0.3,
    "energy_max": 0.55,
    "tempo_min": 60,
    "tempo_max": 110,
    "size": 40
  }
}

What I Learned

🔑

OAuth PKCE Flow

First time implementing a proper OAuth 2.0 PKCE auth flow — learned token storage, scope minimisation, and refresh handling.

📊

API Rate Limiting

Spotify limits audio feature lookups to 100 tracks per request. Batching + sleep(0.3) between calls keeps it clean.

🎚️

Feature Tuning

Instrumentalness and energy alone aren't enough — some "instrumental" tracks are still jarring. Combining with loudness improves results significantly.

🤖

Automation Mindset

The real win was realising that small daily-life friction points are ideal automation candidates. Study playlists took 20 mins to automate and saved hours.

Want the Script?

Drop me a message if you'd like the source code or a walkthrough on setting it up with your own Spotify account.

Get in Touch →