Example #1
0
def get_playlist_info(sp: spotipy.client.Spotify,
                      playlist_uri: str) -> Tuple[str, list, list, list]:
    """
    Extract track names and URIs from a playlist

    :param sp: spotify client object
    :param playlist_uri: playlist uri
    :return: playlist name, track names, artist names, song uris
    """

    # Initialize vars
    offset = 0
    tracks, uris, names, artists = [], [], [], []

    # Get playlist id and name from URI
    playlist_id = playlist_uri.split(':')[2]
    playlist_name = sp.playlist(playlist_id)['name']

    # Get all tracks in given playlist (max limit is 100 at a time --> use offset)
    while True:
        results = sp.playlist_items(playlist_id, offset=offset)
        tracks += results['items']
        if results['next'] is not None:
            offset += 100
        else:
            break

    # Get track metadata
    for track in tracks:
        names.append(track['track']['name'])
        artists.append(track['track']['artists'][0]['name'])
        uris.append(track['track']['uri'])

    return playlist_name, names, artists, uris
Example #2
0
def _get_playlist_from_spotify(
        spotify: spotipy.client.Spotify,
        spotify_playlist_id: SpotifyPlaylistId) -> PlayList:
    return spotify.playlist(spotify_playlist_id)