From 8eadbf9ec3174dd495a74cac233deb2c4a4aef89 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 20 May 2025 00:48:22 -0700 Subject: use separate script to download images in background --- iptv-menu-dl.lua | 17 +++++++++++++ main.lua | 72 ++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 61 insertions(+), 28 deletions(-) create mode 100644 iptv-menu-dl.lua diff --git a/iptv-menu-dl.lua b/iptv-menu-dl.lua new file mode 100644 index 0000000..9ff4650 --- /dev/null +++ b/iptv-menu-dl.lua @@ -0,0 +1,17 @@ +-- Copyright 2025 David Vazgenovich Shakaryan + +local utils = require('mp.utils') + +mp.register_script_message('download-image', function(target, name, url, path) + if utils.file_info(path) then + return + end + + local cmd = 'curl -sSfLo \'' .. path .. '\'' .. ' \'' .. url .. '\'' + print('exec: ' .. cmd) + local ret = os.execute(cmd) + + if ret == 0 then + mp.commandv('script-message-to', target, name, url, path) + end +end) diff --git a/main.lua b/main.lua index db9f3c4..396e300 100644 --- a/main.lua +++ b/main.lua @@ -22,6 +22,7 @@ local colours = { icon_active='{\\c&H99FF&}', } +local script_name = mp.get_script_name() local script_dir = mp.get_script_directory() local xc_server = mp.get_opt('iptv_menu.xc_server') local xc_user = mp.get_opt('iptv_menu.xc_user') @@ -132,18 +133,17 @@ local function get_image_path(url, dl) local path = 'img/' .. url:gsub('%W', '_') local f = utils.file_info(path) - if not f then - if not dl then - return - end - - local cmd = 'curl -sSfLo \'' .. path .. '\'' .. - ' \'' .. url .. '\'' - print('exec: ' .. cmd) - os.execute(cmd) + if f then + return path end - return path + if dl then + mp.commandv('script-message-to', + 'iptv_menu_dl', 'download-image', + script_name, 'downloaded-image', -- callback + url, path) + return path + end end local function add_object(obj) @@ -421,28 +421,29 @@ local function update_osd_image(path, menu_res) local x = math.floor(osd_width - padding - w) local y = math.floor(padding) - local cmd - - if osd_img and path == osd_img.path and w == osd_img.cmd.w and - h == osd_img.cmd.h then + local disp + if osd_img and osd_img.loaded and path == osd_img.path and + w == osd_img.cmd.w and h == osd_img.cmd.h then if x == osd_img.cmd.x and y == osd_img.cmd.y then return end - goto disp + disp = true + else + local f = utils.file_info(path) + if f then + local cmd = 'magick \'' .. path .. '\'' .. + ' -background none' .. + ' -gravity northwest' .. + ' -resize ' .. w .. 'x' .. h .. + ' -extent ' .. w .. 'x' .. h .. + ' tmp.rgba' + print('exec: ' .. cmd) + os.execute(cmd) + disp = true + end end - cmd = 'magick \'' .. path .. '\'' .. - ' -background none' .. - ' -gravity northwest' .. - ' -resize ' .. w .. 'x' .. h .. - ' -extent ' .. w .. 'x' .. h .. - ' tmp.rgba' - print('exec: ' .. cmd) - os.execute(cmd) - - ::disp:: - print('reloading img') osd_img = { path=path, cmd={ @@ -458,7 +459,15 @@ local function update_osd_image(path, menu_res) stride=4*w, }, } - mp.command_native(osd_img.cmd) + + if disp then + osd_img.loaded = true + if not osd.hidden then + mp.command_native(osd_img.cmd) + end + else + mp.command_native({name='overlay-remove', id=osd_img.cmd.id}) + end end local function remove_osd_image() @@ -498,7 +507,7 @@ local function update_osd() osd_option_path(opt, info) if selected and opt.stream_icon then - img = get_image_path(opt.stream_icon) + img = get_image_path(opt.stream_icon, true) end end @@ -1336,6 +1345,12 @@ local function toggle_menu() end end +mp.register_script_message('downloaded-image', function(url, path) + if osd_img and path == osd_img.path then + update_osd() + end +end) + mp.observe_property('osd-dimensions', 'native', function(_, val) osd_width = val.w osd_height = val.h @@ -1358,3 +1373,4 @@ mp.add_forced_key_binding('TAB', 'toggle-menu', toggle_menu) bind_menu_keys() load_data() push_group_menu(objects['root']) +mp.commandv('load-script', script_dir .. '/iptv-menu-dl.lua') -- cgit v1.2.3-70-g09d2