From 4fda62f588eda8891fb6fd065e5f21663d239934 Mon Sep 17 00:00:00 2001 From: Kyle Yasuda Date: Mon, 9 Sep 2024 23:38:43 -0700 Subject: [PATCH] update save_queue function and add alt command (#21) - add option to save full or unwatched videos only - fix when only 1 valid video - fix error handling - show messages on save/load --- README.md | 10 ++++++++-- mpv-youtube-queue.conf | 2 ++ mpv-youtube-queue.lua | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index fb67046..166d7b0 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,10 @@ This script requires the following software to be installed on the system ### Default Keybindings - `add_to_queue - ctrl+a`: Add a video in the clipboard to the queue +- `default_save_method - unwatched`: The default method to use when saving the + queue. Valid options are `unwatched` or `all`. Defaults to `unwatched` + - Whichever option is chosen is the default method for the `save_queue` + binding, and the other method will be bound to `save_queue_alt` - `download_current_video - ctrl+d`: Download the currently playing video - `download_selected_video - ctrl+D`: Download the currently selected video in the queue @@ -54,8 +58,10 @@ This script requires the following software to be installed on the system - `print_current_video - ctrl+P`: Print the name and channel of the currently playing video to the OSD - `print_queue - ctrl+q`: Print the contents of the queue to the OSD -- `save_queue - ctrl+s`: Saves the remainder of the queue (excluding the - currently playing video) to the database for retrevial at a later time +- `save_queue - ctrl+s`: Saves the queue using the chosen method in + `default_save_method` +- `save_queue_alt - ctrl+S`: Saves the queue using the method not chosen in + `default_save_method` - `remove_from_queue - ctrl+x`: Remove the currently selected video from the queue - `play_selected_video - ctrl+ENTER`: Play the currently selected video in diff --git a/mpv-youtube-queue.conf b/mpv-youtube-queue.conf index 5a06972..5dce524 100644 --- a/mpv-youtube-queue.conf +++ b/mpv-youtube-queue.conf @@ -1,4 +1,5 @@ add_to_queue=ctrl+a +default_save_method=unwatched download_current_video=ctrl+d download_selected_video=ctrl+D move_cursor_down=ctrl+j @@ -12,6 +13,7 @@ play_previous_in_queue=ctrl+p print_current_video=ctrl+P print_queue=ctrl+q save_queue=ctrl+s +save_full_queue=ctrl+S remove_from_queue=ctrl+x play_selected_video=ctrl+ENTER browser=firefox diff --git a/mpv-youtube-queue.lua b/mpv-youtube-queue.lua index a642693..371f3b2 100644 --- a/mpv-youtube-queue.lua +++ b/mpv-youtube-queue.lua @@ -64,6 +64,8 @@ local options = { backend_host = "http://localhost", backend_port = "42069", save_queue = "ctrl+s", + save_queue_alt = "ctrl+S", + default_save_method = "unwatched", load_queue = "ctrl+l" } mp.options.read_options(options, "mpv-youtube-queue") @@ -240,7 +242,7 @@ end -- Returns a list of URLs in the queue from index + 1 to the end function YouTubeQueue._get_urls(start_index) - if start_index < 0 or start_index + 1 >= #video_queue then return nil end + if start_index < 0 or start_index > #video_queue then return nil end local urls = {} for i = start_index + 1, #video_queue do table.insert(urls, video_queue[i].video_url) @@ -263,13 +265,14 @@ end -- Saves the remainder of the videos in the queue (all videos after the currently playing -- video) to the history database -function YouTubeQueue.save_queue() +function YouTubeQueue.save_queue(idx) if not options.use_history_db then return false end + if idx == nil then idx = index end local url = options.backend_host .. ":" .. options.backend_port .. "/save_queue" local data = YouTubeQueue._convert_to_json("urls", - YouTubeQueue._get_urls(index)) - if data == nil then + YouTubeQueue._get_urls(idx)) + if data == nil or data == '{"urls": []}' then print_osd_message("Failed to save queue: No videos remaining in queue", MSG_DURATION, style.error) return false @@ -288,12 +291,21 @@ function YouTubeQueue.save_queue() playback_only = false, capture_stdout = true, args = command - }, function(success, _, err) + }, function(success, result, err) if not success then print_osd_message("Failed to save queue: " .. err, MSG_DURATION, style.error) return false end + if debug then print("Status: " .. result.status) end + if result.status == 0 then + if idx > 1 then + print_osd_message("Queue saved to history from index: " .. idx, + MSG_DURATION) + else + print_osd_message("Queue saved to history.", MSG_DURATION) + end + end end) end @@ -328,6 +340,7 @@ function YouTubeQueue.load_queue() for _, turl in ipairs(urls) do YouTubeQueue.add_to_queue(turl) end + print_osd_message("Loaded queue from history.", MSG_DURATION) end end end) @@ -807,7 +820,20 @@ mp.add_key_binding(options.move_video, "move_video", YouTubeQueue.mark_and_move_video) mp.add_key_binding(options.remove_from_queue, "delete_video", YouTubeQueue.remove_from_queue) -mp.add_key_binding(options.save_queue, "save_queue", YouTubeQueue.save_queue) +mp.add_key_binding(options.save_queue, "save_queue", function() + if options.default_save_method == "unwatched" then + YouTubeQueue.save_queue(index) + else + YouTubeQueue.save_queue(1) + end +end) +mp.add_key_binding(options.save_queue_alt, "save_queue_alt", function() + if options.default_save_method == "unwatched" then + YouTubeQueue.save_queue(1) + else + YouTubeQueue.save_queue(index) + end +end) mp.add_key_binding(options.load_queue, "load_queue", YouTubeQueue.load_queue) mp.register_event("end-file", on_end_file)