示例#1
0
    def download(self, sp: spotipy.client.Spotify, playlist_id: str) -> None:
        page_size = 100
        offset = 0
        fields = ','.join([
            'items.track.name',
            'items.track.artists.name',
            'items.added_by.id',
            'items.added_at',
        ])
        while True:
            rsp = sp.playlist_items(
                playlist_id,
                limit=page_size,
                offset=offset,
                fields=fields,
                additional_types=['track']
            )
            if not rsp['items']:
                break

            self.__items += [
                PlaylistItem(s['track']['name'], s['added_at'], s['added_by']['id'], s['track']['artists']) 
                for s in rsp['items'] if s['track']
            ]
            offset += page_size
示例#2
0
def get_playlist_tracks(spotify: spotipy.client.Spotify, playlist_id: str) -> list:
    """
    This function takes an authenticated Spotify client, and a playlist ID, and returns a list of song details of every song in the playlist
    Parameters required: Authenticated Spotify Client, and playlist ID or URL
    Return Data: List of song details in the playlist
    """
    # Get first 100 or lesser songs' details
    results = spotify.playlist_items(playlist_id)
    # Check if there are more songs for which details need to be obtained
    tracks = results["items"]
    while results["next"]:
        # Get next 100 songs' details, and append to the list of results already obtained
        results = spotify.next(results)
        tracks.extend(results["items"])
    # Create new list to hold track IDs
    track_id = {}
    # Extract each track detail from the extracted information, and append to track_id list
    track_id["IDs"] = []
    track_id["Name"] = []
    track_id["Artist"] = []
    track_id["Popularity"] = []
    for i in tracks:  # Looping through all tracks
        if i["track"]["id"] != None:
            track_id["IDs"].append(
                "spotify:track:" + i["track"]["id"]
            )  # Get ID of song
            track_id["Name"].append(i["track"]["name"])  # Get Name of song
            track_id["Artist"].append(
                i["track"]["artists"][0]["name"]
            )  # Get main Artist of song
            track_id["Popularity"].append(
                i["track"]["popularity"]
            )  # Get popularity of songs
    # Return all track IDs
    return track_id
示例#3
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