Skip to content

Instantly share code, notes, and snippets.

@dchristensen
Created July 18, 2020 14:57
Show Gist options
  • Save dchristensen/0b4239c31454987f7dcb6d1866afc0a6 to your computer and use it in GitHub Desktop.
Save dchristensen/0b4239c31454987f7dcb6d1866afc0a6 to your computer and use it in GitHub Desktop.
Add MLS in 15 videos to VLC
function descriptor()
return { title="MLS in 15"}
end
function main()
vlc.msg.dbg("Loading MLS in 15 graphql query")
local videos, err = query_mls_graphql()
if err then
vlc.msg.warn(err)
return
end
for i, v in ipairs(ReverseTable(videos)) do
vlc.sd.add_item({
path=v["source"],
title=v["name"],
duration=v["duration"] / 1000
})
end
end
function query_mls_graphql()
local query = [[
query {
videos(channels: ["MLS in 15"], limit: 20) {
id,
name,
duration,
source
}
}]]
local url = "https://blender-api.mlsdigital.net/graphql?query=" .. vlc.strings.encode_uri_component(query)
local obj, pos, err = parse_json(url)
if err then
return nil, "Error querying GraphQL: " .. err
end
if obj.errors then
local err = "GraphQL query error: " .. obj.errors[0].message
return nil, err
end
return obj["data"]["videos"]
end
-- -- Download and parse a JSON document from the specified URL
-- -- Returns: obj, pos, err (see dkjson docs)
function parse_json(url)
local json = require("dkjson")
local stream = vlc.stream(url)
local string = ""
local line = ""
if not stream then
return nil, nil, "Failed creating VLC stream"
end
while true do
line = stream:readline()
if not line then
break
end
string = string .. line
end
return json.decode(string)
end
function ReverseTable(t)
local reversedTable = {}
local itemCount = #t
for k, v in ipairs(t) do
reversedTable[itemCount + 1 - k] = v
end
return reversedTable
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment