aboutsummaryrefslogtreecommitdiff
path: root/lib/curl.lua
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2023-09-29 18:02:59 +0530
committersinanmohd <sinan@sinanmohd.com>2023-09-30 14:52:39 +0530
commit05a9be65f8e0d97fd36711acfe5d626e316722ae (patch)
treed00771702c4628f8d27297938ebfbdb924541a58 /lib/curl.lua
repo: init
Diffstat (limited to 'lib/curl.lua')
-rw-r--r--lib/curl.lua39
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/curl.lua b/lib/curl.lua
new file mode 100644
index 0000000..8bc4eec
--- /dev/null
+++ b/lib/curl.lua
@@ -0,0 +1,39 @@
+#!/usr/bin/env lua
+
+local util = require 'lib/util'
+
+local def_headr = {
+ ['User-Agent'] = 'cia',
+ ['Accept-Encoding'] = 'gzip, deflate, br'
+}
+
+local gen_head = function (t)
+ local heads = ' '
+
+ for k, v in pairs(t) do
+ heads = heads .. "-H '" .. k .. ": " .. v .. "' "
+ end
+
+ return heads
+end
+
+local get = function (url, headr, args)
+ local cmd, fetch, scode
+
+ headr = util.table_merge(headr, def_headr)
+ cmd = 'curl -s --compressed --write-out %{http_code} '
+ cmd = cmd .. url .. gen_head(headr)
+ if args then
+ cmd = cmd .. ' ' .. args
+ end
+
+ fetch = io.popen(cmd):read('*all')
+ scode = string.match(fetch, '%d*$')
+ fetch = string.gsub(fetch, '%s*%d*$', '')
+
+ return fetch, tonumber(scode)
+end
+
+return {
+ get = get,
+}