def do_add(self, rawInput): """Adds an ID or the results of a search term to a playlist. Playlist is created if not already existing. Syntax: add <ID or search string> to <playlist name>""" try: end = rawInput.find('to')#Some shitty parsing here, assumes everything between the 4th character and the word "to" is the search query or ID, # and everything past the word "to" (end+3) is the playlist name. Hey, it works if end == -1: #"to" not found, it's necessary! self.onecmd('help add') elif rawInput[end+2:end+3] != ' ': self.onecmd('help add') else: playlistName = rawInput[end+3:]#This grabs the playlist name in a string. if playlistName == '' or not playlistName: self.onecmd('help add') try: if self.currentPlaylists[playlistName]: pass except KeyError: self.currentPlaylists[playlistName] = database.playlist(self.db, playlistName) print "Created", playlistName if userInput[1].isdigit(): self.currentPlaylists[playlistName].add(self.db.getLocationByID(userInput[1])) else: searchQuery = rawInput[4:end-1] results = self.db.searchForSongs(searchQuery) for item in results: self.currentPlaylists[playlistName].add(item['location']) except (IndexError): self.onecmd('help add')
def do_load(self, rawInput): """Loads a playlist from the workingDir specified in config.yml. Do not specify the .xspf. Syntax: load <playlist file name>""" plName = rawInput.strip() if plName == 'all': self.scanForPlaylists() else: try: if self.currentPlaylists[plName]: print self.currentPlaylists[plName].loadFromDisk(plName, self.dir) except KeyError: self.currentPlaylists[plName] = database.playlist(self.db) print self.currentPlaylists[plName].loadFromDisk(plName, self.dir)
def scanForPlaylists(self): """Scans the current working directory (ie the script directory) recursively for playlists and loads them.""" listOfPlaylists = self.scnr.scanForFiles(startDirectory=self.dir, fileTypes=['.xspf'], dontvisit=[]) for item in listOfPlaylists: plName = os.path.split(item)[-1].split('.')[0] if not plName == 'random': try: if self.currentPlaylists[plName]: print self.currentPlaylists[plName].loadFromDisk(plName, self.dir) except KeyError: self.currentPlaylists[plName] = database.playlist(self.db, plName) print self.currentPlaylists[plName].loadFromDisk(plName, self.dir) else: os.remove(os.path.join(self.dir, plName +'.xspf'))
def playRandom(self, startWith=None): if 'random' in self.cmdSh.currentPlaylists.keys(): if not startWith == None: self.playLocation(self.dbName.getLocationByID(startWith)) random.shuffle(self.cmdSh.currentPlaylists['random']) self.playAList('random', 0) else: self.cmdSh.currentPlaylists['random'] = database.playlist(self.dbName, 'random') songList = self.dbName.getListOfSongs() for item in songList: self.cmdSh.currentPlaylists['random'].append(item['location']) # self.cmdSh.currentPlaylists['random'].randomize() random.shuffle(self.cmdSh.currentPlaylists['random']) if not startWith == None: self.playLocation(self.dbName.getLocationByID(startWith)) else: self.playAList('random')
def do_play(self, rawInput): """Play on it's own simply starts the player playing, if it is currently paused. Add some extra words to play, and it is used to get the ball rolling. Acceptable formats: play random play <song ID> play <search string>""" userInput = rawInput.split(' ') try: if not rawInput or rawInput == '': self.plyr.play() elif userInput[0].isdigit(): songIDtoPlay = int(userInput[0]) self.plyr.playRandom(songIDtoPlay) elif userInput[0] == 'random': self.plyr.playRandom() elif userInput[0] in self.currentPlaylists: #is it a playlist name? print self.currentPlaylists[userInput[0]] self.plyr.playAList(userInput[0]) else:#must be a search query, let's make a temporary playlist with the results and play that randomName = [u'temp', unicode(random.getrandbits(50))] randomName = ''.join(randomName) searchResults = self.db.searchForSongs(rawInput) if searchResults: self.currentPlaylists[randomName] = database.playlist(self.db, randomName) for song in searchResults: self.currentPlaylists[randomName].add(song['location'], True) print self.currentPlaylists[randomName] self.plyr.playAList(randomName) else:#no results found, obviously print 'No results found, try harder.' except ValueError: pass
""" This file is only a helper file to be able to build seperate executables when building the standalone samples. """ import database database.playlist()