From 1c297ff014862911ac4eab14b59752a1771fad84 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Sat, 5 Nov 2022 16:26:53 -0400 Subject: [PATCH] Add country code arg for better feed --- .../tasks/VideoDetailsLoaderTask.brs | 3 +- src/source/services/Invidious.bs | 54 +++++++++++-------- src/source/webserver/common/General.brs | 32 +++++------ 3 files changed, 52 insertions(+), 37 deletions(-) diff --git a/src/components/tasks/VideoDetailsLoaderTask.brs b/src/components/tasks/VideoDetailsLoaderTask.brs index 8975f881..d45bc2a0 100644 --- a/src/components/tasks/VideoDetailsLoaderTask.brs +++ b/src/components/tasks/VideoDetailsLoaderTask.brs @@ -26,8 +26,9 @@ function GetVideoMetadata() videoMetadata = RokuYoutube.Services.Invidious.GetVideoMetadata(videoId) lastItag = videoMetadata.formatStreams[videoMetadata.formatStreams.Count() - 1].itag - ' GetVideoUrl returns a url with the local=true, leading to less change of rate limit (403) - maybe? + ' GetVideoUrl returns a url with the local=true, which is proxied through the instance ' TODO: is this reliable? + ' TODO: try direct videos first, before trying proxy, to avoid additional slowdowns videoUrl = RokuYoutube.Services.Invidious.GetVideoUrl(videoId, lastItag) videoMetadata.formatStreams.push({ itag: lastItag, diff --git a/src/source/services/Invidious.bs b/src/source/services/Invidious.bs index 4908aada..4922ba27 100644 --- a/src/source/services/Invidious.bs +++ b/src/source/services/Invidious.bs @@ -15,9 +15,26 @@ namespace RokuYoutube.Services.Invidious const AUTH_FEED = "/api/v1/auth/feed" + function GetCountryCode() + if m.countryCode = invalid or m.countryCode = "" then + m.countryCode = CreateObject("roDeviceInfo").GetUserCountryCode() + end if + return m.countryCode + end function + + function DefaultArgs() + return { + params: { + region: GetCountryCode() + } + } + end function + function GetPopular() url = `${API_URL}${POPULAR_ENDPOINT}` - response = Requests().get(url) + + args = DefaultArgs() + response = Requests().get(url, args) if response.statuscode = 200 return response.json @@ -27,7 +44,7 @@ namespace RokuYoutube.Services.Invidious function GetTrending(trendingType = invalid as string) url = `${API_URL}${TRENDING_ENDPOINT}` - args = { params: {} } + args = DefaultArgs() if trendingType <> invalid args.params.type = trendingType end if @@ -45,7 +62,9 @@ namespace RokuYoutube.Services.Invidious function GetVideoMetadata(videoId as string) url = `${API_URL}${VIDEOS_ENDPOINT}/${videoId}` - response = Requests().get(url) + + args = DefaultArgs() + response = Requests().get(url, args) ' TODO: handle 500 {"error":"This video is unavailable"} if response.statuscode = 200 @@ -55,11 +74,9 @@ namespace RokuYoutube.Services.Invidious function SearchSuggestions(q as string) as SearchSuggestionResult url = `${API_URL}${SEARCH_SUGGEST_ENDPOINT}` - args = { - params: { - q: q - } - } + + args = DefaultArgs() + args.params.q = q response = Requests().get(url, args) if response.statuscode = 200 @@ -70,33 +87,28 @@ namespace RokuYoutube.Services.Invidious function Search(q as string, page = invalid as integer, sort_by = invalid as RokuYoutube.Models.Invidious.SearchFilter.SortBy, date = invalid as RokuYoutube.Models.Invidious.SearchFilter.Date, duration = invalid as RokuYoutube.Models.Invidious.SearchFilter.Duration, _type = invalid as RokuYoutube.Models.Invidious.SearchFilter.SearchType, features = invalid as RokuYoutube.Models.Invidious.SearchFilter.Features) url = `${API_URL}${SEARCH_ENDPOINT}` - params = { - q: q - } + args = DefaultArgs() + args.params.q = q if page <> invalid - params.page = Str(page) + args.params.page = Str(page) end if if sort_by <> invalid - params.sort_by = sort_by + args.params.sort_by = sort_by end if if date <> invalid - params.date = date + args.params.date = date end if if duration <> invalid - params.duration = duration + args.params.duration = duration end if if _type <> invalid - params.type = _type + args.params.type = _type end if if features <> invalid - params.features = features.join(",") + args.params.features = features.join(",") end if - args = { - params: params - } - response = Requests().get(url, args) if response.statuscode = 200 diff --git a/src/source/webserver/common/General.brs b/src/source/webserver/common/General.brs index 2774bcf8..796ed2bd 100644 --- a/src/source/webserver/common/General.brs +++ b/src/source/webserver/common/General.brs @@ -9,38 +9,40 @@ REM '****************************************************** 'Get our device version '****************************************************** -Function GetDeviceVersion() - if m.softwareVersion = invalid OR m.softwareVersion = "" then +function GetDeviceVersion() + if m.softwareVersion = invalid or m.softwareVersion = "" then m.softwareVersion = CreateObject("roDeviceInfo").GetVersion() end if return m.softwareVersion -End Function +end function '****************************************************** 'Get our serial number '****************************************************** -Function GetDeviceESN() - if m.serialNumber = invalid OR m.serialNumber = "" then +function GetDeviceESN() + if m.serialNumber = invalid or m.serialNumber = "" then m.serialNumber = CreateObject("roDeviceInfo").GetDeviceUniqueId() end if return m.serialNumber -End Function +end function '****************************************************** 'Determine if the UI is displayed in SD or HD mode '****************************************************** -Function IsHD() +function IsHD() di = CreateObject("roDeviceInfo") if di.GetDisplayMode() = "720p" then return true return false -End Function +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 +function GetLocalIpAddress() + if m.ipAddress = invalid or m.ipAddress = "" then + ips = di.GetIPAddrs() + for each ip in ips + m.ipAddress = ips[ip] + return m.ipAddress + end for + end if +end function \ No newline at end of file