A small script that creates and manages Spotify playlists using the Spotify Web API via the spotipy library. It demonstrates OAuth authentication, searching tracks, and creating playlists programmatically.
Main script: Spotify playlist generator.py
- Uses the Spotify Web API through
spotipyto authenticate a user (OAuth) and obtain access tokens. - Searches for tracks or playlists by query and can create a new playlist or add tracks to an existing playlist.
- Handles pagination and rate limits lightly (for larger operations consider batching and retries).
- Python 3.x
spotipy(pip install spotipy)
- Register an app on the Spotify Developer Dashboard and get your Client ID, Client Secret, and set a Redirect URI.
- Set environment variables (recommended):
SPOTIPY_CLIENT_IDSPOTIPY_CLIENT_SECRETSPOTIPY_REDIRECT_URI
- Install dependencies:
pip install spotipy - Run:
python "Spotify playlist generator.py"and follow the browser-based OAuth flow to authorize the app.
import spotipy
from spotipy.oauth2 import SpotifyOAuth
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope="playlist-modify-public"))
results = sp.search(q='Nirvana Smells Like Teen Spirit', type='track', limit=1)
track_id = results['tracks']['items'][0]['id']
playlist = sp.user_playlist_create(user=sp.current_user()['id'], name='My Playlist')
sp.playlist_add_items(playlist['id'], [track_id])- OAuth scopes: adjust scopes (e.g.,
playlist-modify-public,playlist-modify-private) according to what the script needs. - Keep your client secret private; prefer environment variables over hard-coding credentials.
- Respect Spotify's Web API rate limits and Terms of Service when using the script.
Practical project for learning API authentication flows and integrating with third-party services. Consider adding configuration options, error handling, and batching for large playlists.