mirror of
https://github.com/c0de-archive/spotipy.git
synced 2024-11-04 23:17:48 +00:00
Added artist recs example
This commit is contained in:
parent
5ebd231cbf
commit
b412de8d75
35
examples/artist_recommendations.py
Normal file
35
examples/artist_recommendations.py
Normal file
@ -0,0 +1,35 @@
|
||||
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
|
Loading…
Reference in New Issue
Block a user