Created
August 19, 2019 19:38
-
-
Save badalnabizade/5fc2d8499e5a5003c2daa9651954ffb1 to your computer and use it in GitHub Desktop.
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_album_tracks(album_id): | |
""" | |
Returns: dictionary object. | |
Track ids as keys, track names as values. | |
""" | |
tracks = [] | |
results = sp.album_tracks(album_id) | |
tracks.extend(results['items']) | |
# By default album_tracks function can obtain up to 50 item from spotify API in one try. | |
# If there is more than 50 items, this function will return 'next'. | |
while results['next']: | |
results = sp.next(results) | |
tracks.extend(results['items']) | |
seen = set() # to avoid duplicates | |
track_dct = dict() # to keep name and id of albums. | |
tracks.sort(key=lambda track:track['name'].lower()) # sort tracks by name. | |
for track in tracks: | |
name = track['name'] | |
track_id = track['id'] | |
if name not in seen: | |
seen.add(name) | |
track_dct[track_id] = name | |
return track_dct |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment