-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathampache-common.lua
More file actions
160 lines (141 loc) · 5.05 KB
/
ampache-common.lua
File metadata and controls
160 lines (141 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
-----------------------------------------------------
-- ----------------------------------------------- --
-- ▄ ▄ ▄ ▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄ ▄ --
-- ▐░▌ ▐░▌ ▐░▌▐░█▀▀▀▀▀ ▀▀█░█▀▀ ▐░▌ ▐░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░█ █░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░░░░░░░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▀▀▀▀▀█░▌ --
-- ▐░█▄▄▄▄▄ ▐░█▄▄▄█░▌▐░█▄▄▄▄▄ ▄▄█░█▄▄ ▐░▌ --
-- ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀ --
-- ----------------------------------------------- --
---- Luci4 utils for Ampache API calls -----
-- -------- https://github.com/icefields --------- --
-----------------------------------------------------
function shouldPrintHelp()
-- Check for the help flag (-h)
if arg[1] == "-h" then
return true
end
-- Ensure that the server_url, username, and password are provided
if not arg[1] or not arg[2] or not arg[3] then
print("Error: Missing required arguments (server_url, username, password). Use -h for help.")
return true
end
return false
end
function parseArgs(arg)
local limit = 100 -- Default limit
local isJsonOutput = false
local typeValue = "" -- Default filter
local filterValue = "" -- Default filter
local isPrintUrl = false
local include = nil
local serverUrl = arg[1]
local username = arg[2]
local password = arg[3]
-- Parse the command-line arguments
for i = 4, #arg do
local arg_val = arg[i]
if arg_val == "-l" then
-- Limit argument
limit = tonumber(arg[i + 1]) or 100
i = i + 1 -- Skip the next argument
elseif arg_val == "-f" then
-- Filter argument
filterValue = arg[i + 1] or ""
i = i + 1 -- Skip the next argument
elseif arg_val == "-t" then
-- Filter argument
typeValue = arg[i + 1] or ""
i = i + 1 -- Skip the next argument
elseif arg_val == "-i" then
include = arg[i + 1]
i = i + 1
elseif arg_val == "-j" then
isJsonOutput = true
elseif arg_val == "-d" then
isPrintUrl = true
end
end
return serverUrl, username, password, limit, filterValue, isJsonOutput, isPrintUrl, include, typeValue
end
-- Print the help guide
local function printHelp(name)
print("Usage: lua " .. name ..
[[ <server_url> <username> <password> [OPTIONS]
Required arguments:
<server_url> The URL of the Ampache server
<username> The username for authentication
<password> The password for authentication
Optional arguments:
-l <limit> Limit the number of items to retrieve (default: 100)
-f <filter> Specify the filter for the items
-j Prints the original json from the network response, when this is passed, all other optional args are ignored
-t Type
-h Show this help message
-d Print the request url, useful for debugging
]])
end
-- Function to check if a value is valid (not nil, empty, or 'null')
local function isValid(value)
return value ~= nil and value ~= '' and value ~= 'null' and tostring(value) ~= 'userdata: (nil)'
end
local function format_value(v)
if type(v) == "number" and v % 1 == 0 then
return string.format("%d", v) -- remove .0
else
return tostring(v) -- fallback
end
end
-- Function to print only if valid
local function safePrint(label, value)
if isValid(value) then
print(string.format("%s %s", label, format_value(value)))
end
end
function urlencode(str)
return (str:gsub("([^%w%-%.%_~])", function(c)
return string.format("%%%02X", string.byte(c)) -- Replace each non-URL-safe character with its encoded form
end))
end
function fileExists(path)
local f = io.open(path, "r")
if f then
f:close()
return true
else
return false
end
end
local function isFileEmpty(path)
local f = io.open(path, "r")
if not f then return true end -- file doesn't exist
local size = f:seek("end")
f:close()
return size == 0
end
local function readFile(path)
local f = io.open(path, "r")
if not f then return nil end
local content = f:read("*a") -- read entire file
f:close()
return content
end
local function writeFile(path, string)
local file = io.open(path, "w")
file:write(string)
file:close()
end
return {
format_value = format_value,
isValid = isValid,
safePrint = safePrint,
urlencode = urlencode,
printHelp = printHelp,
shouldPrintHelp = shouldPrintHelp,
parseArgs = parseArgs,
fileExists = fileExists,
isFileEmpty = isFileEmpty,
readFile = readFile,
writeFile = writeFile
}