diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/curl.lua | 39 | ||||
| -rw-r--r-- | lib/util.lua | 72 | 
2 files changed, 111 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, +} diff --git a/lib/util.lua b/lib/util.lua new file mode 100644 index 0000000..d962961 --- /dev/null +++ b/lib/util.lua @@ -0,0 +1,72 @@ +#!/usr/bin/env lua + +local table_merge = function (t1, t2) +	t1 = t1 or {} +	t2 = t2 or {} + +	for k, v in pairs(t2) do +		t1[k] = v +	end + +	return t1 +end + +local table_print = function (t) +	for k, v in pairs(t) do +		print( '|'.. k .. '=' .. v .. '|') +	end +end + +local table_match_or_any = function (t, key) +	local str + +	str= t[key] +	if not str then +		_, str = next(t, nil) +	end + +	return str +end + +local zip_ext_first = function (zip, out) +	local dir, rc, srt + +	dir = os.tmpname() +	os.remove(dir) +	os.execute('mkdir -p ' .. dir) + +	rc = os.execute('unzip -qq ' .. zip .. ' -d ' .. dir) +	srt = io.popen('find ' .. dir .. ' -type f -name *.srt'):read('*l') +	os.rename(srt, out) +	os.remove(dir) + +	return rc +end + +local string_rm_vid_ext = function (str) +	local extensions = { +		"mkv", +		"mp4", +		"webm", +		"flv", +		"gif", +		"gifv", +		"avi", +		"mpeg", +		"3gp" +	} + +	for _, ext in ipairs(extensions) do +		str = str:gsub('.' .. ext, '') +	end + +	return str +end + +return { +	table_merge = table_merge, +	table_print = table_print, +	table_match_or_any = table_match_or_any, +	zip_ext_first = zip_ext_first, +	string_rm_vid_ext = string_rm_vid_ext, +}  | 
