This commit is contained in:
2026-03-19 13:08:15 -07:00
parent 5cbc2c09df
commit cc9c1850a8
7 changed files with 340 additions and 55 deletions

View File

@@ -4,7 +4,7 @@ local msg = require 'mp.msg'
local M = {}
local MEDEIA_LUA_VERSION = '2026-03-19.1'
local MEDEIA_LUA_VERSION = '2026-03-19.2'
-- Expose a tiny breadcrumb for debugging which script version is loaded.
pcall(mp.set_property, 'user-data/medeia-lua-version', MEDEIA_LUA_VERSION)
@@ -1184,6 +1184,53 @@ local function _extract_query_param(url, key)
return nil
end
local function _download_url_for_current_item(url)
url = trim(tostring(url or ''))
if url == '' then
return '', false
end
local base, query = url:match('^([^?]+)%?(.*)$')
if not base or not query or query == '' then
return url, false
end
local base_lower = tostring(base or ''):lower()
local has_explicit_video = false
if base_lower:match('youtu%.be/') then
has_explicit_video = true
elseif base_lower:match('youtube%.com/watch') or base_lower:match('youtube%-nocookie%.com/watch') then
has_explicit_video = _extract_query_param(url, 'v') ~= nil
end
if not has_explicit_video then
return url, false
end
local kept = {}
local changed = false
for pair in query:gmatch('[^&]+') do
local raw_key = pair:match('^([^=]+)') or pair
local key = tostring(_percent_decode(raw_key) or raw_key or ''):lower()
local keep = true
if key == 'list' or key == 'index' or key == 'start_radio' or key == 'pp' or key == 'si' then
keep = false
changed = true
end
if keep then
kept[#kept + 1] = pair
end
end
if not changed then
return url, false
end
if #kept > 0 then
return base .. '?' .. table.concat(kept, '&'), true
end
return base, true
end
local function _normalize_url_for_store_lookup(url)
url = trim(tostring(url or ''))
if url == '' then
@@ -1522,14 +1569,78 @@ local function _normalize_tag_list(value)
return tags
end
local function _queue_pipeline_in_repl(pipeline_cmd, queued_message, failure_prefix, queue_label)
local function _write_repl_queue_file_local(command_text, source_text, metadata)
command_text = trim(tostring(command_text or ''))
if command_text == '' then
return nil, 'empty pipeline command'
end
local repo_root = _detect_repo_root()
if repo_root == '' then
return nil, 'repo root not found'
end
local log_dir = utils.join_path(repo_root, 'Log')
if not _path_exists(log_dir) then
return nil, 'Log directory not found'
end
local stamp = tostring(math.floor(mp.get_time() * 1000))
local token = tostring(math.random(100000, 999999))
local path = utils.join_path(log_dir, 'medeia-repl-queue-' .. stamp .. '-' .. token .. '.json')
local payload = {
id = stamp .. '-' .. token,
command = command_text,
source = trim(tostring(source_text or 'external')),
created_at = os.time(),
}
if type(metadata) == 'table' and next(metadata) ~= nil then
payload.metadata = metadata
end
local encoded = utils.format_json(payload)
if type(encoded) ~= 'string' or encoded == '' then
return nil, 'failed to encode queue payload'
end
local fh = io.open(path, 'w')
if not fh then
return nil, 'failed to open queue file'
end
fh:write(encoded)
fh:close()
return path, nil
end
local function _queue_pipeline_in_repl(pipeline_cmd, queued_message, failure_prefix, queue_label, metadata)
pipeline_cmd = trim(tostring(pipeline_cmd or ''))
if pipeline_cmd == '' then
mp.osd_message((failure_prefix or 'REPL queue failed') .. ': empty pipeline command', 5)
return false
end
local queue_metadata = { kind = 'mpv-download' }
if type(metadata) == 'table' then
for key, value in pairs(metadata) do
queue_metadata[key] = value
end
end
_lua_log(queue_label .. ': queueing repl cmd=' .. pipeline_cmd)
do
local queue_path, queue_err = _write_repl_queue_file_local(
pipeline_cmd,
queue_label,
queue_metadata
)
if queue_path then
_lua_log(queue_label .. ': queued repl command locally path=' .. tostring(queue_path))
mp.osd_message(tostring(queued_message or 'Queued in REPL'), 3)
return true
end
_lua_log(queue_label .. ': local queue write failed err=' .. tostring(queue_err or 'unknown') .. '; falling back to helper')
end
ensure_mpv_ipc_server()
if not ensure_pipeline_helper_running() then
mp.osd_message((failure_prefix or 'REPL queue failed') .. ': helper not running', 5)
@@ -1542,7 +1653,7 @@ local function _queue_pipeline_in_repl(pipeline_cmd, queued_message, failure_pre
data = {
command = pipeline_cmd,
source = queue_label,
metadata = { kind = 'mpv-download' },
metadata = queue_metadata,
},
},
4.0,
@@ -1553,6 +1664,14 @@ local function _queue_pipeline_in_repl(pipeline_cmd, queued_message, failure_pre
mp.osd_message(tostring(queued_message or 'Queued in REPL'), 3)
return
end
local err_text = tostring(err or '')
if err_text:find('timeout waiting response', 1, true) ~= nil then
_lua_log(queue_label .. ': queue ack timeout; assuming repl command queued')
mp.osd_message(tostring(queued_message or 'Queued in REPL'), 3)
return
end
local detail = tostring(err or (resp and resp.error) or 'unknown')
_lua_log(queue_label .. ': queue failed err=' .. detail)
mp.osd_message((failure_prefix or 'REPL queue failed') .. ': ' .. detail, 5)
@@ -3764,13 +3883,25 @@ local function _start_download_flow_for_current()
pipeline_cmd,
'Queued in REPL: store copy',
'REPL queue failed',
'download-store-copy'
'download-store-copy',
{
mpv_notify = {
success_text = 'Copy completed: store ' .. tostring(store_hash.store),
failure_text = 'Copy failed: store ' .. tostring(store_hash.store),
duration_ms = 3500,
},
}
)
return
end
-- Non-store URL flow: use the current yt-dlp-selected format and ask for save location.
local url = tostring(target)
local download_url, stripped_playlist = _download_url_for_current_item(url)
if stripped_playlist then
_lua_log('download: stripped hidden playlist params from current url -> ' .. tostring(download_url))
url = tostring(download_url)
end
local fmt = _current_ytdl_format_string()
if not fmt or fmt == '' then
@@ -3966,7 +4097,14 @@ mp.register_script_message('medios-download-pick-store', function(json)
pipeline_cmd,
'Queued in REPL: save to store ' .. store,
'REPL queue failed',
'download-store-save'
'download-store-save',
{
mpv_notify = {
success_text = 'Download completed: store ' .. store .. ' [' .. fmt .. ']',
failure_text = 'Download failed: store ' .. store .. ' [' .. fmt .. ']',
duration_ms = 3500,
},
}
)
_pending_download = nil
end)
@@ -3996,7 +4134,14 @@ mp.register_script_message('medios-download-pick-path', function()
pipeline_cmd,
'Queued in REPL: save to folder',
'REPL queue failed',
'download-folder-save'
'download-folder-save',
{
mpv_notify = {
success_text = 'Download completed: folder [' .. fmt .. ']',
failure_text = 'Download failed: folder [' .. fmt .. ']',
duration_ms = 3500,
},
}
)
_pending_download = nil
end)