import "pkg:/components/Services/Innertube/InnertubeService.bs" import "pkg:/components/Services/Invidious/InvidiousService.bs" import "pkg:/source/utils/MessagePortUtils.bs" @job("MarkVideoWatchedJob") function ExecuteJob() as void input = JobGetInput() videoId = input.videoId invidiousNode = input.invidious authToken = input.authToken profileType = input.profileType playbackTrackingUrls = input.playbackTrackingUrls cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true) if profileType = "invidious" service = new Invidious.InvidiousService(invidiousNode) service.MarkVideoWatched(videoId, cancellation) return end if if profileType <> "youtube" JobError(`Unsupported profile type: ${profileType}`) return end if if playbackTrackingUrls = invalid or StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsPlaybackUrl) or StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsWatchtimeUrl) playbackTrackingUrls = InnertubeService.AuthGetPlaybackTrackingUrls(videoId, { accessToken: authToken.token onBehalfOfUser: authToken.activeIdentifyToken cancellation: cancellation }) end if if playbackTrackingUrls.error <> invalid JobError(`Failed to get playback tracking URLs: ${playbackTrackingUrls.error}`) return end if if StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsPlaybackUrl) JobError("videostatsPlaybackUrl is empty") return end if markedAsWatched = InnertubeService.AuthMarkVideoWatched(playbackTrackingUrls.videostatsPlaybackUrl, { accessToken: authToken.token onBehalfOfUser: authToken.activeIdentifyToken cancellation: cancellation }) if not markedAsWatched JobError("Failed to mark video as watched") return end if if StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsWatchtimeUrl) JobError("videostatsWatchtimeUrl is empty") return end if if CancellationUtils.IsCancelled(cancellation) JobCancel() return end if port = CreateObject("roMessagePort") callbackNode = JobGetCallbackNode() MessagePortUtils.ObserveFieldScopedExSafe(callbackNode, "watchTime", port) CancellationUtils.ObserveCancellationScoped(cancellation, port) while true message = wait(60000, port) if CancellationUtils.IsCancellationEvent(cancellation, message) JobCancel() return end if if message = invalid continue while end if watchTime = message.getData() InnertubeService.AuthMarkVideoWatchTime(watchTime, playbackTrackingUrls.videostatsWatchtimeUrl, { accessToken: authToken.token onBehalfOfUser: authToken.activeIdentifyToken ' TODO:P1 if we pass cancellation here, http client will remove scoped ' observer, which is stored on the current node. ' This requires an extra observer node to be independent. cancellation: invalid }) end while end function