-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathampache-http.lua
More file actions
165 lines (143 loc) · 5.89 KB
/
ampache-http.lua
File metadata and controls
165 lines (143 loc) · 5.89 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
160
161
162
163
164
-----------------------------------------------------
-- ----------------------------------------------- --
-- ▄ ▄ ▄ ▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄ ▄ --
-- ▐░▌ ▐░▌ ▐░▌▐░█▀▀▀▀▀ ▀▀█░█▀▀ ▐░▌ ▐░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░█ █░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░░░░░░░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▀▀▀▀▀█░▌ --
-- ▐░█▄▄▄▄▄ ▐░█▄▄▄█░▌▐░█▄▄▄▄▄ ▄▄█░█▄▄ ▐░▌ --
-- ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀ --
-- ----------------------------------------------- --
---- Luci4 utils for Ampache API calls -----
-- -------- https://github.com/icefields --------- --
-----------------------------------------------------
local handshake = require("ampache-handshake")
local http = require("socket.http")
local https = require("ssl.https")
local ltn12 = require("ltn12")
local cjson = require("cjson")
local ampache = require("ampache-common")
local function parseUrlArgs(args)
local serverUrl = args.serverUrl or nil
local action = args.action or nil
local limit = args.limit or 100
local filterValue = args.filterValue or ''
local authToken = args.authToken or nil
local showDupes = args.showDupes or 1
local type = args.type or 'album'
local offset = args.offset or 0
local exact = args.exact or 0
local username = args.usernameData or nil
local include = args.include or nil
return serverUrl, action, limit, filterValue, authToken, showDupes, type, offset, exact, username, include
end
local function getUrl(args)
local serverUrl, action, limit, filterValue, authToken, showDupes, type, offset, exact, username, include =
parseUrlArgs(args)
local url = string.format(
"%s/server/json.server.php?action=%s&limit=%d&filter=%s&exact=%d&offset=%d&type=%s&show_dupes=%d&auth=%s",
serverUrl, action, limit, filterValue, exact, offset, type, showDupes, authToken, username
)
if username ~= nil then
url = url .. "&username=" .. username
end
if include ~= nil then
url = url .. "&include=" .. include
end
return url
end
local function makeRequestFromUrl(url)
local max_redirects = 5
local response_body = {}
for _ = 1, max_redirects do
response_body = {}
local request = url:match("^https") and https or http
local res, code, response_headers, status = request.request{
url = url,
sink = ltn12.sink.table(response_body),
redirect = false -- handle redirects manually
}
-- Follow redirect if needed
if (code == 301 or code == 302) and response_headers and response_headers.location then
url = response_headers.location
else
local json_response = nil
local data = nil
if code == 200 then
if response_body and #response_body > 0 then
json_response = table.concat(response_body)
local ok, decoded = pcall(cjson.decode, json_response)
if ok then
data = decoded
-- The server can be returning an error json despite of the 200 response
if data.error ~= nil then
return nil, data.error.errorCode, {}, "Error Returned by server", json_response, data
end
else
return nil, 404, {}, "error decoding json", nil, nil
end
end
end
return res, code, response_headers, status, json_response, data
end
end
return nil, 310, {}, "Too many redirects", nil, nil
end
function authToken(serverUrl, username, password)
local filename = "token"
local token = nil
if ampache.isFileEmpty(filename) then
token = handshake.getAuthToken(serverUrl, username, password)
ampache.writeFile(filename, token)
else
token = ampache.readFile(filename)
end
return token
end
function makeRequest(args, printUrl)
res, code, response_headers, status, json_response, data = getTokenAndPerformRequest(args, printUrl)
if code == 200 then
return res, code, response_headers, status, json_response, data
else
ampache.writeFile("token", "")
return getTokenAndPerformRequest(args, printUrl)
end
end
function getTokenAndPerformRequest(args, printUrl)
local authToken = authToken(args.serverUrl, args.username, args.password)
args.authToken = authToken
local url = getUrl(args)
if printUrl == true then
print(url)
end
return makeRequestFromUrl(url)
end
function streamUrl(serverUrl, username, password, songId, authToken)
local authToken = authToken(serverUrl, username, password)
return string.format(
"%s/server/json.server.php?action=stream&auth=%s&type=song&id=%s",
serverUrl, authToken, songId
)
end
return {
makeRequest = makeRequest,
streamUrl = streamUrl,
authToken = authToken
}
-- DEPRECATED
--local function makeRequestFromUrlOLD(url)
-- local response_body = {}
-- local res, code, response_headers, status = http.request{
-- url = url,
-- sink = ltn12.sink.table(response_body) -- Capture the response into the table
-- }
--
-- local json_response = nil
-- local data = nil
-- if (code == 200) then
-- json_response = table.concat(response_body)
-- data = cjson.decode(json_response)
-- end
--
-- return res, code, response_headers, status, json_response, data
--end