2022-02-09 15:54:15 +00:00
|
|
|
#!/usr/bin/env luajit
|
2021-09-28 23:08:39 +00:00
|
|
|
|
2022-02-09 15:54:15 +00:00
|
|
|
local function checkreq(name, display)
|
|
|
|
local success, library = pcall(function() return require(name) end)
|
|
|
|
if not success then
|
|
|
|
print("'" .. display or name .. "' missing.")
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
return library
|
|
|
|
end
|
2021-09-28 23:08:39 +00:00
|
|
|
end
|
|
|
|
|
2022-02-09 15:54:15 +00:00
|
|
|
local urlencode = checkreq('urlencode')
|
|
|
|
checkreq('cjson', 'lua-cjson')
|
|
|
|
local music = checkreq('music')
|
|
|
|
|
2021-09-28 23:08:39 +00:00
|
|
|
local help = [[Usage:
|
|
|
|
|
|
|
|
search <count> <funkwhale>
|
|
|
|
|
|
|
|
<count>: How many tracks to search for. Defaults to 10
|
|
|
|
(Opens a new tab per track in your default browser searching Google.)
|
|
|
|
<funkwhale>: Whether or not to search my FunkWhale instance for the track also.
|
|
|
|
|
|
|
|
Optionally requires urlencode to be installed from LuaRocks.
|
2022-02-09 15:54:15 +00:00
|
|
|
Currently only tested on MacOS 11.4 to 12.1.
|
2021-09-28 23:08:39 +00:00
|
|
|
]]
|
|
|
|
if arg[1]:find("h") then
|
|
|
|
print(help)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local count = tonumber(arg[1]) or 10
|
|
|
|
local funkwhale = arg[2]
|
|
|
|
|
|
|
|
local results = music.random(count, nil, nil, {downloaded = true, searched = true})
|
2021-10-06 22:50:44 +00:00
|
|
|
local errors_occurred, name, encoded = false
|
2021-09-28 23:08:39 +00:00
|
|
|
for _,v in ipairs(results) do
|
2021-10-06 22:50:44 +00:00
|
|
|
name = music.name(v) -- music.data[v].names[1]
|
|
|
|
if not name then
|
2021-09-30 05:17:08 +00:00
|
|
|
print("Track '" .. v .. "' does not exist?")
|
|
|
|
errors_occurred = true
|
|
|
|
break
|
|
|
|
end
|
2021-09-28 23:08:39 +00:00
|
|
|
if urlencode then
|
2021-10-06 22:50:44 +00:00
|
|
|
encoded = urlencode.encode_url(name)
|
2021-09-28 23:08:39 +00:00
|
|
|
else
|
2021-10-06 22:50:44 +00:00
|
|
|
encoded = name:gsub("%s", "+"):gsub("&", "&")
|
2021-09-28 23:08:39 +00:00
|
|
|
end
|
|
|
|
if funkwhale then
|
2021-10-06 22:50:44 +00:00
|
|
|
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("&", "&")
|
|
|
|
end
|
|
|
|
os.execute("open \"https://funkwhale.tangentfox.com/search?q=" .. name .. "&type=tracks\"")
|
2021-09-28 23:08:39 +00:00
|
|
|
end
|
2021-10-06 22:50:44 +00:00
|
|
|
os.execute("open \"https://google.com/search?q=" .. encoded .. "\"")
|
2021-09-28 23:08:39 +00:00
|
|
|
end
|
2021-09-30 05:17:08 +00:00
|
|
|
if errors_occurred then
|
|
|
|
print("Database not saved because errors occurred.")
|
|
|
|
else
|
|
|
|
music.set(results, {searched = true})
|
|
|
|
music.save()
|
|
|
|
end
|