music.spotify
music.spotify
- class platypush.backend.music.spotify.MusicSpotifyBackend(librespot_path: str = 'librespot', device_name: Optional[str] = None, device_type: str = 'speaker', audio_backend: str = 'alsa', audio_device: Optional[str] = None, mixer: str = 'softvol', mixer_name: str = 'PCM', mixer_card: str = 'default', mixer_index: int = 0, volume: int = 100, volume_ctrl: str = 'linear', bitrate: int = 160, autoplay: bool = False, disable_gapless: bool = False, username: Optional[str] = None, password: Optional[str] = None, client_id: Optional[str] = None, client_secret: Optional[str] = None, proxy: Optional[str] = None, ap_port: Optional[int] = None, disable_discovery: bool = False, cache_dir: Optional[str] = None, system_cache_dir: Optional[str] = None, disable_audio_cache=False, enable_volume_normalization: bool = False, normalization_method: str = 'dynamic', normalization_pre_gain: Optional[float] = None, normalization_threshold: float = - 1.0, normalization_attack: int = 5, normalization_release: int = 100, normalization_knee: float = 1.0, **kwargs)[source]
This backend uses librespot to turn Platypush into an audio client compatible with Spotify Connect and discoverable by a device running a Spotify client or app. It can be used to stream Spotify through the Platypush host. After the backend has started, you should see a new entry in the Spotify Connect devices list in your app.
Triggers:
platypush.message.event.music.MusicPlayEvent
if the playback state changed to playplatypush.message.event.music.MusicPauseEvent
if the playback state changed to pauseplatypush.message.event.music.MusicStopEvent
if the playback state changed to stopplatypush.message.event.music.NewPlayingTrackEvent
if a new track is being playedplatypush.message.event.music.VolumeChangeEvent
if the volume changes
Requires:
librespot. Consult the README for instructions.
- __init__(librespot_path: str = 'librespot', device_name: Optional[str] = None, device_type: str = 'speaker', audio_backend: str = 'alsa', audio_device: Optional[str] = None, mixer: str = 'softvol', mixer_name: str = 'PCM', mixer_card: str = 'default', mixer_index: int = 0, volume: int = 100, volume_ctrl: str = 'linear', bitrate: int = 160, autoplay: bool = False, disable_gapless: bool = False, username: Optional[str] = None, password: Optional[str] = None, client_id: Optional[str] = None, client_secret: Optional[str] = None, proxy: Optional[str] = None, ap_port: Optional[int] = None, disable_discovery: bool = False, cache_dir: Optional[str] = None, system_cache_dir: Optional[str] = None, disable_audio_cache=False, enable_volume_normalization: bool = False, normalization_method: str = 'dynamic', normalization_pre_gain: Optional[float] = None, normalization_threshold: float = - 1.0, normalization_attack: int = 5, normalization_release: int = 100, normalization_knee: float = 1.0, **kwargs)[source]
- Parameters:
librespot_path – Librespot path/executable name (default:
librespot
).device_name – Device name (default: same as configured Platypush
device_id
or hostname).device_type – Device type to be shown in the icon. Available types:
unknown
,computer
,tablet
,smartphone
,speaker
,tv
,avr
(Audio/Video Receiver),stb
(Set-Top Box), oraudiodongle
(default:speaker
).audio_backend – Audio backend to be used. Supported values:
alsa
,portaudio
,pulseaudio
,jackaudio
,gstreamer
,rodio
,rodiojack
,sdl
(default:alsa
).audio_device – Output audio device. Type
librespot --device ?
to get a list of the available devices.mixer – Mixer to be used to control the volume. Supported values:
alsa
orsoftvol
(default:softvol
).mixer_name – Mixer name if using the ALSA mixer. Supported values:
PCM
orMaster
(default:PCM
).mixer_card – ALSA mixer output card, as reported by
aplay -l
(default:default
).mixer_index – ALSA card index, as reported by
aplay -l
(default: 0).volume – Initial volume, as an integer between 0 and 100 if
volume_ctrl=linear
or in dB ifvolume_ctrl=logarithmic
.volume_ctrl – Volume control scale. Supported values:
linear
andlogarithmic
(default:linear
).bitrate – Audio bitrate. Choose 320 for maximum quality (default: 160).
autoplay – Play similar tracks when the queue ends (default: False).
disable_gapless – Disable gapless audio (default: False).
username – Spotify user/device username (used if you want to enable Spotify Connect remotely).
password – Spotify user/device password (used if you want to enable Spotify Connect remotely).
client_id – Spotify client ID, required if you want to retrieve track and album info through the Spotify Web API. You can generate one by creating a Spotify app here <https://developer.spotify.com/>.
client_secret – Spotify client secret, required if you want to retrieve track and album info through the Spotify Web API. You can generate one by creating a Spotify app here <https://developer.spotify.com/>.
proxy – Optional HTTP proxy configuration.
ap_port – Spotify AP port to be used (default: default ports, i.e. 80, 443 and 4070).
disable_discovery – Disable discovery mode.
cache_dir – Data files cache directory.
system_cache_dir – System cache directory - it includes audio settings and credentials.
disable_audio_cache – Disable audio caching (default: False).
enable_volume_normalization – Play all the tracks at about the same volume (default: False).
normalization_method – If
enable_volume_normalization=True
, this setting specifies the volume normalization method. Supported values:basic
,dynamic
(default:dynamic
).normalization_pre_gain – Pre-gain applied to volume normalization if
enable_volume_normalization=True
, expressed in dB.normalization_threshold – If
enable_volume_normalization=True
, this setting specifies the normalization threshold (in dBFS) to prevent audio clipping (default: -1.0).normalization_attack – If
enable_volume_normalization=True
, this setting specifies the attack time (in ms) during which the dynamic limiter is reducing the gain (default: 5).normalization_release – If
enable_volume_normalization=True
, this setting specifies the release time (in ms) for the dynamic limiter to restore the gain (default: 100).normalization_knee – Knee steepness of the dynamic limiter (default: 1.0).
- property daemon
A boolean value indicating whether this thread is a daemon thread.
This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.
The entire Python program exits when only daemon threads are left.
- getName()
Return a string used for identification purposes only.
This method is deprecated, use the name attribute instead.
- property ident
Thread identifier of this thread or None if it has not been started.
This is a nonzero integer. See the get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.
- isDaemon()
Return whether this thread is a daemon.
This method is deprecated, use the daemon attribute instead.
- is_alive()
Return whether the thread is alive.
This method returns True just before the run() method starts until just after the run() method terminates. See also the module function enumerate().
- join(timeout=None)
Wait until the thread terminates.
This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception or until the optional timeout occurs.
When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call is_alive() after join() to decide whether a timeout happened – if the thread is still alive, the join() call timed out.
When the timeout argument is not present or None, the operation will block until the thread terminates.
A thread can be join()ed many times.
join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.
- property name
A string used for identification purposes only.
It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.
- property native_id
Native integral thread ID of this thread, or None if it has not been started.
This is a non-negative integer. See the get_native_id() function. This represents the Thread ID as reported by the kernel.
- on_message(msg)
Callback when a message is received on the backend. It parses and posts the message on the main bus. It should be called by the derived classes whenever a new message should be processed.
- Parameters:
msg – Received message. It can be either a key-value dictionary, a platypush.message.Message object, or a string/byte UTF-8 encoded string
- register_service(port: Optional[int] = None, name: Optional[str] = None, srv_type: Optional[str] = None, srv_name: Optional[str] = None, udp: bool = False, properties: Optional[Dict] = None)
Initialize the Zeroconf service configuration for this backend.
- Parameters:
port – Service listen port (default: the backend
port
attribute if available, orNone
).name – Service short name (default: backend name).
srv_type – Service type (default:
_platypush-{name}._{proto}.local.
).srv_name – Full service name (default:
{hostname or device_id}.{type}
).udp – Set to True if this is a UDP service.
properties –
Extra properties to be passed on the service. Default:
{ "name": "Platypush", "vendor": "Platypush", "version": "{platypush_version}" }
- run()[source]
Starts the backend thread. To be implemented in the derived classes if the loop method isn’t defined.
- send_event(event, **kwargs)
Send an event message on the backend.
- Parameters:
event – Event to send. It can be a dict, a string/bytes UTF-8 JSON, or a platypush.message.event.Event object.
- send_message(msg, queue_name=None, **_)
Sends a platypush.message.Message to a node. To be implemented in the derived classes. By default, if the Redis backend is configured then it will try to deliver the message to other consumers through the configured Redis main queue.
- Parameters:
msg – The message to send
queue_name – Send the message on a specific queue (default: the queue_name configured on the Redis backend)
- send_request(request, on_response=None, response_timeout=5, **kwargs)
Send a request message on the backend.
- Parameters:
request – The request, either a dict, a string/bytes UTF-8 JSON, or a platypush.message.request.Request object.
on_response (function) – Optional callback that will be called when a response is received. If set, this method will synchronously wait for a response before exiting.
response_timeout (float) – If on_response is set, the backend will raise an exception if the response isn’t received within this number of seconds (default: None)
- send_response(response, request, **kwargs)
Send a response message on the backend.
- Parameters:
response – The response, either a dict, a string/bytes UTF-8 JSON, or a
platypush.message.response.Response
object.request – Associated request, used to set the response parameters that will link them
- setDaemon(daemonic)
Set whether this thread is a daemon.
This method is deprecated, use the .daemon property instead.
- setName(name)
Set the name string for this thread.
This method is deprecated, use the name attribute instead.
- spotify_api_authenticate()
Authenticate to the Spotify API for requests that don’t require access to user data.
- spotify_api_call(url: str, method='get', scopes: Optional[Iterable[str]] = None, **kwargs) dict
Send an API request to a Spotify endpoint.
- Parameters:
url – URL to be requested.
method – HTTP method (default:
get
).scopes – List of scopes required by the call.
kwargs – Extra keyword arguments to be passed to the request.
- Returns:
The response payload.
- spotify_user_authenticate(scopes: Optional[Iterable[str]] = None)
Authenticate to the Spotify API for requests that require access to user data.
- spotify_user_call(url: str, method='get', scopes: Optional[Iterable[str]] = None, **kwargs) dict
Shortcut for
spotify_api_call
that requires all the application scopes if none are passed.
- start()
Start the thread’s activity.
It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.
This method will raise a RuntimeError if called more than once on the same thread object.
- stop()
Stops the backend thread by sending a STOP event on its bus
- unregister_service()
Unregister the Zeroconf service configuration if available.