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 innertubeNode = input.innertube 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 visitorData: innertubeNode.visitorData } ' 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" metadata = InnertubeService.GetVideoMetadata(videoId, requestOptions) 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