2. API Reference

This page lists all of the last.fm interfaces exposed by the pydrag package.

2.1. Track Class

class pydrag.Track(name, artist, url=None, mbid=None, image=None, playcount=None, userplaycount=None, listeners=None, duration=None, match=None, wiki=None, album=None, top_tags=None, loved=None, timestamp=None, rank=None)[source]

Bases: pydrag.services.ApiMixin, pydrag.models.common.BaseModel

Last.FM track, chart and geo api wrapper.

Parameters
property date: Optional[datetime.datetime]

If the timestamp property is available return a datetime instance.

Return type

datetime.datetime

classmethod from_dict(data)[source]

Construct a BaseModel from a dictionary based on the class fields type annotations. Only primitive types are supported.

Parameters

data (Type[BaseModel]) –

Return type

BaseModel

classmethod find(artist, track, user=None, lang='en')[source]

Get the metadata for a track.

Parameters
  • artist (str) – The artist name

  • track (str) – The track name

  • user (Optional[str]) – The username for the context of the request. If supplied, response will include the user’s playcount for this track

  • lang (str) – The language to return the biography in, ISO 639

Return type

Track

classmethod find_by_mbid(mbid, user=None, lang='en')[source]

Get the metadata for a track.

Parameters
  • mbid (str) – The musicbrainz id for the track

  • user (Optional[str]) – The username for the context of the request. If supplied, response will include the user’s playcount for this track

  • lang (str) – The language to return the biography in, ISO 639

Return type

Track

get_info(user=None, lang='en')[source]

There are many ways we end up with an incomplete instance of a track instance likes charts, tags etc, This is a quick method to refresh our object with complete data from the find methods.

Parameters
  • user (Optional[str]) – The username for the context of the request. If supplied, response will include the user’s playcount and loved status

  • lang (str) – The language to return the biography in, ISO-639

Return type

Track

classmethod get_correction(track, artist)[source]

Use the last.fm corrections data to check whether the supplied track has a correction to a canonical track.

Return type

Track

classmethod search(track, limit=50, page=1)[source]

Search for an track by name. Returns track matches sorted by relevance.

Parameters
  • track (str) – The track name.

  • page (int) – The page number to fetch.

  • limit (int) – The number of results to fetch per page.

Return type

pydrag.models.common.ListModel of Track

classmethod get_top_tracks_by_country(country, limit=50, page=1)[source]
Parameters
  • country (str) – The country to fetch the top tracks.

  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

pydrag.models.common.ListModel of Track

classmethod get_top_tracks_chart(limit=50, page=1)[source]

Get the top tracks chart.

Parameters
  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

pydrag.models.common.ListModel of Track

add_tags(tags)[source]

Tag an track with one or more user supplied tags.

Parameters

tags (list of str) – A list of user supplied tags to apply to this track. Accepts a maximum of 10 tags.

Return type

RawResponse

remove_tag(tag)[source]

Remove a user’s tag from an track.

Parameters

tag (str) – A single user tag to remove from this track.

Return type

RawResponse

get_similar(limit=50)[source]

Get all the tracks similar to this track.

Parameters

limit (int) – Limit the number of similar tracks returned

Return type

pydrag.models.common.ListModel of Track

get_tags(user)[source]

Get the tags applied by an individual user to an track on Last.fm.

Parameters

user (str) – The username for the context of the request.

Return type

pydrag.models.common.ListModel of Tag

get_top_tags()[source]

Get the top tags for an track on Last.fm, ordered by popularity.

Return type

pydrag.models.common.ListModel of Tag

love()[source]

Love a track for a user profile.

Return type

RawResponse

unlove()[source]

Unlove a track for a user profile.

Return type

RawResponse

classmethod scrobble_tracks(tracks, batch_size=10)[source]

Split tracks into the desired batch size, with maximum size set to 50 and send the tracks for processing, I am debating if this even belongs here.

Parameters
  • tracks (List[ScrobbleTrack]) – The tracks to scrobble

  • batch_size – The number of tracks to submit per cycle

Return type

pydrag.models.common.ListModel of ScrobbleTrack

classmethod update_now_playing(artist, track, album=None, track_number=None, context=None, duration=None, album_artist=None)[source]
Parameters
  • artist (str) – The artist name

  • track (str) – The track name

  • album (Optional[str]) – The album name

  • track_number (Optional[int]) – The track number of the track on the album

  • context (Optional[str]) – Sub-client version (not public)

  • duration (Optional[int]) – The length of the track in seconds

  • album_artist (Optional[str]) – The album artist

Return type

RawResponse

2.2. Album Class

class pydrag.Album(name, mbid=None, url=None, image=None, playcount=None, artist=None, listeners=None, tags=None, tracks=None, wiki=None, rank=None)[source]

Bases: pydrag.models.common.BaseModel, pydrag.services.ApiMixin

Last.FM track, chart and geo api wrapper.

Parameters
classmethod from_dict(data)[source]

Construct a BaseModel from a dictionary based on the class fields type annotations. Only primitive types are supported.

Parameters

data (Type[BaseModel]) –

Return type

BaseModel

classmethod find(artist, album, user=None, lang='en')[source]

Get the metadata and tracklist for an album on Last.fm.

Parameters
  • album (str) – The album name to find.

  • artist (str) – The album artist to find.

  • user (Optional[str]) – The username for the context of the request. If supplied, response will include the user’s playcount for this album

  • lang (str) – The language to return the biography in, ISO-639

Return type

Album

classmethod find_by_mbid(mbid, user=None, lang='en')[source]

Get the metadata and tracklist for an album on Last.fm.

Parameters
  • mbid (str) – The musicbrainz id for the album.

  • user (Optional[str]) – The username for the context of the request. If supplied, response will include the user’s playcount for this album

  • lang (str) – The language to return the biography in, ISO-639

Return type

Album

get_info(user=None, lang='en')[source]

There are many ways we end up with an incomplete instance of an album instance likes charts, tags etc, This is a quick method to refresh our object with complete data from the find methods.

Parameters
  • user (Optional[str]) – The username for the context of the request. If supplied, response will include the user’s playcount

  • lang (str) – The language to return the biography in, ISO-639

Return type

Album

classmethod search(album, limit=50, page=1)[source]

Search for an album by name.Returns album matches sorted by relevance.

Parameters
  • album (str) – The album name to search.

  • page (int) – The page number to fetch.

  • limit (int) – The number of results to fetch per page.

Return type

pydrag.models.common.ListModel of Album

add_tags(tags)[source]

Tag an album using a list of user supplied tags.

Parameters

tags (List[str]) – A list of user supplied tags to apply to this album. Accepts a maximum of 10 tags.

Return type

RawResponse

remove_tag(tag)[source]

Remove a user’s tag from an album.

Parameters

tag (str) – A single user tag to remove from this album.

Return type

RawResponse

get_tags(user)[source]

Get the tags applied by an individual user to an album on Last.fm.

Parameters

user (str) – The username for the context of the request.

Return type

pydrag.models.common.ListModel of Tag

get_top_tags()[source]

Get the top tags for an album on Last.fm, ordered by popularity.

Return type

pydrag.models.common.ListModel of Tag

2.3. Artist Class

class pydrag.Artist(name, mbid=None, url=None, tag_count=None, listeners=None, playcount=None, userplaycount=None, image=None, match=None, tags=None, bio=None, on_tour=None, similar=None, rank=None)[source]

Bases: pydrag.models.common.BaseModel, pydrag.services.ApiMixin

Last.FM track, chart and geo api wrapper.

Parameters
classmethod from_dict(data)[source]

Construct a BaseModel from a dictionary based on the class fields type annotations. Only primitive types are supported.

Parameters

data (Type[BaseModel]) –

Return type

BaseModel

classmethod find(artist, user=None, lang='en')[source]

Get the metadata for an artist. Includes biography, truncated at 300 characters.

Parameters
  • artist (str) – The artist name to retrieve.

  • user (Optional[str]) – The username for the context of the request. If supplied, response will include the user’s playcount

  • lang (str) – The language to return the biography in, ISO-639

Return type

Artist

classmethod find_by_mbid(mbid, user=None, lang='en')[source]

Get the metadata for an artist. Includes biography, truncated at 300 characters.

Parameters
  • mbid (str) – The musicbrainz id for the artist

  • user (Optional[str]) – The username for the context of the request. If supplied, response will include the user’s playcount

  • lang (str) – The language to return the biography in, ISO-639

Return type

Artist

get_info(user=None, lang='en')[source]

There are many ways we end up with an incomplete instance of an artist instance likes charts, tags etc, This is a quick method to refresh our object with complete data from the find methods.

Parameters
  • user (Optional[str]) – The username for the context of the request. If supplied, response will include the user’s playcount

  • lang (str) – The language to return the biography in, ISO-639

Return type

Artist

classmethod search(artist, limit=50, page=1)[source]

Search for an artist by name. Returns artist matches sorted by relevance.

Parameters
  • artist (str) – The artist name to search.

  • page (int) – The page number to fetch.

  • limit (int) – The number of results to fetch per page.

Return type

pydrag.models.common.ListModel of Artist

classmethod get_top_artists_by_country(country, limit=50, page=1)[source]
Parameters
  • country (str) – The country name to fetch results.

  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

pydrag.models.common.ListModel of Artist

classmethod get_top_artists_chart(limit=50, page=1)[source]

Get the top artists chart.

Parameters
  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

pydrag.models.common.ListModel of Artist

add_tags(tags)[source]

Tag an artist with one or more user supplied tags.

Parameters

tags (List[str]) – A list of user supplied tags to apply to this artist. Accepts a maximum of 10 tags.

Return type

RawResponse

remove_tag(tag)[source]

Remove a user’s tag from an artist.

Parameters

tag (str) – A single user tag to remove from this artist.

Return type

RawResponse

get_correction()[source]

Use the last.fm corrections data to check whether the supplied artist has a correction to a canonical artist.

Return type

Artist

get_similar(limit=50)[source]

Get all the artists similar to this artist.

Parameters

limit (int) – Limit the number of similar artists returned

Return type

pydrag.models.common.ListModel of Artist

get_tags(user)[source]

Get the tags applied by an individual user to an artist on Last.fm.

Parameters

user (str) – The username for the context of the request.

Return type

pydrag.models.common.ListModel of Tag

get_top_tags()[source]

Get the top tags for an artist on Last.fm, ordered by popularity.

Return type

pydrag.models.common.ListModel of Tag

get_top_tracks(limit=50, page=1)[source]

Get the top tags for an artist on Last.fm, ordered by popularity.

Parameters
  • page (int) – The page number to fetch.

  • limit (int) – The number of results to fetch per page.

Return type

pydrag.models.common.ListModel of Track

2.4. User Class

class pydrag.User(playlists, playcount, gender, name, url, country, image, age, registered, real_name=None, recent_track=None)[source]

Bases: pydrag.models.common.BaseModel, pydrag.services.ApiMixin

Last.FM user and user library api wrapper.

Parameters
  • playcount (int) – Total track playcount

  • gender (str) – Gender

  • name (str) – Display name

  • url (str) – Last.fm profile url

  • country (str) – Country name

  • image (List[Image]) – User’s avatar in multiple sizes

  • age (int) – Self explanatory

  • registered (int) – Unix timestamp of the registration date

  • real_name (Optional[str]) – The full name

  • recent_track (Optional[Track]) – User’s most recent scrobble track

property date_registered: datetime.datetime

Return a datetime instance of the user’s registration date.

Return type

datetime.datetime

classmethod from_dict(data)[source]

Construct a BaseModel from a dictionary based on the class fields type annotations. Only primitive types are supported.

Parameters

data (Type[BaseModel]) –

Return type

BaseModel

classmethod find(username)[source]

Get information about a user profile.

Return type

User

get_artists(limit=50, page=1)[source]

Retrieve a paginated list of all the artists in the user’s library, with playcounts and tagcounts.

Parameters
  • page (int) – The page number to fetch.

  • limit (int) – The number of results to fetch per page.

Return type

ListModel of Artist

get_artist_tracks(artist, from_date=None, to_date=None, page=1)[source]

Get a list of tracks by a given artist scrobbled by this user, including scrobble time. Can be limited to specific timeranges, defaults to all time.

Parameters
  • artist (str) – The artist name you are interested in

  • from_date (Optional[str]) – An unix timestamp to start at.

  • to_date (Optional[str]) – An unix timestamp to end at.

  • page (int) – The page number to fetch.

Return type

ListModel of Track

get_friends(recent_tracks, limit=50, page=1)[source]

Get a list of the user’s friends on Last.fm.

Parameters
  • recent_tracks (bool) – Whether or not to include information about friends’ recent listening in the response.

  • page (int) – The page number to fetch.

  • limit (int) – The number of results to fetch per page.

Return type

ListModel of User

get_loved_tracks(limit=50, page=1)[source]

Get the user’s loved tracks list.

Parameters
  • page (int) – The page number to fetch.

  • limit (int) – The number of results to fetch per page.

Return type

ListModel of Track

get_personal_tags(tag, category, limit=50, page=1)[source]

Get the user’s personal tags.

Parameters
  • tag (str) – The tag you’re interested in.

  • category (str) – The type of items which have been tagged

  • page (int) – The page number to fetch.

  • limit (int) – The number of results to fetch per page.

Return type

ListModel of Track or Artist or Album

get_recent_tracks(from_date=None, to_date=None, limit=50, page=1)[source]

Get a list of the recent tracks listened to by this user. Also includes the currently playing track with the nowplaying=”true” attribute if the user is currently listening.

Parameters
  • from_date (Optional[str]) – Beginning timestamp of a range - only display scrobbles after this time, in UNIX timestamp format (integer number of seconds since 00:00:00, January 1st 1970 UTC). This must be in the UTC time zone.

  • to_date (Optional[str]) – End timestamp of a range - only display scrobbles before this time, in UNIX timestamp format (integer number of seconds since 00:00:00, January 1st 1970 UTC). This must be in the UTC time zone.

  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

ListModel of Track

get_top_albums(period, limit=50, page=1)[source]

Get the top albums listened to by a user. You can stipulate a time period.

Parameters
  • period (Period) –

  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

ListModel of Album

Return type

List[Album]

get_top_artists(period, limit=50, page=1)[source]

Get the top artists listened to by a user. You can stipulate a time period.

Parameters
  • period (Period) –

  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

ListModel of Artist

get_top_tags(limit=50)[source]

Get the top tags used by this user.

Parameters

limit (int) – Limit the number of tags returned

Return type

ListModel of Tag

Return type

List[Tag]

get_top_tracks(period, limit=50, page=1)[source]

Get the top tracks listened to by a user. You can stipulate a time period.

Parameters
  • period (Period) –

  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

ListModel of Track

get_weekly_album_chart(from_date=None, to_date=None)[source]
Parameters
  • from_date (Optional[str]) – The date at which the chart should start from.

  • to_date (Optional[str]) – The date at which the chart should end on.

Return type

ListModel of Album

get_weekly_artist_chart(from_date=None, to_date=None)[source]

Get an album chart for a user profile, for a given date range. If no date range is supplied, it will return the most recent album chart for this user.

Parameters
  • from_date (Optional[str]) – The date at which the chart should start from.

  • to_date (Optional[str]) – The date at which the chart should end on.

Return type

ListModel of Artist

get_weekly_chart_list()[source]

Get an artist chart for a user profile, for a given date range. If no date range is supplied, it will return the most recent artist chart for this user.

Return type

ListModel of Chart

get_weekly_track_chart(from_date=None, to_date=None)[source]

Get a list of available charts for this user, expressed as date ranges which can be sent to the chart services.

Parameters
  • from_date (Optional[str]) – The date at which the chart should start from.

  • to_date (Optional[str]) – The date at which the chart should end on.

Return type

ListModel of Track

2.5. Tag Class

class pydrag.Tag(name, reach=None, url=None, taggings=None, count=None, total=None, wiki=None)[source]

Bases: pydrag.models.common.BaseModel, pydrag.services.ApiMixin

Last.FM tag, chart and geo api wrapper.

Parameters
classmethod from_dict(data)[source]

Construct a BaseModel from a dictionary based on the class fields type annotations. Only primitive types are supported.

Parameters

data (Type[BaseModel]) –

Return type

BaseModel

classmethod find(name, lang=None)[source]

Get the metadata for a tag.

Parameters
  • name (str) – The tag name

  • lang (Optional[str]) – The language to return the wiki in, ISO-639

Return type

Tag

classmethod get_top_tags(limit=50, page=1)[source]

Fetches the top global tags on Last.fm, sorted by popularity Old school pagination on this endpoint, keep uniformity.

Parameters
  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

pydrag.models.common.ListModel of Tag

classmethod get_top_tags_chart(limit=50, page=1)[source]

Get the top tags chart.

Parameters
  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

pydrag.models.common.ListModel of Tag

get_similar()[source]

Search for tags similar to this one. Returns tags ranked by similarity, based on listening data.

Return type

pydrag.models.common.ListModel of Tag

get_top_albums(limit=50, page=1)[source]

Get the top albums tagged by this tag, ordered by tag count.

Parameters
  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

pydrag.models.common.ListModel of Album

get_top_artists(limit=50, page=1)[source]

Get the top artists tagged by this tag, ordered by tag count.

Parameters
  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

pydrag.models.common.ListModel of Artist

get_top_tracks(limit=50, page=1)[source]

Get the top tracks tagged by this tag, ordered by tag count.

Parameters
  • limit (int) – The number of results to fetch per page.

  • page (int) – The page number to fetch.

Return type

pydrag.models.common.ListModel of Track

get_weekly_chart_list()[source]

Get a list of available charts for this tag, expressed as date ranges which can be sent to the chart services.

Return type

pydrag.models.common.ListModel of Chart

2.6. Common Classes

class pydrag.models.common.BaseModel[source]

Bases: object

Pydrag Base Model.

Parameters

params – The params used to fetch the api response data

to_dict()[source]

Convert our object to a traditional dictionary. Filter out None values and dictionary values. The last one is like a validation for the unit tests in case we forgot to properly deserialize an dict to an object.

Return type

Dict

classmethod from_dict(data)[source]

Construct a BaseModel from a dictionary based on the class fields type annotations. Only primitive types are supported.

Parameters

data (Type[BaseModel]) –

Return type

BaseModel

class pydrag.models.common.ListModel(data=<factory>, page=None, limit=None, total=None, tag=None, user=None, artist=None, track=None, album=None, country=None, from_date=None, to_date=None, search_terms=None)[source]

Bases: collections.UserList, Sequence[T], pydrag.models.common.BaseModel

Wrap a list of BaseModel objects with metadata.

Parameters
classmethod from_dict(data)[source]

Construct a BaseModel from a dictionary based on the class fields type annotations. Only primitive types are supported.

Parameters

data (Type[BaseModel]) –

Return type

BaseModel

class pydrag.models.common.RawResponse(data=None)[source]

Bases: pydrag.models.common.BaseModel

Most of the write operations don’t return any response body but still for consistency we need to return a BaseModel with all the metadata params.

Parameters

data (Optional[Dict]) – The raw response dictionary

to_dict()[source]

Convert our object to a traditional dictionary. Filter out None values and dictionary values. The last one is like a validation for the unit tests in case we forgot to properly deserialize an dict to an object.

Return type

Dict

classmethod from_dict(data)[source]

Construct a BaseModel from a dictionary based on the class fields type annotations. Only primitive types are supported.

Parameters

data (Type[BaseModel]) –

Return type

BaseModel

class pydrag.models.common.Config(api_key, api_secret, username, password, session=None)[source]

Bases: object

Pydrag config object for your last.fm api.

Parameters
  • api_key (str) – Your application api key

  • api_secret (Optional[str]) – Your application api secret

  • username (Optional[str]) – The user’ name you want to authenticate

  • password (Optional[str]) – The user’s password you want to authenticate

  • session (Optional[ForwardRef]) – The already authenticated user’s session key

static instance(api_key=None, api_secret=None, username=None, password=None, session=None)[source]

Get/Create a config instance, if no api key is specified it attempt to read the settings from environmental variables.

class pydrag.models.common.Image(size, text)[source]

Bases: pydrag.models.common.BaseModel

class pydrag.models.common.Chart(text, from_date, to_date)[source]

Bases: pydrag.models.common.BaseModel

Bases: pydrag.models.common.BaseModel

class pydrag.models.common.Wiki(content=None, summary=None, published=None, links=None)[source]

Bases: pydrag.models.common.BaseModel

classmethod from_dict(data)[source]

Construct a BaseModel from a dictionary based on the class fields type annotations. Only primitive types are supported.

Parameters

data (Type[BaseModel]) –

Return type

BaseModel

2.7. Api Mixin

class pydrag.services.ApiMixin[source]

Bases: object

classmethod get_session()[source]

Return the session from configuration or attempt to authenticate the configuration user.

Return type

AuthSession

classmethod retrieve(bind, flatten=None, params=None)[source]

Perform an api retrieve/get resource action.

Parameters
  • bind (BaseModel) – Class type to construct from the api response.

  • flatten (str) – A dot separated string used to flatten nested list of values

  • params (Dict) – A dictionary of query string params

Return type

BaseModel

classmethod submit(bind, flatten=None, params=None, sign=False, stateful=False, authenticate=False)[source]

Perform an api write/update resource action.

Parameters
  • bind (BaseModel) – Class type to construct from the api response.

  • flatten (str) – A dot separated string used to flatten nested list of values

  • params (Dict) – A dictionary of body params

  • sign (bool) – Sign the request with the api secret

  • stateful (bool) – Requires a session

  • authenticate (bool) – Perform an authentication request

Return type

BaseModel

classmethod prepare_params(params, sign, stateful, authenticate)[source]

Perform common parameter tasks before sending the web request.

  • Filter out None values,

  • Set the preferred api format json

  • Add the api key, session or signature based on the state flags

Parameters
  • params (Dict) – A dictionary of body or query string params

  • sign (bool) – Sign the request with the api secret

  • stateful (bool) – Add the session key to the params

  • authenticate (bool) – Add the username and auth token to the params

Return type

Dict

classmethod bind_data(bind, body, flatten=None)[source]

Construct a BaseModel from the response body and the flatten directive.

Parameters
  • bind (BaseModel) – Class type to construct from the api response.

  • body (Dict) – The api response

  • flatten (str) – A dot separated string used to flatten nested list of values

Return type

BaseModel

static raise_for_error(body)[source]

Parse and raise api errors.

Parameters

body (Dict) – Response body

Raise

ApiError

static sign(params)[source]

Last.fm signing formula for webservice calls. Exclude format, sort params, append the api secret key and hash the params string.

Parameters

params (Dict) –

Return type

str