cleanup code

This commit is contained in:
2025-08-07 21:37:40 -07:00
parent 9d1b6d7eab
commit dc1743ba38

View File

@@ -15,8 +15,9 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>. -- along with this program. If not, see <https://www.gnu.org/licenses/>.
local mp = require("mp") local mp = require("mp")
mp.options = require("mp.options") mp.options = require("mp.options")
local utils = require("mp.utils")
local assdraw = require("mp.assdraw") local assdraw = require("mp.assdraw")
local msg = require("mp.msg")
local utils = require("mp.utils")
local styleOn = mp.get_property("osd-ass-cc/0") local styleOn = mp.get_property("osd-ass-cc/0")
local styleOff = mp.get_property("osd-ass-cc/1") local styleOff = mp.get_property("osd-ass-cc/1")
local YouTubeQueue = {} local YouTubeQueue = {}
@@ -76,7 +77,8 @@ local function destroy()
destroyer = nil destroyer = nil
end end
timeout = mp.add_periodic_timer(options.menu_timeout, destroy) timeout = mp.add_timeout(options.menu_timeout, destroy)
timeout:kill()
-- STYLE {{{ -- STYLE {{{
local colors = { local colors = {
@@ -121,8 +123,11 @@ local style = {
--- @param s string - the string to surround with quotes --- @param s string - the string to surround with quotes
--- @return string | nil - the string surrounded with quotes --- @return string | nil - the string surrounded with quotes
local function surround_with_quotes(s) local function surround_with_quotes(s)
if string.sub(s, 0, 1) == '"' and string.sub(s, -1) == '"' then if type(s) ~= "string" then
return nil return s
end
if string.sub(s, 1, 1) == '"' and string.sub(s, -1) == '"' then
return s
else else
return '"' .. s .. '"' return '"' .. s .. '"'
end end
@@ -174,13 +179,11 @@ end
---@return boolean - true if the path is a file, false otherwise ---@return boolean - true if the path is a file, false otherwise
local function is_file(filepath) local function is_file(filepath)
local result = utils.file_info(filepath) local result = utils.file_info(filepath)
if debug and type(result) == "table" then msg.debug("IS_FILE() check: " .. tostring(result.is_file))
print("IS_FILE() check: " .. tostring(result.is_file))
end
if result == nil or type(result) ~= "table" then if result == nil or type(result) ~= "table" then
return false return false
end end
return true return result.is_file == true
end end
---returns the filename given a path (eg. /home/user/file.txt -> file.txt) ---returns the filename given a path (eg. /home/user/file.txt -> file.txt)
@@ -215,8 +218,14 @@ end
---Open a URL in the browser ---Open a URL in the browser
---@param url string ---@param url string
local function open_url_in_browser(url) local function open_url_in_browser(url)
local command = options.browser .. " " .. surround_with_quotes(url) if isnull(url) then
os.execute(command) return
end
mp.command_native({
name = "subprocess",
detach = true,
args = { options.browser, url },
})
end end
--- Opens the current video in the browser --- Opens the current video in the browser
@@ -236,10 +245,10 @@ end
-- Internal function to print the contents of the internal playlist to the console -- Internal function to print the contents of the internal playlist to the console
local function print_internal_playlist() local function print_internal_playlist()
local count = mp.get_property_number("playlist-count") local count = mp.get_property_number("playlist-count")
print("Playlist contents:") msg.info("Playlist contents:")
for i = 0, count - 1 do for i = 0, count - 1 do
local uri = mp.get_property(string.format("playlist/%d/filename", i)) local uri = mp.get_property(string.format("playlist/%d/filename", i))
print(string.format("%d: %s", i, uri)) msg.info(string.format("%d: %s", i, uri))
end end
end end
@@ -323,26 +332,11 @@ end
--- @return string | nil - The resulting JSON string, or nil if the input is invalid. --- @return string | nil - The resulting JSON string, or nil if the input is invalid.
local function convert_to_json(key, val) local function convert_to_json(key, val)
if type(key) == "table" then if type(key) == "table" then
-- Handle the case where key is a table of key-value pairs return utils.format_json(key)
local json = "{"
local first = true
for k, v in pairs(key) do
if not first then
json = json .. ", "
end
first = false
local quoted_val = string.format('"%s"', v)
json = json .. string.format('"%s": %s', k, quoted_val)
end
json = json .. "}"
return json
else else
if type(val) == "string" then local t = {}
return string.format('{"%s": "%s"}', key, val) t[key] = val
else return utils.format_json(t)
return string.format('{"%s": %s}', key, tostring(val))
end
end end
end end
@@ -411,7 +405,7 @@ function YouTubeQueue.get_video_info(url)
if res.status ~= 0 or isnull(res.stdout) then if res.status ~= 0 or isnull(res.stdout) then
print_osd_message("Failed to get video info (yt-dlp error)", MSG_DURATION, style.error) print_osd_message("Failed to get video info (yt-dlp error)", MSG_DURATION, style.error)
print("yt-dlp status: " .. res.status) msg.warn("yt-dlp status: " .. res.status)
return nil return nil
end end
@@ -450,7 +444,7 @@ end
function YouTubeQueue.print_current_video() function YouTubeQueue.print_current_video()
destroy() destroy()
local current = current_video local current = current_video
if current and current.vidro_url ~= "" and is_file(current.video_url) then if current and current.video_url ~= "" and is_file(current.video_url) then
print_osd_message("Playing: " .. current.video_url, 3) print_osd_message("Playing: " .. current.video_url, 3)
else else
if current and current.video_url then if current and current.video_url then
@@ -469,7 +463,7 @@ end
--- @return table | nil - the video at the new index --- @return table | nil - the video at the new index
function YouTubeQueue.set_video(direction) function YouTubeQueue.set_video(direction)
local amt local amt
direction = string.upper(direction) direction = (direction and string.upper(direction)) or "NEXT"
if direction == "NEXT" or direction == nil then if direction == "NEXT" or direction == nil then
amt = 1 amt = 1
elseif direction == "PREV" or direction == "PREVIOUS" then elseif direction == "PREV" or direction == "PREVIOUS" then
@@ -503,9 +497,7 @@ end
--- @param update_history boolean - whether to update the history database --- @param update_history boolean - whether to update the history database
--- @return number | nil - the index of the currently playing video --- @return number | nil - the index of the currently playing video
function YouTubeQueue.update_current_index(update_history) function YouTubeQueue.update_current_index(update_history)
if debug then msg.debug("Updating current index")
print("Updating current index")
end
if #video_queue == 0 then if #video_queue == 0 then
return return
end end
@@ -588,7 +580,6 @@ function YouTubeQueue.print_queue(duration)
-- Reset and prepare OSD -- Reset and prepare OSD
timeout:kill() timeout:kill()
mp.set_osd_ass(0, 0, "") mp.set_osd_ass(0, 0, "")
timeout:resume()
if #video_queue == 0 then if #video_queue == 0 then
print_osd_message("No videos in the queue or history.", duration, style.error) print_osd_message("No videos in the queue or history.", duration, style.error)
@@ -616,9 +607,13 @@ function YouTubeQueue.print_queue(duration)
ass:append(style.font .. message .. "\n") ass:append(style.font .. message .. "\n")
end end
mp.set_osd_ass(0, 0, ass.text) mp.set_osd_ass(0, 0, ass.text)
if duration then -- restart one-shot timer for menu hide
mp.add_timeout(duration, destroy) if duration and type(duration) == "number" then
timeout.timeout = duration
else
timeout.timeout = options.menu_timeout
end end
timeout:resume()
destroyer = destroy destroyer = destroy
end end
@@ -628,6 +623,7 @@ end
--- @param amt number - the number of steps to move the cursor. Positive values move up, negative values move down. --- @param amt number - the number of steps to move the cursor. Positive values move up, negative values move down.
function YouTubeQueue.move_cursor(amt) function YouTubeQueue.move_cursor(amt)
timeout:kill() timeout:kill()
timeout.timeout = options.menu_timeout
timeout:resume() timeout:resume()
selected_index = selected_index - amt selected_index = selected_index - amt
if selected_index < 1 then if selected_index < 1 then
@@ -846,10 +842,8 @@ function YouTubeQueue.add_to_history_db(v)
local url = options.backend_host .. ":" .. options.backend_port .. "/add_video" local url = options.backend_host .. ":" .. options.backend_port .. "/add_video"
local json = convert_to_json(v) local json = convert_to_json(v)
local command = { "curl", "-X", "POST", url, "-H", "Content-Type: application/json", "-d", json } local command = { "curl", "-X", "POST", url, "-H", "Content-Type: application/json", "-d", json }
if debug then msg.debug("Adding video to history")
print("Adding video to history") msg.debug("Command: " .. table.concat(command, " "))
print("Command: " .. table.concat(command, " "))
end
print_osd_message("Adding video to history...", MSG_DURATION) print_osd_message("Adding video to history...", MSG_DURATION)
mp.command_native_async({ mp.command_native_async({
name = "subprocess", name = "subprocess",
@@ -878,19 +872,16 @@ function YouTubeQueue.save_queue(idx)
idx = index idx = index
end end
local url = options.backend_host .. ":" .. options.backend_port .. "/save_queue" local url = options.backend_host .. ":" .. options.backend_port .. "/save_queue"
local data = convert_to_json("urls", YouTubeQueue.get_urls(idx + 1)) local urls_list = YouTubeQueue.get_urls(idx + 1)
if data == nil or data == '{"urls": []}' then if urls_list == nil or #urls_list == 0 then
print_osd_message("Failed to save queue: No videos remaining in queue", MSG_DURATION, style.error) print_osd_message("Failed to save queue: No videos remaining in queue", MSG_DURATION, style.error)
return false return false
end end
if debug then local data = convert_to_json({ urls = urls_list })
print("Data: " .. data) msg.debug("Data: " .. data)
end
local command = { "curl", "-X", "POST", url, "-H", "Content-Type: application/json", "-d", data } local command = { "curl", "-X", "POST", url, "-H", "Content-Type: application/json", "-d", data }
if debug then msg.debug("Saving queue to history")
print("Saving queue to history") msg.debug("Command: " .. table.concat(command, " "))
print("Command: " .. table.concat(command, " "))
end
mp.command_native_async({ mp.command_native_async({
name = "subprocess", name = "subprocess",
playback_only = false, playback_only = false,
@@ -901,9 +892,7 @@ function YouTubeQueue.save_queue(idx)
print_osd_message("Failed to save queue: " .. err, MSG_DURATION, style.error) print_osd_message("Failed to save queue: " .. err, MSG_DURATION, style.error)
return false return false
end end
if debug then msg.debug("Status: " .. result.status)
print("Status: " .. result.status)
end
if result.status == 0 then if result.status == 0 then
if idx > 1 then if idx > 1 then
print_osd_message("Queue saved to history from index: " .. idx, MSG_DURATION) print_osd_message("Queue saved to history from index: " .. idx, MSG_DURATION)
@@ -934,18 +923,16 @@ function YouTubeQueue.load_queue()
return false return false
else else
if result.status == 0 then if result.status == 0 then
-- split urls based on commas local ok, parsed = pcall(utils.parse_json, result.stdout)
local urls = {} if not ok or type(parsed) ~= "table" then
-- Remove the brackets from json list print_osd_message("Invalid JSON from history backend", MSG_DURATION, style.error)
local l = result.stdout:sub(2, -3) return false
local item
for turl in l:gmatch("[^,]+") do
item = turl:match("^%s*(.-)%s*$"):gsub('"', "'")
table.insert(urls, item)
end end
for _, turl in ipairs(urls) do for _, turl in ipairs(parsed) do
if type(turl) == "string" then
YouTubeQueue.add_to_queue(turl, 0) YouTubeQueue.add_to_queue(turl, 0)
end end
end
print_osd_message("Loaded queue from history.", MSG_DURATION) print_osd_message("Loaded queue from history.", MSG_DURATION)
end end
end end
@@ -959,9 +946,7 @@ end
-- This function is called when the current file ends or when moving to the -- This function is called when the current file ends or when moving to the
-- next or previous item in the internal playlist -- next or previous item in the internal playlist
local function on_end_file(event) local function on_end_file(event)
if debug then msg.debug("End file event triggered: " .. event.reason)
print("End file event triggered: " .. event.reason)
end
if event.reason == "eof" then -- The file ended normally if event.reason == "eof" then -- The file ended normally
YouTubeQueue.update_current_index(true) YouTubeQueue.update_current_index(true)
end end
@@ -969,24 +954,18 @@ end
-- Function to be called when the track-changed event is triggered -- Function to be called when the track-changed event is triggered
local function on_track_changed() local function on_track_changed()
if debug then msg.debug("Track changed event triggered.")
print("Track changed event triggered.")
end
YouTubeQueue.update_current_index() YouTubeQueue.update_current_index()
end end
local function on_file_loaded() local function on_file_loaded()
if debug then msg.debug("Load file event triggered.")
print("Load file event triggered.")
end
YouTubeQueue.update_current_index(true) YouTubeQueue.update_current_index(true)
end end
-- Function to be called when the playback-restart event is triggered -- Function to be called when the playback-restart event is triggered
local function on_playback_restart() local function on_playback_restart()
if debug then msg.debug("Playback restart event triggered.")
print("Playback restart event triggered.")
end
if current_video == nil then if current_video == nil then
local url = mp.get_property("path") local url = mp.get_property("path")
YouTubeQueue.add_to_queue(url) YouTubeQueue.add_to_queue(url)