Help Wizard

Step 1


Organising your playlist

Organising your playlist

Hey everyone,


The goal is for the script to analyse the original playlist you provide aka analyse every single song in that playlist for danceability, energy, valence, instrumentales, acousticness, speechiness and so on. It should analyse the value of each track for those attributes and then gather tracks with approximately the same values of ALL attributes into new playlists. Each track that has successfully been analysed and sorted out into a new playlist should then be removed from the original playlist. If every song has been removed, the script should end and print out "Done".


I had many attempts at this script, but every single one failed at some point, some didn't work at all, some just didn't have that cherry on top effect I want to have. The closest to being good script is probably the following: 




import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

# Replace 'YOUR_CLIENT_ID' and 'YOUR_CLIENT_SECRET' with your own Spotify API credentials
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'

# Authenticate with Spotify API
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

def get_playlist_tracks(playlist_id: (
playlist_tracks = sp.playlist_tracks(playlist_id)
tracks = []

for item in playlist_tracks['items']:
track = item['track']

return tracks

def create_new_playlist(original_playlist_id, attribute, min_value, max_value: (
# Get the tracks from the original playlist
original_tracks = get_playlist_tracks(original_playlist_id)

# Analyze tracks and filter based on the attribute values
filtered_tracks = []
for track in original_tracks:
features = sp.audio_features(track['uri'])[0]
attribute_value = features[attribute]
if min_value <= attribute_value <= max_value:

# Create a new playlist
user =
new_playlist = sp.user_playlist_create(user['id'], f'{attribute.capitalize()} Playlist')
new_playlist_id = new_playlist['id']

# Add filtered tracks to the new playlist
sp.user_playlist_add_tracks(user['id'], new_playlist_id, filtered_tracks)

# Remove the filtered tracks from the original playlist
track_uris = [track['uri'] for track in original_tracks]
sp.user_playlist_remove_all_occurrences_of_tracks(user['id'], original_playlist_id, track_uris)

return new_playlist_id

def main():
# Provide the ID of the original playlist
original_playlist_id = 'original_playlist_id'

# Define the attribute values for filtering
attribute_values = {
'danceability': (0.5, 1.0),
'energy': (0.7, 1.0),
'valence': (0.7, 1.0),
'instrumentalness': (0.0, 0.3),
'acousticness': (0.0, 0.3),
'speechiness': (0.0, 0.3)

# Create new playlists based on attribute values
for attribute, (min_value, max_value) in attribute_values.items():
playlist_id = create_new_playlist(original_playlist_id, attribute, min_value, max_value)
print(f'Created playlist based on {attribute}: {playlist_id}')


if __name__ == '__main__':
Now this only takes into account genres and Spotify differs by A LOT of genres, so that's not really what I'm aiming for. But it's the only script that worked from start to finish as is should, only thing that needs to be adjusted are the attributes it sorts by. Since there are multiple attributes though, I failed at implementing this into the script correctly. Maybe you have an idea and can help me?
Thank you!
0 Replies

Suggested posts