Published:

Recreating the “Recently Added” playlist for Spotify

Written by Kieren Sainsbury

Disclaimer: The personal views expressed may not align with the views of my employer.

For those of us who grew up with iTunes, one of the great features was the “Recently Added” playlist, where all the latest tracks would automatically populate as soon as they were downloaded or synced.

While the world of music streaming has evolved, that same concept is still appealing (at least to me). I often find myself using Release Radar and Discover Weekly to find new music that I will add to my Liked Songs.

However, more often than not when speaking to my phone, Alexa speakers or the car – I will default to asking it to play one of the playlists I’ve created. This means all that new music often gets a single listen, never to be heard from again. So I created a playlist that I would manually populate on a Monday morning before work with the top handful of songs I’ve added over the past week.

But, who has time for that? Enter: this Python script.

With just a few lines of code and the power of the Spotipy (which leverages the Spotify API), you can replicate the “Recently Added” playlist experience minus the syncing cables. It’s a simple but effective way to keep track of your musical journey.

What the script does

  1. Authenticates the script using OAuth
  2. Clears the target playlist
  3. Fetches the last 100 tracks you’ve liked on Spotify
  4. Adds your recent tracks to the target playlist
  5. Updates the playlist description with a timestamp

Before you run the script

  1. Install Spotipy
  2. Create an app using the Spotify for Developers dashboard
  3. Copy your Spotify username from your account profile
  4. Create a target playlist in your Spotify account
  5. Fetch the playlist ID (easy method: Share → Copy link → grab the ID part)

The code

The #Configure section of the code requires input for each of the “insert” values. The #How many tracks do you want to get? section is designed to fetch 100 but it’s coded dynamically to allow for any number of songs. However, the Spotify API limits to 20 per fetch so the higher the number, the longer it’ll take.

This script works for me, but your mileage may vary. I’m sharing it as-is, feel free to tweak and adapt it to your needs. Just a friendly reminder: I’m not responsible if things break, go haywire or if your playlist ends up filled with sea shanties by mistake.

I understand the risk and still want to view the script code
import datetime
from spotipy import Spotify
from spotipy.oauth2 import SpotifyOAuth

#Configure
CLIENT_ID = "insert" #Go to: https://developer.spotify.com/dashboard/
CLIENT_SECRET = "insert"
REDIRECT_URI = "http://127.0.0.1:8888/callback" #If 8888 doesn't work try a different port
SCOPE = "playlist-modify-public playlist-modify-private user-library-read"
USERNAME = "insert" #Find this here: https://www.spotify.com/account/profile/
PLAYLIST_ID = "insert" #Press share and https://open.spotify.com/playlist/COPYTHIS?si=IGNORETHIS

#Authentication
sp = Spotify(auth_manager=SpotifyOAuth(
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    redirect_uri=REDIRECT_URI,
    scope=SCOPE,
    username=USERNAME,
    show_dialog=True
))

#Check Playlist ID
if not PLAYLIST_ID:
    raise Exception("Playlist ID is not set or is invalid.")
    
playlist_id = PLAYLIST_ID

#Delete all songs currently in the playlist
def clear_playlist(playlist_id):
    results = sp.playlist_items(playlist_id)
    tracks = [item['track']['uri'] for item in results['items']]
    if tracks:
        sp.playlist_replace_items(playlist_id, [])  #Removes all tracks

#How many tracks do you want to get?
def get_recent_liked_tracks(limit=100):
    track_uris = []
    #Spotify only allows 20 at a time, determining how many cycles we need
    requests_needed = limit // 20
    for i in range(requests_needed):
        offset = i * 20
        try:
            #Fetch tracks with the correct offset
            results = sp.current_user_saved_tracks(limit=20, offset=offset)
            track_uris.extend([item['track']['uri'] for item in results['items']])
            print(f"Fetched {len(track_uris)} tracks so far...")
        except Exception as e:
            print(f"Error fetching tracks: {e}")
    return track_uris

#Add tracks to the playlist
def add_tracks_to_playlist(playlist_id, track_uris):
    sp.playlist_add_items(playlist_id, track_uris)

#Updates the playlist description
def update_playlist_description(playlist_id):
    now = datetime.datetime.now().strftime("Updated %Y-%m-%d %H:%M")
    sp.playlist_change_details(playlist_id, description=now)

#Run the process
clear_playlist(playlist_id)
track_uris = get_recent_liked_tracks()
add_tracks_to_playlist(playlist_id, track_uris)
update_playlist_description(playlist_id)

print("Playlist successfully updated!")

Level up: automate it

You could easily automate this or host it on a server with a Cron job, but I’ve decided manually triggering it every so often is enough for my typical usage. Most of my “new music” these days comes from Release Radar (Friday) or Discover Weekly (Monday). This still saves me at least 15 minutes of a Monday morning deleting and re-adding tracks, it also means if some new tracks drop throughout the week I can do a quick and easy update with a single click.

A note about ports

If you’re running the script locally and using the OAuth authentication flow, you might run into issues with the redirect URI and ports. By default, the Spotify OAuth flow expects a local server to run on the port you specified (as seen in the redirect_uri variable). If that port is in use or blocked, the authentication might fail.

If you face any issues with ports, try changing the port number in both the script and on the apps basic information page (via the Spotify for Developers dashboard).

Let me know what you’ve done with Spotipy

Whether you’re a data nerd, a playlist perfectionist or just someone who loves discovering music: this script is a fun and easy win.

Let me know if you end up customising it further. I’d love to hear what you build or what’s playing in your version of “Recently Added” this week.



Recent posts


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.