def getTrackList(): verbo("Request to get track list") req = { "method": "core.tracklist.get_tracks", "jsonrpc": "2.0", "params": {}, "id": "getTL" } return (json.dumps(req))
def getTrack(): verbo("Request to get current track") req = { "method": "core.playback.get_current_track", "jsonrpc": "2.0", "params": {}, "id": "getTrack" } return (json.dumps(req))
def getPlayback(): verbo("Request to get playback state") req = { "method": "core.playback.get_state", "jsonrpc": "2.0", "params": {}, "id": "checkPlay" } return (json.dumps(req))
def getTrackListLength(): verbo("Request to get tracklist length") req = { "method": "core.tracklist.get_length", "jsonrpc": "2.0", "params": {}, "id": "getTLLength" } return (json.dumps(req))
def addTrack(track): verbo("Request to add track " + str(track) + " to tracklist") req = { "method": "core.tracklist.add", "jsonrpc": "2.0", "params": { "uri": track }, "id": "addTrack" } return (json.dumps(req))
def getPlItems(playlist): verbo("Request to get " + str(playlist) + " playlist items") req = { "method": "core.playlists.get_items", "jsonrpc": "2.0", "params": { "uri": playlist }, "id": "getPlItems" } return (json.dumps(req))
def setConsume(value): verbo("Request to set consume to " + str(value)) req = { "method": "core.tracklist.set_consume", "jsonrpc": "2.0", "params": { "value": value }, "id": "setConsume" } return (json.dumps(req))
def play(): verbo("Request to play") req = { "method": "core.playback.play", "jsonrpc": "2.0", "params": { "tl_track": None, "tlid": None }, "id": "play" } return (json.dumps(req))
def add_next_track(): global currentPlTrack global leadingTrackByMe currentPlTrack += 1 try: req = track_manager.request.addTrack(playlist[currentPlTrack]) except: currentPlTrack = 0 shuffle(playlist) req = track_manager.request.addTrack(playlist[currentPlTrack]) verbo("Reached end of playlist. Repeating...") leadingTrackByMe = True webs.client.send(req)
def removeTrack(track): verbo("Request to remove track " + str(track) + " from tracklist") req = { "method": "core.tracklist.remove", "jsonrpc": "2.0", "params": { "criteria": { 'uri': [track] } }, "id": "removeTrack" } return (json.dumps(req))
def start_websocket_server(cert, key, port): stat("Starting websocket server") try: verbo("Reading SSL/TLS certificate") with open(cert) as f: pass except: stat("!! FAILED TO READ SSL/TLS CERTIFICATE !!") exit(2) try: verbo("Reading SSL/TLS key") with open(key) as f: pass except: stat("!! FAILED TO READ SSL/TLS PRIVATE KEY!!") exit(2) verbo("SSL/TLS Check PASSED") verbo("Will now start websocket server on port " + str(port)) server = SimpleSSLWebSocketServer('', port, SimpleServer, cert, key) thread.start_new_thread(server.serveforever, ()) stat("Websocket server is now running")
def init_music_server(): stat("Configuring music server") webs.client.send(track_manager.request.setConsume(True)) verbo("Consume mode set") webs.client.send(track_manager.request.getPlayback()) verbo("Getting playback state") webs.client.send(track_manager.request.getPlItems(globals.args.playlist)) verbo("Getting playlist " + globals.args.playlist) stat("Finished Configuring")
def read_message(txtMessage): global firstRun global isPlaying global playlist global currentPlTrack global trackListLength global leadingTrackByMe global trackList message = json.loads(txtMessage) if ('event' in message): if (message['event'] == "playback_state_changed"): verbo("Playback State Changed from " + str(message['old_state']) + " to " + str(message['new_state'])) if (message['old_state'] == "playing" and message['new_state'] == "stopped"): # this should ideally never run stat("Tracklist has no tracks! :O - Adding two") for i in range(2): add_next_track() # add two tracks webs.client.send(track_manager.request.play()) if (message['old_state'] == 'playing' and message['new_state'] == 'playing' ): # if one track has finished and another is queued pass if (message['new_state'] == "playing"): isPlaying = True else: isPlaying = False elif (message['event'] == "track_playback_started"): webs.server.send(json.dumps(message).decode()) elif (message['event'] == "track_playback_ended"): verbo("Track playback had ended.") ### elif (message['event'] == "tracklist_changed"): webs.client.send(track_manager.request.getTrackListLength() ) # See how big the tracklist is webs.client.send(track_manager.request.getTrackList() ) # Get the current tracklist for comparison verbo("Tracklist has changed. Will send to WebSocket server") if ('jsonrpc' in message): if (message['id'] == "checkPlay"): verbo("Got playback state") if (message['result'] == "playing"): isPlaying = True verbo("Currently playing") else: isPlaying = False verbo("Not currently playing") elif (message['id'] == "getPlItems"): stat("Reccieved the playlist") playlist = [] for track in message['result']: debug("Adding track " + track['uri']) playlist.append(track['uri']) shuffle(playlist) verbo("Shuffled playlist") if (firstRun == True and isPlaying == False): for i in range(2): add_next_track() # add two tracks webs.client.send(track_manager.request.play()) # Start playing stat("First track requested. Finished start up.") firstRun = False elif (message['id'] == "getTL"): webs.server.send(json.dumps(message).decode()) trackList = [] for track in message['result']: trackList.append(track) elif (message['id'] == "getTLLength"): prevLength = trackListLength trackListLength = message['result'] if (prevLength < trackListLength): # if a track was added verbo("A track was added") if (trackListLength == 3 and leadingTrackByMe == True): leadingTrackByMe = False verbo("Leading track was added by me, will now delete") webs.client.send( track_manager.request.removeTrack(trackList[1]['uri'])) else: verbo("No need to delete.") elif (prevLength > trackListLength): # if a track was removed verbo("A track was removed") if (trackListLength == 1): # if tracklist only has one track in it add_next_track()
def send(message): verbo("Sending message to all clients") for client in clients: client.sendMessage(message)