Files
Medios-Macina/LUA/main.lua
2025-11-27 10:59:01 -08:00

122 lines
3.3 KiB
Lua

local mp = require 'mp'
local utils = require 'mp.utils'
local msg = require 'mp.msg'
local M = {}
-- Configuration
local opts = {
python_path = "python",
cli_path = nil -- Will be auto-detected if nil
}
-- Detect CLI path
local script_dir = mp.get_script_directory()
if not opts.cli_path then
-- Assuming the structure is repo/LUA/script.lua and repo/CLI.py
-- We need to go up one level
local parent_dir = script_dir:match("(.*)[/\\]")
if parent_dir then
opts.cli_path = parent_dir .. "/CLI.py"
else
opts.cli_path = "CLI.py" -- Fallback
end
end
-- Helper to run pipeline
function M.run_pipeline(pipeline_cmd, seeds)
local args = {opts.python_path, opts.cli_path, "pipeline", pipeline_cmd}
if seeds then
local seeds_json = utils.format_json(seeds)
table.insert(args, "--seeds")
table.insert(args, seeds_json)
end
msg.info("Running pipeline: " .. pipeline_cmd)
local res = utils.subprocess({
args = args,
cancellable = false,
})
if res.status ~= 0 then
msg.error("Pipeline error: " .. (res.stderr or "unknown"))
mp.osd_message("Error: " .. (res.stderr or "unknown"), 5)
return nil
end
return res.stdout
end
-- Helper to run pipeline and parse JSON output
function M.run_pipeline_json(pipeline_cmd, seeds)
-- Append | output-json if not present
if not pipeline_cmd:match("output%-json$") then
pipeline_cmd = pipeline_cmd .. " | output-json"
end
local output = M.run_pipeline(pipeline_cmd, seeds)
if output then
local ok, data = pcall(utils.parse_json, output)
if ok then
return data
else
msg.error("Failed to parse JSON: " .. output)
return nil
end
end
return nil
end
-- Command: Get info for current file
function M.get_file_info()
local path = mp.get_property("path")
if not path then return end
-- We can pass the path as a seed item
local seed = {{path = path}}
-- Run pipeline: get-metadata
local data = M.run_pipeline_json("get-metadata", seed)
if data then
-- Display metadata
msg.info("Metadata: " .. utils.format_json(data))
mp.osd_message("Metadata loaded (check console)", 3)
end
end
-- Command: Delete current file
function M.delete_current_file()
local path = mp.get_property("path")
if not path then return end
local seed = {{path = path}}
M.run_pipeline("delete-file", seed)
mp.osd_message("File deleted", 3)
mp.command("playlist-next")
end
-- Menu integration with UOSC
function M.show_menu()
local menu_data = {
title = "Medios Macina",
items = {
{ title = "Get Metadata", value = "script-binding medios-info", hint = "Ctrl+i" },
{ title = "Delete File", value = "script-binding medios-delete", hint = "Ctrl+Del" },
}
}
local json = utils.format_json(menu_data)
mp.commandv('script-message-to', 'uosc', 'open-menu', json)
end
-- Keybindings
mp.add_key_binding("m", "medios-menu", M.show_menu)
mp.add_key_binding("mbtn_right", "medios-menu-right-click", M.show_menu)
mp.add_key_binding("ctrl+i", "medios-info", M.get_file_info)
mp.add_key_binding("ctrl+del", "medios-delete", M.delete_current_file)
return M