spotipy/examples/artist_recommendations.py
2016-03-30 15:43:47 -04:00

36 lines
1.0 KiB
Python

import sys
import spotipy
''' shows recommendations for the given artist
'''
from spotipy.oauth2 import SpotifyClientCredentials
client_credentials_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
sp.trace=False
def get_artist(name):
results = sp.search(q='artist:' + name, type='artist')
items = results['artists']['items']
if len(items) > 0:
return items[0]
else:
return None
def show_recommendations_for_artist(artist):
albums = []
results = sp.recommendations(seed_artists = [artist['id']])
for track in results['tracks']:
print track['name'], '-', track['artists'][0]['name']
if __name__ == '__main__':
if len(sys.argv) < 2:
print(('Usage: {0} artist name'.format(sys.argv[0])))
else:
name = ' '.join(sys.argv[1:])
artist = get_artist(name)
if artist:
show_recommendations_for_artist(artist)
else:
print "Can't find that artist", name