Advertisement
lxqlifrnv

Spotify playlist trimmer script

Jul 24th, 2024
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. import spotipy
  2. from spotipy.oauth2 import SpotifyOAuth
  3.  
  4. # Authentication - Replace with your Spotify Developer credentials
  5. sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
  6.     client_id='insert your client id here',
  7.     client_secret='insert your secret here',
  8.     redirect_uri='http://localhost:8888/callback',
  9.     scope='playlist-modify-private playlist-modify-public playlist-read-private'))
  10.  
  11. # Playlist ID, keyword, and minimum track length in seconds
  12. playlist_id = 'insert your playlist id here'
  13. keyword = 'your keyword here'  # Leave as an empty string if you want to ignore the keyword filter
  14. min_length_seconds = 60  # Minimum length of 60 seconds
  15.  
  16. # Convert minimum length to milliseconds
  17. min_length_ms = min_length_seconds * 1000
  18.  
  19. # Function to get all tracks from a playlist (handles pagination)
  20. def get_all_tracks(playlist_id):
  21.     tracks = []
  22.     results = sp.playlist_tracks(playlist_id)
  23.     tracks.extend(results['items'])
  24.     while results['next']:
  25.         results = sp.next(results)
  26.         tracks.extend(results['items'])
  27.     return tracks
  28.  
  29. # Function to remove tracks in batches
  30. def remove_tracks_in_batches(playlist_id, tracks_to_remove, batch_size=100):
  31.     for i in range(0, len(tracks_to_remove), batch_size):
  32.         batch = tracks_to_remove[i:i + batch_size]
  33.         sp.playlist_remove_specific_occurrences_of_items(playlist_id, batch)
  34.  
  35. # Get all tracks in the playlist
  36. tracks = get_all_tracks(playlist_id)
  37.  
  38. # Find tracks to remove
  39. tracks_to_remove = []
  40. for item in tracks:
  41.     track = item['track']
  42.     # Check if keyword is present in the track name or keyword is empty
  43.     if (keyword == '' or keyword.lower() in track['name'].lower()) or track['duration_ms'] <= min_length_ms:
  44.         tracks_to_remove.append({'uri': track['uri'], 'positions': [item['track']['track_number']-1]})
  45.  
  46. # Remove tracks from playlist in batches
  47. if tracks_to_remove:
  48.     remove_tracks_in_batches(playlist_id, tracks_to_remove)
  49.     print(f"Removed {len(tracks_to_remove)} tracks containing the keyword '{keyword}' and shorter than {min_length_seconds} seconds from the playlist.")
  50. else:
  51.     print(f"No tracks found containing the keyword '{keyword}' and shorter than {min_length_seconds} seconds.")
  52.  
  53. # Debugging: Print track names that were not removed but match the criteria
  54. remaining_tracks = get_all_tracks(playlist_id)
  55. for item in remaining_tracks:
  56.     track = item['track']
  57.     if (keyword == '' or keyword.lower() in track['name'].lower()) and track['duration_ms'] >= min_length_ms:
  58.         print(f"Track not removed: {track['name']}")
  59.  
  60.  
  61.  
Tags: Script spotify
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement