Spotify-Local: A multi-platform API to control the local Spotify Client

Spotify-Local library is designed to make controlling the Spotify client on your local machine possible! This is a wrapper for the web helper process which exposes a simple api. Spotify-Local is inspired by SpotifyAPI-NET. This library allows you to perform simple actions quickly, or listen to events and register callbacks when a song changes, or the pause button is pushed.

When using this library you automatically get:

  • The ability to play/pause the current song
  • The ability to change tracks
  • You can register callbacks and listen for events when the state of Spotify changes
  • A nice context manager api using with

Installation

$ pipenv install spotify-local

Only Python 3.3+ is supported.

Tutorial & Usage

Connect to the Spotify Client (Spotify must be open to do this):

>>> from spotify_local import SpotifyLocal

>>> with SpotifyLocal() as s:
        pass

Pause the Spotify Client:

>>> with SpotifyLocal() as s:
        s.pause()

Grab the current state of the Spotify client, including now playing information:

>>> with SpotifyLocal() as s:
        print(s.get_current_status())

Play a playlist, song, album, artist, etc using a Spotify uri link:

>>> with SpotifyLocal() as s:
        s.playURI('spotify:track:0thLhIqWsqqycEqFONOyhu')

Register a callback and listen for events:

>>> from spotify_local import SpotifyLocal
>>> s = SpotifyLocal()
>>> @s.on('track_change')
>>> def test(event):
...     print(event)
>>> s.listen(blocking=False)
>>> print("Do more stuff because that runs in the background")

License

MIT

API Documentation

Main Classes

These classes are the main interface to spotify_local:

class spotify_local.SpotifyLocal[source]

Controller for the local spotify web helper, throws events when the state of spotify changes.

add_event_handler(event, func)[source]

Add function and event to Ordered Dict

Parameters:
  • event – Name of the event you wish to register the function
  • func – Function to register witht the associated event
emit(event, *args, **kwargs)[source]

Send out an event and call it’s associated functions

Parameters:event – Name of the event to trigger
get_current_status()[source]

Returns the current state of the local spotify client

listen(wait=60, blocking=True) → None[source]

Listen for events and call any associated callbacks when there is an event. There are three events you can subscribe too; status_change, play_state_change, track_change

Parameters:
  • wait – how long to wait for a response before starting a new connection
  • blocking – if listen should block the current process or not
listeners(event)[source]

Return list of listners associated to a particular event

Parameters:event – Name of the event you wish to query
on(event)[source]

Decorator function that adds function to callback list for event system There are three events you can subscribe too; status_change, play_state_change, track_change.

Parameters:event – name of the event you wish to register the function under, you can use multiple decorators
pause(pause=True)[source]

Pauses the spotify player

Parameters:pause – boolean value to choose the pause/play state
playURI(uri)[source]

Play a Spotify uri, for example spotify:track:5Yn8WCB4Dqm8snemB5Mu4K

Parameters:uri – Playlist, Artist, Album, or Song Uri
static previous()[source]

Goes to the beginning of the track, or if called twice goes to the previous track.

remove_all_listeners(event=None)[source]

Remove all functions for all events, or one event if one is specifed.

Parameters:event – Optional event you wish to remove all functions from
remove_listener(event, func)[source]

Remove an event listner

Parameters:
  • event – Event you wish to remove the function from
  • func – Function you wish to remove
static skip()[source]

Skips the current song

unpause()[source]

Unpauses the player by calling pause()

version

Spotify version information

Indices and tables