spotipy/examples/artist_discography.py

60 lines
1.6 KiB
Python
Raw Normal View History

2014-05-19 10:49:08 +00:00
import sys
import spotipy
''' shows the albums and tracks for a given artist.
'''
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_album_tracks(album):
tracks = []
results = sp.album_tracks(album['id'])
tracks.extend(results['items'])
while results['next']:
results = sp.next(results)
tracks.extend(results['items'])
for track in tracks:
2015-06-05 09:07:28 +00:00
print(' ', track['name'])
print()
print(track)
2014-05-19 10:49:08 +00:00
def show_artist_albums(id):
albums = []
results = sp.artist_albums(artist['id'], album_type='album')
albums.extend(results['items'])
while results['next']:
results = sp.next(results)
albums.extend(results['items'])
2015-06-05 09:07:28 +00:00
print('Total albums:', len(albums))
2014-05-19 10:49:08 +00:00
unique = set() # skip duplicate albums
for album in albums:
name = album['name']
if not name in unique:
2015-06-05 09:07:28 +00:00
print(name)
2014-05-19 10:49:08 +00:00
unique.add(name)
show_album_tracks(album)
def show_artist(artist):
2015-06-05 09:07:28 +00:00
print('====', artist['name'], '====')
print('Popularity: ', artist['popularity'])
2014-05-19 10:49:08 +00:00
if len(artist['genres']) > 0:
2015-06-05 09:07:28 +00:00
print('Genres: ', ','.join(artist['genres']))
2014-05-19 10:49:08 +00:00
if __name__ == '__main__':
sp = spotipy.Spotify()
sp.trace = False
if len(sys.argv) < 2:
2015-06-05 09:07:28 +00:00
print(('Usage: {0} artist name'.format(sys.argv[0])))
2014-05-19 10:49:08 +00:00
else:
name = ' '.join(sys.argv[1:])
artist = get_artist(name)
show_artist(artist)
show_artist_albums(artist)