def play(self, track): t = self.session.get_link(track["uri"]).as_track().load() if self.get_position() < 3000 and len(BohnifyQueue.Instance().history) > 0: BohnifyQueue.Instance().history.pop(0) self.session.player.unload() try: self.time = 0 self.position = 0 self.audio_driver.new_track() self.session.player.load(t) self.session.player.play() if "isstandard" in track and self.status["repeat"]: track["index"] = (self.getMaxIndex()+10000) BohnifyQueue.Instance().standardqueue.append(track) self.updatequeue() self.status["track"] = track self.status["paused"] = False self.updateStatus() BohnifyQueue.Instance().history.insert(0,self.status["track"].copy()) self.updatehistory() except StandardError as e: print e print("next pga can't play") self.next()
def track(self, track, context): track.load() t = Cache.Instance().getTrack(track.link.uri) if t != None: BohnifyQueue.Instance().setVoteToTrack(t) t["context"] = "spotify:" + context return t else: t = BohnifyQueue.Instance().getTrackIfIsAnyQueue(track.link.uri) if t != None: BohnifyQueue.Instance().setVoteToTrack(t) t["context"] = "spotify:" + context return t else: if track.availability == spotify.TrackAvailability.AVAILABLE: t = { "title": track.name, "popularity": track.popularity, "duration": track.duration, "uri": track.link.uri, "album": self.album(track.album), "artists": self.artists(track.artists) } Cache.Instance().addTrack(t) BohnifyQueue.Instance().setVoteToTrack(t) t["context"] = "spotify:" + context return t else: return None
def voteDown(self,uri): track = self.getUriFromList(BohnifyQueue.Instance().votequeue, uri) if track != None: track["vote"] = track["vote"] - 1 self.voteChange(track["uri"],track["vote"]) if track["vote"] == 0: BohnifyQueue.Instance().votequeue.remove(track) self.updatequeue()
def getArtist(self, uri, browse=False): uri = uri + "_b" if browse else uri artist = self.artists[uri] if uri in self.artists else None if browse and artist != None: for track in artist["toptracks"]: BohnifyQueue.Instance().setVoteToTrack(track) for album in artist["albums"]: if "tracks" in album: for track in album["tracks"]: BohnifyQueue.Instance().setVoteToTrack(track) return artist
def setTracks(tracks): BohnifyQueue.Instance().standardqueue = tracks if track != None: for t in tracks: if t["origindex"] == track: BohnifyQueue.Instance().standardqueue.remove(t) self.play(t) break elif start: t = BohnifyQueue.Instance().standardqueue.pop(0) self.play(t) self.updatequeue()
def voteUp(self,context,uri): if self.status["track"] == None: self.play(uri) else: track = self.getUriFromList(BohnifyQueue.Instance().votequeue, uri) if track != None: track["vote"] = track["vote"] + 1 else: track = Transformer().track(self.session.get_link(uri).as_track(),context) track["vote"] = 1 BohnifyQueue.Instance().votequeue.append(track) self.voteChange(track["uri"],track["vote"]) self.updatequeue()
def prev(self): if self.get_position() > 3000: self.seek(0) else: track = None if len(BohnifyQueue.Instance().standardqueue) > 0: playtrack = self.status["track"] if playtrack != None and "isstandard" in playtrack: playtrack["index"] = self.getMinIndex()-1 self.sortQueue() track = BohnifyQueue.Instance().standardqueue.pop(len(BohnifyQueue.Instance().standardqueue)-1) if track != None: self.play(track)
def getPlaylist(self, uri, search=False): uri = uri + "_s" if search else uri pl = self.playlists[uri] if uri in self.playlists else None if pl != None and not search: for track in pl["tracks"]: BohnifyQueue.Instance().setVoteToTrack(track) return pl
def getAlbum(self, uri, browse=False): uri = uri + "_b" if browse else uri album = self.albums[uri] if uri in self.albums else None if browse and album != None: for track in album["tracks"]: BohnifyQueue.Instance().setVoteToTrack(track) return album
def getTracks(tracks): if self.status["random"]: if self.status["track"]["context"] == self.context: tracks[self.status["track"]["origindex"]]["index"] = self.getMaxIndex()+10000 else: for ht in reversed(BohnifyQueue.Instance().history): if "isstandard" in ht: if ht["context"] == self.context: index = ht["origindex"] for st in tracks: if st["origindex"] >= index: st["index"] = self.getMaxIndex()+10000 break BohnifyQueue.Instance().standardqueue = tracks self.updateStatus() self.updatequeue()
def getUser(self, uri, browse=False): uri = uri + "_b" if browse else uri user = self.users[uri] if uri in self.users else None if browse and user != None: for playlist in user["playlists"]: for track in playlist["tracks"]: BohnifyQueue.Instance().setVoteToTrack(track) return user
def gotTracks(tracks): for index in indices: for track in tracks: if track["origindex"] == index: if self.status["party"]: voteUp(track) else: BohnifyQueue.Instance().manualqueue.append(track.copy()) self.updatequeue()
def updatequeue(self): self.sortQueue() for tindex in range(len(BohnifyQueue.Instance().manualqueue)): BohnifyQueue.Instance().manualqueue[tindex]["origindex"] = tindex BohnifyQueue.Instance().manualqueue[tindex]["index"] = tindex list.sort(BohnifyQueue.Instance().manualqueue, key=lambda track:track["index"]) list.sort(BohnifyQueue.Instance().votequeue, key=lambda track: (track["vote"]*-1)) for tindex in range(len(BohnifyQueue.Instance().votequeue)): BohnifyQueue.Instance().votequeue[tindex]["origindex"] = tindex BohnifyQueue.Instance().votequeue[tindex]["index"] = tindex cherrypy.engine.publish('websocket-broadcast', json.dumps({"queueupdated" : True}))
def removeFromQueue(self,removetracks): for track in removetracks: if track["context"] == "spotify:get:queue:manual": for t in BohnifyQueue.Instance().manualqueue: if t["origindex"] == track["index"]: BohnifyQueue.Instance().manualqueue.remove(t) break elif track["context"] == "spotify:get:queue:standard": for t in BohnifyQueue.Instance().standardqueue: if t["origindex"] == track["index"]: BohnifyQueue.Instance().standardqueue.remove(t) break elif track["context"] == "spotify:get:queue:vote": for t in BohnifyQueue.Instance().votequeue: if t["origindex"] == track["index"]: self.voteDown(t["uri"]) break self.updatequeue()
def filterTracks(tracks, skiprandom = False): if dofilter: i = len(tracks)-1 while i >= 0: if BohnifyQueue.Instance().removedstandard.count(i) > 0: tracks.remove(tracks[i]) else: tracks[i]["index"] = i tracks[i]["context"] = context tracks[i]["isstandard"] = True i = i - 1 if self.status["random"] and not skiprandom: shuffle(tracks) for index in range(len(tracks)): tracks[index]["index"] = index cb(tracks)
def next(self): track = None if self.status["party"] and len(BohnifyQueue.Instance().votequeue) > 0: track = BohnifyQueue.Instance().votequeue.pop(0) if track != None: track["vote"] = 0 self.voteChange(track["uri"],track["vote"]) elif len(BohnifyQueue.Instance().manualqueue) > 0: track = BohnifyQueue.Instance().manualqueue.pop(0) elif len(BohnifyQueue.Instance().standardqueue) > 0: track = BohnifyQueue.Instance().standardqueue.pop(0) if track != None: self.play(track)
def playContent(self, uri, track = None, start = False): def setTracks(tracks): BohnifyQueue.Instance().standardqueue = tracks if track != None: for t in tracks: if t["origindex"] == track: BohnifyQueue.Instance().standardqueue.remove(t) self.play(t) break elif start: t = BohnifyQueue.Instance().standardqueue.pop(0) self.play(t) self.updatequeue() BohnifyQueue.Instance().removedstandard = [] if uri.find("spotify:get:queue:manual") == 0 and track != None: for t in BohnifyQueue.Instance().manualqueue: if t["origindex"] == track: self.play(t) BohnifyQueue.Instance().manualqueue.remove(t) self.updatequeue() break elif uri.find("spotify:get:queue:vote") == 0 and track != None: for t in BohnifyQueue.Instance().votequeue: if t["origindex"] == track: t["vote"] = t["vote"] + 1 self.voteChange(t["uri"],t["vote"]) self.updatequeue() break elif uri.find("spotify:get:queue:standard") == 0 and track != None: while len(BohnifyQueue.Instance().standardqueue) > 0: t = BohnifyQueue.Instance().standardqueue.pop(0) if t["origindex"] == track: self.play(t) break else: BohnifyQueue.Instance().removedstandard.append(t["origindex"]) self.updatequeue() else: self.context = uri self.getContextTracks(self.context,setTracks)
def toggleRepeat(self): self.status["repeat"] = not self.status["repeat"] if self.status["repeat"]: if self.status["track"] != None and "isstandard" in self.status["track"] and self.status["track"]["context"] == self.context: t = self.status["track"] t["index"] = self.getMaxIndex() + 10000 BohnifyQueue.Instance().standardqueue.append(t) while BohnifyQueue.Instance().removedstandard.count(t["origindex"]) > 0: BohnifyQueue.Instance().removedstandard.remove(t["origindex"]) else: maxindex = self.getMaxOrigIndex() i = len(BohnifyQueue.Instance().standardqueue) - 1 while i >= 0: t = BohnifyQueue.Instance().standardqueue[i] if t["index"] > maxindex: BohnifyQueue.Instance().removedstandard.append(t["origindex"]) BohnifyQueue.Instance().standardqueue.pop(i) i = i - 1 self.updatequeue() self.updateStatus()
def getMaxOrigIndex(self): maxindex = -1000000 for t in BohnifyQueue.Instance().standardqueue: if t["origindex"] > maxindex: maxindex = t["origindex"] return maxindex
def sortQueue(self): list.sort(BohnifyQueue.Instance().standardqueue, key=lambda track:track["index"])
def getContextTracks(self,context,cb, dofilter = True): def filterTracks(tracks, skiprandom = False): if dofilter: i = len(tracks)-1 while i >= 0: if BohnifyQueue.Instance().removedstandard.count(i) > 0: tracks.remove(tracks[i]) else: tracks[i]["index"] = i tracks[i]["context"] = context tracks[i]["isstandard"] = True i = i - 1 if self.status["random"] and not skiprandom: shuffle(tracks) for index in range(len(tracks)): tracks[index]["index"] = index cb(tracks) def artistDone(artist): filterTracks(artist["toptracks"]) def albumDone(album): filterTracks(album["tracks"]) def searchDone(tracks): filterTracks(tracks) def trackDone(album): filterTracks(album["tracks"]) def playlistDone(playlist): filterTracks(playlist["tracks"]) if context.find("spotify:") == 0: search = context[8:] if search.find("spotify:") == 0: link = self.session.get_link(search) if link.type == spotify.LinkType.ARTIST: self.browseArtist(link,artistDone) elif link.type == spotify.LinkType.ALBUM: self.browseAlbum(link,albumDone) elif link.type == spotify.LinkType.TRACK: self.browseTrack(link,trackDone) elif link.type == spotify.LinkType.PLAYLIST: self.browsePlaylist(link,playlistDone) elif link.type == spotify.LinkType.STARRED: self.browseStarred(playlistDone) elif search.find("get:toplist") == 0: self.toplist(searchDone) elif search.find("get:history") == 0: filterTracks(BohnifyQueue.Instance().history[:], True) elif search.find("get:search:") == 0: search = search[11:] self.search(search,searchDone) elif search.find("get:suggest:") == 0: search = search[12:] self.track_suggest(search,searchDone) elif search.find("get:queue:manual") == 0: cb(BohnifyQueue.Instance().manualqueue) elif search.find("get:queue:standard") == 0: cb(BohnifyQueue.Instance().standardqueue) elif search.find("get:queue:vote") == 0: cb(BohnifyQueue.Instance().votequeue)
def updatehistory(self): for tindex in range(len(BohnifyQueue.Instance().history)): BohnifyQueue.Instance().history[tindex]["origindex"] = tindex BohnifyQueue.Instance().history[tindex]["index"] = tindex list.sort(BohnifyQueue.Instance().history, key=lambda track:track["index"]) cherrypy.engine.publish('websocket-broadcast', json.dumps({"historyupdated" : True}))
def received_message(self, m): try: cmd = json.loads(m.data) print(cmd) if "login" in cmd: Bohnify.Instance().login(cmd["login"]["username"], cmd["login"]["password"]) elif "search" in cmd: if cmd["search"].find("spotify:") == 0: search = cmd["search"][8:] print search if search.find("spotify:") == 0: link = Bohnify.Instance().session.get_link(search) if link.type == spotify.LinkType.ARTIST: def artistBrowsed(artist): self.send( json.dumps({ "search": { "type": "artist", "data": artist, "search": link.uri } })) Bohnify.Instance().browseArtist( link, artistBrowsed) elif link.type == spotify.LinkType.ALBUM: def albumBrowsed(album): self.send( json.dumps({ "search": { "type": "album", "data": album, "search": link.uri } })) Bohnify.Instance().browseAlbum(link, albumBrowsed) elif link.type == spotify.LinkType.TRACK: def trackBrowsed(album): self.send( json.dumps({ "search": { "type": "track", "data": album, "search": link.uri } })) Bohnify.Instance().browseTrack(link, trackBrowsed) elif link.type == spotify.LinkType.PLAYLIST: def playlistBrowsed(playlist): self.send( json.dumps({ "search": { "type": "playlist", "data": playlist, "search": link.uri } })) Bohnify.Instance().browsePlaylist( link, playlistBrowsed) elif link.type == spotify.LinkType.PROFILE: pass elif link.type == spotify.LinkType.STARRED: def playlistBrowsed(playlist): self.send( json.dumps({ "search": { "type": "playlist", "data": playlist, "search": link.uri } })) Bohnify.Instance().browseStarred(playlistBrowsed) else: def searchDone(tracks): self.send( json.dumps({ "search": { "type": "search", "data": tracks, "search": search } })) Bohnify.Instance().search(search, searchDone) elif "suggest" in cmd: if cmd["suggest"].find("spotify:") == 0: def suggestDone(data): self.send( json.dumps({ "suggest": { "data": data, "suggest": suggest } })) suggest = cmd["suggest"][8:] Bohnify.Instance().suggest(suggest, suggestDone) elif "gettoplist" in cmd: def toplistDone(tracks): self.send(json.dumps({"toplist": tracks})) Bohnify.Instance().toplist(toplistDone) elif "getqueue" in cmd: vote = [] manual = [] standard = [] for t in BohnifyQueue.Instance().votequeue: track = t.copy() track["context"] = "spotify:get:queue:vote" vote.append(track) for t in BohnifyQueue.Instance().manualqueue: track = t.copy() track["context"] = "spotify:get:queue:manual" manual.append(track) for t in BohnifyQueue.Instance().standardqueue: track = t.copy() track["context"] = "spotify:get:queue:standard" standard.append(track) if Bohnify.Instance().status["party"]: self.send( json.dumps({ "queues": [{ "type": "vote", "queue": vote }, { "type": "standard", "queue": standard }] })) else: self.send( json.dumps({ "queues": [{ "type": "manual", "queue": manual }, { "type": "standard", "queue": standard }] })) elif "gethistory" in cmd: history = BohnifyQueue.Instance().history[:] for t in history: t["context"] = "spotify:get:history" self.send(json.dumps({"history": history})) elif "prev" in cmd: if not Bohnify.Instance().status["party"]: Bohnify.Instance().prev() elif "next" in cmd: if not Bohnify.Instance().status["party"]: Bohnify.Instance().next() elif "play" in cmd: track = cmd["play"]["track"] if "track" in cmd["play"] else None start = cmd["play"]["start"] if "start" in cmd[ "play"] else False Bohnify.Instance().playContent(cmd["play"]["uri"], track, start) elif "party" in cmd: Bohnify.Instance().toggleParty() elif "random" in cmd: Bohnify.Instance().toggleRandom() elif "repeat" in cmd: Bohnify.Instance().toggleRepeat() elif "manualqueue" in cmd: Bohnify.Instance().addToManual(cmd["manualqueue"]["context"], cmd["manualqueue"]["indices"]) elif "removefromqueue" in cmd: Bohnify.Instance().removeFromQueue(cmd["tracks"]) elif "seek" in cmd: Bohnify.Instance().seek(cmd["seek"]) elif "volume" in cmd: Bohnify.Instance().volume(cmd["volume"]) elif "pause" in cmd: Bohnify.Instance().togglePause() elif "increasevolume" in cmd: Bohnify.Instance().increaseVolume() elif "decreasevolume" in cmd: Bohnify.Instance().decreaseVolume() else: print("else") except StandardError as e: print e
def getMinIndex(self): minindex = 1000000 for t in BohnifyQueue.Instance().standardqueue: if t["index"] < minindex: minindex = t["index"] return minindex