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
|
-- Copyright 2025 David Vazgenovich Shakaryan
local mp_utils = require('mp.utils')
local downloader = {}
local mt = {}
mt.__index = mt
function downloader.new()
return setmetatable({
pending = {},
running = false,
}, mt)
end
function mt:exec(url, file, cb)
if mp_utils.file_info(file) then
self:exec_next()
return
end
local tmp = file .. '.tmp'
local cmd = {'curl', '-sSfLo', tmp, url}
print('downloading ' .. url)
self.running = true
mp.command_native_async({
name = 'subprocess',
args = cmd,
playback_only = false,
}, function(success, res)
success = success and res.status == 0
if success then
success = os.rename(tmp, file)
else
os.remove(tmp)
end
-- execute next download after renaming the file but before
-- executing the callback function. this avoids duplicate
-- downloads when the same file is requested twice in a row,
-- without unnecessarily waiting for the callback to complete
-- before starting the next download.
self.running = false
self:exec_next()
if success and cb then
cb(url, file)
end
end)
end
function mt:exec_next()
if #self.pending > 0 then
self:exec(unpack(table.remove(self.pending)))
end
end
-- more recently requested downloads are executed first, as they are more
-- likely to be used for the current display state
function mt:schedule(...)
if self.running then
self.pending[#self.pending+1] = {...}
else
self:exec(...)
end
end
return downloader
|