Skip to content

album

Artist module for retrieving artist data from Spotify.

Album(name, url, urls, songs, artist) dataclass ¤

Bases: SongList

Album class for retrieving album data from Spotify.

get_metadata(url) staticmethod ¤

Get metadata for album.

Arguments¤
  • url: The URL of the album.
Returns¤
  • A dictionary with metadata.
Source code in spotdl/types/album.py
 28
 29
 30
 31
 32
 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
107
108
109
110
111
112
113
114
115
116
117
118
@staticmethod
def get_metadata(url: str) -> Tuple[Dict[str, Any], List[Song]]:
    """
    Get metadata for album.

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

    ### Returns
    - A dictionary with metadata.
    """

    spotify_client = SpotifyClient()

    album_metadata = spotify_client.album(url)
    if album_metadata is None:
        raise AlbumError(
            "Couldn't get metadata, check if you have passed correct album id"
        )

    metadata = {
        "name": album_metadata["name"],
        "artist": album_metadata["artists"][0],
        "url": url,
    }

    album_response = spotify_client.album_tracks(url)
    if album_response is None:
        raise AlbumError(
            "Couldn't get metadata, check if you have passed correct album id"
        )

    tracks = album_response["items"]

    # Get all tracks from album
    while album_response["next"]:
        album_response = spotify_client.next(album_response)

        # Failed to get response, break the loop
        if album_response is None:
            break

        tracks.extend(album_response["items"])

    if album_response is None:
        raise AlbumError(f"Failed to get album response: {url}")

    songs = []
    for track in tracks:
        if not isinstance(track, dict) or track.get("is_local"):
            continue

        release_date = album_metadata["release_date"]
        artists = artists = [artist["name"] for artist in track["artists"]]

        song = Song.from_missing_data(
            name=track["name"],
            artists=artists,
            artist=artists[0],
            album_id=album_metadata["id"],
            album_name=album_metadata["name"],
            album_artist=album_metadata["artists"][0]["name"],
            album_type=album_metadata["album_type"],
            disc_number=track["disc_number"],
            disc_count=int(album_metadata["tracks"]["items"][-1]["disc_number"]),
            duration=int(track["duration_ms"] / 1000),
            year=release_date[:4],
            date=release_date,
            track_number=track["track_number"],
            tracks_count=album_metadata["total_tracks"],
            song_id=track["id"],
            explicit=track["explicit"],
            publisher=album_metadata["label"],
            url=track["external_urls"]["spotify"],
            cover_url=(
                max(
                    album_metadata["images"], key=lambda i: i["width"] * i["height"]
                )["url"]
                if album_metadata["images"]
                else None
            ),
            copyright_text=(
                album_metadata["copyrights"][0]["text"]
                if album_metadata["copyrights"]
                else None
            ),
        )

        songs.append(song)

    return metadata, songs

AlbumError ¤

Bases: Exception

Base class for all exceptions related to albums.