A module using the Spotify Web API based off of the Twilio library.
Install from PyPi using pip, a package manager for Python.
pip install spotify-python
Using spotify-python requires either Client Credentials or User Credentials.
Client Credentials When using Client Credentials, you can pass them directly or use environment variables.
from spotify.auth.client import Client
from spotify import Client as Spotify
SPOTIFY_CLIENT_ID = 'CLIENT_ID'
SPOTIFY_CLIENT_SECRET = 'CLIENT_SECRET'
credentials = Client(SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET)
spotify = Spotify(credentials)Alternatively, calling the Client constructor without these parameters will look for the
SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET in the current environment. I suggest using
this method.
from spotify.auth.client import Client
from spotify import Client as Spotify
spotify = Spotify(Client())User Credentials
For more advanced features, User Credentials may be required. You can find a complete description here. To get a user token, you can use the user token utility method. If none are passed into the user_token method, client_id, client_secret and redirect_uri will be retrieved using the SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, and SPOTIFY_REDIRECT_URI environment variables.
from spotify.auth.util import user_token
from spotify import Client as Spotify
scopes = ['scope-a', 'scope-b']
token = user_token(scopes)
# Acknowledge browser prompt, and paste in the redirect URI
spotify = Spotify(token)from spotify import Client as Spotify
token = fetch_user_token()
spotify = Spotify(token)
search_result = spotify.v1.search.get('Never gonna give you up', ['track'])
for track in search_result.tracks:
print track.namefrom spotify import Client as Spotify
token = fetch_user_token()
spotify = Spotify(token)
recently_played = spotify.v1.me.player.recently_played.list()
for rp in recently_played:
print rp.track.nameClient- Spotify Web APIv1- Version 1 of the Spotify Web APIalbums- /albums endpointlist()- List albums by ID, returnsPage[AlbumInstance]get()- Get the context of a single album, returnsAlbumContextfetch()- Get details of the album, returnsAlbumInstancetracks- /albums/{album_id}/tracks endpointlist()- List the tracks on the album, returnsPage[TrackInstance]
artists- /artists endpointlist()- List artists by ID, returnsPage[ArtistInstance]get()- Get the context of a single artist, returnsArtistContextfetch()- Get the details of the artist, returnsArtistInstancealbums- /artists/{artist_id}/albums endpointlist()- List albums by the artist, returnsPage[AlbumInstance]
top_tracks- /artists/{artist_id}/top-tracks endpointlist()- List top tracks by the artist, returnsPage[TrackInstance]
related_artists- /artists/{artist_id}/related-artists endpointlist()- List related artists, returnsPage[ArtistInstance]
audio_analysis- /audio-analysis endpointget()- Get the context of the audio analysis for a track, returnsAudioAnalysisContextfetch()- Get the audio analysis details, returnsAudioAnalysisInstance
audio_features- /audio-features endpointlist()- List audio features by track ID, returnsPage[AudioFeaturesInstance]get()- Get the audio features context of a track, returnsAudioFeaturesContextfetch()- Get the audio features details of a track, returnsAudioFeaturesInstance
browse- /browse endpointfeatured_playlists- /browse/featured-playlists endpointfetch()- Get the latest featured playlists, returnsFeaturedPlaylistInstance
new_releases- /browse/new-releases endpointlist()- List the new release, returnsPage[AlbumInstance]
categories- /browse/categories endpointlist()- List the categories, returnsPage[CategoryInstance]get()- Get the context of a category, returnsCategoryContextfetch()- Get the details of a category, returnsCategoryInstanceplaylists- /browse/categories/{category_id}/playlists endpointlist()- List the playlists that are in this category, returns `Page[PlaylistInstance]
me- /me endpointfetch()- Fetch the details of the authenticated user, returnsMeInstancealbums- /me/albums endpointlist()- List the user's saved albums, returnsPage[SavedAlbum]save()- Save an album, returnsboolremove()- Remove a soved album, returnsboolcontains()- Checks to see if the user has saved the albums, returnsList[bool]
following- /me/following endpointlist()- List the artists followed by the user, returnsPage[ArtistInstance]add()- Add a follower, returnsboolremove()- Remove a follower, returnsboolcontains()- Checks if the user has followed the artists, returnsList[bool]
player- /me/player endpointfetch()- Get the player details, returnsPlayerInstancetransfer()- Transfer playing to another device, returnsboolplay()- Set what is being played, returnsboolpause()- Pause playback, returnsboolnext_()- Skip the current track, returnsboolprevious()- Go back a track, returnsboolseek()- Go to a point in the current track, returnsboolrepeat()- Set repeat status, returnsboolvolume()- Set the volume, returnsboolshuffle()- Set the shuffle status, returnsboolcurrently_playing- /me/player/currently-playing endpointfetch()- Get the currently playing item, returnsCurrentlyPlayingInstance
devices- /me/player/devices endpointlist()- List the devices that the user owns, returnsPage[DeviceInstance]
recently_played- /me/player/recently-played endpointlist()- List the recently played items, returnsPage[RecentlyPlayedInstance]
playlists- /me/playlists endpointlist()- List the user created playlists, returnsPage[PlaylistInstance]
top- /me/top endpointartists- /me/top/artists endpointlist()- List the user's top artists, returnsPage[ArtistInstance]
tracks- /me/top/tracks endpointlist()- List the user's top tracks, returnsPage[TrackInstance]
tracks- /me/tracks endpointlist()- List the user's saved tracks, returnsPage[SavedTrack]save()- Save a track, returnsboolremove()- Remove a saved track, returnsboolcontains()- Check to see if the user has saved tracks, returnsList[bool]
recommendations- /recommendations endpointfetch()- Fetch recommendations, returnsRecommendationsInstanceavailable_genre_seeds- /recommendations/available-genre-seeds endpointfetch()- Fetch available genre seeds, returnsList[str]
search- /search endpointget()- Execute a serach, returnsSearchInstance
tracks- /tracks endpointlist()- List tracks by ID, returnsPage[TrackInstance]get()- Get a context of a track, returnsTrackContextfetch()- Get the details of a track, returnsTrackInstance
users- /users endpointget()- Get a context of a user, returnsUserContextfetch()- Get the details of a user, returnsUserInstanceplaylists- /users/{user_id}/playlists endpointlist()- List the user's playlists, returnsPage[PlaylistInstance]create()- Create a playlist, returnsPlaylistIntanceget()- Get a context of a playlist, returnsPlaylistContextfetch()- Get the details of the playlist, returnsPlaylistInstanceupdate()- Update playlist details, returnsboolfollow()- Follow the playlist, returnsboolunfollow()- Unfollow the playlist, returnsboolfollowers- /users/{user_id}/playlists/{playlist_id}/followers endpointcontains()- Check if users follow this playlist, returnsList[bool]
images- /users/{user_id}/playlists/{playlist_id}/images endpointreplace()- Replace the playlist cover image, returnsbool
tracks- /users/{user_id}/playlists/{playlist_id}/tracks endpointlist()- List the tracks in the playlist, returnsPage[PlaylistTrackInstance]add()- Add a track to the playlist, returnsSnapshotremove()- Remove a track from the playlist, returnsSnapshotreorder()- Reorder playlist tracks, returnsSnapshotreplace()- Replace all tracks in the playlist, returnsbool