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" const POPULAR_ENDPOINT = "/api/v1/popular" const TRENDING_ENDPOINT = "/api/v1/trending" const VIDEOS_ENDPOINT = "/api/v1/videos" const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions" const SEARCH_ENDPOINT = "/api/v1/search" const AUTH_FEED = "/api/v1/auth/feed" const AUTH_PLAYLISTS = "/api/v1/auth/playlists" 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}` args = DefaultArgs() response = Requests().get(url, args) if response.statuscode = 200 return response.json end if end function function GetTrending(trendingType = invalid as string) url = `${API_URL}${TRENDING_ENDPOINT}` args = DefaultArgs() if trendingType <> invalid args.params.type = trendingType end if response = Requests().get(url, args) if response.statuscode = 200 return response.json end if end function function GetVideoUrl(videoId as string, itag as string, local as bool) url = `${API_URL}/latest_version?id=${videoId}&itag=${itag}` if local url = url + "&local=true" end if return url end function function GetVideoMetadata(videoId as string) url = `${API_URL}${VIDEOS_ENDPOINT}/${videoId}` args = DefaultArgs() response = Requests().get(url, args) ' TODO: handle 500 {"error":"This video is unavailable"} if response.statuscode = 200 videoMetadata = response.json lastItag = videoMetadata.formatStreams[videoMetadata.formatStreams.Count() - 1].itag ' GetVideoUrl returns a url that redirects to the video stream ' TODO: is this reliable? videoUrl = RokuYoutube.Services.Invidious.GetVideoUrl(videoId, lastItag, false) videoMetadata.formatStreams.push({ itag: lastItag, url: videoUrl }) end if return videoMetadata end function function SearchSuggestions(q as string) as SearchSuggestionResult url = `${API_URL}${SEARCH_SUGGEST_ENDPOINT}` args = DefaultArgs() args.params.q = q response = Requests().get(url, args) if response.statuscode = 200 return response.json end if end function 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}` args = DefaultArgs() args.params.q = q if page <> invalid args.params.page = Str(page).Trim() end if if sort_by <> invalid args.params.sort_by = sort_by end if if date <> invalid args.params.date = date end if if duration <> invalid args.params.duration = duration end if if _type <> invalid args.params.type = _type end if if features <> invalid args.params.features = features.join(",") end if response = Requests().get(url, args) if response.statuscode = 200 return response.json end if end function function GetCurrentHost() return API_URL end function function GetAuthorizeTokenLink() ' 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 = ":*" localIp = GetLocalIpAddress() webServerPort = 8888 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}` end function function GetAuthenticationHeaders() token = RokuYoutube.Utils.Registry.Read("token") if token = invalid return invalid end if return { "Authorization": `Bearer ${token}` } end function function GetUserFeed() headers = GetAuthenticationHeaders() if headers = invalid return invalid end if url = `${API_URL}${AUTH_FEED}` args = { headers: headers, retryCount: 3 'Sometimes authentication fails randomly with a valid token... } response = Requests().get(url, args) if response.statuscode = 403 RokuYoutube.Utils.Registry.Delete("token") end if if response.statuscode = 200 return response.json.videos end if end function function GetUserPlaylists() headers = GetAuthenticationHeaders() if headers = invalid return invalid end if url = `${API_URL}${AUTH_PLAYLISTS}` args = { headers: headers, retryCount: 3 'Sometimes authentication fails randomly with a valid token... } response = Requests().get(url, args) if response.statuscode = 403 RokuYoutube.Utils.Registry.Delete("token") end if if response.statuscode = 200 return response.json end if end function end namespace