improved random function, tried to fix funkwhale option in search.lua

This commit is contained in:
Rose Liverman 2021-10-06 16:50:44 -06:00
parent f17a44e91f
commit 5a8636b7b4
3 changed files with 16 additions and 9 deletions

File diff suppressed because one or more lines are too long

View File

@ -120,7 +120,7 @@ function music.random(count, match, include, exclude)
matches = filter(matches, include or {}, exclude or {}) matches = filter(matches, include or {}, exclude or {})
while count > 0 and #matches > 0 do while count > 0 and #matches > 0 do
for i = #matches, 1, -1 do for i = #matches, 1, -1 do
if math.random() > 0.5 then if math.random() < 1 / #matches then
table.insert(results, table.remove(matches, i)) table.insert(results, table.remove(matches, i))
count = count - 1 count = count - 1
end end

View File

@ -31,23 +31,30 @@ local count = tonumber(arg[1]) or 10
local funkwhale = arg[2] local funkwhale = arg[2]
local results = music.random(count, nil, nil, {downloaded = true, searched = true}) local results = music.random(count, nil, nil, {downloaded = true, searched = true})
local errors_occurred, track = false local errors_occurred, name, encoded = false
for _,v in ipairs(results) do for _,v in ipairs(results) do
track = music.name(v) -- music.data[v].names[1] name = music.name(v) -- music.data[v].names[1]
if not track then if not name then
print("Track '" .. v .. "' does not exist?") print("Track '" .. v .. "' does not exist?")
errors_occurred = true errors_occurred = true
break break
end end
if urlencode then if urlencode then
track = urlencode.encode_url(track) encoded = urlencode.encode_url(name)
else else
track = track:gsub("%s", "+"):gsub("&", "&amp;") encoded = name:gsub("%s", "+"):gsub("&", "&amp;")
end end
if funkwhale then if funkwhale then
os.execute("open \"https://funkwhale.tangentfox.com/search?q=" .. track .. "&type=tracks\"") local separator = name:find(" %- ")
if separator then name = name:sub(separator + 3) end
if urlencode then
name = urlencode.encode_url(name)
else
name = name:gsub("%s", "+"):gsub("&", "&amp;")
end end
os.execute("open \"https://google.com/search?q=" .. track .. "\"") os.execute("open \"https://funkwhale.tangentfox.com/search?q=" .. name .. "&type=tracks\"")
end
os.execute("open \"https://google.com/search?q=" .. encoded .. "\"")
end end
if errors_occurred then if errors_occurred then
print("Database not saved because errors occurred.") print("Database not saved because errors occurred.")