Created
August 19, 2019 14:45
-
-
Save badalnabizade/6bdc52965798d7aa3508a2c32ee27f88 to your computer and use it in GitHub Desktop.
Get albums data for each artist.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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