Session manager

class spotify.manager.SpotifySessionManager(username=None, password=None, remember_me=False, login_blob='', proxy=None, proxy_username=None, proxy_password=None)

Client for Spotify. Inherit from this class to have your callbacks called on the appropriate events.

Exceptions raised in your callback handlers will be displayed on the standard error output (stderr).

When logging in a user, the application can pass one of:
  • username + password: standard login using a plaintext password
  • nothing: logs in the last user which credentials have been stored using remember_me.
  • username + login_blob: the blob is encrypted data from libspotify, for when a multi-user application wants to use the re-login feature. The blob is obtained from the credentials_blob_updated() callback after a successful login to the Spotify AP.
When behind a proxy, the application can specify:
  • proxy: url to the proxy server that should be used. The format

    is protocol://<host>:port (where protocol is http/https/socks4/socks5)

  • proxy_username: username to authenticate with the proxy server.

  • proxy_password: password to authenticate with the proxy server.

connect()

Connect to the Spotify API using the given username and password. If username is None, reconnection of the last user will be attempted.

This method does not return before we disconnect from the Spotify service.

disconnect()

Terminate the current Spotify session.

logged_in(session, error)

Callback.

Called when the login completes. You almost certainly want to do something with session.playlist_container() if the login succeded.

Parameters:
  • session (spotify.Session) – the current session.
  • error (string or None) – an error message, None if all went well.
logged_out(session)

Callback.

The user has or has been logged out from Spotify.

Parameters:session (spotify.Session) – the current session.
metadata_updated(session)

Callback.

The current user’s metadata has been updated.

Parameters:session (spotify.Session) – the current session.
connection_error(session, error)

Callback.

A connection error occured in libspotify.

Parameters:
  • session (spotify.Session) – the current session.
  • error (string or None) – an error message. If None, the connection is back.
message_to_user(session, message)

Callback.

An informative message from libspotify, destinated to the user.

Parameters:
  • session (spotify.Session) – the current session.
  • message (string) – a message.
notify_main_thread(session=None)

Callback.

When this method is called by libspotify, one should call session.process_events().

If you use the SessionManager‘s default loop, the default implementation of this method does the job. Though, if you implement your own loop for handling Spotify events, you’ll need to override this method.

Warning

This method is called from an internal thread in libspotify. You should make sure not to use the Spotify API from within it, as libspotify isn’t thread safe.

Parameters:session (spotify.Session) – the current session.
music_delivery(session, frames, frame_size, num_frames, sample_type, sample_rate, channels)

Callback.

Called whenever new music data arrives from Spotify.

You should override this method or music_delivery_safe(), not both.

Warning

This method is called from an internal thread in libspotify. You should make sure not to use the Spotify API from within it, as libspotify isn’t thread safe.

Parameters:
  • session (spotify.Session) – the current session
  • frames (buffer) – the audio data
  • frame_size (int) – bytes per frame
  • num_frames (int) – number of frames in this delivery
  • sample_type (int) – currently this is always 0 which means 16-bit signed native endian integer samples
  • sample_rate (int) – audio sample rate, in samples per second
  • channels (int) – number of audio channels. Currently 1 or 2
Returns:

number of frames consumed

Return type:

int

music_delivery_safe(session, frames, frame_size, num_frames, sample_type, sample_rate, channels)

This method does the same as music_delivery(), except that it’s called from the SpotifySessionManager loop. You can safely use Spotify APIs from within this method.

You should override this method or music_delivery(), not both.

play_token_lost(session)

Callback.

The playback stopped because a track was played from another application, with the same account.

Parameters:session (spotify.Session) – the current session.
log_message(session, message)

Callback.

A log message from libspotify.

Parameters:
  • session (spotify.Session) – the current session.
  • message (string) – the message.
end_of_track(session)

Callback.

Playback has reached the end of the current track.

Parameters:session (spotify.Session) – the current session.
credentials_blob_updated(session, blob)

Callback.

Called when storable credentials have been updated, usually called when we have connected to the AP.

Warning

This method is called from an internal thread in libspotify. You should make sure not to use the Spotify API from within it, as libspotify isn’t thread safe.

Parameters:
  • session (spotify.Session) – the current session.
  • blob – a string which contains an encrypted token that can be stored safely on disk instead of storing plaintext passwords.