-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpv-torrserver.lua
57 lines (46 loc) · 1.29 KB
/
mpv-torrserver.lua
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
-- Install [Torrserver](https://github.com/YouROK/TorrServer)
local opts = {
server = "http://localhost:8090",
}
(require 'mp.options').read_options(opts)
local function post_torrent(url)
local curl_cmd = {
"curl",
"-X",
"POST",
opts.server .. "/torrent/upload",
"-H",
"accept: application/json",
"-H",
"Content-Type: multipart/form-data",
"-F",
"file=@\""..url.."\""
}
local res, err = mp.command_native({
name = "subprocess",
capture_stdout = true,
playback_only = false,
args = curl_cmd
})
if err then
return nil, err
end
return (require 'mp.utils').parse_json(res.stdout)
end
function string:endswith(suffix)
return self:sub(-#suffix) == suffix
end
mp.add_hook("on_load", 5, function()
local url = mp.get_property("stream-open-filename")
if url:endswith(".torrent") then
local res, err = post_torrent(url)
if err then
mp.msg.error(err)
return
end
mp.set_property("stream-open-filename", opts.server .. "/stream?m3u&link=" .. res.hash)
end
if url:find("magnet:") == 1 then
mp.set_property("stream-open-filename", opts.server .. "/stream?m3u&link=" .. url)
end
end)