Session handling

The session handling is usually done by inheriting the spotify.manager.SpotifySessionManager class from the spotify.manager module. Then the manager’s connect() method calls the Session.create() and Session.login() functions.

The Session class

class spotify.Session

A Spotify session object.

classmethod create(manager, settings)

Creates a new Spotify session. Call once per process.

Parameters:
  • manager – an object that has the session callbacks as methods
  • settings – an Settings object
Returns:

a Session object embedding the newly created Spotify session

login(username[, password, remember_me, blob])

Logs in the specified user to the Spotify service.

The application must not store any user password in plain text. If password storage is needed, the application must store the encrypted binary blob corresponding to the user and obtained via the manager.SpotifySessionManager.credentials_blob_updated() session callback. One of password or blob must be specified.

Parameters:
  • username (string) – the user’s login to Spotify Premium
  • password (string) – the user’s password to Spotify Premium
  • remember_me (bool) – set this flag if you want libspotify to remember this user
  • blob (str) – binary login blob
relogin()

Use this method if you want to re-login the last user who set the remember_me flag in Session.login()

browse_album(album, callback[, userdata])

Browse an album, calling the callback when the browser’s metadata is loaded.

Parameters:
  • album (Album) – a Spotify album (does not have to be loaded)
  • callback – a function with signature : (AlbumBrowser browser, Object userdata)
  • userdata – any object
Returns:

An AlbumBrowser object containing the results

Deprecated since version 1.7: Use AlbumBrowser instead.

browse_artist(artist, callback[, userdata])

Browse an artist, calling the callback when the browser’s metadata is loaded.

Parameters:
  • artist (Artist) – a Spotify artist (does not have to be loaded)
  • callback – a function with signature : (ArtistBrowser browser, Object userdata)
  • userdata – any object
Returns:

An ArtistBrowser object containing the results.

Deprecated since version 1.7: Use ArtistBrowser instead.

display_name()
Return type:string
Returns:the full name for the logged in user.

Raises SpotifyError if not logged in.

flush_caches()

This will make libspotify write all data that is meant to be stored on disk to the disk immediately. libspotify does this periodically by itself and also on logout. So under normal conditions this should never need to be used.

image_create(id)
Parameters:id (string) – the id of the image to be fetched.
Returns:an Image object.

Create an image of album cover art.

is_available(track)
Parameters:track (Track) – a track
Return type:int
Returns:Whether the track is available for playback.
load(track)
Parameters:track (Track) – a track
Raises:SpotifyError

Load the specified track on the player.

logout()

Logout from the session.

play(play)
Parameters:play (int) – Pause playback if 0, else play.

Play or pause the currently loaded track

playlist_container()
Return type:PlaylistContainer
Returns:the playlist container for the currently logged in user.
process_events()

Make the libspotify library process any pending event. This should be called from the notify_main_thread session callback.

search(query, callback[, track_offset=0, track_count=32, album_offset=0, album_count=32, artist_offset=0, artist_count=32, playlist_offset=0, playlist_count=32, search_type='standard', userdata=None])
Parameters:
  • query – Query search string
  • callback – signature (Results results, Object userdata)
  • track_offset – The offset among the tracks of the result
  • track_count – The number of tracks to ask for
  • album_offset – The offset among the albums of the result
  • album_count – The number of albums to ask for
  • artist_offset – The offset among the artists of the result
  • artist_count – The number of artists to ask for
  • playlist_offset – The offset among the playlists of the result
  • playlist_count – The number of playlists to ask for
  • search_type – ‘standard’ or ‘suggest’
Returns:

The search results

Return type:

Results

Conduct a search, calling the callback when the results are available.

seek(offset)

Seek to offset (in milliseconds) in the currently loaded track.

set_preferred_bitrate(bitrate)

Set the preferred bitrate for the audio stream.

0:
160 kbps
1:
320 kbps
2:
96 kbps
starred()
Return type:Playlist object.
Returns:the playlist of starred tracks for the logged in user.
unload()

Stop the currently playing track and unloads it from the player.

user_is_loaded()

Return whether the user is loaded or not, as an int.

If the user is not logged in, this method raises a SpotifyError.

username()

Return a string containing the canonical username for the logged in user.

If the user is not logged in, this method raises a SpotifyError.