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.name
from 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.name
Client
- 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, returnsAlbumContext
fetch()
- Get details of the album, returnsAlbumInstance
tracks
- /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, returnsArtistContext
fetch()
- Get the details of the artist, returnsArtistInstance
albums
- /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, returnsAudioAnalysisContext
fetch()
- 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, returnsAudioFeaturesContext
fetch()
- 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, returnsCategoryContext
fetch()
- Get the details of a category, returnsCategoryInstance
playlists
- /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, returnsMeInstance
albums
- /me/albums endpointlist()
- List the user's saved albums, returnsPage[SavedAlbum]
save()
- Save an album, returnsbool
remove()
- Remove a soved album, returnsbool
contains()
- 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, returnsbool
remove()
- Remove a follower, returnsbool
contains()
- Checks if the user has followed the artists, returnsList[bool]
player
- /me/player endpointfetch()
- Get the player details, returnsPlayerInstance
transfer()
- Transfer playing to another device, returnsbool
play()
- Set what is being played, returnsbool
pause()
- Pause playback, returnsbool
next_()
- Skip the current track, returnsbool
previous()
- Go back a track, returnsbool
seek()
- Go to a point in the current track, returnsbool
repeat()
- Set repeat status, returnsbool
volume()
- Set the volume, returnsbool
shuffle()
- Set the shuffle status, returnsbool
currently_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, returnsbool
remove()
- Remove a saved track, returnsbool
contains()
- Check to see if the user has saved tracks, returnsList[bool]
recommendations
- /recommendations endpointfetch()
- Fetch recommendations, returnsRecommendationsInstance
available_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, returnsTrackContext
fetch()
- Get the details of a track, returnsTrackInstance
users
- /users endpointget()
- Get a context of a user, returnsUserContext
fetch()
- Get the details of a user, returnsUserInstance
playlists
- /users/{user_id}/playlists endpointlist()
- List the user's playlists, returnsPage[PlaylistInstance]
create()
- Create a playlist, returnsPlaylistIntance
get()
- Get a context of a playlist, returnsPlaylistContext
fetch()
- Get the details of the playlist, returnsPlaylistInstance
update()
- Update playlist details, returnsbool
follow()
- Follow the playlist, returnsbool
unfollow()
- Unfollow the playlist, returnsbool
followers
- /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, returnsSnapshot
remove()
- Remove a track from the playlist, returnsSnapshot
reorder()
- Reorder playlist tracks, returnsSnapshot
replace()
- Replace all tracks in the playlist, returnsbool