Skip to content

artist

Artist module for retrieving artist data from Spotify.

Artist(name, url, urls, songs, genres, albums) dataclass ¤

Bases: SongList

Artist class. Contains all the information about an artist. Frozen to prevent accidental modification.

get_metadata(url) staticmethod ¤

Get metadata for artist.

Arguments¤
  • url: The URL of the artist.
Returns¤
  • Dict with metadata for artist.
Source code in spotdl/types/artist.py
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
@staticmethod
def get_metadata(url: str) -> Tuple[Dict[str, Any], List[Song]]:
    """
    Get metadata for artist.

    ### Arguments
    - url: The URL of the artist.

    ### Returns
    - Dict with metadata for artist.
    """

    # query spotify for artist details
    spotify_client = SpotifyClient()

    # get artist info
    raw_artist_meta = spotify_client.artist(url)

    if raw_artist_meta is None:
        raise ArtistError(
            "Couldn't get metadata, check if you have passed correct artist id"
        )

    artist_albums = spotify_client.artist_albums(url, album_type="album,single")
    # check if there is response
    if not artist_albums:
        raise ArtistError(
            "Couldn't get albums, check if you have passed correct artist id"
        )

    # get artist albums and remove duplicates
    # duplicates can occur if the artist has the same album available in
    # different countries
    albums: List[str] = []
    known_albums: Set[str] = set()
    for album in artist_albums["items"]:
        albums.append(album["external_urls"]["spotify"])
        known_albums.add(slugify(album["name"]))

    # Fetch all artist albums
    while artist_albums and artist_albums["next"]:
        artist_albums = spotify_client.next(artist_albums)
        if artist_albums is None:
            break

        for album in artist_albums["items"]:
            album_name = slugify(album["name"])

            if album_name not in known_albums:
                albums.append(album["external_urls"]["spotify"])
                known_albums.add(album_name)

    songs = []
    for album in albums:
        album_obj = Album.from_url(album, fetch_songs=False)
        songs.extend(album_obj.songs)

    # Very aggressive deduplication
    songs_list = []
    songs_names = set()
    for song in songs:
        slug_name = slugify(song.name)
        if song.name not in songs_names:
            songs_list.append(song)
            songs_names.add(slug_name)

    metadata = {
        "name": raw_artist_meta["name"],
        "genres": raw_artist_meta["genres"],
        "url": url,
        "albums": albums,
    }

    return metadata, songs_list

ArtistError ¤

Bases: Exception

Base class for all exceptions related to artists.