180 lines
5.9 KiB
Plaintext
180 lines
5.9 KiB
Plaintext
import "pkg:/source/roku_modules/rokurequests/Requests.brs"
|
|
import "pkg:/source/utils/RegistryUtils.bs"
|
|
import "pkg:/source/utils/StringUtils.bs"
|
|
import "pkg:/source/utils/General.bs"
|
|
import "pkg:/source/utils/TimeUtils.bs"
|
|
import "pkg:/source/utils/WebUtils.bs"
|
|
import "pkg:/source/services/InvidiousSettings.bs"
|
|
|
|
namespace Invidious
|
|
const TOKEN_CALLBACK = "/invidious/token_callback"
|
|
|
|
' TODO: query server status, check if it supports APIs and/or signups
|
|
const STATS_ENDPOINT = "/api/v1/stats"
|
|
|
|
const VIDEOS_ENDPOINT = "/api/v1/videos"
|
|
const HISTORY_ENDPOINT = "/api/v1/auth/history"
|
|
|
|
const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions"
|
|
const SEARCH_ENDPOINT = "/api/v1/search"
|
|
|
|
const AUTH_SCOPE = "GET:feed,GET:playlists,POST:tokens/unregister,POST:history*"
|
|
const AUTH_FEED = "/api/v1/auth/feed"
|
|
const AUTH_PLAYLISTS = "/api/v1/auth/playlists"
|
|
const AUTH_TOKENS_UNREGISTER = "/api/v1/auth/tokens/unregister"
|
|
|
|
function DefaultArgs() as object
|
|
return {
|
|
params: {
|
|
region: GetUserCountryCode()
|
|
}
|
|
}
|
|
end function
|
|
|
|
function GetServerStats() as object
|
|
instance = InvidiousSettings.GetSelectedInstance()
|
|
url = `${instance}${Invidious.STATS_ENDPOINT}`
|
|
|
|
response = Requests().get(url)
|
|
if response.statuscode = 200
|
|
return response.json
|
|
end if
|
|
return invalid
|
|
end function
|
|
|
|
function GetVideoUrl(videoId as string, itag as string, local as boolean) as string
|
|
instance = InvidiousSettings.GetSelectedInstance()
|
|
url = `${instance}/latest_version?id=${videoId}&itag=${itag}`
|
|
if local
|
|
url = url + "&local=true"
|
|
end if
|
|
return url
|
|
end function
|
|
|
|
function GetVideoMetadata(videoId as string) as object
|
|
instance = InvidiousSettings.GetSelectedInstance()
|
|
url = `${instance}${Invidious.VIDEOS_ENDPOINT}/${videoId}`
|
|
|
|
args = Invidious.DefaultArgs()
|
|
args.cacheSeconds = 60 * 60 * 6 ' 6 hours
|
|
|
|
response = Requests().get(url, args)
|
|
|
|
return {
|
|
success: response.ok,
|
|
statusCode: response.statuscode,
|
|
data: response.json
|
|
}
|
|
end function
|
|
|
|
function MarkVideoWatched(videoId as string) as boolean
|
|
authToken = InvidiousSettings.GetAuthToken()
|
|
if authToken = invalid
|
|
return false
|
|
end if
|
|
|
|
url = `${authToken.instance}${Invidious.HISTORY_ENDPOINT}/${videoId}`
|
|
headers = Invidious.GetAuthenticationHeaders(authToken.token)
|
|
args = {
|
|
headers: headers,
|
|
parseJson: false
|
|
}
|
|
|
|
response = Requests().post(url, args)
|
|
return response.ok
|
|
end function
|
|
|
|
function SearchSuggestions(q as string) as object 'SearchSuggestionResult
|
|
instance = InvidiousSettings.GetSelectedInstance()
|
|
url = `${instance}${Invidious.SEARCH_SUGGEST_ENDPOINT}`
|
|
|
|
args = Invidious.DefaultArgs()
|
|
args.params.q = q
|
|
args.cacheSeconds = 60 * 60 * 6 ' 6 hours
|
|
|
|
response = Requests().get(url, args)
|
|
|
|
if response.statuscode = 200
|
|
return response.json
|
|
end if
|
|
return invalid
|
|
end function
|
|
|
|
function Search(q as string, args = invalid as dynamic) as object
|
|
instance = InvidiousSettings.GetSelectedInstance()
|
|
url = `${instance}${Invidious.SEARCH_ENDPOINT}`
|
|
|
|
_args = Invidious.DefaultArgs()
|
|
_args.params.q = q
|
|
|
|
if args <> invalid
|
|
if args.page <> invalid
|
|
_args.params.page = Str(args.page).Trim()
|
|
end if
|
|
if args.sort_by <> invalid
|
|
_args.params.sort_by = args.sort_by
|
|
end if
|
|
if args.date <> invalid
|
|
_args.params.date = args.date
|
|
end if
|
|
if args.duration <> invalid
|
|
_args.params.duration = args.duration
|
|
end if
|
|
if args.type <> invalid
|
|
_args.params.type = args.type
|
|
end if
|
|
if args.features <> invalid
|
|
_args.params.features = args.features.join(",")
|
|
end if
|
|
end if
|
|
response = Requests().get(url, _args)
|
|
|
|
if response.statuscode = 200
|
|
return response.json
|
|
end if
|
|
return invalid
|
|
end function
|
|
|
|
function GetAuthorizeTokenLink() as string
|
|
instance = InvidiousSettings.GetSelectedInstance()
|
|
' Make sure the host is not on https, because https cannot redirect to http, which is our local server
|
|
invidiousHost = instance.Replace("https://", "http://")
|
|
|
|
localIp = GetLocalIpAddress()
|
|
webServerPort = 8888
|
|
expire = TimeUtils.Now().AsSeconds() + 60 * 60 * 24 * 365 * 2 '2 years from now
|
|
|
|
callbackUrl = `http://${localIp}:${webServerPort}${Invidious.TOKEN_CALLBACK}?ref=${instance}`.EncodeUriComponent()
|
|
scope = `${Invidious.AUTH_SCOPE}`.EncodeUriComponent()
|
|
return `${invidiousHost}/authorize_token?scopes=${scope}&callback_url=${callbackUrl}&expire=${expire}`
|
|
end function
|
|
|
|
function GetAuthenticationHeaders(token as string) as object
|
|
return {
|
|
"Authorization": `Bearer ${token}`
|
|
}
|
|
end function
|
|
|
|
function Logout() as boolean
|
|
authToken = InvidiousSettings.GetAuthToken()
|
|
if authToken = invalid
|
|
return false
|
|
end if
|
|
headers = Invidious.GetAuthenticationHeaders(authToken.token)
|
|
|
|
url = `${authToken.instance}${Invidious.AUTH_TOKENS_UNREGISTER}`
|
|
|
|
' authToken.token is already valid json that contains the "session" key needed by the unregister endpoint
|
|
headers["Content-Type"] = "application/json"
|
|
args = {
|
|
headers: headers,
|
|
data: authToken.token
|
|
}
|
|
|
|
Requests().post(url, args)
|
|
|
|
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN)
|
|
return true
|
|
end function
|
|
end namespace
|