Skip to content

Instantly share code, notes, and snippets.

@niekvandepas
Last active October 12, 2024 15:55
Show Gist options
  • Save niekvandepas/248e7f2e1c464fc21b25835813ad300d to your computer and use it in GitHub Desktop.
Save niekvandepas/248e7f2e1c464fc21b25835813ad300d to your computer and use it in GitHub Desktop.
Create a playlist from all your saved Spotify albums, so you can shuffle your entire library
#!/usr/bin/env python
import spotipy
from spotipy.oauth2 import SpotifyOAuth
# INSTRUCTIONS:
# Go to https://developer.spotify.com/dashboard, create an app,
# and paste the Client ID and Client Secret below.
# Also make sure to add a random localhost callback URL to 'redirect URLs' in the dashboard, e.g., "http://localhost:8888/callback" as used below.
sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(
client_id="CLIENT_ID_HERE",
client_secret="CLIENT_SECRET_HERE",
redirect_uri="http://localhost:8888/callback",
scope="user-library-read playlist-modify-private",
)
)
user_id = sp.current_user()["id"]
all_saved_tracks = []
offset = 0
while True:
offset += 50
results = sp.current_user_saved_albums(offset=offset, limit=50)
if results["items"] == []:
break
for idx, item in enumerate(results["items"]):
album = item["album"]
for track in album["tracks"]["items"]:
all_saved_tracks.append(track)
print(idx, album["artists"][0]["name"], " – ", album["name"])
all_saved_track_ids = []
for track in all_saved_tracks:
all_saved_track_ids.append(track["id"])
# Create the playlist
playlist = sp.user_playlist_create(
user=user_id,
name="All songs " + datetime.datetime.now().strftime("%Y-%m-%d"),
public=False,
)
# Add the songs in chunks of 100, because Spotify is angry when you try to add too many at once
for i in range(0, len(all_saved_track_ids), 100):
print("Adding songs", i, "to", i + 100)
sp.playlist_add_items(playlist["id"], all_saved_track_ids[i : i + 100])
print("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment