lua: Fix formatting with lua-format
This commit is contained in:
parent
b57db544b7
commit
0e34af1992
13 changed files with 3867 additions and 3385 deletions
|
|
@ -4,24 +4,22 @@ local lqprofile = "lowquality"
|
|||
local hqprofile = "highquality"
|
||||
|
||||
local function powerstate()
|
||||
local f =io.open("/sys/class/power_supply/AC/online")
|
||||
if f == nil then
|
||||
return
|
||||
end
|
||||
local t = f:read("*n")
|
||||
f:close()
|
||||
return t
|
||||
local f = io.open("/sys/class/power_supply/AC/online")
|
||||
if f == nil then return end
|
||||
local t = f:read("*n")
|
||||
f:close()
|
||||
return t
|
||||
end
|
||||
|
||||
local function adjust()
|
||||
local state = powerstate()
|
||||
-- this actually overrides automatically applied profiles
|
||||
-- like 'protocol.http'
|
||||
if state == 0 then
|
||||
mp.msg.info("Running on battery, setting low-quality options.")
|
||||
mp.set_property("profile", lqprofile)
|
||||
else
|
||||
mp.msg.info("Not running on battery, setting high-quality options.")
|
||||
end
|
||||
local state = powerstate()
|
||||
-- this actually overrides automatically applied profiles
|
||||
-- like 'protocol.http'
|
||||
if state == 0 then
|
||||
mp.msg.info("Running on battery, setting low-quality options.")
|
||||
mp.set_property("profile", lqprofile)
|
||||
else
|
||||
mp.msg.info("Not running on battery, setting high-quality options.")
|
||||
end
|
||||
end
|
||||
mp.add_hook("on_load", 1, adjust)
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -2,8 +2,7 @@
|
|||
--
|
||||
-- This script skips sponsored segments of YouTube videos
|
||||
-- using data from https://github.com/ajayyy/SponsorBlock
|
||||
|
||||
local ON_WINDOWS = package.config:sub(1,1) ~= '/'
|
||||
local ON_WINDOWS = package.config:sub(1, 1) ~= '/'
|
||||
|
||||
local options = {
|
||||
server_address = "https://sponsor.ajay.app",
|
||||
|
|
@ -71,9 +70,7 @@ mp.options = require "mp.options"
|
|||
mp.options.read_options(options, "sponsorblock")
|
||||
|
||||
local legacy = mp.command_native_async == nil
|
||||
if legacy then
|
||||
options.local_database = false
|
||||
end
|
||||
if legacy then options.local_database = false end
|
||||
|
||||
local utils = require "mp.utils"
|
||||
if mp.get_script_directory == nil then
|
||||
|
|
@ -83,7 +80,9 @@ else
|
|||
end
|
||||
local sponsorblock = utils.join_path(scripts_dir, "shared/sponsorblock.py")
|
||||
local uid_path = utils.join_path(scripts_dir, "shared/sponsorblock.txt")
|
||||
local database_file = options.local_database and utils.join_path(scripts_dir, "shared/sponsorblock.db") or ""
|
||||
local database_file = options.local_database and
|
||||
utils.join_path(scripts_dir, "shared/sponsorblock.db") or
|
||||
""
|
||||
local youtube_id = nil
|
||||
local ranges = {}
|
||||
local init = false
|
||||
|
|
@ -96,8 +95,13 @@ local fade_dir = nil
|
|||
local volume_before = mp.get_property_number("volume")
|
||||
|
||||
function file_exists(name)
|
||||
local f = io.open(name,"r")
|
||||
if f ~= nil then io.close(f) return true else return false end
|
||||
local f = io.open(name, "r")
|
||||
if f ~= nil then
|
||||
io.close(f)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
function t_count(t)
|
||||
|
|
@ -106,15 +110,14 @@ function t_count(t)
|
|||
return count
|
||||
end
|
||||
|
||||
function time_sort(a, b)
|
||||
return a.time < b.time
|
||||
end
|
||||
function time_sort(a, b) return a.time < b.time end
|
||||
|
||||
function clean_chapters()
|
||||
local chapters = mp.get_property_native("chapter-list")
|
||||
local new_chapters = {}
|
||||
for _, chapter in pairs(chapters) do
|
||||
if chapter.title ~= "Preview segment start" and chapter.title ~= "Preview segment end" then
|
||||
if chapter.title ~= "Preview segment start" and chapter.title ~=
|
||||
"Preview segment end" then
|
||||
table.insert(new_chapters, chapter)
|
||||
end
|
||||
end
|
||||
|
|
@ -124,7 +127,11 @@ end
|
|||
function create_chapter(chapter_title, chapter_time)
|
||||
local chapters = mp.get_property_native("chapter-list")
|
||||
local duration = mp.get_property_native("duration")
|
||||
table.insert(chapters, {title=chapter_title, time=(duration == nil or duration > chapter_time) and chapter_time or duration - .001})
|
||||
table.insert(chapters, {
|
||||
title = chapter_title,
|
||||
time = (duration == nil or duration > chapter_time) and chapter_time or
|
||||
duration - .001
|
||||
})
|
||||
table.sort(chapters, time_sort)
|
||||
mp.set_property_native("chapter-list", chapters)
|
||||
end
|
||||
|
|
@ -134,7 +141,8 @@ function getranges(_, exists, db, more)
|
|||
if options.server_fallback then
|
||||
mp.add_timeout(0, function() getranges(true, true, "") end)
|
||||
else
|
||||
return mp.osd_message("[sponsorblock] database update failed, gave up")
|
||||
return mp.osd_message(
|
||||
"[sponsorblock] database update failed, gave up")
|
||||
end
|
||||
end
|
||||
if db ~= "" and db ~= database_file then db = database_file end
|
||||
|
|
@ -151,20 +159,23 @@ function getranges(_, exists, db, more)
|
|||
end
|
||||
local sponsors
|
||||
local args = {
|
||||
options.python_path,
|
||||
sponsorblock,
|
||||
"ranges",
|
||||
db,
|
||||
options.server_address,
|
||||
options.python_path, sponsorblock, "ranges", db, options.server_address,
|
||||
youtube_id
|
||||
}
|
||||
if not legacy then
|
||||
sponsors = mp.command_native({name = "subprocess", capture_stdout = true, playback_only = false, args = args})
|
||||
sponsors = mp.command_native({
|
||||
name = "subprocess",
|
||||
capture_stdout = true,
|
||||
playback_only = false,
|
||||
args = args
|
||||
})
|
||||
else
|
||||
sponsors = utils.subprocess({args = args})
|
||||
end
|
||||
if not string.match(sponsors.stdout, "^%s*(.*%S)") then return end
|
||||
if string.match(sponsors.stdout, "error") then return getranges(true, true) end
|
||||
if string.match(sponsors.stdout, "error") then
|
||||
return getranges(true, true)
|
||||
end
|
||||
local new_ranges = {}
|
||||
local r_count = 0
|
||||
if more then r_count = -1 end
|
||||
|
|
@ -176,7 +187,8 @@ function getranges(_, exists, db, more)
|
|||
start_time = tonumber(string.match(t, '[^,]+'))
|
||||
end_time = tonumber(string.sub(string.match(t, ',[^,]+'), 2))
|
||||
for o_uuid, o_t in pairs(ranges) do
|
||||
if (start_time >= o_t.start_time and start_time <= o_t.end_time) or (o_t.start_time >= start_time and o_t.start_time <= end_time) then
|
||||
if (start_time >= o_t.start_time and start_time <= o_t.end_time) or
|
||||
(o_t.start_time >= start_time and o_t.start_time <= end_time) then
|
||||
new_ranges[o_uuid] = o_t
|
||||
goto continue
|
||||
end
|
||||
|
|
@ -189,29 +201,31 @@ function getranges(_, exists, db, more)
|
|||
}
|
||||
end
|
||||
if options.make_chapters then
|
||||
create_chapter("Sponsor start (" .. string.sub(uuid, 1, 6) .. ")", start_time)
|
||||
create_chapter("Sponsor end (" .. string.sub(uuid, 1, 6) .. ")", end_time)
|
||||
create_chapter("Sponsor start (" .. string.sub(uuid, 1, 6) ..
|
||||
")", start_time)
|
||||
create_chapter("Sponsor end (" .. string.sub(uuid, 1, 6) .. ")",
|
||||
end_time)
|
||||
end
|
||||
end
|
||||
::continue::
|
||||
r_count = r_count + 1
|
||||
end
|
||||
local c_count = t_count(ranges)
|
||||
if c_count == 0 or r_count >= c_count then
|
||||
ranges = new_ranges
|
||||
end
|
||||
if c_count == 0 or r_count >= c_count then ranges = new_ranges end
|
||||
end
|
||||
|
||||
function fast_forward()
|
||||
local last_speed = mp.get_property_number("speed")
|
||||
local new_speed = math.min(last_speed + options.fast_forward_increase, options.fast_forward_cap)
|
||||
local new_speed = math.min(last_speed + options.fast_forward_increase,
|
||||
options.fast_forward_cap)
|
||||
if new_speed <= last_speed then return end
|
||||
mp.set_property("speed", new_speed)
|
||||
end
|
||||
|
||||
function fade_audio(step)
|
||||
local last_volume = mp.get_property_number("volume")
|
||||
local new_volume = math.max(options.audio_fade_cap, math.min(last_volume + step, volume_before))
|
||||
local new_volume = math.max(options.audio_fade_cap,
|
||||
math.min(last_volume + step, volume_before))
|
||||
if new_volume == last_volume then
|
||||
if step >= 0 then fade_dir = nil end
|
||||
if fade_timer ~= nil then fade_timer:kill() end
|
||||
|
|
@ -225,7 +239,8 @@ function skip_ads(name, pos)
|
|||
if pos == nil then return end
|
||||
local sponsor_ahead = false
|
||||
for uuid, t in pairs(ranges) do
|
||||
if (options.fast_forward == uuid or not options.skip_once or not t.skipped) and t.start_time <= pos and t.end_time > pos then
|
||||
if (options.fast_forward == uuid or not options.skip_once or
|
||||
not t.skipped) and t.start_time <= pos and t.end_time > pos then
|
||||
if options.fast_forward == uuid then return end
|
||||
if options.fast_forward == false then
|
||||
mp.osd_message("[sponsorblock] sponsor skipped")
|
||||
|
|
@ -237,20 +252,18 @@ function skip_ads(name, pos)
|
|||
last_skip = {uuid = uuid, dir = nil}
|
||||
if options.report_views or options.auto_upvote then
|
||||
local args = {
|
||||
options.python_path,
|
||||
sponsorblock,
|
||||
"stats",
|
||||
database_file,
|
||||
options.server_address,
|
||||
youtube_id,
|
||||
uuid,
|
||||
options.report_views and "1" or "",
|
||||
uid_path,
|
||||
options.user_id,
|
||||
options.auto_upvote and "1" or ""
|
||||
options.python_path, sponsorblock, "stats", database_file,
|
||||
options.server_address, youtube_id, uuid,
|
||||
options.report_views and "1" or "", uid_path,
|
||||
options.user_id, options.auto_upvote and "1" or ""
|
||||
}
|
||||
if not legacy then
|
||||
mp.command_native_async({name = "subprocess", playback_only = false, args = args}, function () end)
|
||||
mp.command_native_async(
|
||||
{
|
||||
name = "subprocess",
|
||||
playback_only = false,
|
||||
args = args
|
||||
}, function() end)
|
||||
else
|
||||
utils.subprocess_detached({args = args})
|
||||
end
|
||||
|
|
@ -260,22 +273,29 @@ function skip_ads(name, pos)
|
|||
speed_timer = mp.add_periodic_timer(1, fast_forward)
|
||||
end
|
||||
return
|
||||
elseif (not options.skip_once or not t.skipped) and t.start_time <= pos + 1 and t.end_time > pos + 1 then
|
||||
elseif (not options.skip_once or not t.skipped) and t.start_time <= pos +
|
||||
1 and t.end_time > pos + 1 then
|
||||
sponsor_ahead = true
|
||||
end
|
||||
end
|
||||
if options.audio_fade then
|
||||
if sponsor_ahead then
|
||||
if fade_dir ~= false then
|
||||
if fade_dir == nil then volume_before = mp.get_property_number("volume") end
|
||||
if fade_dir == nil then
|
||||
volume_before = mp.get_property_number("volume")
|
||||
end
|
||||
if fade_timer ~= nil then fade_timer:kill() end
|
||||
fade_dir = false
|
||||
fade_timer = mp.add_periodic_timer(.1, function() fade_audio(-options.audio_fade_step) end)
|
||||
fade_timer = mp.add_periodic_timer(.1, function()
|
||||
fade_audio(-options.audio_fade_step)
|
||||
end)
|
||||
end
|
||||
elseif fade_dir == false then
|
||||
fade_dir = true
|
||||
if fade_timer ~= nil then fade_timer:kill() end
|
||||
fade_timer = mp.add_periodic_timer(.1, function() fade_audio(options.audio_fade_step) end)
|
||||
fade_timer = mp.add_periodic_timer(.1, function()
|
||||
fade_audio(options.audio_fade_step)
|
||||
end)
|
||||
end
|
||||
end
|
||||
if options.fast_forward and options.fast_forward ~= true then
|
||||
|
|
@ -286,25 +306,27 @@ function skip_ads(name, pos)
|
|||
end
|
||||
|
||||
function vote(dir)
|
||||
if last_skip.uuid == "" then return mp.osd_message("[sponsorblock] no sponsors skipped, can't submit vote") end
|
||||
if last_skip.uuid == "" then
|
||||
return mp.osd_message(
|
||||
"[sponsorblock] no sponsors skipped, can't submit vote")
|
||||
end
|
||||
local updown = dir == "1" and "up" or "down"
|
||||
if last_skip.dir == dir then return mp.osd_message("[sponsorblock] " .. updown .. "vote already submitted") end
|
||||
if last_skip.dir == dir then
|
||||
return mp.osd_message("[sponsorblock] " .. updown ..
|
||||
"vote already submitted")
|
||||
end
|
||||
last_skip.dir = dir
|
||||
local args = {
|
||||
options.python_path,
|
||||
sponsorblock,
|
||||
"stats",
|
||||
database_file,
|
||||
options.server_address,
|
||||
youtube_id,
|
||||
last_skip.uuid,
|
||||
"",
|
||||
uid_path,
|
||||
options.user_id,
|
||||
dir
|
||||
options.python_path, sponsorblock, "stats", database_file,
|
||||
options.server_address, youtube_id, last_skip.uuid, "", uid_path,
|
||||
options.user_id, dir
|
||||
}
|
||||
if not legacy then
|
||||
mp.command_native_async({name = "subprocess", playback_only = false, args = args}, function () end)
|
||||
mp.command_native_async({
|
||||
name = "subprocess",
|
||||
playback_only = false,
|
||||
args = args
|
||||
}, function() end)
|
||||
else
|
||||
utils.subprocess({args = args})
|
||||
end
|
||||
|
|
@ -312,13 +334,14 @@ function vote(dir)
|
|||
end
|
||||
|
||||
function update()
|
||||
mp.command_native_async({name = "subprocess", playback_only = false, args = {
|
||||
options.python_path,
|
||||
sponsorblock,
|
||||
"update",
|
||||
database_file,
|
||||
options.server_address
|
||||
}}, getranges)
|
||||
mp.command_native_async({
|
||||
name = "subprocess",
|
||||
playback_only = false,
|
||||
args = {
|
||||
options.python_path, sponsorblock, "update", database_file,
|
||||
options.server_address
|
||||
}
|
||||
}, getranges)
|
||||
end
|
||||
|
||||
function file_loaded()
|
||||
|
|
@ -327,15 +350,18 @@ function file_loaded()
|
|||
segment = {a = 0, b = 0, progress = 0, first = true}
|
||||
last_skip = {uuid = "", dir = nil}
|
||||
local video_path = mp.get_property("path")
|
||||
local youtube_id1 = string.match(video_path, "https?://youtu%.be/([%a%d%-_]+).*")
|
||||
local youtube_id2 = string.match(video_path, "https?://w?w?w?%.?youtube%.com/v/([%a%d%-_]+).*")
|
||||
local youtube_id1 = string.match(video_path,
|
||||
"https?://youtu%.be/([%a%d%-_]+).*")
|
||||
local youtube_id2 = string.match(video_path,
|
||||
"https?://w?w?w?%.?youtube%.com/v/([%a%d%-_]+).*")
|
||||
local youtube_id3 = string.match(video_path, "/watch%?v=([%a%d%-_]+).*")
|
||||
local youtube_id4 = string.match(video_path, "/embed/([%a%d%-_]+).*")
|
||||
local local_pattern = nil
|
||||
if options.local_pattern ~= "" then
|
||||
local_pattern = string.match(video_path, options.local_pattern)
|
||||
end
|
||||
youtube_id = youtube_id1 or youtube_id2 or youtube_id3 or youtube_id4 or local_pattern
|
||||
youtube_id = youtube_id1 or youtube_id2 or youtube_id3 or youtube_id4 or
|
||||
local_pattern
|
||||
if not youtube_id then return end
|
||||
init = true
|
||||
if not options.local_database then
|
||||
|
|
@ -344,7 +370,9 @@ function file_loaded()
|
|||
local exists = file_exists(database_file)
|
||||
if exists and options.server_fallback then
|
||||
getranges(true, true)
|
||||
mp.add_timeout(0, function() getranges(true, true, "", true) end)
|
||||
mp.add_timeout(0, function()
|
||||
getranges(true, true, "", true)
|
||||
end)
|
||||
elseif exists then
|
||||
getranges(true, true)
|
||||
elseif options.server_fallback then
|
||||
|
|
@ -352,30 +380,25 @@ function file_loaded()
|
|||
end
|
||||
end
|
||||
if initialized then return end
|
||||
if options.skip then
|
||||
mp.observe_property("time-pos", "native", skip_ads)
|
||||
end
|
||||
if options.skip then mp.observe_property("time-pos", "native", skip_ads) end
|
||||
if options.display_name ~= "" then
|
||||
local args = {
|
||||
options.python_path,
|
||||
sponsorblock,
|
||||
"username",
|
||||
database_file,
|
||||
options.server_address,
|
||||
youtube_id,
|
||||
"",
|
||||
"",
|
||||
uid_path,
|
||||
options.user_id,
|
||||
options.display_name
|
||||
options.python_path, sponsorblock, "username", database_file,
|
||||
options.server_address, youtube_id, "", "", uid_path,
|
||||
options.user_id, options.display_name
|
||||
}
|
||||
if not legacy then
|
||||
mp.command_native_async({name = "subprocess", playback_only = false, args = args}, function () end)
|
||||
mp.command_native_async({
|
||||
name = "subprocess",
|
||||
playback_only = false,
|
||||
args = args
|
||||
}, function() end)
|
||||
else
|
||||
utils.subprocess_detached({args = args})
|
||||
end
|
||||
end
|
||||
if not options.local_database or (not options.auto_update and file_exists(database_file)) then return end
|
||||
if not options.local_database or
|
||||
(not options.auto_update and file_exists(database_file)) then return end
|
||||
update()
|
||||
end
|
||||
|
||||
|
|
@ -383,17 +406,19 @@ function set_segment()
|
|||
if not youtube_id then return end
|
||||
local pos = mp.get_property_number("time-pos")
|
||||
if pos == nil then return end
|
||||
if segment.progress > 1 then
|
||||
segment.progress = segment.progress - 2
|
||||
end
|
||||
if segment.progress > 1 then segment.progress = segment.progress - 2 end
|
||||
if segment.progress == 1 then
|
||||
segment.progress = 0
|
||||
segment.b = pos
|
||||
mp.osd_message("[sponsorblock] segment boundary B set, press again for boundary A", 3)
|
||||
mp.osd_message(
|
||||
"[sponsorblock] segment boundary B set, press again for boundary A",
|
||||
3)
|
||||
else
|
||||
segment.progress = 1
|
||||
segment.a = pos
|
||||
mp.osd_message("[sponsorblock] segment boundary A set, press again for boundary B", 3)
|
||||
mp.osd_message(
|
||||
"[sponsorblock] segment boundary A set, press again for boundary B",
|
||||
3)
|
||||
end
|
||||
if options.make_chapters and not segment.first then
|
||||
local start_time = math.min(segment.a, segment.b)
|
||||
|
|
@ -414,25 +439,30 @@ function submit_segment()
|
|||
if end_time - start_time == 0 or end_time == 0 then
|
||||
mp.osd_message("[sponsorblock] empty segment, not submitting")
|
||||
elseif segment.progress <= 1 then
|
||||
mp.osd_message(string.format("[sponsorblock] press Shift+G again to confirm: %.2d:%.2d:%.2d to %.2d:%.2d:%.2d", math.floor(start_time/(60*60)), math.floor(start_time/60%60), math.floor(start_time%60), math.floor(end_time/(60*60)), math.floor(end_time/60%60), math.floor(end_time%60)), 5)
|
||||
mp.osd_message(string.format(
|
||||
"[sponsorblock] press Shift+G again to confirm: %.2d:%.2d:%.2d to %.2d:%.2d:%.2d",
|
||||
math.floor(start_time / (60 * 60)),
|
||||
math.floor(start_time / 60 % 60),
|
||||
math.floor(start_time % 60),
|
||||
math.floor(end_time / (60 * 60)),
|
||||
math.floor(end_time / 60 % 60),
|
||||
math.floor(end_time % 60)), 5)
|
||||
segment.progress = segment.progress + 2
|
||||
else
|
||||
mp.osd_message("[sponsorblock] submitting segment...", 30)
|
||||
local submit
|
||||
local args = {
|
||||
options.python_path,
|
||||
sponsorblock,
|
||||
"submit",
|
||||
database_file,
|
||||
options.server_address,
|
||||
youtube_id,
|
||||
tostring(start_time),
|
||||
tostring(end_time),
|
||||
uid_path,
|
||||
options.user_id
|
||||
options.python_path, sponsorblock, "submit", database_file,
|
||||
options.server_address, youtube_id, tostring(start_time),
|
||||
tostring(end_time), uid_path, options.user_id
|
||||
}
|
||||
if not legacy then
|
||||
submit = mp.command_native({name = "subprocess", capture_stdout = true, playback_only = false, args = args})
|
||||
submit = mp.command_native({
|
||||
name = "subprocess",
|
||||
capture_stdout = true,
|
||||
playback_only = false,
|
||||
args = args
|
||||
})
|
||||
else
|
||||
submit = utils.subprocess({args = args})
|
||||
end
|
||||
|
|
@ -445,14 +475,21 @@ function submit_segment()
|
|||
create_chapter("Submitted segment end", end_time)
|
||||
end
|
||||
elseif string.match(submit.stdout, "error") then
|
||||
mp.osd_message("[sponsorblock] segment submission failed, server may be down. try again", 5)
|
||||
mp.osd_message(
|
||||
"[sponsorblock] segment submission failed, server may be down. try again",
|
||||
5)
|
||||
elseif string.match(submit.stdout, "502") then
|
||||
mp.osd_message("[sponsorblock] segment submission failed, server is down. try again", 5)
|
||||
mp.osd_message(
|
||||
"[sponsorblock] segment submission failed, server is down. try again",
|
||||
5)
|
||||
elseif string.match(submit.stdout, "400") then
|
||||
mp.osd_message("[sponsorblock] segment submission failed, impossible inputs", 5)
|
||||
mp.osd_message(
|
||||
"[sponsorblock] segment submission failed, impossible inputs", 5)
|
||||
segment = {a = 0, b = 0, progress = 0, first = true}
|
||||
elseif string.match(submit.stdout, "429") then
|
||||
mp.osd_message("[sponsorblock] segment submission failed, rate limited. try again", 5)
|
||||
mp.osd_message(
|
||||
"[sponsorblock] segment submission failed, rate limited. try again",
|
||||
5)
|
||||
elseif string.match(submit.stdout, "409") then
|
||||
mp.osd_message("[sponsorblock] segment already submitted", 3)
|
||||
segment = {a = 0, b = 0, progress = 0, first = true}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
if mp.get_script_directory == nil then
|
||||
dofile(mp.find_config_file("scripts/sponsorblock/main.lua"))
|
||||
dofile(mp.find_config_file("scripts/sponsorblock/main.lua"))
|
||||
end
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue