Welcome to Spotipy!¶
Spotipy is a lightweight Python library for the Spotify Web API. With Spotipy you get full access to all of the music data provided by the Spotify platform.
Assuming you set the SPOTIPY_CLIENT_ID and SPOTIPY_CLIENT_SECRET environment variables, heres a quick example of using Spotipy to list the names of all the albums released by the artist Birdy:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
birdy_uri = 'spotify:artist:2WX2uTcsvV5OnS0inACecP'
spotify = spotipy.Spotify[client_credentials_manager=SpotifyClientCredentials[]]
results = spotify.artist_albums[birdy_uri, album_type='album']
albums = results['items']
while results['next']:
results = spotify.next[results]
albums.extend[results['items']]
for album in albums:
print[album['name']]
Heres another example showing how to get 30 second samples and cover art for the top 10 tracks for Led Zeppelin:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
lz_uri = 'spotify:artist:36QJpDe2go2KgaRleHCDTp'
spotify = spotipy.Spotify[client_credentials_manager=SpotifyClientCredentials[]]
results = spotify.artist_top_tracks[lz_uri]
for track in results['tracks'][:10]:
print['track : ' + track['name']]
print['audio : ' + track['preview_url']]
print['cover art: ' + track['album']['images'][0]['url']]
print[]
Finally, heres an example that will get the URL for an artist image given the artists name:
import spotipy
import sys
from spotipy.oauth2 import SpotifyClientCredentials
spotify = spotipy.Spotify[auth_manager=SpotifyClientCredentials[]]
if len[sys.argv] > 1:
name = ' '.join[sys.argv[1:]]
else:
name = 'Radiohead'
results = spotify.search[q='artist:' + name, type='artist']
items = results['artists']['items']
if len[items] > 0:
artist = items[0]
print[artist['name'], artist['images'][0]['url']]