Create Component Library (#43)
* Move src under playlet-lib * Update configuration * Update script * Update script * Working version * Vs code config WIP * Package lock sync * Lint fix * Fix images * Reuse config files * Kind of a working setup * Lock node version * Rename scripts to tools * Fix build scripts, lib url system * Lint fix * rale * Fix args and web server * Load fallbacks and build script cleanups * Remove unused method * no message * no message * Remove generic message field * Lint fix * no message * Use loading indicator on main app * Add playlet lib version to settings --------- Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
1 parent
90df81df4b
commit
9d8a3d1cea
156 files changed
+3341
-213
No files matched your search
@@ -0,0 +1,160 @@
|
||||
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 SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions"
|
||||
const SEARCH_ENDPOINT = "/api/v1/search"
|
||||
|
||||
const AUTH_SCOPE = "GET:feed,GET:playlists,POST:tokens/unregister"
|
||||
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.statuscode = 200,
|
||||
statusCode: response.statuscode,
|
||||
data: response.json ?? response.text
|
||||
}
|
||||
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()
|
||||
return `${invidiousHost}/authorize_token?scopes=${Invidious.AUTH_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
|
||||
@@ -0,0 +1,175 @@
|
||||
import "pkg:/source/roku_modules/rokurequests/Requests.brs"
|
||||
import "pkg:/source/utils/TimeUtils.bs"
|
||||
import "pkg:/source/utils/RegistryUtils.bs"
|
||||
import "pkg:/source/utils/General.bs"
|
||||
import "pkg:/source/services/InvidiousSettings.bs"
|
||||
|
||||
class InvidiousService
|
||||
private endpoints
|
||||
private responseHanlders
|
||||
|
||||
function new()
|
||||
apiDefnitions = ParseJson(ReadAsciiFile("libpkg:/config/invidious_video_api.json"))
|
||||
m.endpoints = {}
|
||||
|
||||
for each apiDefnition in apiDefnitions.endpoints
|
||||
m.endpoints[apiDefnition.name] = apiDefnition
|
||||
end for
|
||||
|
||||
m.responseHanlders = {
|
||||
DefaultHandler: m.DefaultHandler,
|
||||
AuthFeedHandler: m.AuthFeedHandler,
|
||||
AuthPlaylistsHandler: m.AuthPlaylistsHandler,
|
||||
PlaylistHandler: m.PlaylistHandler
|
||||
}
|
||||
end function
|
||||
|
||||
function MakeRequest(requestData as object) as object
|
||||
endpoint = m.endpoints[requestData.endpoint]
|
||||
if endpoint = invalid
|
||||
return invalid
|
||||
end if
|
||||
|
||||
instance = InvidiousSettings.GetSelectedInstance()
|
||||
|
||||
url = instance + endpoint.url
|
||||
|
||||
args = { params: {} }
|
||||
|
||||
if endpoint.authenticated = true
|
||||
authToken = InvidiousSettings.GetAuthToken()
|
||||
if authToken = invalid
|
||||
return invalid
|
||||
end if
|
||||
url = authToken.instance + endpoint.url
|
||||
args.headers = m.GetAuthenticationHeaders(authToken.token)
|
||||
end if
|
||||
|
||||
if endpoint.queryParams <> invalid
|
||||
for each queryParamKey in endpoint.queryParams
|
||||
queryParam = endpoint.queryParams[queryParamKey]
|
||||
if queryParam.default <> invalid
|
||||
if queryParam.type = "string"
|
||||
args.params[queryParamKey] = queryParam.default
|
||||
else if queryParam.type = "#ISO3166"
|
||||
if queryParam.default = "GetUserCountryCode"
|
||||
args.params[queryParamKey] = GetUserCountryCode()
|
||||
else
|
||||
args.params[queryParamKey] = queryParam.default
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
|
||||
if requestData.queryParams <> invalid
|
||||
args.params.append(requestData.queryParams)
|
||||
end if
|
||||
|
||||
if requestData.pathParams <> invalid
|
||||
for each param in requestData.pathParams
|
||||
url = url.Replace(`:${param}`, requestData.pathParams[param])
|
||||
end for
|
||||
end if
|
||||
|
||||
if endpoint.requestArgs <> invalid
|
||||
args.append(endpoint.requestArgs)
|
||||
end if
|
||||
|
||||
response = Requests().get(url, args)
|
||||
|
||||
responseHandler = endpoint.responseHandler <> invalid ? m.responseHanlders[endpoint.responseHandler] : m.responseHanlders["DefaultHandler"]
|
||||
|
||||
return responseHandler(m, requestData, response)
|
||||
end function
|
||||
|
||||
function GetAuthenticationHeaders(token as string) as object
|
||||
return {
|
||||
"Authorization": `Bearer ${token}`
|
||||
}
|
||||
end function
|
||||
|
||||
function DefaultHandler(m as object, requestData as object, response as object) as object
|
||||
if response.statuscode = 200
|
||||
return response.json
|
||||
end if
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function AuthFeedHandler(m as object, requestData as object, response as object) as object
|
||||
m.DeleteExpiredToken(response)
|
||||
|
||||
if response.statuscode = 200
|
||||
videos = []
|
||||
videos.Append(response.json.notifications)
|
||||
videos.Append(response.json.videos)
|
||||
return videos
|
||||
end if
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function AuthPlaylistsHandler(m as object, requestData as object, response as object) as object
|
||||
m.DeleteExpiredToken(response)
|
||||
if response.statuscode = 200
|
||||
playlists = response.json
|
||||
result = {}
|
||||
for each playlist in playlists
|
||||
result.append(m.ProcessPlaylist(requestData, playlist))
|
||||
end for
|
||||
return result
|
||||
end if
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function PlaylistHandler(m as object, requestData as object, response as object) as object
|
||||
if response.statuscode = 200
|
||||
playlist = response.json
|
||||
return m.ProcessPlaylist(requestData, playlist)
|
||||
end if
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function ProcessPlaylist(requestData as object, playlist as object) as object
|
||||
title = requestData.title
|
||||
title = m.ProcessTemplate(title, playlist)
|
||||
result = {}
|
||||
result[title] = playlist.videos
|
||||
return result
|
||||
end function
|
||||
|
||||
function ProcessTemplate(template as string, data as object) as string
|
||||
for each key in data
|
||||
template = template.Replace(`%${key}%`, `${data[key]}`)
|
||||
end for
|
||||
return template
|
||||
end function
|
||||
|
||||
function DeleteExpiredToken(response as object)
|
||||
if response.statuscode = 403
|
||||
print(`Server responded with 403: ${response.text}`)
|
||||
if m.IsTokenMaybeExpired() = true
|
||||
print("deleting token...")
|
||||
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN)
|
||||
end if
|
||||
end if
|
||||
end function
|
||||
|
||||
' Public Invidious instances fail a lot randomly, even with valid tokens
|
||||
' For that reason, it's better to make sure the token actually expired by date
|
||||
function IsTokenMaybeExpired() as dynamic
|
||||
authToken = InvidiousSettings.GetAuthToken()
|
||||
if authToken = invalid
|
||||
return invalid
|
||||
end if
|
||||
token = ParseJson(authToken.token)
|
||||
if token = invalid
|
||||
return invalid
|
||||
end if
|
||||
if not token.DoesExist("expire")
|
||||
return invalid
|
||||
end if
|
||||
expire = token["expire"]
|
||||
currentTime = TimeUtils.Now().AsSeconds()
|
||||
return expire < currentTime
|
||||
end function
|
||||
end class
|
||||
@@ -0,0 +1,107 @@
|
||||
namespace InvidiousSettings
|
||||
|
||||
const DEFAULT_INSTANCE = "https://vid.puffyan.us"
|
||||
|
||||
const INSTANCES_API = "https://api.invidious.io/instances.json?sort_by=type,users"
|
||||
|
||||
function GetCurrentInstances() as object
|
||||
' TODO: cache this call to avoid json parsing every time
|
||||
instances = InvidiousSettings.GetRegistryInstances()
|
||||
if instances.Count() > 0
|
||||
return instances
|
||||
end if
|
||||
|
||||
publicInstances = InvidiousSettings.GetPublicInstances()
|
||||
if publicInstances <> invalid
|
||||
for i = 0 to publicInstances.Count() - 1
|
||||
if InvidiousSettings.IsValidPublicInstance(publicInstances[i])
|
||||
instances.push(publicInstances[i].uri)
|
||||
end if
|
||||
if instances.Count() >= 5
|
||||
exit for
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
|
||||
if instances.Count() > 0
|
||||
InvidiousSettings.SetRegistryInstances(instances)
|
||||
return instances
|
||||
end if
|
||||
|
||||
return [InvidiousSettings.DEFAULT_INSTANCE]
|
||||
end function
|
||||
|
||||
function GetSelectedInstance() as string
|
||||
instances = InvidiousSettings.GetCurrentInstances()
|
||||
return instances[0]
|
||||
end function
|
||||
|
||||
function SetAuthToken(token as string, instance as string)
|
||||
json = FormatJson({
|
||||
token: token,
|
||||
instance: instance
|
||||
})
|
||||
RegistryUtils.Write(RegistryUtils.INVIDIOUS_TOKEN, json)
|
||||
|
||||
instances = InvidiousSettings.GetCurrentInstances()
|
||||
index = ArrayIndexOf(instances, instance)
|
||||
if index = -1
|
||||
instances.push(instance)
|
||||
InvidiousSettings.SetRegistryInstances(instances)
|
||||
end if
|
||||
end function
|
||||
|
||||
function GetAuthToken() as dynamic
|
||||
tokenPayload = RegistryUtils.Read(RegistryUtils.INVIDIOUS_TOKEN)
|
||||
if tokenPayload = invalid
|
||||
return invalid
|
||||
end if
|
||||
return ParseJson(tokenPayload)
|
||||
end function
|
||||
|
||||
function GetRegistryInstances() as object
|
||||
instances = RegistryUtils.Read(RegistryUtils.INVIDIOUS_INSTANCES)
|
||||
if instances = invalid
|
||||
return []
|
||||
end if
|
||||
return ParseJson(instances)
|
||||
end function
|
||||
|
||||
function SetRegistryInstances(instances as object)
|
||||
RegistryUtils.Write(RegistryUtils.INVIDIOUS_INSTANCES, FormatJson(instances))
|
||||
end function
|
||||
|
||||
function GetPublicInstances() as object
|
||||
if m.invidiousPublicInstances <> invalid
|
||||
return m.invidiousPublicInstances
|
||||
end if
|
||||
|
||||
response = Requests().get(InvidiousSettings.INSTANCES_API)
|
||||
if response.statuscode = 200
|
||||
json = response.json
|
||||
instances = []
|
||||
|
||||
for each instance in json
|
||||
data = instance[1]
|
||||
if data.monitor <> invalid and data.monitor.dailyRatios <> invalid
|
||||
data.monitor.dailyRatios = invalid
|
||||
end if
|
||||
data["name"] = instance[0]
|
||||
instances.push(data)
|
||||
end for
|
||||
|
||||
m.invidiousPublicInstances = instances
|
||||
end if
|
||||
|
||||
return m.invidiousPublicInstances
|
||||
end function
|
||||
|
||||
function IsValidPublicInstance(instance as object) as boolean
|
||||
if instance = invalid
|
||||
return false
|
||||
end if
|
||||
|
||||
return instance.type = "https" and instance.api = true
|
||||
end function
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,106 @@
|
||||
import "pkg:/source/roku_modules/rokurequests/Requests.brs"
|
||||
import "pkg:/source/utils/WebUtils.bs"
|
||||
import "pkg:/source/utils/Crypto.bs"
|
||||
|
||||
namespace SponsorBlock
|
||||
const API_URL = "https://sponsor.ajay.app"
|
||||
const SKIP_SEGMENT_ENDPOINT = "/api/skipSegments"
|
||||
const VIEWED_SEGMENT_ENDPOINT = "/api/viewedVideoSponsorTime"
|
||||
|
||||
function SegmentColor(category as string) as integer
|
||||
map = m.SponsorBlockColors
|
||||
if map = invalid
|
||||
map = {
|
||||
sponsor: &h00D400FF,
|
||||
selfpromo: &hFFFF00FF,
|
||||
exclusive_access: &h008A5CFF,
|
||||
interaction: &hCC00FFFF,
|
||||
poi_highlight: &hFF1684FF,
|
||||
intro: &h00FFFFFF,
|
||||
outro: &h0202EDFF,
|
||||
preview: &h008FD6FF,
|
||||
filler: &h7300FFFF,
|
||||
music_offtopic: &hFF9900FF
|
||||
}
|
||||
m.SponsorBlockColors = map
|
||||
end if
|
||||
if map.doesexist(category)
|
||||
return map[category]
|
||||
end if
|
||||
return 0
|
||||
end function
|
||||
|
||||
function SegmentTitle(category as string) as string
|
||||
map = m.SponsorBlockTitles
|
||||
if map = invalid
|
||||
map = {
|
||||
sponsor: "Sponsor",
|
||||
selfpromo: "Unpaid/Self Promotion",
|
||||
exclusive_access: "Exclusive Access",
|
||||
interaction: "Interaction Reminder (Subscribe)",
|
||||
poi_highlight: "Highlight",
|
||||
intro: "Intermission/Intro Animation",
|
||||
outro: "Endcards/Credits",
|
||||
preview: "Preview/Recap",
|
||||
filler: "Filler Tangent/Jokes",
|
||||
music_offtopic: "Non-Music Section"
|
||||
}
|
||||
m.SponsorBlockTitles = map
|
||||
end if
|
||||
if map.doesexist(category)
|
||||
return map[category]
|
||||
end if
|
||||
return ""
|
||||
end function
|
||||
|
||||
function GetSkipSegmentsForVideo(videoId as string) as object
|
||||
categories = ["sponsor", "selfpromo", "interaction", "intro", "outro", "preview", "music_offtopic", "poi_highlight", "chapter", "filler", "exclusive_access"]
|
||||
actionTypes = ["skip", "mute", "chapter", "full", "poi"]
|
||||
|
||||
categoriesJson = FormatJson(categories).EncodeUriComponent()
|
||||
actionTypesJson = FormatJson(actionTypes).EncodeUriComponent()
|
||||
|
||||
' From SponsorBlock docs: It should be the first 4 - 32 characters (4 is recommended).
|
||||
hashPrefix = GetSha256(videoId).Left(4)
|
||||
url = `${SponsorBlock.API_URL}${SponsorBlock.SKIP_SEGMENT_ENDPOINT}/${hashPrefix}?categories=${categoriesJson}&actionTypes=${actionTypesJson}`
|
||||
response = Requests().get(url)
|
||||
|
||||
if response.statuscode = 200
|
||||
results = response.json
|
||||
for each item in results
|
||||
if item.videoId = videoId
|
||||
return item.segments
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function PostSegmentViewed(segmentId as string) as boolean
|
||||
url = `${SponsorBlock.API_URL}${SponsorBlock.VIEWED_SEGMENT_ENDPOINT}?UUID=${segmentId}`
|
||||
response = Requests().post(url, { parseJson: false })
|
||||
return response.statuscode = 200
|
||||
end function
|
||||
|
||||
function GenerateProgressBarBackground(segments as object, path as string)
|
||||
bar = CreateObject("roBitmap", { width: 1000, height: 20, AlphaEnable: true })
|
||||
bar.Clear(&hFFFFFF80)
|
||||
width = bar.GetWidth()
|
||||
height = bar.GetHeight()
|
||||
for each segment in segments
|
||||
pixelStart = (segment.segment[0] / segment.videoDuration) * width
|
||||
pixelEnd = (segment.segment[1] / segment.videoDuration) * width
|
||||
color = SponsorBlock.SegmentColor(segment.category)
|
||||
' highlight's duration is zero, so it is not visble on the bar.
|
||||
' Add a couple of pixels to see it
|
||||
if pixelStart = pixelEnd
|
||||
pixelEnd += 2
|
||||
end if
|
||||
bar.DrawRect(pixelStart, 0, pixelEnd - pixelStart, height, color)
|
||||
end for
|
||||
bar.Finish()
|
||||
buffer = bar.GetPng(0, 0, width, height)
|
||||
buffer.WriteFile(path)
|
||||
end function
|
||||
|
||||
end namespace
|
||||
Reference in new issue
Block a user