aboutsummaryrefslogtreecommitdiff
path: root/lib/util.lua
blob: 5b524bfc3f8dcc36353e8941c748cb3d3fd01ee2 (plain) (blame)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env lua

local table_to_cmd = function (t)
	local str = ""

	for _, v in ipairs(t) do
		v = v:gsub("'", "\'")
		str = str .. " '" .. v .. "' "
	end

	return str
end

local file_exists = function (path)
	return io.open(path, 'r') and true or false
end

local run = function (args)
	local sig, rc, stdout, cmd

	if mp then
		cmd = mp.command_native({
			name = 'subprocess',
			capture_stdout = true,
			args = args,
		})
		if cmd then
			stdout = cmd.stdout
			rc = (cmd.status >= 0)
		end
	else
		cmd = io.popen(table_to_cmd(args))
		if cmd then
			stdout = cmd:read('*all')
			_, sig, rc = cmd:close()
			rc = (sig == 'signal') or (sig == 'exit' and rc == 0)
		end
	end

	return stdout or "", rc
end

local table_merge = function (t1, t2)
	local t = {}
	t1 = t1 or {}
	t2 = t2 or {}

	for k, v in pairs(t1) do
		t[k] = v
	end
	for k, v in pairs(t2) do
		t[k] = v
	end

	return t
end

local array_merge = function (a1, a2)
	local a = {}
	a1 = a1 or {}
	a2 = a2 or {}

	for _, v in ipairs(a1) do
		a[#a + 1] = v
	end
	for _, v in ipairs(a2) do
		a[#a + 1] = v
	end

	return a
end

local array_search = function (a, key)
	for _, v in pairs(a) do
		if v == key then
			return true
		end
	end

	return false
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, findcmd

	dir = os.tmpname()
	os.remove(dir)
	findcmd = { 'find', dir, '-type', 'f', '-name', '*.srt' }

	_, rc = run({ 'unzip',  '-qq',  zip, '-d',  dir })
	srt = run(findcmd):match('[^\n]*')
	run({ 'mv', srt, out })
	os.remove(dir)

	return rc
end

local string_vid_path_to_name = function (str)
	local extensions = {
		"mkv",
		"mp4",
		"webm",
		"flv",
		"gif",
		"gifv",
		"avi",
		"mpeg",
		"3gp"
	}

	str = str:match('[^/]*$')
	for _, ext in ipairs(extensions) do
		str = str:gsub('.' .. ext, '')
	end

	return str
end

local error = function (str)
	str = 'error: ' .. str
	if mp then
		mp.msg.warn(str)
	else
		print(str)
	end
end

local opensubtitles_hash = function (fileName)
	local fil = io.open(fileName, "rb")
	if not fil then
		return nil
	end

	local lo, hi = 0, 0
	for _ = 1, 8192 do
		local a, b, c, d = fil:read(4):byte(1, 4)
		lo = lo + a + b * 256 + c * 65536 + d * 16777216
		a, b, c, d = fil:read(4):byte(1, 4)
		hi = hi + a + b * 256 + c * 65536 + d * 16777216
		while lo >= 4294967296 do
			lo = lo - 4294967296
			hi = hi + 1
		end
		while hi >= 4294967296 do
			hi = hi - 4294967296
		end
	end

	local size = fil:seek("end", -65536) + 65536
	for _ = 1, 8192 do
		local a, b, c, d = fil:read(4):byte(1, 4)
		lo = lo + a + b * 256 + c * 65536 + d * 16777216
		a, b, c, d = fil:read(4):byte(1, 4)
		hi = hi + a + b * 256 + c * 65536 + d * 16777216
		while lo >= 4294967296 do
			lo = lo - 4294967296
			hi = hi + 1
		end
		while hi >= 4294967296 do
			hi = hi - 4294967296
		end
	end

	lo = lo + size
	while lo >= 4294967296 do
		lo = lo - 4294967296
		hi = hi + 1
	end
	while hi >= 4294967296 do
		hi = hi - 4294967296
	end

	fil:close()
	return string.format("%08x%08x", hi, lo), size
end

return {
	table_merge = table_merge,
	table_print = table_print,
	table_match_or_any = table_match_or_any,
	array_merge = array_merge,
	array_search = array_search,
	zip_ext_first = zip_ext_first,
	string_vid_path_to_name = string_vid_path_to_name,
	opensubtitles_hash = opensubtitles_hash,
	file_exists = file_exists,
	run = run,
	error = error,
}