Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Creating playlists not working as expected #179

Closed
jds0102 opened this issue Nov 23, 2015 · 5 comments
Closed

Creating playlists not working as expected #179

jds0102 opened this issue Nov 23, 2015 · 5 comments

Comments

@jds0102
Copy link

jds0102 commented Nov 23, 2015

I am trying to create a playlist that is a copy of another playlist and add it to a folder.

When I create the playlist it works fine and I get no error, but when I open the Spotify client the playlist is nowhere to be found. I can grab the uri of the playlist when I create it and use that to find the playlist through search, but it never shows up in my list of playlists in the side bar. It seems like the playlist is created but never associated to my users account. Any help is much appreciated, see the code below.

import datetime
import spotify
import getpass
import threading
import sys

def create_playlist(session, name, tracks, index):
  new_playlist = session.playlist_container.add_new_playlist(name, index)
  new_playlist.add_tracks(tracks)

############Session Setup
logged_in_event = threading.Event()
def connection_state_listener(session):
  if session.connection.state is spotify.ConnectionState.LOGGED_IN:
    logged_in_event.set()

session = spotify.Session()
loop = spotify.EventLoop(session)
loop.start()
session.on(spotify.SessionEvent.CONNECTION_STATE_UPDATED, connection_state_listener)
print "Enter Spotfify User Name:"
uname = raw_input()
pw = getpass.getpass()
session.login(uname, pw)
logged_in_event.wait()
###############End session setup

session.playlist_container.load()

nmf_playlist = 0
archive_folder = 0
archive_index = -1
index = 0
session.playlist_container.load()
#Look through playlists for one to make a copy of
#Find index of folder to insert playlist into
for playlist in session.playlist_container:
  if type(playlist) is spotify.PlaylistFolder:
    if playlist.name == "Archive":
      archive_folder = playlist
      archive_index = index
  else:
    playlist.load()
    if playlist.name == 'New Music Friday':
      nmf_playlist = playlist
  index += 1

#If we found the original and a folder to copy to make a copy
if archive_index >= 0 and nmf_playlist != 0:
  create_playlist(session, 'New Music Friday Archive ' + str(datetime.datetime.today()), nmf_playlist.tracks, archive_index + 1)


session.logout()


@jodal
Copy link
Owner

jodal commented Nov 23, 2015

When you've edited a playlist, you need to run session.process_events() (this is what the event loop does for you in the background if you use that) until session.playlists_container.has_pending_changes is False. If not, you'll logout before the changes is synced to the Spotify servers.

@jodal
Copy link
Owner

jodal commented Nov 23, 2015

Also, session.logout() is async, so you should wait for the LOGGED_OUT event before you exit the process.

@jds0102
Copy link
Author

jds0102 commented Nov 24, 2015

Thanks, I will incorporate that into the code.

@jds0102
Copy link
Author

jds0102 commented Nov 24, 2015

Hey, thanks again @jodal , but I am still having some issues.

I am guessing when you said session.playlists_container.has_pending_changes you meant just playlist.has_pending_changes? I added that and my playlists are consistently showing up in the list, but they don't seem to be added at the index I indicate, specifically within a folder. They are all being added to the first position.

See code here:

import datetime
import spotify
import getpass
import threading
import sys

def create_playlist(session, name, tracks, index):
  new_playlist = session.playlist_container.add_new_playlist(name, index)
  new_playlist.add_tracks(tracks)
  while new_playlist.has_pending_changes:
        session.process_events();

############Session Setup
logged_in_event = threading.Event()
playlist_created_event = threading.Event()
logout_event = threading.Event()

def connection_state_listener(session):
  if session.connection.state is spotify.ConnectionState.LOGGED_IN:
    logged_in_event.set()

def logged_out_listener(session):
        logout_event.set()

session = spotify.Session()
loop = spotify.EventLoop(session)
loop.start()
session.on(spotify.SessionEvent.CONNECTION_STATE_UPDATED, connection_state_listener)
session.on(spotify.SessionEvent.LOGGED_OUT, logged_out_listener)


print "Enter Spotfify User Name:"
uname = raw_input()
pw = getpass.getpass()
session.login(uname, pw)
logged_in_event.wait()
###############End session setup

session.playlist_container.load()

nmf_playlist = 0
discover_playlist = 0
archive_folder = 0
archive_index = -1
index = 0
session.playlist_container.load()
#Look through playlists for one to make a copy of
#Find index of folder to insert playlist into
for playlist in session.playlist_container:
  if type(playlist) is spotify.PlaylistFolder:
    if playlist.name == "Archive":
      archive_folder = playlist
      archive_index = index
  else:
    playlist.load()
    if playlist.name == 'New Music Friday':
      nmf_playlist = playlist
    elif playlist.name == 'Discover Weekly':
      discover_playlist = playlist
  index += 1

#If we found the original and a folder to copy to make a copy
if archive_index >= 0 and nmf_playlist != 0:
  create_playlist(session, 'New Music Friday Archive ' + str(datetime.datetime.today()), nmf_playlist.tracks, archive_index + 1)

if archive_index >= 0 and discover_playlist != 0:
  create_playlist(session, uname + ' Discover Weekly Archive ' + str(datetime.datetime.today()), discover_playlist.tracks, archive_index + 1)


session.logout()

logout_event.wait()


@jodal
Copy link
Owner

jodal commented Jul 8, 2019

Closing on the assumption that you haven't been waiting for my answer for 4y. If anyone else have the same issue, shout out.

@jodal jodal closed this as completed Jul 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants