summaryrefslogtreecommitdiff
path: root/downloader.lua
diff options
context:
space:
mode:
Diffstat (limited to 'downloader.lua')
-rw-r--r--downloader.lua55
1 files changed, 55 insertions, 0 deletions
diff --git a/downloader.lua b/downloader.lua
new file mode 100644
index 0000000..67fb675
--- /dev/null
+++ b/downloader.lua
@@ -0,0 +1,55 @@
+-- Copyright 2025 David Vazgenovich Shakaryan
+
+local 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 utils.file_info(file) then
+ self:exec_next()
+ return
+ end
+
+ local cmd = {'curl', '-sSfLo', file, url}
+ print('exec: ' .. utils.to_string(cmd))
+
+ self.running = true
+ mp.command_native_async({
+ name = 'subprocess',
+ args = cmd,
+ playback_only = false,
+ }, function(success, res)
+ self.running = false
+ self:exec_next()
+ if cb and success and res.status == 0 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