def main(): parser = argparse.ArgumentParser(description = "Video player wrapper for RaspberryPI.") parser.add_argument("--list", action='store_true', help = "List available files") parser.add_argument("--delete", help = "Delete provided file from repository") parser.add_argument("--play", help = "Play provided playlist") parser.add_argument("--stop", action='store_true', help = "Abort/stop player") parser.add_argument("--status", action='store_true', help = "Get status of player") args = parser.parse_args() if not (args.list or args.delete or args.play or args.stop or args.status): parser.error("At least one argument is expected.") else: configurations = SafeConfigParser() configurations.read("config.ini") playlistStore = PlaylistStoreMemcache ( configurations.get("Memcache", "Host"), configurations.get("Memcache", "Port") ) player = Player ( configurations.get("General", "SuperUserPrivilegesNeeded"), configurations.get("General", "FilesRepositoryAbsolutePath"), configurations.get("VideoPlayer", "Name"), configurations.get("VideoPlayer", "AbsolutePath"), configurations.get("VideoPlayer", "AdditionalArguments"), configurations.get("VideoUtility", "Name"), configurations.get("VideoUtility", "AbsolutePath"), configurations.get("VideoUtility", "AdditionalArguments"), playlistStore, configurations.get("Daemon", "PidFileAbsolutePath") ) if (args.list): print player.list() elif (args.delete): print player.delete(args.delete) elif (args.play): print player.play(json.loads(args.play)) elif (args.stop): print player.stop() else: print player.status()
def application(env, start_response): output = "" error = False form = cgi.FieldStorage( fp = env['wsgi.input'], environ = env, keep_blank_values = True ) # operation = form.getvalue('operation') queryStr = env['QUERY_STRING'] match = re.match('^operation=(play|stop|status|list|upload|delete)(&file=(.*))?', queryStr) if match is None: # if operation not in ['play','stop','status', 'list', 'upload', 'delete']: error = True else: operation = match.group(1) configurations = SafeConfigParser() configurations.read("config.ini") playlistStore = PlaylistStoreMemcache ( configurations.get("Memcache", "Host"), configurations.get("Memcache", "Port") ) player = Player ( configurations.get("General", "SuperUserPrivilegesNeeded"), configurations.get("General", "FilesRepositoryAbsolutePath"), configurations.get("VideoPlayer", "Name"), configurations.get("VideoPlayer", "AbsolutePath"), configurations.get("VideoPlayer", "AdditionalArguments"), configurations.get("VideoUtility", "Name"), configurations.get("VideoUtility", "AbsolutePath"), configurations.get("VideoUtility", "AdditionalArguments"), playlistStore, configurations.get("Daemon", "PidFileAbsolutePath") ) if (env['REQUEST_METHOD'] == "GET"): if (operation == "list"): output = player.list() elif (operation == "status"): output = player.status() else: error = True elif (env['REQUEST_METHOD'] == "POST"): if (operation == "play"): if getattr(form, "file"): jsonString = form.file.read() output = player.play(json.loads(jsonString)) else: error = True elif (operation == "upload"): fileItem = form['new_file'] if fileItem.filename: output = player.save(fileItem) else: error = True else: error = True elif (env['REQUEST_METHOD'] == "PUT"): if (operation == "stop"): output = player.stop() else: error = True elif (env['REQUEST_METHOD'] == "DELETE"): if (operation == "delete"): fileName = match.group(3) if fileName: output = player.delete(fileName) else: error = True else: error = True else: error = True if error: start_response("500 Internal Server Error", [("Content-Type", "text/html")]) else: start_response ("200 OK", [ ("Access-Control-Allow-Origin", "*"), ("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE"), ("Access-Control-Allow-Headers", "x-requested-with"), ("Content-Type", "application/json") ] ) return output