diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2025-05-20 18:52:15 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2025-05-20 18:52:15 -0700 |
commit | bcb9d82a1f4d5b168d11270d7e3fdfd1b9591831 (patch) | |
tree | cc135d376c8b0db442f43ce6c4e0ecc08aa8bd1f /downloader.lua | |
parent | 55ade5ac655953bef1a7efeeb7fa91ec0fd47a77 (diff) | |
download | mpv-iptv-menu-bcb9d82a1f4d5b168d11270d7e3fdfd1b9591831.tar.gz mpv-iptv-menu-bcb9d82a1f4d5b168d11270d7e3fdfd1b9591831.tar.xz |
reimplement downloader using async subprocesses
Diffstat (limited to 'downloader.lua')
-rw-r--r-- | downloader.lua | 55 |
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 |