Squash bugs
Some checks failed
Luacheck / luacheck (push) Failing after 13s

- update strip command to remove newline characters
- add history db function call to playback restart listener to catch
first video
This commit is contained in:
ksyasuda 2024-09-05 04:12:27 -07:00
parent e484aa5068
commit ed3d280cda
No known key found for this signature in database

View File

@ -114,7 +114,7 @@ local function surround_with_quotes(s)
end end
end end
local function remove_quotes(s) return string.gsub(s, "'", "") end local function strip(s) return string.gsub(s, "['\n\r]", "") end
-- run sleep shell command for n seconds -- run sleep shell command for n seconds
local function sleep(n) os.execute("sleep " .. tonumber(n)) end local function sleep(n) os.execute("sleep " .. tonumber(n)) end
@ -209,16 +209,15 @@ local function _split_command(cmd)
return components return components
end end
function YouTubeQueue._add_to_history_db(video) function YouTubeQueue._add_to_history_db(v)
if not options.use_history_db then return end
local url = options.backend_host .. ":" .. options.backend_port .. local url = options.backend_host .. ":" .. options.backend_port ..
"/add_video" "/add_video"
local current_date = os.date("%Y-%m-%d") -- Get the current date in YYYY-MM-DD format
local command = { local command = {
"curl", "-X", "POST", url, "-H", "Content-Type: application/json", "-d", "curl", "-X", "POST", url, "-H", "Content-Type: application/json", "-d",
string.format( string.format(
'{"video_url": "%s", "video_name": "%s", "channel_url": "%s", "channel_name": "%s", "watch_date": "%s"}', '{"video_url": "%s", "video_name": "%s", "channel_url": "%s", "channel_name": "%s"}',
video.video_url, video.video_name, video.channel_url, v.video_url, v.video_name, v.channel_url, v.channel_name)
video.channel_name, current_date)
} }
mp.command_native_async({ mp.command_native_async({
name = "subprocess", name = "subprocess",
@ -229,8 +228,10 @@ function YouTubeQueue._add_to_history_db(video)
if not success then if not success then
print_osd_message("Failed to send video data to backend: " .. err, print_osd_message("Failed to send video data to backend: " .. err,
MSG_DURATION, style.error) MSG_DURATION, style.error)
return false
end end
end) end)
return true
end end
-- }}} -- }}}
@ -513,9 +514,7 @@ function YouTubeQueue.play_video(direction)
mp.set_property_number("playlist-pos", index - 1) mp.set_property_number("playlist-pos", index - 1)
end end
YouTubeQueue.print_current_video() YouTubeQueue.print_current_video()
if options.use_history_db then -- YouTubeQueue._add_to_history_db(current_video)
YouTubeQueue._add_to_history_db(current_video)
end
end end
-- add the video to the queue from the clipboard or call from script-message -- add the video to the queue from the clipboard or call from script-message
@ -538,7 +537,7 @@ function YouTubeQueue.add_to_queue(url, update_internal_playlist)
local video, channel_url, channel_name, video_name local video, channel_url, channel_name, video_name
if not is_file(url) then if not is_file(url) then
channel_url, channel_name, video_name = YouTubeQueue.get_video_info(url) channel_url, channel_name, video_name = YouTubeQueue.get_video_info(url)
url = remove_quotes(url) url = strip(url)
if (channel_url == nil or channel_name == nil or video_name == nil) or if (channel_url == nil or channel_name == nil or video_name == nil) or
(channel_url == "" or channel_name == "" or video_name == "") then (channel_url == "" or channel_name == "" or video_name == "") then
print_osd_message("Error getting video info.", MSG_DURATION, print_osd_message("Error getting video info.", MSG_DURATION,
@ -637,10 +636,13 @@ end
-- LISTENERS {{{ -- LISTENERS {{{
-- Function to be called when the end-file event is triggered -- Function to be called when the end-file event is triggered
-- This function is called when the current file ends or when moving to the
-- next or previous item in the internal playlist
local function on_end_file(event) local function on_end_file(event)
if event.reason == "eof" then -- The file ended normally if event.reason == "eof" then -- The file ended normally
YouTubeQueue.update_current_index() YouTubeQueue.update_current_index()
end end
YouTubeQueue._add_to_history_db(current_video)
end end
-- Function to be called when the track-changed event is triggered -- Function to be called when the track-changed event is triggered
@ -654,6 +656,7 @@ local function on_playback_restart()
elseif current_video == nil then elseif 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)
YouTubeQueue._add_to_history_db(current_video)
end end
end end