Skip to content

sok.apis.music.lastfm_api

Last.fm API Implementation Support for music search and metadata.

LastFMApi

class LastFMApi(api_key: str)

Bases: BaseAPI

Last.fm API for music.

Provides search and metadata retrieval from Last.fm.

Initialize Last.fm API.

Parameters

NameTypeDescription
api_keystrLast.fm API key
Source

Attributes

NameDescription
BASE_URL

The base URL for Last.fm API.

api_key

The Last.fm API key.

supported_content_typesproperty

List[ContentType]

Get supported content types.

supported_media_typesproperty

List[MediaType]

Get supported media types.

get_album_infoasync

async def get_album_info(artist: str, album: str) -> Dict[str, Any]:

Get full album details by artist and album name.

Parameters

NameTypeDescription
artiststrArtist name.
albumstrAlbum name.

Returns

  • Dict[str, Any]Dictionary containing album details.
Source

get_artist_infoasync

async def get_artist_info(artist: str) -> Dict[str, Any]:

Get detailed artist information.

Parameters

NameTypeDescription
artiststrArtist name.

Returns

  • Dict[str, Any]Dictionary containing artist details.
Source

get_detailsasync

async def get_details(
item_id: str,
content_type: ContentType,
**kwargs,
) -> Dict[str, Any]:

Retrieve details of an item.

Note: For albums, item_id must be 'Artist|Album' as Last.fm needs both.

Parameters

NameTypeDescription
item_idstrItem identifier.
content_typeContentTypeType of content.
**kwargsAdditional parameters.

Returns

  • Dict[str, Any]Dictionary with item details.
Source
async def get_related_items(
item_id: str,
content_type: ContentType,
**kwargs,
) -> List[Dict[str, Any]]:

Retrieve related items.

For an ALBUM, returns tracks.

Parameters

NameTypeDescription
item_idstrItem identifier.
content_typeContentTypeType of content.
**kwargsAdditional parameters.

Returns

  • List[Dict[str, Any]]List of related items.
Source
async def search(
query: str,
content_type: ContentType,
**kwargs,
) -> Dict[str, Any]:

Search for music content.

Parameters

NameTypeDescription
querystrSearch query.
content_typeContentTypeType of content (ALBUM, ARTIST, TRACK).
**kwargsAdditional parameters including 'limit'.

Returns

  • Dict[str, Any]Dictionary with 'results' key containing search results.
Source

search_albumasync

async def search_album(query: str, limit: int = 20) -> List[Dict[str, Any]]:

Search for albums on Last.fm.

Parameters

NameTypeDefaultDescription
querystrAlbum search query.
limitint20Maximum number of results.

Returns

  • List[Dict[str, Any]]List of album dictionaries.
Source

search_artistasync

async def search_artist(query: str, limit: int = 20) -> List[Dict[str, Any]]:

Search for artists on Last.fm.

Parameters

NameTypeDefaultDescription
querystrArtist search query.
limitint20Maximum number of results.

Returns

  • List[Dict[str, Any]]List of artist dictionaries.
Source

search_trackasync

async def search_track(
title: str,
limit: int = 20,
artist: Optional[str] = None,
) -> List[Dict[str, Any]]:

Search for tracks on Last.fm.

Last.fm does not give the album of a track in its search results.

Parameters

NameTypeDefaultDescription
titlestrTrack title, or free search text.
limitint20Maximum number of results.
artistOptional[str]NoneArtist name, to narrow the search.

Returns

  • List[Dict[str, Any]]List of track dictionaries.
Source
Built with Kiln