python 2.7 - Get Spotipy album ID from album -


using spotipy i'm trying list tracks supplied name of artist , album.

this should quite straight forward, don't know how albumid in order tracklist. thought like:

sp.album_tracks(q = "album:" + album, type = "album") 

...only doesn't work.

here's i've got far. albums selected artist (hard coded here "phosgore" no particular reason other have 3 albums , didn't want swamped dictionary):

#!/usr/bin/python # -*- coding: utf-8 -*-  # shows album trackname  import sys import spotipy  def get_albums_from_artist_name(name):    results = sp.search(q = "artist:" + name, type = "artist")   items = results["artists"]["items"]   if len(items) == 0:     return none   else:     d = items[0]    # artistid , artist name dict   artid = d["id"]     # 3cf1gbbu9uhls3veyiak2x   aname = d["name"]   # phosgore    artisturi = "spotify:artist:" + artid    results = sp.artist_albums(artisturi, album_type = "album")   albums = results["items"]   while results["next"]:     results = sp.next(results)     albums.extend(results["items"])    unique = set() # ignore duplicate albums   album in albums:     name = album["name"]     if not name in unique:        unique.add(name) # unique set    print ("\nalbums %s:\n" %(aname))   unique = list(unique)   in range(0, len(unique)):     print unique[i]    # let's return list instead of set   return list(unique)  #------------------------------------------------ def get_tracks_from_album(album):   tracks = []   # results = sp.album_tracks(q = "album:" + album, type = "album")   # don't know how album id   # list tracks here   sp = spotipy.spotify() sp.trace = false  ask = "phosgore"  artistalbums = get_albums_from_artist_name(ask)  get_tracks_from_album("pestbringer") 

get uri of album , pass .album_tracks() method:

import spotipy  sp = spotipy.spotify() sp.trace = false  # find album name album = "pestbringer" results = sp.search(q = "album:" + album, type = "album")  # first album uri album_id = results['albums']['items'][0]['uri']  # album tracks tracks = sp.album_tracks(album_id) track in tracks['items']:     print(track['name']) 

prints:

embrace our gift here comes pain pestbringer dein licht aggression incarnate countdown destruction nightmare noise monsters lobotomy holy inquisition tote musikanten 

Comments