-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkodi-mpv-hook.lua
80 lines (66 loc) · 2.47 KB
/
kodi-mpv-hook.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- kodi-mpv-hook.lua
-- Parse Kodi streaming URLs with 'protocol options' when using MPV as an External Player.
-- Source: https://github.com/Eskander/kodi-mpv-hook
local mp = require 'mp'
-- Decode URL-encoded strings
function url_decode(str)
-- Replace `+` with space, then decode %xx encoded characters
str = str:gsub("%+", " ")
return str:gsub("%%(%x%x)", function(hex)
return string.char(tonumber(hex, 16))
end)
end
-- Parse the extra data
function parse_extra_data(extra_data)
local params = {}
for key, value in string.gmatch(extra_data, "([^&]+)=([^&]+)") do
params[url_decode(key)] = url_decode(value)
end
return params
end
-- Modify the URL and apply extra parameters
function modify_url(url)
-- Find the position of '|'
local pipe_position = string.find(url, "|")
local modified_url = url
local extra_data = nil
if pipe_position then
-- Truncate the URL at the '|' position
modified_url = string.sub(url, 1, pipe_position - 1)
-- Get the part after the '|'
extra_data = string.sub(url, pipe_position + 1)
end
-- If there's extra data, parse it
if extra_data then
local params = parse_extra_data(extra_data)
-- Print the original and modified URLs for debugging
mp.msg.verbose("Received query: " .. url)
mp.msg.info("Playing: " .. modified_url)
-- Apply headers
if params["User-Agent"] then
mp.msg.info("User-Agent: " .. params["User-Agent"])
mp.set_property("options/user-agent", params["User-Agent"])
end
if params["Referer"] then
mp.msg.info("Referer: " .. params["Referer"])
mp.set_property("options/referer", params["Referer"])
end
if params["Origin"] then
local headers = string.format("Origin: %s", params["Origin"])
mp.msg.info("Origin: " .. params["Origin"])
mp.set_property("options/http-header-fields", headers)
end
end
return modified_url
end
-- Hook into 'on_load' event to intercept the URL
mp.add_hook("on_load", 50, function()
-- Get the original URL
local original_url = mp.get_property("path")
-- Modify the URL and apply headers
local modified_url = modify_url(original_url)
-- Reload the file with the modified URL
if modified_url ~= original_url then
mp.commandv("loadfile", modified_url, "replace")
end
end)