diff --git a/source/JellyfinAPI.brs b/source/JellyfinAPI.brs index af6ca038..2e2945c5 100644 --- a/source/JellyfinAPI.brs +++ b/source/JellyfinAPI.brs @@ -3,7 +3,7 @@ function APIRequest(url as String, params={} as Object) req = createObject("roUrlTransfer") - server = get_var("server") + server = get_setting("server") if server_is_https() then req.setCertificatesFile("common:/certs/ca-bundle.crt") @@ -40,7 +40,7 @@ function parseRequest(req) end function function server_is_https() as Boolean - server = get_var("server") + server = get_setting("server") i = server.Instr(":") @@ -77,8 +77,10 @@ function get_token(user as String, password as String) json = ParseJson(resp.GetString()) - GetGlobalAA().AddReplace("user_id", json.User.id) - GetGlobalAA().AddReplace("user_token", json.AccessToken) + 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) return json end function @@ -89,12 +91,12 @@ function authorize_request(request) auth = auth + ", DeviceId=" + Chr(34) + "12345" + Chr(34) auth = auth + ", Version=" + Chr(34) + "10.1.0" + Chr(34) - user = get_var("user_id") + user = get_setting("active_user") if user <> invalid and user <> "" then auth = auth + ", UserId=" + Chr(34) + user + Chr(34) end if - token = get_var("user_token") + token = get_user_setting("token") if token <> invalid and token <> "" then auth = auth + ", Token=" + Chr(34) + token + Chr(34) end if @@ -110,7 +112,7 @@ end function ' Params: None ' Returns { Items, TotalRecordCount } function LibraryList() - url = Substitute("Users/{0}/Views/", get_var("user_id")) + url = Substitute("Users/{0}/Views/", get_setting("active_user")) resp = APIRequest(url) return parseRequest(resp) end function @@ -127,13 +129,13 @@ end function ' Params: Library ID, Limit, Offset, SortBy, SortOrder, IncludeItemTypes, Fields, EnableImageTypes ' Returns { Items, TotalRecordCount } function ItemList(library_id=invalid as String) - url = Substitute("Users/{0}/Items/", get_var("user_id")) + url = Substitute("Users/{0}/Items/", get_setting("active_user")) resp = APIRequest(url, {"parentid": library_id, "limit": 30}) return parseRequest(resp) end function function ItemMetaData(id as String) - url = Substitute("Users/{0}/Items/{1}", get_var("user_id"), id) + url = Substitute("Users/{0}/Items/{1}", get_setting("active_user"), id) resp = APIRequest(url) return parseRequest(resp) end function @@ -143,7 +145,7 @@ end function function VideoStream(id as String) player = createObject("roVideoPlayer") - server = get_var("server") + server = get_setting("server") path = Substitute("Videos/{0}/stream.mp4", id) player.setUrl(server + "/" + path) player = authorize_request(player) diff --git a/source/Main.brs b/source/Main.brs index a775fb26..417d3734 100644 --- a/source/Main.brs +++ b/source/Main.brs @@ -6,22 +6,30 @@ sub Main() m.port = CreateObject("roMessagePort") screen.setMessagePort(m.port) - 'todo - pick the scene based on if we need a server already + if get_setting("server") = invalid then + set_setting("server", get_var("server")) + screen.CreateScene("ServerSelect") + screen.show() + ' TODO - Do server select logic here + end if + + if get_setting("active_user") = invalid then + screen.CreateScene("UserSignIn") + screen.show() + ' TODO - sign in here + get_token(get_var("username"), get_var("password")) + end if + first_scene = "Library" 'Create a scene and load a component' m.scene = screen.CreateScene(first_scene) screen.show() - get_token(get_var("username"), get_var("password")) - libs = LibraryList().items librow = m.scene.findNode("LibrarySelect") 'librow.GetRowListContent() - ' For now, just play whatever is the first item in the list - ' of the first folder - while(true) msg = wait(0, m.port) msgType = type(msg) diff --git a/source/VideoPlayer.brs b/source/VideoPlayer.brs index 1189af6e..5f483927 100644 --- a/source/VideoPlayer.brs +++ b/source/VideoPlayer.brs @@ -20,7 +20,7 @@ function VideoContent(id) as object meta = ItemMetaData(id) content.title = meta.Name - server = get_var("server") + server = get_setting("server") content.url = Substitute("{0}/emby/Videos/{1}/stream.mp4", server, id) content.url = content.url + "?Static=true" diff --git a/source/config.brs b/source/config.brs new file mode 100644 index 00000000..b7f692ba --- /dev/null +++ b/source/config.brs @@ -0,0 +1,55 @@ +' "Registry" is where Roku stores config + + +' Generic registry accessors +function registry_read(key, section=invalid) + if section = invalid then return invalid + reg = CreateObject("roRegistrySection", section) + if reg.exists(key) then return reg.read(key) + return invalid +end function + +function registry_write(key, value, section=invalid) + if section = invalid then return invalid + reg = CreateObject("roRegistrySection", section) + reg.write(key, value) + reg.flush() +end function + +function registry_delete(key, section=invalid) + if section = invalid then return invalid + reg = CreateObject("roRegistrySection", section) + reg.delete(key) + reg.flush() +end function + + +' "Jellyfin" registry accessors for the default global settings +function get_setting(key) + return registry_read(key, "Jellyfin") +end function + +function set_setting(key, value) + registry_write(key, value, "Jellyfin") +end function + +function unset_setting(key) + registry_delete(key, "Jellyfin") +end function + + +' User registry accessors for the currently active user +function get_user_setting(key) + if get_setting("active_user") = invalid then return invalid + return registry_read(key, get_setting("active_user")) +end function + +function set_user_setting(key, value) + if get_setting("active_user") = invalid then return invalid + registry_write(key, value, get_setting("active_user")) +end function + +function unset_user_setting(key) + if get_setting("active_user") = invalid then return invalid + registry_delete(key, get_setting("active_user")) +end function