def startPlaylist(tracks, defaultPosition = 0): if defaultPosition != None and is_valid_num(0, len(tracks) - 1, defaultPosition): data = playlistThreader.getThread(tracks, defaultPosition) else: data = playlistThreader.getThread(tracks) if len(tracks) == 0: return send_error(error_codes.NO_PATH_DEFINED, "No track parameter passed.") errorPos = [] for index, trackPath in enumerate(tracks): if not os.path.exists(trackPath) or not check_path(trackPath): errorPos.append({"index" : index}) errorPos[-1].update(send_error(error_codes.WRONG_PATH_SPECIFIED, "File doesn't exsist.", False)) continue if not is_valid_file(trackPath): errorPos.append({"index" : index}) errorPos[-1].update(send_error(error_codes.INVALID_TYPE, "Invalid filetype.", False)) continue if len(errorPos) > 0: return send_playlist_play_error(errorPos, "Invalid track array") flushTrack() currentPlaylist = data.get('playlist') playlistThreader.startThread() return send_state_playlist_message(currentPlaylist, "Playlist succesfully started", None, 1, False)
def youtube_upload(): path = check_string(request, params.PATH) if not check_path(path): path = None if path == None: return send_error(error_codes.INVALID_PATH, "You need to specify path parameter") if not os.path.isdir(path): return send_no_dir_error(path) url = check_string(request, params.URL) if url == None: return send_error(error_codes.INVALID_URL, "You need to specify URL parameter") threaded = check_boolean(request, params.THREADED) if threaded: thread = threading.Thread(target = explorer.downloadYouTube, args = (path, url)) thread.daemon = True thread.start() else: explorer.downloadYouTube(path, url) return jsonify({ "code" : error_codes.SUCCESFULL_QUERY, "message" : "Query has been dispatched" })
def rewind_track(): currentTrack = trackThreader.currentTrack() destPos = check_integer(request, params.POSITION) unpause = True if destPos == None: return send_error(error_codes.WRONG_PLAYBACK_POSITION, "Wrong playback position") unpause = check_boolean(request, params.UNPAUSE) if currentTrack == None: return send_error(error_codes.NO_TRACK, "No track playing") elif destPos == -1: return send_error(error_codes.WRONG_PLAYBACK_POSITION, "No position specified") elif destPos < 0 or destPos > round(currentTrack.getLength() / 1000): return send_error(error_codes.WRONG_PLAYBACK_POSITION, "Wrong playback position. It should be specified with seconds") else: oldPos = currentTrack.playbackInfo().get('playback').get('position').get('millis') total = currentTrack.playbackInfo().get('playback').get('total').get('millis') currentTrack.setPlaybackPosition(destPos) if unpause and currentTrack.isPaused(): currentTrack.unpause() return send_state_track_message(currentTrack, "Playback position succesfully changed", { 'newPosition' : destPos * 1000, 'oldPosition' : oldPos, 'total' : total })
def pause_track(): currentTrack = trackThreader.currentTrack() if(currentTrack == None): return send_error(error_codes.NO_TRACK, "No track playing") elif(currentTrack.isPaused()): return send_error(error_codes.TRACK_ALREADY_PAUSED, "Track is already paused") else: currentTrack.pause() return send_state_track_message(currentTrack, "Track paused")
def unpause_track(): currentTrack = trackThreader.currentTrack() if currentTrack == None: return send_error(error_codes.NO_TRACK, "No track playing") elif not currentTrack.isPaused(): return send_error(error_codes.TRACK_ALREADY_PLAYING, "Track is already playing") else: currentTrack.unpause() return send_state_track_message(currentTrack, "Track unpaused")
def playlist_prev(): currentPlaylist = playlistThreader.currentPlaylist() if currentPlaylist != None and currentPlaylist.currentTrack != None: if currentPlaylist.prevTrackAvilable(): currentPlaylist.previousTrack(onPlaylistLoadError) return send_state_playlist_message(currentPlaylist, "Track succesfully changed") else: return send_error(error_codes.NO_PREV_TRACK, "No previous track avilable") else: return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
def playlist_pos(): currentPlaylist = playlistThreader.currentPlaylist() if currentPlaylist != None and currentPlaylist.currentTrack != None: index = check_integer(request, params.INDEX) if index == None or not is_valid_num(0, len(currentPlaylist.tracks) - 1, index): return send_error(error_codes.INVALID_TRACK_INDEX, "Invalid track index") else: currentPlaylist.play(index) return send_state_playlist_message(currentPlaylist, "Track succesfully changed") else: return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
def playlist_playback(): currentPlaylist = playlistThreader.currentPlaylist() if currentPlaylist != None and currentPlaylist.currentTrack != None: return jsonify(currentPlaylist.playbackInfo(error_codes.SUCCESFULL_QUERY)) else: return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
def track_online(): currentTrack = trackThreader.currentTrack() if(currentTrack != None): response = currentTrack.playbackInfoExtended() response['code'] = error_codes.SUCCESFULL_QUERY return jsonify(response) return send_error(error_codes.NO_TRACK, "No track playing")
def file_upload(): path = check_string(request, params.PATH) if not check_path(path): path = None if path == None: return send_error(error_codes.INVALID_PATH, "You need to specify path parameter") if not os.path.isdir(path): return send_no_dir_error(path) file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file_path = os.path.join(path, filename) file.save(file_path) return jsonify({ "code" : error_codes.SUCCESFULL_QUERY, "path" : file_path, "message" : "File uploaded" }) return jsonify({ "code" : error_codes.UNALLOWED_EXTENSION, "message" : "Unallowed extension" })
def getAllPlaylists(): local = check_boolean(request, params.LOCAL) sortingMethod = check_integer(request, params.SORT) if sortingMethod == None: sortingMethod = check_string(request, params.SORT) sortingMethod = translate_sorting_method(sortingMethod) trackSortingMethod = check_integer(request, params.TRACK_SORT) if trackSortingMethod == None: trackSortingMethod = check_string(request, params.TRACK_SORT) trackSortingMethod = translate_sorting_method(trackSortingMethod, 1) filters = check_int_array(request, params.FILTER) if len(filters) == 0: filters.append(0) #0 means no filtering initialPath = check_string(request, params.PATH) if not check_path(initialPath): initialPath = None if initialPath == None: initialPath = get_defaults()["defaults"]["default_path"] if not os.path.isdir(initialPath): return send_error(error_codes.INVALID_PATH, "Invalid path") respone = explorer.getAllPlaylists(initialPath, sortingMethod, trackSortingMethod, filters, local) if not 'error' in respone: respone['code'] = error_codes.SUCCESFULL_QUERY return jsonify(respone)
def playlist_unpause(): currentPlaylist = playlistThreader.currentPlaylist() if currentPlaylist != None and currentPlaylist.currentTrack != None: currentTrack = currentPlaylist.currentTrack if(currentTrack == None): return send_error(error_codes.NO_TRACK, "No track playing") elif not currentTrack.isPaused(): return send_error(error_codes.TRACK_ALREADY_PLAYING, "Track is already playing") else: currentTrack.unpause() return send_state_playlist_message(currentPlaylist, "Track unpaused") else: return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
def file_delete(): path = check_string(request, params.PATH) if not check_path(path): path = None if path == None: return send_error(error_codes.INVALID_PATH, "You need to specify path parameter") if not os.path.exists(path): return send_no_file_error(path) try: if(os.path.isdir(path)): shutil.rmtree(path) else: os.remove(path) return jsonify({ "code" : error_codes.SUCCESFULL_QUERY, "message" : "File/directory deleted" }) except: return jsonify({ "code" : error_codes.DATA_MANAGEMENT_ERROR, "message" : "An error while deleting file has occured" })
def create_catalog(): path = check_string(request, params.PATH) name = check_string(request, params.NAME) if not check_path(path): path = None if path == None: return send_error(error_codes.INVALID_PATH, "You need to specify path parameter") if not os.path.isdir(path): return send_no_dir_error(path) if name == "": return jsonify({ "code" : error_codes.INVALID_CATALOG_NAME, "message" : "Invalid name" }) final_path = os.path.join(path, name) if os.path.isdir(final_path): return jsonify({ "code" : error_codes.DATA_MANAGEMENT_ERROR, "message" : "Folder already exsists." }) try: os.mkdir(final_path) return jsonify({ "code" : error_codes.SUCCESFULL_QUERY, "message" : "Folder succesfully created" }) except: return jsonify({ "code" : error_codes.DATA_MANAGEMENT_ERROR, "message" : "An error has occured." })
def getAllTracks(): simple = check_boolean(request, params.SIMPLE) local = check_boolean(request, params.LOCAL) initialPath = check_string(request, params.PATH) sortingMethod = check_integer(request, params.SORT) if sortingMethod == None: sortingMethod = check_string(request, params.SORT) sortingMethod = translate_sorting_method(sortingMethod, 1) if not check_path(initialPath): initialPath = None if initialPath == None: initialPath = get_defaults()["defaults"]["default_path"] if not os.path.isdir(initialPath): return send_error(error_codes.INVALID_PATH, "Invalid path") respone = explorer.getAllTracks(initialPath, sortingMethod, simple, local) if not 'error' in respone: respone['code'] = error_codes.SUCCESFULL_QUERY return jsonify(respone)
def stop_track(): currentTrack = trackThreader.currentTrack() if currentTrack != None: currentTrack.stop() trackThreader.setTrack(None) return jsonify({'message' : "Track stopped"}) else: return send_error(error_codes.NO_TRACK, "No track playing")
def startTrack(trackPath): #trackThreader.registerOnEndCustomCallback(track_endevent) if(trackPath == "" or trackPath == None): return send_error(error_codes.NO_PATH_DEFINED, "No path parameter passed.") if not os.path.exists(trackPath) or not check_path(trackPath): return send_error(error_codes.WRONG_PATH_SPECIFIED, "File doesn't exsist.") if not is_valid_file(trackPath): return send_error(error_codes.INVALID_TYPE, "Invalid filetype.") flushPlaylist() data = trackThreader.getThread(trackPath) currentThread = data.get('thread') currentTrack = data.get('track') currentThread.start() return send_state_track_message(currentTrack, "Track started")
def stop_playlist(): currentPlaylist = playlistThreader.currentPlaylist() if currentPlaylist != None and currentPlaylist.currentTrack != None: currentPlaylist.stop() playlistThreader.setPlaylist(None) return jsonify({'message' : "Playlist stopped" }) else: return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
def send_audio(): path = check_string(request, params.PATH) if not check_path(path): path = None if path == None: return send_error(error_codes.INVALID_PATH, "You need to specify path parameter") if not file_exsists(path): return send_no_file_error(path) return send_from_directory(directory = os.path.dirname(path), filename = os.path.basename(path))
def smartpause_track(): currentTrack = trackThreader.currentTrack() if currentTrack == None: return send_error(error_codes.NO_TRACK, "No track playing") if currentTrack.isPaused(): currentTrack.unpause() return send_state_track_message(currentTrack, "Track unpaused") else: currentTrack.pause() return send_state_track_message(currentTrack, "Track paused")
def set_volume(): respone = volumizer.setVolume(check_float(request, params.VALUE)) if respone == None: return send_error(error_codes.INVALID_VALUE, "Invalid volume value. Only 0 - 100") respone.update({"code" : error_codes.SUCCESFULL_QUERY}) vol = respone['config']['milli'] playlistThreader.setVolume(vol) trackThreader.setVolume(vol) return jsonify(respone)
def getDirectory(): path = check_string(request, params.PATH) if path == None: path = get_defaults()['defaults']['default_path'] metadata = check_boolean(request, params.METADATA) sortingMethod = check_integer(request, params.SORT) if sortingMethod == None: sortingMethod = check_string(request, params.SORT) sortingMethod = translate_sorting_method(sortingMethod, 0) if check_path(path): respone = explorer.getPathContent(path, metadata, sorting = sortingMethod) if respone == None: return send_error(error_codes.INVALID_PATH, "Invalid path") else: return send_error(error_codes.INVALID_PATH, "Invalid path") respone['code'] = error_codes.SUCCESFULL_QUERY return jsonify(respone)
def get_metadata(): path = check_string(request, params.PATH) if path == None: return send_error(error_codes.INVALID_PATH, "You need to specify path parameter") if not file_exsists(path): return send_no_file_error(path) respone = explorer.getMetadata(path) respone['code'] = error_codes.SUCCESFULL_QUERY return jsonify(respone)
def play_track(): currentTrack = trackThreader.currentTrack() trackPath = check_string(request, params.PATH) terminate = check_boolean(request, params.TERMINATE) if not file_exsists(trackPath) or not check_path(trackPath): return send_no_file_error(trackPath) if currentTrack == None: return startTrack(trackPath) else: if terminate: currentTrack.stop() trackThreader.setTrack(None) return startTrack(trackPath) return send_error(error_codes.TRACK_ALREADY_PLAYING, "Track already exsists")
def play_playlist(): currentPlaylist = playlistThreader.currentPlaylist() defaultPosition = check_integer(request, params.INDEX) terminate = check_boolean(request, params.TERMINATE) tracks = check_string_array(request, params.TRACK) #http://localhost:5000/playlist/play?track=tracks/track5.mp3&track=tracks/track2.mp3&track=tracks/track5.mp3&i=0&t=True if currentPlaylist == None: return startPlaylist(tracks, defaultPosition) else: if terminate: if currentPlaylist != None and currentPlaylist.currentTrack != None: currentPlaylist.stop() playlistThreader.setPlaylist(None) playlistThreader.reInit() return startPlaylist(tracks, defaultPosition) return send_error(error_codes.PLAYLIST_EXSIST, "Playlist already exsist")
def getAllTracks(self, initialPath, sort = 1, simple = False, local = False): files_list = [] startTime = int(round(time.time() * 1000)) if not local: for dirpath, dirnames, filenames in os.walk(initialPath): for filename in [f for f in filenames if self.__isWhitelisted(f)]: fullPath = os.path.join(dirpath, filename) basename = os.path.basename(fullPath) f = self.__getFile(fullPath) if not simple: artist = f['artist'][0] if 'artist' in f else None album = f['album'][0] if 'album' in f else None genre = f['genre'][0] if 'genre' in f else None filesize = round(os.stat(fullPath).st_size/1000000, 2) cover = self.__getCover(fullPath) files_list.append({ "basename" : basename, "full" : fullPath, "simple" : os.path.splitext(basename)[0], "artist" : artist, "album" : album, "genre" : genre, "cover" : cover, "filesize" : filesize, "length" : round(f.info.length) }) else: files_list.append({ "basename" : basename, "full" : fullPath, "simple" : os.path.splitext(basename)[0], "length" : round(f.info.length) }) endTime = int(round(time.time() * 1000)) if endTime - startTime > limits.MAX_REQUEST_TIME * 1000: return send_error(error_codes.REQUEST_TIMEOUT, "Request took too much time", False) else: for fil in os.listdir(initialPath): if self.__isWhitelisted(fil): fullPath = os.path.join(initialPath, fil) f = self.__getFile(fullPath) if not simple: artist = f['artist'][0] if 'artist' in f else None album = f['album'][0] if 'album' in f else None genre = f['genre'][0] if 'genre' in f else None cover = self.__getCover(fullPath) filesize = round(os.stat(fullPath).st_size/1000000, 2) files_list.append({ "basename" : fil, "full" : fullPath, "simple" : os.path.splitext(fil)[0], "artist" : artist, "album" : album, "genre" : genre, "cover" : cover, "filesize" : filesize, "length" : round(f.info.length) }) else: files_list.append({ "basename" : fil, "full" : fullPath, "simple" : os.path.splitext(fil)[0], "length" : round(f.info.length) }) return { "tracks" : self.__sortTracks(files_list, sort) }
def onPlaylistLoadError(): playlistThreader.setPlaylist(None) return send_error(error_codes.INVALID_PATH, "Path error occured. Removing playlist...")
def metadata_track(): currentTrack = trackThreader.currentTrack() if currentTrack != None: return jsonify(currentTrack.getMetadata()) return send_error(error_codes.NO_TRACK, "No track playing")