Skip to content

Playlists

Playlists(api, subsonic)

Class that contains all the methods needed to interact with the playlists endpoints in the Subsonic API.

Source code in .venv/lib/python3.11/site-packages/knuckles/_playlists.py
def __init__(self, api: Api, subsonic: "Subsonic") -> None:
    self.api = api

    # Only to pass it to the models
    self.subsonic = subsonic

create_playlist(name, comment=None, public=None, song_ids=None)

Create a new playlist for the authenticated user.

Parameters:

Name Type Description Default
name str

The name of the playlist to be created.

required
comment str | None

A comment to be added to the new created playlist.

None
public bool | None

If the song should be public or not.

None
song_ids list[str] | None

A list of ID of the songs that should be included with the playlist.

None

Returns:

Type Description
Playlist

An object that holds all the info about the new created playlist.

Source code in .venv/lib/python3.11/site-packages/knuckles/_playlists.py
def create_playlist(
    self,
    name: str,
    comment: str | None = None,
    public: bool | None = None,
    song_ids: list[str] | None = None,
) -> Playlist:
    """Create a new playlist for the authenticated user.

    Args:
        name: The name of the playlist to be created.
        comment: A comment to be added to the new created playlist.
        public: If the song should be public or not.
        song_ids: A list of ID of the songs that should be included
            with the playlist.

    Returns:
        An object that holds all the info about the new created playlist.
    """

    response = self.api.json_request(
        "createPlaylist", {"name": name, "songId": song_ids}
    )["playlist"]

    new_playlist = Playlist(self.subsonic, **response)

    # Allow to modify comment and public
    # with a workaround using the updatePlaylist endpoint

    if comment or public:
        self.update_playlist(new_playlist.id, comment=comment, public=public)
        new_playlist.comment = comment
        new_playlist.public = public

    return new_playlist

delete_playlist(playlist_id)

Delete a playlist.

Parameters:

Name Type Description Default
playlist_id str

The ID of the playlist to remove.

required

Returns:

Type Description
Subsonic

The Subsonic object where this method was called to allow method chaining.

Source code in .venv/lib/python3.11/site-packages/knuckles/_playlists.py
def delete_playlist(self, playlist_id: str) -> "Subsonic":
    """Delete a playlist.

    Args:
        playlist_id: The ID of the playlist to remove.

    Returns:
        The Subsonic object where this method was called to allow
            method chaining.
    """

    self.api.json_request("deletePlaylist", {"id": playlist_id})

    return self.subsonic

get_playlist(playlist_id)

Get all the info about a playlist available for the authenticated user.

Parameters:

Name Type Description Default
playlist_id str

The ID of the playlist to get its info.

required

Returns:

Type Description
Playlist

An object that holds all the info about the requested playlist.

Source code in .venv/lib/python3.11/site-packages/knuckles/_playlists.py
def get_playlist(self, playlist_id: str) -> Playlist:
    """Get all the info about a playlist available for the authenticated
    user.

    Args:
        playlist_id: The ID of the playlist to get its info.

    Returns:
        An object that holds all the info about the requested playlist.
    """

    response = self.api.json_request("getPlaylist", {"id": playlist_id})["playlist"]

    return Playlist(self.subsonic, **response)

get_playlists(username=None)

Get all the playlists available to the authenticated user.

Parameters:

Name Type Description Default
username str | None

The username of another user if is wanted to get the playlists they can access.

None

Returns:

Type Description
list[Playlist]

A list that holds all the info about all the playlist that the user can play.

Source code in .venv/lib/python3.11/site-packages/knuckles/_playlists.py
def get_playlists(self, username: str | None = None) -> list[Playlist]:
    """Get all the playlists available to the authenticated user.

    Args:
        username: The username of another user if is wanted to get the
            playlists they can access.

    Returns:
        A list that holds all the info about all the playlist
            that the user can play.
    """

    response = self.api.json_request(
        "getPlaylists",
        {"username": username} if username else {},
    )["playlists"]["playlist"]

    playlists = [Playlist(self.subsonic, **playlist) for playlist in response]

    return playlists

update_playlist(playlist_id, name=None, comment=None, public=None, song_ids_to_add=None, song_indexes_to_remove=None)

Update the info of a playlist.

Parameters:

Name Type Description Default
playlist_id str

The ID of the playlist to update its info.

required
name str | None

A new name for the playlist.

None
comment str | None

A new comment for the playlist.

None
public bool | None

Change if the playlist should be public or private.

None
song_ids_to_add list[str] | None

A list of IDs of new songs to be added to the playlist.

None
song_indexes_to_remove list[int] | None

A list in indexes of songs that should be removed from the playlist.

None

Returns:

Type Description
Playlist

An object that holds all the info about the updated playlist.

Source code in .venv/lib/python3.11/site-packages/knuckles/_playlists.py
def update_playlist(
    self,
    playlist_id: str,
    name: str | None = None,
    comment: str | None = None,
    public: bool | None = None,
    song_ids_to_add: list[str] | None = None,
    song_indexes_to_remove: list[int] | None = None,
) -> Playlist:
    """Update the info of a playlist.

    Args:
        playlist_id: The ID of the playlist to update its info.
        name: A new name for the playlist.
        comment: A new comment for the playlist.
        public: Change if the playlist should be public or private.
        song_ids_to_add: A list of IDs of new songs to be added to the
            playlist.
        song_indexes_to_remove: A list in indexes of songs that should
            be removed from the playlist.

    Returns:
        An object that holds all the info about the updated playlist.
    """

    self.api.json_request(
        "updatePlaylist",
        {
            "playlistId": playlist_id,
            "name": name,
            "comment": comment,
            "public": public,
            "songIdToAdd": song_ids_to_add,
            "songIndexToRemove": song_indexes_to_remove,
        },
    )

    return Playlist(
        self.subsonic, id=playlist_id, name=name, comment=comment, public=public
    )