Search code examples
pythonspotifyspotipy

read response from successful spotify api post (create playlist)


sorry i think this is pretty basic but i've spent a while trying to find an answer and i can't work it out.

i'm using spotify api / spotipy to make a playlist, it works and the playlist is created, but i want to retrieve the playlist_id so i can then add tracks to it, but i can't work out how to get any response information from the api.

def make_playlist(name='python_play'):
    user = sp.me()['id']
    sp.user_playlist_create(user=user,name=name)
response = make_playlist()
pprint (response)

returns "None"

i thought i would call response.text or repsonse.contents or something like that but response is none-type and i can't call anything from it?

i've been happily retrieving info from the api and then using that info to populate a db via sqlalchemy, but i don't understand how to actually get a response when i create the playlist..

example of successful api calls for info/db population:

def get_artists(num=1):
    '''takes num, retrieves num artists, returns list of dicts'''
    if num <=50:
        limit = num
    else:
        limit = 50
    artist_list = []
    after = 0
    for offset in range(0, num, 50):
        response = sp.current_user_followed_artists(limit=limit, after=after)
        for artist in response['artists']['items']:
            artist_name = artist['name']
            artist_id = artist['id']
            artist_dict={"artist name": artist_name,
                         "artist id": artist_id}
            artist_list.append(artist_dict)
            after = artist_id
    return artist_list


def new_artist(artist_list):
    '''takes list of artists, writes to db'''
    session = Session()
    artist_list = artist_list
    for artist in artist_list:
        new_artist = Artist(artist_name=artist['artist name'], artist_id=artist['artist id'])
        session.merge(new_artist)
        session.commit()
    session.close()


def populate_artists(num=1000):
    '''takes num_artists, calls get_artists, calls new_artist'''
    artist_list = get_artists(num)
    new_artist(artist_list)
    return artist_list


artist_list = populate_artists(10)

Solution

  • What @StayPerfect means is, you forgot to return the response of sp.user_playlist_create() in row 3:

    def make_playlist(name='python_play'):
        user = sp.me()['id']
        return sp.user_playlist_create(user=user,name=name)
    response = make_playlist()
    pprint (response)