From 13edce3d7ae6c0ce7a1625091fd6e28c4edb1928 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 4 Nov 2022 17:15:23 -0400 Subject: [PATCH 1/5] Initial auth work --- TODO | 26 ++++++++++++++++ src/components/tasks/MainLoaderTask.brs | 6 ++++ src/source/services/Invidious.bs | 40 ++++++++++++++++++++++++- src/source/utils/Registry.bs | 24 +++++++++++++++ src/source/webserver/HttpReply.bs | 14 +++++++++ src/source/webserver/utils.brs | 16 ++++++++++ 6 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 TODO create mode 100644 src/source/utils/Registry.bs diff --git a/TODO b/TODO new file mode 100644 index 00000000..c9b91f8f --- /dev/null +++ b/TODO @@ -0,0 +1,26 @@ +TODO: + - New template + - Use brighterscript + - Use classes and tests + - Use vs code config + - Use unit tests + - Use bs promises + - Check tools under roku community (linter, etc) + - Use RALE app to make UIs + - Fix non-Latin characters (Unicode on Roku sucks) + + +FEATURES: + - Sign in with account + - Youtube backend abstraction (Invidious vs TubeArchvist) + - Settings page + - Configure SponsorBlock + - Configure Invidious Backend (allow self-hosted) + - Closed caption settings? + - Search page + - Channel page + - Comments page? (especially on live videos) + - Dislike count? + - Closed caption + - Launch app to specific video (deep link) + - Youtube android app -> share -> custom app -> push to tv diff --git a/src/components/tasks/MainLoaderTask.brs b/src/components/tasks/MainLoaderTask.brs index 04af0134..3451783b 100644 --- a/src/components/tasks/MainLoaderTask.brs +++ b/src/components/tasks/MainLoaderTask.brs @@ -21,6 +21,12 @@ sub GetContent() rootChildren.Push(popular) end if + feedJson = RokuYoutube.Services.Invidious.GetUserFeed() + feed = GetCategoryContent("Subscriptions", feedJson) + if feed <> invalid + rootChildren.Push(feed) + end if + ' set up a root ContentNode to represent rowList on the GridScreen contentNode = CreateObject("roSGNode", "ContentNode") contentNode.Update({ diff --git a/src/source/services/Invidious.bs b/src/source/services/Invidious.bs index 0dfc6d04..9307bb94 100644 --- a/src/source/services/Invidious.bs +++ b/src/source/services/Invidious.bs @@ -3,7 +3,7 @@ import "pkg:/source/models/SearchSuggestionResult.bs" import "pkg:/source/models/SearchFilter.bs" namespace RokuYoutube.Services.Invidious - const API_URL = "https://invidious.esmailelbob.xyz" + const API_URL = "https://vid.puffyan.us" const POPULAR_ENDPOINT = "/api/v1/popular" const TRENDING_ENDPOINT = "/api/v1/trending" @@ -12,6 +12,8 @@ namespace RokuYoutube.Services.Invidious const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions" const SEARCH_ENDPOINT = "/api/v1/search" + const AUTH_FEED = "/api/v1/auth/feed" + function GetPopular() url = `${API_URL}${POPULAR_ENDPOINT}` response = Requests().get(url) @@ -90,4 +92,40 @@ namespace RokuYoutube.Services.Invidious return response.json end if end function + + function GetAuthorizeTokenLink() + 'https:///authorize_token?scopes=&callback_url=&expire= + invidiousHost = strReplace(API_URL, "https://", "http://") + scope = "GET:feed" + + localIp = "192.168.1.107" + webServerPort = 8888 + expire = 60 * 60 * 24 * 30 + + callbackUrl = UrlEncode(`http://${localIp}:${webServerPort}/token_callback`) + return `${invidiousHost}/authorize_token?scopes=${scope}&callback_url=${callbackUrl}&expire=${expire}` + end function + + function GetAuthenticationHeaders() + token = RokuYoutube.Utils.Registry.Read("token") + if token = invalid + return invalid + end if + end function + + function GetUserFeed() + headers = GetAuthenticationHeaders() + if headers = invalid + return invalid + end if + + url = `${API_URL}${AUTH_FEED}` + + args = { headers: headers } + + response = Requests().get(url, args) + if response.statuscode = 200 + return response.json.videos + end if + end function end namespace \ No newline at end of file diff --git a/src/source/utils/Registry.bs b/src/source/utils/Registry.bs new file mode 100644 index 00000000..85b66e06 --- /dev/null +++ b/src/source/utils/Registry.bs @@ -0,0 +1,24 @@ +namespace RokuYoutube.Utils.Registry + + function Read(key, section = invalid) + if section = invalid section = "Default" + sec = CreateObject("roRegistrySection", section) + if sec.Exists(key) return sec.Read(key) + return invalid + end function + + function Write(key, value, section = invalid) + if section = invalid section = "Default" + sec = CreateObject("roRegistrySection", section) + sec.Write(key, value) + sec.Flush() + end function + + function Delete(key, section = invalid) + if section = invalid section = "Default" + sec = CreateObject("roRegistrySection", section) + sec.Delete(key) + sec.Flush() + end function + +end namespace \ No newline at end of file diff --git a/src/source/webserver/HttpReply.bs b/src/source/webserver/HttpReply.bs index 32fc29d9..609a61fd 100644 --- a/src/source/webserver/HttpReply.bs +++ b/src/source/webserver/HttpReply.bs @@ -1,3 +1,5 @@ +import "pkg:/source/utils/Registry.bs" + namespace RokuYoutube.Http function GetHttpReplyId() m.http_reply_id = validint(m.http_reply_id) + 1 @@ -58,6 +60,18 @@ namespace RokuYoutube.Http ' work out path of file being requested req = m.request uri = req.uri + + ' TODO: move to proper middleware system + if uri.StartsWith("/token_callback") + components = UrlQueryComponents(uri) + if components["token"] <> invalid + RokuYoutube.Utils.Registry.Write("token", components["token"] ) + print("Got Token!") + end if + m.Default(200, "OK") + return true + end if + decoded_uri = UrlUnescape(uri) path = CreateObject("roPath", m.settings.WwwRoot + decoded_uri) fs = CreateObject("roFilesystem") diff --git a/src/source/webserver/utils.brs b/src/source/webserver/utils.brs index b1c5b0b2..bb7f3601 100644 --- a/src/source/webserver/utils.brs +++ b/src/source/webserver/utils.brs @@ -139,6 +139,22 @@ function UrlUnescape(url as String) as String return ue.unescape(url) end function +function UrlQueryComponents(url as String) + query = {} + index = Instr(0, url, "?") + url = Mid(url, index+1) + components = url.split("&") + for each component in components + if Instr(0, component, "=") + keyValue = component.split("=") + query[keyValue[0]] = keyValue[1] + else + query[keyValue[0]] = "" + end if + end for + return query +end function + function MimeType(uri="" as String) as String map = m.MimeTypes if map = invalid From 908013faeec2ee9b43373450afbaf47233f1a0f5 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 4 Nov 2022 17:23:43 -0400 Subject: [PATCH 2/5] Fix auth header --- src/source/services/Invidious.bs | 4 ++++ src/source/webserver/HttpReply.bs | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/source/services/Invidious.bs b/src/source/services/Invidious.bs index 9307bb94..ab49a6bc 100644 --- a/src/source/services/Invidious.bs +++ b/src/source/services/Invidious.bs @@ -1,6 +1,7 @@ import "pkg:/source/roku_modules/rokurequests/Requests.brs" import "pkg:/source/models/SearchSuggestionResult.bs" import "pkg:/source/models/SearchFilter.bs" +import "pkg:/source/utils/Registry.bs" namespace RokuYoutube.Services.Invidious const API_URL = "https://vid.puffyan.us" @@ -111,6 +112,9 @@ namespace RokuYoutube.Services.Invidious if token = invalid return invalid end if + return { + "Authentication": `Bearer ${token}` + } end function function GetUserFeed() diff --git a/src/source/webserver/HttpReply.bs b/src/source/webserver/HttpReply.bs index 609a61fd..d549c2b3 100644 --- a/src/source/webserver/HttpReply.bs +++ b/src/source/webserver/HttpReply.bs @@ -65,7 +65,9 @@ namespace RokuYoutube.Http if uri.StartsWith("/token_callback") components = UrlQueryComponents(uri) if components["token"] <> invalid - RokuYoutube.Utils.Registry.Write("token", components["token"] ) + token = components["token"].DecodeUriComponent() + + RokuYoutube.Utils.Registry.Write("token", token) print("Got Token!") end if m.Default(200, "OK") From aac579805be21aadea98c1d264e0d87b546474bc Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 4 Nov 2022 18:14:43 -0400 Subject: [PATCH 3/5] Fix auth, subscriptions working --- src/source/services/Invidious.bs | 11 +++++++---- src/source/webserver/HttpReply.bs | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/source/services/Invidious.bs b/src/source/services/Invidious.bs index ab49a6bc..7dbfbe8d 100644 --- a/src/source/services/Invidious.bs +++ b/src/source/services/Invidious.bs @@ -97,11 +97,11 @@ namespace RokuYoutube.Services.Invidious function GetAuthorizeTokenLink() 'https:///authorize_token?scopes=&callback_url=&expire= invidiousHost = strReplace(API_URL, "https://", "http://") - scope = "GET:feed" + scope = ":*" localIp = "192.168.1.107" webServerPort = 8888 - expire = 60 * 60 * 24 * 30 + expire = Now().AsSeconds() + 60 * 60 * 24 * 30 'A month from now callbackUrl = UrlEncode(`http://${localIp}:${webServerPort}/token_callback`) return `${invidiousHost}/authorize_token?scopes=${scope}&callback_url=${callbackUrl}&expire=${expire}` @@ -113,7 +113,7 @@ namespace RokuYoutube.Services.Invidious return invalid end if return { - "Authentication": `Bearer ${token}` + "Authorization": `Bearer ${token}` } end function @@ -125,7 +125,10 @@ namespace RokuYoutube.Services.Invidious url = `${API_URL}${AUTH_FEED}` - args = { headers: headers } + args = { + headers: headers, + retryCount: 3 'Sometimes authentication fails randomly with a valid token... + } response = Requests().get(url, args) if response.statuscode = 200 diff --git a/src/source/webserver/HttpReply.bs b/src/source/webserver/HttpReply.bs index d549c2b3..2b4032a8 100644 --- a/src/source/webserver/HttpReply.bs +++ b/src/source/webserver/HttpReply.bs @@ -65,10 +65,10 @@ namespace RokuYoutube.Http if uri.StartsWith("/token_callback") components = UrlQueryComponents(uri) if components["token"] <> invalid - token = components["token"].DecodeUriComponent() - + ' Token is encoded twice for some reason + token = UrlUnescape(components["token"]).DecodeUriComponent() + print(`Got Token: ${token}`) RokuYoutube.Utils.Registry.Write("token", token) - print("Got Token!") end if m.Default(200, "OK") return true From 2abc5ce7a5eaf17a1b492e005d2be369e05682da Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 4 Nov 2022 18:22:33 -0400 Subject: [PATCH 4/5] Remove hardcoded ip address --- src/source/services/Invidious.bs | 2 +- src/source/webserver/common/General.brs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/source/services/Invidious.bs b/src/source/services/Invidious.bs index 7dbfbe8d..ebf3f444 100644 --- a/src/source/services/Invidious.bs +++ b/src/source/services/Invidious.bs @@ -99,7 +99,7 @@ namespace RokuYoutube.Services.Invidious invidiousHost = strReplace(API_URL, "https://", "http://") scope = ":*" - localIp = "192.168.1.107" + localIp = GetLocalIpAddress() webServerPort = 8888 expire = Now().AsSeconds() + 60 * 60 * 24 * 30 'A month from now diff --git a/src/source/webserver/common/General.brs b/src/source/webserver/common/General.brs index 41cc4ed8..2774bcf8 100644 --- a/src/source/webserver/common/General.brs +++ b/src/source/webserver/common/General.brs @@ -37,3 +37,10 @@ Function IsHD() return false End Function +Function GetLocalIpAddress() + di = CreateObject("roDeviceInfo") + ips = di.GetIPAddrs() + for each ip in ips + return ips[ip] + end for +End Function \ No newline at end of file From 0665e93c8b2054bb5a2a0a042d1a79342e609239 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 4 Nov 2022 18:24:05 -0400 Subject: [PATCH 5/5] Comments --- src/source/Main.brs | 27 --------------------------- src/source/services/Invidious.bs | 2 +- 2 files changed, 1 insertion(+), 28 deletions(-) diff --git a/src/source/Main.brs b/src/source/Main.brs index c7985668..4fb0aa59 100644 --- a/src/source/Main.brs +++ b/src/source/Main.brs @@ -1,30 +1,3 @@ -' TODO: -' - New template -' - Use brighterscript -' - Use classes and tests -' - Use vs code config -' - Use unit tests -' - Use bs promises -' - Check tools under roku community (linter, etc) -' - Use RALE app to make UIs -' - Fix non-Latin characters (Unicode on Roku sucks) - - -' FEATURES: -' - Sign in with account -' - Youtube backend abstraction (Invidious vs TubeArchvist) -' - Settings page -' - Configure SponsorBlock -' - Configure Invidious Backend (allow self-hosted) -' - Closed caption settings? -' - Search page -' - Channel page -' - Comments page? (especially on live videos) -' - Dislike count? -' - Closed caption -' - Launch app to specific video (deep link) -' - Youtube android app -> share -> custom app -> push to tv - function Main(args as dynamic) initScreen() end function diff --git a/src/source/services/Invidious.bs b/src/source/services/Invidious.bs index ebf3f444..1f1bfcfa 100644 --- a/src/source/services/Invidious.bs +++ b/src/source/services/Invidious.bs @@ -95,7 +95,7 @@ namespace RokuYoutube.Services.Invidious end function function GetAuthorizeTokenLink() - 'https:///authorize_token?scopes=&callback_url=&expire= + ' Make sure the host is not on https, because https cannot redirect to http, which is our local server invidiousHost = strReplace(API_URL, "https://", "http://") scope = ":*"