Skip to content

Instantly share code, notes, and snippets.

@badalnabizade
Created August 19, 2019 19:38
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/5fc2d8499e5a5003c2daa9651954ffb1 to your computer and use it in GitHub Desktop.
Save badalnabizade/5fc2d8499e5a5003c2daa9651954ffb1 to your computer and use it in GitHub Desktop.
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