aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2023-10-02 11:18:20 +0530
committersinanmohd <sinan@sinanmohd.com>2023-10-02 11:33:34 +0530
commit9ea6ff9c398a089eec10e84297d68365fe765753 (patch)
tree2b1ff8743bfc6fe1e8cc2c329489d131d8331176 /lib
parent8f0e30c4a23de4757232ec168c100f5fd1d302db (diff)
lib/curl: io.popen -> util.run
Diffstat (limited to 'lib')
-rw-r--r--lib/curl.lua36
1 files changed, 22 insertions, 14 deletions
diff --git a/lib/curl.lua b/lib/curl.lua
index da3b0c7..5ea1d4a 100644
--- a/lib/curl.lua
+++ b/lib/curl.lua
@@ -7,28 +7,36 @@ local def_headr = {
['Accept-Encoding'] = 'gzip, deflate, br'
}
-local gen_head = function (t)
- local heads = ' '
+local head_to_args = function (t)
+ local args = {}
for k, v in pairs(t) do
- heads = heads .. "-H '" .. k .. ": " .. v .. "' "
+ args[#args + 1] = '-H' .. k .. ": " .. v
end
- return heads
+ return args
end
local get = function (url, headr, args)
- local cmd, fetch, scode
+ local fetch, scode, def_args
- headr = util.table_merge(headr, def_headr)
- cmd = "curl -s --compressed --write-out %{http_code} '" ..
- url .. "' --globoff --location " .. gen_head(headr)
- if args then
- cmd = cmd .. ' ' .. args
- end
+ def_args = {
+ 'curl',
+ '--silent',
+ '--compressed',
+ '--write-out',
+ '%{http_code}',
+ '--globoff',
+ '--location',
+ url,
+ }
+
+ args = util.array_merge(def_args, args)
+ headr = util.table_merge(def_headr, headr)
+ args = util.array_merge(args, head_to_args(headr))
- fetch = io.popen(cmd):read('*all')
- scode = fetch:match('%d*$')
+ fetch = util.run(args)
+ scode = fetch:match('%d*$') or 000
fetch = fetch:gsub('%s*%d*$', '')
return fetch, tonumber(scode)
@@ -41,7 +49,7 @@ local zip_to_local_file = function (url, headr, out, retries)
zip = os.tmpname()
repeat
- _, hcode = get(url, headr, '-o ' .. zip)
+ _, hcode = get(url, headr, { '-o'.. zip })
tries = tries + 1
until hcode == 200 or tries > retries