Spotify Playlist Automation

Aaron Philip
3 min readAug 28, 2023

--

Introduction :

In the realm of music streaming, playlists have become an essential aspect of enhancing user experience. Whether it’s setting the mood for a workout, unwinding after a long day, or discovering new artists, playlists curate our musical journeys. In this article, we’ll learn how to save your “Discover Weekly” on Spotify so that you can analyse how your music taste changes over time.

Discover Weekly on Spotify :

“Discover Weekly” is a highly popular and innovative feature offered by Spotify, one of the leading music streaming platforms. Designed to cater to users’ individual music tastes and preferences, Discover Weekly is a personalized playlist that is automatically generated and updated every Monday.

Aim :

The aim of the article is to save the “Discover Weekly” playlist (which updates every week) to a new playlist so that we can have a record of how our music taste has changed over time.

We’ll start by understanding the flow of what we want :

Now looking at the flow, we can see that we’ll need some sort of API call to access the playlist as well as to make a new playlist on Spotify.

This is where the beauty of the Spotify API comes in.

Spotify has made a comprehensive and well-written documentation on how to use their API which you can read about below -

The Python Script

import spotipy
from spotipy.oauth2 import SpotifyOAuth
import re
from datetime import datetime

client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'

# Set up Spotify API client
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri='http://localhost:8001', scope='playlist-modify-private'))

# Get user input for playlist URL
playlist_url = "https://open.spotify.com/playlist/37i9dQZEVXcOedJUHYweZ4?si=a6a94cc1193f41fe"

# Extract playlist ID from URL
regex_pattern = r"playlist\/(\w+)"
playlist_id = re.findall(regex_pattern, playlist_url)[0]

# Get the playlist and its tracks
playlist = sp.playlist(playlist_id, fields="tracks,next")
tracks = playlist["tracks"]["items"]

# Iterate through all pages of tracks
while playlist["tracks"]["next"]:
playlist = sp.next(playlist["tracks"])
tracks.extend(playlist["items"])

# Create new playlist name based on current date
now = datetime.now()
playlist_name = f"{now.strftime('%B')} {now.day//7 + 1} {now.year}"

# Create a new private playlist
new_playlist = sp.user_playlist_create(user=sp.current_user()["id"], name=playlist_name, public=False)

# Add tracks to the new playlist
track_ids = [track["track"]["id"] for track in tracks]
sp.playlist_add_items(new_playlist["id"], track_ids)

Using this code, we can save the Discover Weekly to a new playlist with the name of “Month WeekNumber Year”.

For windows systems:

Now it is an effort to religiously run the script every Monday if we want to save that week’s playlist. So instead of doing it manually, we can use the combination of a batch script + windows task scheduler to automate the generation every Monday at 9 am.

Batch Script :

@echo off
set SCRIPT_PATH=D:\Spotify\spotify.py
python "%SCRIPT_PATH%"

Task Scheduler :

Now this will run every Monday at 9 am and save it on your Spotify library !

If anyone reading this, has an idea regarding how I can extend this idea into a personalized recommendation system, please feel free to comment ! Any idea will be appreciated.

--

--