Created
January 27, 2025 17:12
-
-
Save rcombs/0205b1cb094d1424ed3de677048771ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local mp = require("mp") | |
local utils = require("mp.utils") | |
ROOT_URL = "http://localhost:32700" | |
local char_to_hex = function(c) | |
return string.format("%%%02X", string.byte(c)) | |
end | |
function fetch_json(url) | |
local curl_args = { | |
"curl", | |
"-L", | |
"--silent", | |
"--max-time", "5", | |
"-H", "Accept: application/json", | |
url | |
} | |
local subprocess = mp.command_native { | |
name = "subprocess", | |
capture_stdout = true, | |
playback_only = false, | |
args = curl_args | |
} | |
return utils.parse_json(subprocess.stdout or "") or {} | |
end | |
local function urlencode(url) | |
if url == nil then | |
return | |
end | |
url = url:gsub("([^%w ])", char_to_hex) | |
url = url:gsub(" ", "+") | |
return url | |
end | |
function on_load(defer, cont) | |
local metadataItem = mp.get_property_native("user-data/plex/playing-media/decision/metadataItem") | |
local source = metadataItem["source"] | |
local guid = metadataItem["guid"] | |
if not source or not guid then | |
return | |
end | |
local providers = fetch_json(ROOT_URL .. "/media/providers") | |
local provider = nil | |
for _, curProvider in ipairs((providers.MediaContainer or {}).MediaProvider or {}) do | |
if curProvider["source"] == source then | |
provider = curProvider | |
break | |
end | |
end | |
if not provider then | |
return | |
end | |
local providerPath = "/media/providers/" .. provider["id"] | |
local downloadedMetadataPath = providerPath .. "/library/metadata/" .. urlencode(guid) | |
local downloadedMetadata = fetch_json(ROOT_URL .. downloadedMetadataPath) | |
if not downloadedMetadata then | |
return | |
end | |
local partKey = (((((((downloadedMetadata.MediaContainer or {}).Metadata or {})[1] or {}).Media or {})[1] or {}).Part or {})[1] or {})["key"] | |
if partKey then | |
mp.set_property("stream-open-filename", ROOT_URL .. partKey) | |
end | |
end | |
mp.add_hook("on_load", 30, on_load) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment