Skip to content

Instantly share code, notes, and snippets.

@badalnabizade
Created August 19, 2019 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badalnabizade/6bdc52965798d7aa3508a2c32ee27f88 to your computer and use it in GitHub Desktop.
Save badalnabizade/6bdc52965798d7aa3508a2c32ee27f88 to your computer and use it in GitHub Desktop.
Get albums data for each artist.
def get_artist_albums(artist):
"""
----------
artist: artist's information that obtained by get_artist function.
----------
Returns: dictionary object.
Album names as keys, album ids as values.
"""
albums = []
results = sp.artist_albums(artist['id'], album_type='album')
albums.extend(results['items'])
# By default artist_albums function can obtain up to 50 item in one try.
# If there is more than 50 items, this function will return 'next'.
while results['next']:
results = sp.next(results)
albums.extend(results['items'])
seen = set() # to avoid duplicates.
album_dct = dict() # to keep name and id of album.
albums.sort(key=lambda album:album['name'].lower())
for album in albums:
name = album['name']
album_id = album['id']
if name not in seen:
seen.add(name)
album_dct[name] = album_id
return album_dct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment