Files
playlet-piped/playlet-lib/src/components/VideoQueue/VideoQueueViewContentTask.bs
T
2024-12-26 16:49:28 -05:00

83 lines
2.8 KiB
Plaintext

import "pkg:/components/Services/Innertube/InnertubeService.bs"
import "pkg:/components/Services/Invidious/InvidiousService.bs"
import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs"
import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/MathUtils.bs"
@asynctask
function VideoQueueViewContentTask(input as object) as object
videoNodes = input.videoNodes
invidiousNode = input.invidious
preferencesNode = input.preferences
service = new Invidious.InvidiousService(invidiousNode)
instance = service.GetInstance()
cancellation = m.top.cancellation
index = MathUtils.Clamp(input.index - 2, 0, videoNodes.Count() - 1)
backend = preferencesNode["playback.backend"]
requestOptions = {
cacheSeconds: 8640000 ' 100 days
cancellation: cancellation
tryCount: 1
fetchNext: false
}
' TODO:P1: Use InnertubeService.GetVideoInfoBatched instead of loading each video individually
' TODO:P1: It turns out this might be too much. With a large number of videos
' YouTube starts to rate limit us.
' Need to change it to load on demand using render tracking
for i = index to videoNodes.Count() - 1
videoNode = videoNodes[i]
if not LoadVideoDetail(videoNode, service, instance, requestOptions, backend)
' Sleep for a bit to avoid creating too much traffic
sleep(500)
end if
if m.top.cancel = true
return invalid
end if
end for
for i = index - 1 to 0 step -1
videoNode = videoNodes[i]
if not LoadVideoDetail(videoNode, service, instance, requestOptions, backend)
' Sleep for a bit to avoid creating too much traffic
sleep(1000)
end if
if m.top.cancel = true
return invalid
end if
end for
return invalid
end function
' Returns true if the video was loaded from cache.
function LoadVideoDetail(videoNode as object, service as object, instance as string, requestOptions as object, backend as string) as boolean
videoId = videoNode.videoId
if backend = "playlet"
response = InnertubeService.GetVideoMetadata(videoId, requestOptions)
else
response = service.GetVideoMetadata(videoId, requestOptions)
end if
metadata = response.Json()
if not response.IsSuccess() or metadata = invalid
LogError("Failed to load video metadata", response.ErrorMessage())
if response.StatusCode() = 500
' The video is likely not available.
' We set the title to make sure we don't try to load it again
videoNode.title = "Video not available"
end if
return false
end if
InvidiousContent.ToVideoContentNode(videoNode, metadata, instance)
return response.IsCached()
end function