0

I want to create a function that compiles of list of track URIs from a Spotify playlist using Spotipy. The function I have written now works, it's just returning way too much information and I am confused on how to filter out what I want.

I tried indexing the results themselves and messed around with the field parameter in sp.the user_playlist_tracks() function, but nothing seems to be returning ONLY the track URIs.

The below function grabs ever track from the Spotify playlist specified by the 'playlist_id' parameter:

def get_playlist_tracks(auth_client, username, playlist_id):
    # Ref - https://stackoverflow.com/questions/39086287/spotipy-how-to-read-more-than-100-tracks-from-a-playlist?noredirect=1&lq=1
    results = sp.user_playlist_tracks(username,playlist_id)
    tracks = results['items']
    while results['next']:
        results = sp.next(results)
        tracks.extend(results['items'])
    return tracks

The function returns a list of objects in JSON format:

[{'album_type': 'album', 'artists': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/4WVDlzQCHAIWzBP5iZwuOJ'}, 'href': 'https://api.spotify.com/v1/artists/4WVDlzQCHAIWzBP5iZwuOJ', 'id': '4WVDlzQCHAIWzBP5iZwuOJ', 'name': 'Sybyr', 'type': 'artist', 'uri': 'spotify:artist:4WVDlzQCHAIWzBP5iZwuOJ'}], 'available_markets': ['AD', 'AE', 'AR', 'AT', 'AU', 'BE', 'BG', 'BH', 'BO', 'BR', 'CA', 'CH', 'CL', 'CO', 'CR', 'CY', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EC', 'EE', 'EG', 'ES', 'FI', 'FR', 'GB', 'GR', 'GT', 'HK', 'HN', 'HU', 'ID', 'IE', 'IL', 'IN', 'IS', 'IT', 'JO', 'JP', 'KW', 'LB', 'LI', 'LT', 'LU', 'LV', 'MA', 'MC', 'MT', 'MX', 'MY', 'NI', 'NL', 'NO', 'NZ', 'OM', 'PA', 'PE', 'PH', 'PL', 'PS', 'PT', 'PY', 'QA', 'RO', 'SA', 'SE', 'SG', 'SK', 'SV', 'TH', 'TN', 'TR', 'TW', 'US', 'UY', 'VN', 'ZA'], 'external_urls': {'spotify': 'https://open.spotify.com/album/4c64we435X1d5Bz6YAgrOZ'}, 'href': 'https://api.spotify.com/v1/albums/4c64we435X1d5Bz6YAgrOZ', 'id': '4c64we435X1d5Bz6YAgrOZ', 'images': [{'height': 640, 'url': 'https://i.scdn.co/image/e95d7e36e91eebde585841005d5e384b5a751c9f', 'width': 640}, {'height': 300, 'url': 'https://i.scdn.co/image/0fb974f445a9e757456f95875621b471f36e3601', 'width': 300}, {'height': 64, 'url': 'https://i.scdn.co/image/72435c83decf6b49ce5f117ea86d442245c67b97', 'width': 64}], 'name': 'Anti-World', 'release_date': '2016-03-11', 'release_date_precision': 'day', 'total_tracks': 19, 'type': 'album', 'uri': 'spotify:album:4c64we435X1d5Bz6YAgrOZ'}, 'artists': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/4WVDlzQCHAIWzBP5iZwuOJ'}, 'href': 'https://api.spotify.com/v1/artists/4WVDlzQCHAIWzBP5iZwuOJ', 'id': '4WVDlzQCHAIWzBP5iZwuOJ', 'name': 'Sybyr', 'type': 'artist', 'uri': 'spotify:artist:4WVDlzQCHAIWzBP5iZwuOJ'}], 'available_markets': ['AD', 'AE', 'AR', 'AT', 'AU', 'BE', 'BG', 'BH', 'BO', 'BR', 'CA', 'CH', 'CL', 'CO', 'CR', 'CY', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EC', 'EE', 'EG', 'ES', 'FI', 'FR', 'GB', 'GR', 'GT', 'HK', 'HN', 'HU', 'ID', 'IE', 'IL', 'IN', 'IS', 'IT', 'JO', 'JP', 'KW', 'LB', 'LI', 'LT', 'LU', 'LV', 'MA', 'MC', 'MT', 'MX', 'MY', 'NI', 'NL', 'NO', 'NZ', 'OM', 'PA', 'PE', 'PH', 'PL', 'PS', 'PT', 'PY', 'QA', 'RO', 'SA', 'SE', 'SG', 'SK', 'SV', 'TH', 'TN', 'TR', 'TW', 'US', 'UY', 'VN', 'ZA'], 'disc_number': 1, 'duration_ms': 196257, 'episode': False, 'explicit': True, 'external_ids': {'isrc': 'QM42K1611819'}, 'external_urls': {'spotify': 'https://open.spotify.com/track/1t3hLzPZV1BBl9BoQXPHVg'}, 'href': 'https://api.spotify.com/v1/tracks/1t3hLzPZV1BBl9BoQXPHVg', 'id': '1t3hLzPZV1BBl9BoQXPHVg', 'is_local': False, 'name': 'I Like Being', 'popularity': 41, 'preview_url': 'https://p.scdn.co/mp3-preview/d50e51258de77503b0944764adcc67df48e45a5d?cid=de8dcc8f79484e728e1c16279585f8a1', 'track': True, 'track_number': 11, 'type': 'track', 'uri': 'spotify:track:1t3hLzPZV1BBl9BoQXPHVg'}, 'video_thumbnail': {'url': None}}, ....] 

Does anyone know how to index the return results to grab only the track URIs? Or know how to use the field parameter to only grab the track URIs? I've looked through the Spotify API and Spotipy documentation but nothing seems to answer this specific question.

  • so given the playlist id you want that the function return a list of only this field? 'spotify': 'https://open.spotify.com/artist/4WVDlzQCHAIWzBP5iZwuOJ' – S.C.A Sep 05 '19 at 16:03
  • That's the artist URI, I'm interested in grabbing the specific track URI (i.e. 'uri': 'spotify:track:1t3hLzPZV1BBl9BoQXPHVg') – Mikal Hayden-Gates Sep 05 '19 at 17:12

1 Answers1

1

I think I figured it out! When looping through each item in 'results', you can index the the track URIs with item['track']['uri']. Here is the updated get_playlist_tracks() function I have that ignores any local tracks grabbed, and appends the URIs found to an array:

def get_playlist_tracks(auth_client, username, playlist_id):
    # Ref - https://stackoverflow.com/questions/39086287/spotipy-how-to-read-more-than-100-tracks-from-a-playlist?noredirect=1&lq=1
    results = sp.user_playlist_tracks(username,playlist_id)
    playlist_items = results['items']
    uris = []

    while results['next']:
        results = sp.next(results)
        playlist_items.append(results['items'])

    for item in playlist_items:
        is_local = item["is_local"]
        if is_local == True: # Filtering out any local tracks (i.e. not hosted by Spotify)
            continue
        else:
            track_uri = item["track"]["uri"]
            uris.append(track_uri)
    return uris