Skip to content

Commit

Permalink
update save_queue function and add alt command
Browse files Browse the repository at this point in the history
- add option to save full or unwatched videos only
- fix when only 1 valid video
- fix error handling
- show messages on save/load
  • Loading branch information
ksyasuda committed Sep 10, 2024
1 parent 3bbbcae commit 7e8a38d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions mpv-youtube-queue.conf
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
38 changes: 32 additions & 6 deletions mpv-youtube-queue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7e8a38d

Please sign in to comment.