2019-02-04 02:57:52 +00:00
|
|
|
' Functions for making requests to the API
|
|
|
|
|
2019-02-03 21:15:17 +00:00
|
|
|
function APIRequest(url as String, params={} as Object)
|
2019-02-01 02:26:53 +00:00
|
|
|
req = createObject("roUrlTransfer")
|
|
|
|
|
|
|
|
if server_is_https() then
|
|
|
|
req.setCertificatesFile("common:/certs/ca-bundle.crt")
|
|
|
|
end if
|
|
|
|
|
2019-02-23 03:14:21 +00:00
|
|
|
full_url = get_base_url() + "/emby/" + url
|
2019-02-03 21:15:17 +00:00
|
|
|
if params.count() > 0
|
|
|
|
full_url = full_url + "?"
|
|
|
|
|
|
|
|
param_array = []
|
|
|
|
for each field in params.items()
|
|
|
|
if type(field.value) = "String" then
|
|
|
|
item = field.key + "=" + req.escape(field.value.trim())
|
|
|
|
else if type(field.value) = "roInteger" then
|
|
|
|
item = field.key + "=" + req.escape(str(field.value).trim())
|
|
|
|
else
|
|
|
|
item = field.key + "=" + req.escape(field.value)
|
|
|
|
end if
|
|
|
|
param_array.push(item)
|
|
|
|
end for
|
|
|
|
full_url = full_url + param_array.join("&")
|
|
|
|
end if
|
|
|
|
|
|
|
|
req.setUrl(full_url)
|
2019-02-01 02:26:53 +00:00
|
|
|
|
|
|
|
req = authorize_request(req)
|
|
|
|
|
|
|
|
return req
|
|
|
|
end function
|
|
|
|
|
|
|
|
function parseRequest(req)
|
2019-03-02 22:03:11 +00:00
|
|
|
'req.retainBodyOnError(True)
|
|
|
|
'print req.GetToString()
|
2019-02-01 02:26:53 +00:00
|
|
|
json = ParseJson(req.GetToString())
|
|
|
|
return json
|
|
|
|
end function
|
|
|
|
|
2019-02-23 03:14:21 +00:00
|
|
|
function get_base_url()
|
|
|
|
base = get_setting("server")
|
|
|
|
port = get_setting("port")
|
|
|
|
if port <> "" and port <> invalid then
|
|
|
|
base = base + ":" + port
|
|
|
|
end if
|
|
|
|
return base
|
|
|
|
end function
|
|
|
|
|
2019-02-01 02:26:53 +00:00
|
|
|
function server_is_https() as Boolean
|
2019-02-10 19:15:20 +00:00
|
|
|
server = get_setting("server")
|
2019-02-01 02:26:53 +00:00
|
|
|
|
|
|
|
i = server.Instr(":")
|
|
|
|
|
|
|
|
' No protocol found
|
|
|
|
if i = 0 then
|
|
|
|
return False
|
|
|
|
end if
|
|
|
|
|
|
|
|
protocol = Left(server, i)
|
|
|
|
if protocol = "https" then
|
|
|
|
return True
|
|
|
|
end if
|
|
|
|
return False
|
|
|
|
end function
|
|
|
|
|
|
|
|
function get_token(user as String, password as String)
|
|
|
|
bytes = createObject("roByteArray")
|
|
|
|
bytes.FromAsciiString(password)
|
|
|
|
digest = createObject("roEVPDigest")
|
|
|
|
digest.setup("sha1")
|
|
|
|
hashed_pass = digest.process(bytes)
|
|
|
|
|
|
|
|
url = "Users/AuthenticateByName?format=json"
|
2019-03-02 05:55:28 +00:00
|
|
|
|
2019-02-01 02:26:53 +00:00
|
|
|
req = APIRequest(url)
|
|
|
|
|
|
|
|
' BrightScript will only return a POST body if you call post asynch
|
|
|
|
' and then wait for the response
|
|
|
|
req.setMessagePort(CreateObject("roMessagePort"))
|
|
|
|
req.AsyncPostFromString("Username=" + user + "&Password=" + hashed_pass)
|
|
|
|
resp = wait(5000, req.GetMessagePort())
|
|
|
|
if type(resp) <> "roUrlEvent"
|
|
|
|
return invalid
|
|
|
|
end if
|
|
|
|
|
|
|
|
json = ParseJson(resp.GetString())
|
|
|
|
|
2019-02-10 19:15:20 +00:00
|
|
|
set_setting("active_user", json.User.id)
|
|
|
|
set_user_setting("id", json.User.id) ' redundant, but could come in handy
|
|
|
|
set_user_setting("token", json.AccessToken)
|
|
|
|
set_user_setting("response", json)
|
2019-02-01 02:26:53 +00:00
|
|
|
return json
|
|
|
|
end function
|
|
|
|
|
|
|
|
function authorize_request(request)
|
|
|
|
auth = "MediaBrowser"
|
|
|
|
auth = auth + " Client=" + Chr(34) + "Jellyfin Roku" + Chr(34)
|
|
|
|
auth = auth + ", Device=" + Chr(34) + "Roku Model" + Chr(34)
|
|
|
|
auth = auth + ", DeviceId=" + Chr(34) + "12345" + Chr(34)
|
|
|
|
auth = auth + ", Version=" + Chr(34) + "10.1.0" + Chr(34)
|
|
|
|
|
2019-02-10 19:15:20 +00:00
|
|
|
user = get_setting("active_user")
|
2019-02-01 02:26:53 +00:00
|
|
|
if user <> invalid and user <> "" then
|
|
|
|
auth = auth + ", UserId=" + Chr(34) + user + Chr(34)
|
|
|
|
end if
|
|
|
|
|
2019-02-10 19:15:20 +00:00
|
|
|
token = get_user_setting("token")
|
2019-02-01 02:26:53 +00:00
|
|
|
if token <> invalid and token <> "" then
|
|
|
|
auth = auth + ", Token=" + Chr(34) + token + Chr(34)
|
|
|
|
end if
|
|
|
|
|
|
|
|
request.AddHeader("X-Emby-Authorization", auth)
|
|
|
|
return request
|
|
|
|
end function
|
|
|
|
|
2019-02-03 21:15:17 +00:00
|
|
|
|
|
|
|
' ServerBrowsing
|
|
|
|
|
|
|
|
' List Available Libraries for the current logged in user
|
|
|
|
' Params: None
|
|
|
|
' Returns { Items, TotalRecordCount }
|
|
|
|
function LibraryList()
|
2019-02-10 19:15:20 +00:00
|
|
|
url = Substitute("Users/{0}/Views/", get_setting("active_user"))
|
2019-02-03 21:15:17 +00:00
|
|
|
resp = APIRequest(url)
|
|
|
|
return parseRequest(resp)
|
|
|
|
end function
|
|
|
|
|
|
|
|
' Search for a string
|
|
|
|
' Params: Search Query
|
|
|
|
' Returns: { SearchHints, TotalRecordCount }
|
|
|
|
function SearchMedia(query as String)
|
|
|
|
resp = APIRequest("Search/Hints", {"searchTerm": query})
|
|
|
|
return parseRequest(resp)
|
|
|
|
end function
|
|
|
|
|
|
|
|
' List items from within a Library
|
|
|
|
' Params: Library ID, Limit, Offset, SortBy, SortOrder, IncludeItemTypes, Fields, EnableImageTypes
|
|
|
|
' Returns { Items, TotalRecordCount }
|
|
|
|
function ItemList(library_id=invalid as String)
|
2019-02-10 19:15:20 +00:00
|
|
|
url = Substitute("Users/{0}/Items/", get_setting("active_user"))
|
2019-02-03 21:15:17 +00:00
|
|
|
resp = APIRequest(url, {"parentid": library_id, "limit": 30})
|
|
|
|
return parseRequest(resp)
|
|
|
|
end function
|
|
|
|
|
|
|
|
function ItemMetaData(id as String)
|
2019-02-10 19:15:20 +00:00
|
|
|
url = Substitute("Users/{0}/Items/{1}", get_setting("active_user"), id)
|
2019-02-01 02:26:53 +00:00
|
|
|
resp = APIRequest(url)
|
|
|
|
return parseRequest(resp)
|
|
|
|
end function
|
|
|
|
|
2019-02-03 21:15:17 +00:00
|
|
|
' Video
|
|
|
|
|
2019-02-01 02:26:53 +00:00
|
|
|
function VideoStream(id as String)
|
2019-03-05 04:31:58 +00:00
|
|
|
|
2019-02-01 02:26:53 +00:00
|
|
|
player = createObject("roVideoPlayer")
|
|
|
|
|
2019-03-05 04:31:58 +00:00
|
|
|
content = VideoContent(id)
|
|
|
|
|
|
|
|
player.addContent(content)
|
|
|
|
|
|
|
|
player.play()
|
2019-02-01 02:26:53 +00:00
|
|
|
|
|
|
|
return player
|
|
|
|
end function
|