from threading import Thread
from time import sleep
from queue import Queue, Empty
import spotify

class Spotbox:
    def __init__(self, username, password):
        self.session = spotify.Session()
        self.session.login(username, password)
        self.cmd_queue = Queue()
        self.status_queue = Queue()
        self.running = True
        self.track = None
        self.audio = spotify.AlsaSink(self.session)
        self.playing = False
        self.session.on(spotify.SessionEvent.END_OF_TRACK, self.end_of_track)

    def run(self):
        timeout = 0
        while self.running:
            if timeout != 0:
                self.process()
                try:
                    cmd = self.cmd_queue.get_nowait()
                    self.process_command(cmd)
                except Empty:
                    t = min(timeout, 100)
                    sleep(t / 1000.0)
            timeout = self.session.process_events()

    def end_of_track(self, not_used):
        self.session.player.unload()
        self.send("track")
        self.send("paused")

    def process_command(self, cmd):
        if cmd[0] == "track":
            self.track = self.session.get_link(cmd[1]).as_track()
        elif cmd[0] == "play":
            self.playing = True
        elif cmd[0] == "pause":
            self.playing = False

    def process(self):
        if self.track != None and self.track.is_loaded:
            self.session.player.load(self.track)
            self.send("track", self.track.link.uri)
            self.track = None

        if self.playing and (self.session.player.state == spotify.PlayerState.LOADED or
                        self.session.player.state == spotify.PlayerState.PAUSED):
            self.send("playing")
            self.session.player.play()

        if not self.playing and self.session.player.state == spotify.PlayerState.PLAYING:
            self.send("paused")
            self.session.player.pause()

    def send(self, *msg):
        self.status_queue.put(msg)

    def command(self, cmd):
        self.cmd_queue.put(cmd)

@@ -76,16 +76,46 @@
 , Empty%0A
+from collections import deque%0A
 import s
@@ -330,32 +330,66 @@
 queue = Queue()%0A
+        self.play_queue = deque()%0A
         self.run
@@ -1052,32 +1052,127 @@
 elf.
-session.player.unload()%0A
+next_track()%0A%0A    def next_track(self):%0A        self.session.player.unload()%0A        if len(self.play_queue) == 0:%0A    
     
@@ -1194,16 +1194,20 @@
 track%22)%0A
+    
         
@@ -1225,16 +1225,81 @@
 paused%22)
+%0A        else:%0A            self.track = self.play_queue.popleft()
 %0A%0A    de
@@ -1556,31 +1556,441 @@
 lse%0A
-%0A    def process(self):
+        elif cmd%5B0%5D == %22add%22:%0A            self.play_queue.append(self.session.get_link(cmd%5B1%5D).as_track())%0A        elif cmd%5B0%5D == %22clear%22:%0A            self.play_queue = deque()%0A        elif cmd%5B0%5D == %22next%22:%0A            self.next_track()%0A%0A    def process(self):%0A%0A        if self.playing and self.session.player.state == spotify.PlayerState.UNLOADED and self.track == None and len(self.play_queue) %3E 0:%0A             self.next_track()%0A
 %0A   

Add simple play queue