Playlists¶
-
class
spotify.Playlist(session, uri=None, sp_playlist=None, add_ref=True)[source]¶ A Spotify playlist.
You can get playlists from the
playlist_container,inbox,get_starred(),search(), etc., or you can create a playlist yourself from a Spotify URI:>>> session = spotify.Session() # ... >>> playlist = session.get_playlist( ... 'spotify:user:fiat500c:playlist:54k50VZdvtnIPt4d8RBCmZ') >>> playlist.load().name u'500C feelgood playlist'
-
property
is_loaded¶ Whether the playlist’s data is loaded.
-
load(timeout=None)[source]¶ Block until the playlist’s data is loaded.
After
timeoutseconds with no resultsTimeoutis raised. IftimeoutisNonethe default timeout is used.The method returns
selfto allow for chaining of calls.
-
property
tracks¶ The playlist’s tracks.
Will always return an empty list if the playlist isn’t loaded.
-
property
tracks_with_metadata¶ The playlist’s tracks, with metadata specific to the playlist as a a list of
PlaylistTrackobjects.Will always return an empty list if the playlist isn’t loaded.
-
property
name¶ The playlist’s name.
Assigning to
namewill rename the playlist.Will always return
Noneif the playlist isn’t loaded.
-
property
collaborative¶ Whether the playlist can be modified by all users or not.
Set to
TrueorFalseto change.
-
set_autolink_tracks(link=True)[source]¶ If a playlist is autolinked, unplayable tracks will be made playable by linking them to other Spotify tracks, where possible.
-
property
description¶ The playlist’s description.
Will return
Noneif the description is unset.
-
image(callback=None)[source]¶ The playlist’s
Image.Due to limitations in libspotify’s API you can’t specify the
ImageSizeof these images.If
callbackisn’tNone, it is expected to be a callable that accepts a single argument, anImageinstance, when the image is done loading.Will always return
Noneif the playlist isn’t loaded or the playlist has no image.
-
property
has_pending_changes¶ Check if the playlist has local changes that has not been acknowledged by the server yet.
-
add_tracks(tracks, index=None)[source]¶ Add the given
tracksto playlist at the givenindex.trackscan either be a singleTrackor a list ofTrackobjects. Ifindexisn’t specified, the tracks are added to the end of the playlist.
-
remove_tracks(indexes)[source]¶ Remove the tracks at the given
indexesfrom the playlist.indexescan be a single index or a list of indexes to remove.
-
reorder_tracks(indexes, new_index)[source]¶ Move the tracks at the given
indexesto anew_indexin the playlist.indexescan be a single index or a list of indexes to move.new_indexmust be equal to or lower than the current playlist length.
-
property
num_subscribers¶ The number of subscribers to the playlist.
The number can be higher than the length of the
subscriberscollection, especially if the playlist got many subscribers.May be zero until you call
update_subscribers()and theSUBSCRIBERS_CHANGEDevent is emitted from the playlist.
-
property
subscribers¶ The canonical usernames of up to 500 of the subscribers of the playlist.
May be empty until you call
update_subscribers()and theSUBSCRIBERS_CHANGEDevent is emitted from the playlist.
-
update_subscribers()[source]¶ Request an update of
num_subscribersand thesubscriberscollection.The
SUBSCRIBERS_CHANGEDevent is emitted from the playlist when the subscriber data has been updated.
-
property
is_in_ram¶ Whether the playlist is in RAM, and not only on disk.
A playlist must currently be in RAM for tracks to be available. A playlist must have been in RAM for other metadata to be available.
By default, playlists are kept in RAM unless
initially_unload_playlistsis set toTruebefore creating theSession. If the playlists are initially unloaded, useset_in_ram()to have a playlist loaded into RAM.
-
set_in_ram(in_ram=True)[source]¶ Control whether or not to keep the playlist in RAM.
See
is_in_ramfor further details.
-
set_offline_mode(offline=True)[source]¶ Mark the playlist to be synchronized for offline playback.
The playlist must be in the current user’s playlist container.
-
property
offline_status¶ The playlist’s
PlaylistOfflineStatus.
-
property
offline_download_completed¶ The download progress for an offline playlist.
A number in the range 0-100. Always
Noneifoffline_statusisn’tPlaylistOfflineStatus.DOWNLOADING.
-
on(event, listener, *user_args)[source]¶ Register a
listenerto be called onevent.The listener will be called with any extra arguments passed to
emit()first, and then the extra arguments passed toon()last.If the listener function returns
False, it is removed and will not be called the next time theeventis emitted.
-
off(event=None, listener=None)[source]¶ Remove a
listenerthat was to be called onevent.If
listenerisNone, all listeners for the giveneventwill be removed.If
eventisNone, all listeners for all events on this object will be removed.
-
call(event, *event_args)¶ Call the single registered listener for
event.The listener will be called with any extra arguments passed to
call()first, and then the extra arguments passed toon()Raises
AssertionErrorif there is none or multiple listeners forevent. Returns the listener’s return value on success.
-
emit(event, *event_args)¶ Call the registered listeners for
event.The listeners will be called with any extra arguments passed to
emit()first, and then the extra arguments passed toon()
-
num_listeners(event=None)¶ Return the number of listeners for
event.Return the total number of listeners for all events on this object if
eventisNone.
-
property
-
class
spotify.PlaylistEvent[source]¶ Playlist events.
Using
Playlistobjects, you can register listener functions to be called when various events occurs in the playlist. This class enumerates the available events and the arguments your listener functions will be called with.Example usage:
import spotify def tracks_added(playlist, tracks, index): print('Tracks added to playlist') session = spotify.Session() # Login, etc... playlist = session.playlist_container[0] playlist.on(spotify.PlaylistEvent.TRACKS_ADDED, tracks_added)
All events will cause debug log statements to be emitted, even if no listeners are registered. Thus, there is no need to register listener functions just to log that they’re called.
-
TRACKS_ADDED= 'tracks_added'¶ Called when one or more tracks have been added to the playlist.
-
TRACKS_REMOVED= 'tracks_removed'¶ Called when one or more tracks have been removed from the playlist.
- Parameters
playlist (
Playlist) – the playlistindexes (list of ints) – indexes of the tracks that were removed
-
TRACKS_MOVED= 'tracks_moved'¶ Called when one or more tracks have been moved within a playlist.
- Parameters
playlist (
Playlist) – the playlistold_indexes (list of ints) – old indexes of the tracks that were moved
new_index (int) – the new index in the playlist the tracks were moved to
-
PLAYLIST_RENAMED= 'playlist_renamed'¶ Called when the playlist has been renamed.
- Parameters
playlist (
Playlist) – the playlist
-
PLAYLIST_STATE_CHANGED= 'playlist_state_changed'¶ Called when the state changed for a playlist.
There are three states that trigger this callback:
Collaboration for this playlist has been turned on or off. See
Playlist.is_collaborative().The playlist started having pending changes, or all pending changes have now been committed. See
Playlist.has_pending_changes.The playlist started loading, or finished loading. See
Playlist.is_loaded.
- Parameters
playlist (
Playlist) – the playlist
-
PLAYLIST_UPDATE_IN_PROGRESS= 'playlist_update_in_progress'¶ Called when a playlist is updating or is done updating.
This is called before and after a series of changes are applied to the playlist. It allows e.g. the user interface to defer updating until the entire operation is complete.
- Parameters
playlist (
Playlist) – the playlistdone (bool) – if the update is completed
-
PLAYLIST_METADATA_UPDATED= 'playlist_metadata_updated'¶ Called when metadata for one or more tracks in the playlist have been updated.
- Parameters
playlist (
Playlist) – the playlist
-
TRACK_CREATED_CHANGED= 'track_created_changed'¶ Called when the create time and/or creator for a playlist entry changes.
-
TRACK_SEEN_CHANGED= 'track_seen_changed'¶ Called when the seen attribute of a playlist entry changes.
- Parameters
playlist (
Playlist) – the playlistindex (int) – the index of the entry in the playlist that was changed
seen (bool) – whether the entry is seen or not
-
DESCRIPTION_CHANGED= 'description_changed'¶ Called when the playlist description has changed.
- Parameters
playlist (
Playlist) – the playlistdescription (string) – the new description
-
IMAGE_CHANGED= 'image_changed'¶ Called when the playlist image has changed.
-
-
class
spotify.PlaylistContainer(session, sp_playlistcontainer, add_ref=True)[source]¶ A Spotify playlist container.
The playlist container can be accessed as a regular Python collection to work with the playlists:
>>> import spotify >>> session = spotify.Session() # Login, etc. >>> container = session.playlist_container >>> container.is_loaded False >>> container.load() [Playlist(u'spotify:user:jodal:playlist:6xkJysqhkj9uwufFbUb8sP'), Playlist(u'spotify:user:jodal:playlist:0agJjPcOhHnstLIQunJHxo'), PlaylistFolder(id=8027491506140518932L, name=u'Shared playlists', type=<PlaylistType.START_FOLDER: 1>), Playlist(u'spotify:user:p3.no:playlist:7DkMndS2KNVQuf2fOpMt10'), PlaylistFolder(id=8027491506140518932L, name=u'', type=<PlaylistType.END_FOLDER: 2>)] >>> container[0] Playlist(u'spotify:user:jodal:playlist:6xkJysqhkj9uwufFbUb8sP')
As you can see, a playlist container can contain a mix of
PlaylistandPlaylistFolderobjects.The container supports operations that changes the container as well.
To add a playlist you can use
append()orinsert()with either the name of a new playlist or an existing playlist object. For example:>>> playlist = session.get_playlist( ... 'spotify:user:fiat500c:playlist:54k50VZdvtnIPt4d8RBCmZ') >>> container.insert(3, playlist) >>> container.append('New empty playlist')
To remove a playlist or folder you can use
remove_playlist(), or:>>> del container[0]
To replace an existing playlist or folder with a new empty playlist with the given name you can use
remove_playlist()andadd_new_playlist(), or:>>> container[0] = 'My other new empty playlist'
To replace an existing playlist or folder with an existing playlist you can :use
remove_playlist()andadd_playlist(), or:>>> container[0] = playlist
-
property
is_loaded¶ Whether the playlist container’s data is loaded.
-
load(timeout=None)[source]¶ Block until the playlist container’s data is loaded.
After
timeoutseconds with no resultsTimeoutis raised. IftimeoutisNonethe default timeout is used.The method returns
selfto allow for chaining of calls.
-
add_new_playlist(name, index=None)[source]¶ Add an empty playlist with
nameat the givenindex.The playlist name must not be space-only or longer than 255 chars.
If the
indexisn’t specified, the new playlist is added at the end of the container.Returns the new playlist.
-
add_playlist(playlist, index=None)[source]¶ Add an existing
playlistto the playlist container at the givenindex.The playlist can either be a
Playlist, or aLinklinking to a playlist.If the
indexisn’t specified, the playlist is added at the end of the container.Returns the added playlist, or
Noneif the playlist already existed in the container. If the playlist already exists, it will not be moved to the givenindex.
-
add_folder(name, index=None)[source]¶ Add a playlist folder with
nameat the givenindex.The playlist folder name must not be space-only or longer than 255 chars.
If the
indexisn’t specified, the folder is added at the end of the container.
-
remove_playlist(index, recursive=False)[source]¶ Remove playlist at the given index from the container.
If the item at the given
indexis the start or the end of a playlist folder, and the other end of the folder is found, it is also removed. The folder content is kept, but is moved one level up the folder hierarchy. IfrecursiveisTrue, the folder content is removed as well.Using
del playlist_container[3]is equivalent toplaylist_container.remove_playlist(3). Similarly,del playlist_container[0:2]is equivalent to calling this method with indexes1and0.
-
move_playlist(from_index, to_index, dry_run=False)[source]¶ Move playlist at
from_indextoto_index.If
dry_runisTruethe move isn’t actually done. It is only checked if the move is possible.
-
get_unseen_tracks(playlist)[source]¶ Get a list of unseen tracks in the given
playlist.The list is a
PlaylistUnseenTracksinstance.The tracks will remain “unseen” until
clear_unseen_tracks()is called on the playlist.
-
on(event, listener, *user_args)[source]¶ Register a
listenerto be called onevent.The listener will be called with any extra arguments passed to
emit()first, and then the extra arguments passed toon()last.If the listener function returns
False, it is removed and will not be called the next time theeventis emitted.
-
off(event=None, listener=None)[source]¶ Remove a
listenerthat was to be called onevent.If
listenerisNone, all listeners for the giveneventwill be removed.If
eventisNone, all listeners for all events on this object will be removed.
-
append(value)¶ S.append(value) – append value to the end of the sequence
-
call(event, *event_args)¶ Call the single registered listener for
event.The listener will be called with any extra arguments passed to
call()first, and then the extra arguments passed toon()Raises
AssertionErrorif there is none or multiple listeners forevent. Returns the listener’s return value on success.
-
clear() → None -- remove all items from S¶
-
count(value) → integer -- return number of occurrences of value¶
-
emit(event, *event_args)¶ Call the registered listeners for
event.The listeners will be called with any extra arguments passed to
emit()first, and then the extra arguments passed toon()
-
extend(values)¶ S.extend(iterable) – extend sequence by appending elements from the iterable
-
index(value[, start[, stop]]) → integer -- return first index of value.¶ Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
-
num_listeners(event=None)¶ Return the number of listeners for
event.Return the total number of listeners for all events on this object if
eventisNone.
-
pop([index]) → item -- remove and return item at index (default last).¶ Raise IndexError if list is empty or index is out of range.
-
remove(value)¶ S.remove(value) – remove first occurrence of value. Raise ValueError if the value is not present.
-
reverse()¶ S.reverse() – reverse IN PLACE
-
property
-
class
spotify.PlaylistContainerEvent[source]¶ Playlist container events.
Using
PlaylistContainerobjects, you can register listener functions to be called when various events occurs in the playlist container. This class enumerates the available events and the arguments your listener functions will be called with.Example usage:
import spotify def container_loaded(playlist_container): print('Playlist container loaded') session = spotify.Session() # Login, etc... session.playlist_container.on( spotify.PlaylistContainerEvent.CONTAINER_LOADED, container_loaded)
All events will cause debug log statements to be emitted, even if no listeners are registered. Thus, there is no need to register listener functions just to log that they’re called.
-
PLAYLIST_ADDED= 'playlist_added'¶ Called when a playlist is added to the container.
- Parameters
playlist_container (
PlaylistContainer) – the playlist containerplaylist (
Playlist) – the added playlistindex (int) – the index the playlist was added at
-
PLAYLIST_REMOVED= 'playlist_removed'¶ Called when a playlist is removed from the container.
- Parameters
playlist_container (
PlaylistContainer) – the playlist containerplaylist (
Playlist) – the removed playlistindex (int) – the index the playlist was removed from
-
PLAYLIST_MOVED= 'playlist_moved'¶ Called when a playlist is moved in the container.
- Parameters
playlist_container (
PlaylistContainer) – the playlist containerplaylist (
Playlist) – the moved playlistold_index (int) – the index the playlist was moved from
new_index (int) – the index the playlist was moved to
-
CONTAINER_LOADED= 'container_loaded'¶ Called when the playlist container is loaded.
- Parameters
playlist_container (
PlaylistContainer) – the playlist container
-
-
class
spotify.PlaylistFolder[source]¶ An object marking the start or end of a playlist folder.
-
id¶ An opaque ID that matches the ID of the
PlaylistFolderobject at the other end of the folder.
-
name¶ Name of the playlist folder. This is an empty string for the
END_FOLDER.
-
type¶ The
PlaylistTypeof the folder. EitherSTART_FOLDERorEND_FOLDER.
-
-
class
spotify.PlaylistPlaceholder[source]¶ An object marking an unknown entry in the playlist container.
-
class
spotify.PlaylistTrack(session, sp_playlist, index)[source]¶ A playlist track with metadata specific to the playlist.
Use
tracks_with_metadatato get a list ofPlaylistTrack.-
property
create_time¶ When the track was added to the playlist, as seconds since Unix epoch.
-
property
seen¶ Whether the track is marked as seen or not.
-
property
message¶ A message attached to the track. Typically used in the inbox.
-
property
-
class
spotify.PlaylistUnseenTracks(session, sp_playlistcontainer, sp_playlist)[source]¶ A list of unseen tracks in a playlist.
The list may contain items that are
None.Returned by
PlaylistContainer.get_unseen_tracks().-
count(value) → integer -- return number of occurrences of value¶
-
index(value[, start[, stop]]) → integer -- return first index of value.¶ Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
-