add open_channel_in_browser and error checking for add_to_queue

- add function to open the channel page for the currently playing video in a new tab
- add error checking for url passed to add_to_queue function
   - ensure url is a YouTube url
   - ensure the url is able to be processed by yt-dlp
This commit is contained in:
ksyasuda@umich.edu 2023-08-02 16:11:35 -07:00
parent fdb02bc58d
commit e2e4d2ddd5
2 changed files with 49 additions and 10 deletions

View File

@ -6,6 +6,7 @@ move_selection_up=ctrl+UP
move_selection_down=ctrl+DOWN move_selection_down=ctrl+DOWN
play_selected_video=ctrl+ENTER play_selected_video=ctrl+ENTER
open_video_in_browser=ctrl+o open_video_in_browser=ctrl+o
open_channel_in_browser=ctrl+O
print_current_video=ctrl+P print_current_video=ctrl+P
browser=firefox browser=firefox
clipboard_command=xclip -o clipboard_command=xclip -o

View File

@ -33,6 +33,7 @@ local options = {
move_selection_down = "ctrl+DOWN", move_selection_down = "ctrl+DOWN",
play_selected_video = "ctrl+ENTER", play_selected_video = "ctrl+ENTER",
open_video_in_browser = "ctrl+o", open_video_in_browser = "ctrl+o",
open_channel_in_browser = "ctrl+O",
print_current_video = "ctrl+P", print_current_video = "ctrl+P",
browser = "firefox", browser = "firefox",
clipboard_command = "xclip -o" clipboard_command = "xclip -o"
@ -58,6 +59,25 @@ local function get_video_name(url)
return result:gsub("%s+$", "") return result:gsub("%s+$", "")
end end
-- get the channel url from a video url
local function get_channel_url(url)
local command = 'yt-dlp --print channel_url --playlist-items 1 ' .. url
local handle = io.popen(command)
if not handle then return nil end
local result = handle:read("*a")
handle:close()
return result:gsub("%s+$", "")
end
local function is_valid_ytdlp_url(url)
local command = 'yt-dlp --simulate \'' .. url .. '\' >/dev/null 2>&1'
mp.msg.log("info", "Checking if URL is valid: " .. command)
local handle = io.popen(command .. "; echo $?")
local result = handle:read("*a")
handle:close()
return result:gsub("%s+$", "") == "0"
end
-- }}} -- }}}
-- QUEUE GETTERS AND SETTERS {{{ -- QUEUE GETTERS AND SETTERS {{{
@ -113,7 +133,6 @@ function YouTubeQueue.play_video_at(idx)
end end
index = idx index = idx
current_video = video_queue[index] current_video = video_queue[index]
mp.msg.log("info", "Playing video at index " .. index)
mp.set_property_number("playlist-pos", index - 1) -- zero-based index mp.set_property_number("playlist-pos", index - 1) -- zero-based index
return current_video return current_video
end end
@ -176,7 +195,10 @@ end
-- returns the content of the clipboard -- returns the content of the clipboard
local function get_clipboard_content() local function get_clipboard_content()
local handle = io.popen(options.clipboard_command) local handle = io.popen(options.clipboard_command)
if not handle then return nil end if not handle then
mp.osd_message("Error getting clipboard content")
return nil
end
local result = handle:read("*a") local result = handle:read("*a")
handle:close() handle:close()
return result return result
@ -227,15 +249,28 @@ end
-- add the video to the queue from the clipboard -- add the video to the queue from the clipboard
local function add_to_queue() local function add_to_queue()
local url = get_clipboard_content() local url = get_clipboard_content()
-- get video name in background if not url then
local name = get_video_name(url) mp.osd_message("Nothing found in the clipboard.")
return
-- check to make sure the video is not already in the queue end
if not string.match(url, "^https://www.youtube.com") then
mp.osd_message("Not a YouTube URL.")
return
end
if YouTubeQueue.is_in_queue(url) then if YouTubeQueue.is_in_queue(url) then
mp.osd_message("Video already in queue.") mp.osd_message("Video already in queue.")
return return
elseif not is_valid_ytdlp_url(url) then
mp.osd_message("Invalid URL.")
return
end end
YouTubeQueue.add_to_queue({ url = url, name = name }) local name = get_video_name(url)
local channel_url = get_channel_url(url)
YouTubeQueue.add_to_queue({
url = url,
name = name,
channel_url = channel_url
})
if not YouTubeQueue.get_current_video() then if not YouTubeQueue.get_current_video() then
play_next_in_queue() play_next_in_queue()
else else
@ -261,9 +296,10 @@ local function open_url_in_browser(url)
os.execute(command) os.execute(command)
end end
local function open_video_in_browser() local function open_video_in_browser() open_url_in_browser(current_video.url) end
local current_url = mp.get_property("path")
open_url_in_browser(current_url) local function open_channel_in_browser()
open_url_in_browser(current_video.channel_url)
end end
local function print_current_video() local function print_current_video()
@ -288,6 +324,8 @@ mp.add_key_binding(options.open_video_in_browser, "open_video_in_browser",
open_video_in_browser) open_video_in_browser)
mp.add_key_binding(options.print_current_video, "print_current_video", mp.add_key_binding(options.print_current_video, "print_current_video",
print_current_video) print_current_video)
mp.add_key_binding(options.open_channel_in_browser, "open_channel_in_browser",
open_channel_in_browser)
-- Listen for the file-loaded event -- Listen for the file-loaded event
mp.register_event("end-file", YouTubeQueue.on_end_file) mp.register_event("end-file", YouTubeQueue.on_end_file)