Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

How to save changes on playlists? #117

Closed
lukas-schulze opened this Issue Feb 21, 2014 · 3 comments

Comments

Projects
None yet
3 participants

I'm using the latest alpha release which is available over pip (2.0.0.a1)

I followed the instructions on the documentation but I don't know how to save my changes.
I try to create a new playlist, but it seems to have no impact on my playlists...

import spotify
import threading

config = spotify.Config()
config.user_agent = 'My awesome Spotify client'
config.tracefile = b'/tmp/libspotify-trace.log'
session = spotify.Session(config=config)

session.login('username', 'password')

logged_in_event = threading.Event()

def logged_in_listener(session, error_type):
    logged_in_event.set()

def get_playlists(session):
    playlists = session.playlist_container
    if not playlists.is_loaded:
        playlists.load()
    return playlists

session.on(spotify.SessionEvent.LOGGED_IN, logged_in_listener)

while not logged_in_event.wait(0.1):
    session.process_events()

playlists = get_playlists(session)
print len(playlists)

search = session.search('mas').load()
track = search.tracks[0].load()

playlist = session.playlist_container.add_new_playlist('My Test Playlist')
tracks = [track]
playlist.add_tracks(tracks)
Member

kingosticks commented Feb 21, 2014

I think you'll be needing to somehow call session.process_events() for these playlist changes to actually occur.

Owner

jodal commented Feb 21, 2014

I think @kingosticks is right. The next alpha will include an event loop helper thread that'll do the right amount of process_events() calls in the background. In other words, this will soon be a lot easier. For now, doing process_events() until playlist.has_pending_changes is False is probably what you want to do.

Other tips:

  1. playlist_container.load() is a noop if the container is already loaded, so no need to check is_loaded first.
  2. playlist.add_tracks() takes single Track objects as well, so no need to wrap it in a list.

So, a somewhat simpler implementation is:

import spotify
import threading

logged_in_event = threading.Event()

def logged_in_listener(session, error_type):
    # TODO Handle error_type != spotify.ErrorType.OK
    logged_in_event.set()

session = spotify.Session()
session.on(spotify.SessionEvent.LOGGED_IN, logged_in_listener)
session.login('username', 'password')
while not logged_in_event.wait(0.1):
    session.process_events()

print len(session.playlist_container.load())

search = session.search('mas').load()
track = search.tracks[0].load()
playlist = session.playlist_container.add_new_playlist('My Test Playlist')
playlist.add_tracks(track)

while playlist.has_pending_changes:
    session.process_events()

Thank you kingosticks and jodal.
This works great.
I'm looking forward to your next release with the solution for session.process_events() loops :-)

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment