Files
playlet-piped/playlet-lib/src/components/VideoQueue/VideoQueueViewContentJob.bs
T
Brahim Hadriche 82dea9af37 Remove asynctask code (#666)
* Remove asynctask code

* add job test example

* Fix testing

* Fix a couple of imports
2025-06-22 17:39:58 -04:00

92 lines
3.1 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/CancellationUtils.bs"
import "pkg:/source/utils/MathUtils.bs"
@job("VideoQueueViewContentJob")
function ExecuteJob() as void
input = JobGetInput()
videoNodes = input.videoNodes
innertubeNode = input.innertube
invidiousNode = input.invidious
preferencesNode = input.preferences
service = new Invidious.InvidiousService(invidiousNode)
instance = service.GetInstance()
cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true)
index = MathUtils.Clamp(input.index - 2, 0, videoNodes.Count() - 1)
backend = preferencesNode["backend.selected"]
requestOptions = {
cacheSeconds: 8640000 ' 100 days
cancellation: cancellation
tryCount: 1
fetchNext: false
visitorData: innertubeNode.visitorData
}
' TODO:P1: Use InnertubeService.GetVideoInfoBatched with an array 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
' Or maybe not load this stuff at all.
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 JobIsCancelled()
JobCancel()
return
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 JobIsCancelled()
JobCancel()
return
end if
end for
return
end function
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"
requestOptions["retry"] = false
metadata = InnertubeService.GetVideoInfoBatched([videoId], requestOptions)
if IsArray(metadata.videos) and metadata.videos.Count() > 0
metadata = metadata.videos[0]
else
metadata = {
"error": "Failed to get video info with video_info_batched=true"
}
end if
else
metadata = service.GetVideoMetadata(videoId, requestOptions)
end if
if metadata.error <> invalid
LogError("Failed to load video metadata", metadata.error)
videoNode.title = "Video not available"
return false
end if
InvidiousContent.ToVideoContentNode(videoNode, metadata, instance)
return false
end function