SponsorBlock job

This commit is contained in:
Brahim Hadriche committed 2025-06-21 19:56:36 -04:00
1 parent 115b457241
commit 9f5efb0ba7
4 files changed
+35 -31

No files matched your search

@@ -2,15 +2,18 @@ import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs"
import "pkg:/components/VideoPlayer/SponsorBlockSegmentAction.bs"
import "pkg:/source/utils/Types.bs"
@asynctask
function SponsorBlockTask(input as object) as object
videoId = input.metadata.videoId
lengthSeconds = input.metadata.lengthSeconds
@job("SponsorBlockSegmentsJob")
function ExecuteJob() as void
input = JobGetInput()
videoId = input.videoId
lengthSeconds = input.lengthSeconds
showNotifications = input.showNotifications
categories = input.categories
service = new SponsorBlock.SponsorBlockService()
skipSegments = service.GetSkipSegmentsForVideo(videoId)
cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true)
skipSegments = service.GetSkipSegmentsForVideo(videoId, cancellation)
skipSegments = ProcessSkipSegments(skipSegments, showNotifications, categories, service)
@@ -32,11 +35,11 @@ function SponsorBlockTask(input as object) as object
skipSegments.SortBy("start")
end if
return {
JobSuccessData({
videoId: videoId
skipSegments: skipSegments
barPath: barPath
}
})
end function
function ProcessSkipSegments(skipSegments as object, showNotifications as boolean, categories as object, service as SponsorBlock.SponsorBlockService) as object
@@ -69,7 +69,7 @@ namespace SponsorBlock
return m.sponsorBlockConfig.categories[category].short_title
end function
function GetSkipSegmentsForVideo(videoId as string) as object
function GetSkipSegmentsForVideo(videoId as string, cancellation = invalid as dynamic) as object
if m.sponsorBlockConfig = invalid or StringUtils.IsNullOrEmpty(m.sponsorBlockConfig.serverAddress)
return invalid
end if
@@ -85,9 +85,9 @@ namespace SponsorBlock
' From SponsorBlock docs: It should be the first 4 - 32 characters (4 is recommended).
hashPrefix = CryptoUtils.GetSha256(videoId).Left(4)
url = `${m.sponsorBlockConfig.serverAddress}${SponsorBlock.SKIP_SEGMENT_ENDPOINT}/${hashPrefix}?categories=${categoriesJson}&actionTypes=${actionTypesJson}`
response = HttpClient.Get(url).Await()
response = HttpClient.Get(url).Cancellation(cancellation).Await()
if not response.IsSuccess()
LogError("SponsorBlockService: GetSkipSegmentsForVideo failed", response.ErrorMessage())
LogError("GetSkipSegmentsForVideo failed:", response.ErrorMessage())
return invalid
end if
@@ -21,30 +21,31 @@ namespace SponsorBlock
return
end if
if m.sponsorBlockTask <> invalid
m.sponsorBlockTask.cancel = true
if m.sponsorBlockSegmentsJob <> invalid
m.sponsorBlockSegmentsJob.cancel = true
end if
m.sponsorBlockTask = AsyncTask.Start(Tasks.SponsorBlockTask, {
metadata: metadata
input = {
videoId: metadata.videoId
lengthSeconds: metadata.lengthSeconds
showNotifications: m.preferences["sponsorblock.show_notifications"]
categories: m.preferences["sponsorblock.categories"]
}, OnSponsorBlockTaskResults)
}
m.sponsorBlockSegmentsJob = JobSystem.CreateCallback(OnSponsorBlockSegmentsJobDone)
JobSystem.QueueJob(m.jobQueue, Jobs.SponsorBlockSegmentsJob, input, m.sponsorBlockSegmentsJob)
end function
function OnSponsorBlockTaskResults(output as object) as void
m.sponsorBlockTask = invalid
if output.cancelled or m.onCloseCalled = true
function OnSponsorBlockSegmentsJobDone(event as object) as void
JobSystem.UnobserveCallback(event.getRoSGNode())
m.sponsorBlockSegmentsJob = invalid
info = event.getInfo()
if not info.success
return
end if
if not output.success
error = ErrorUtils.Format(output.error)
LogError(error)
return
end if
skipSegments = output.result.skipSegments
skipSegments = info.data.skipSegments
if skipSegments = invalid or skipSegments.Count() = 0
return
end if
@@ -52,7 +53,7 @@ namespace SponsorBlock
m.skipSegments = skipSegments
m.top.seekMode = "accurate"
barPath = output.result.barPath
barPath = info.data.barPath
if barPath <> invalid
m.top.trickPlayBar.trackImageUri = barPath
m.top.trickPlayBar.filledBarBlendColor = "#FF000080"
@@ -40,7 +40,7 @@ function Init()
' Checks if current RokuOS version has showUI field in Video node
m.showUIEnabled = m.top.hasField("showUI")
LogInfo(`Custom OK handler is ${m.showUIEnabled ? "enabled" : "disabled"}`)
LogDebug(`Custom OK handler is ${m.showUIEnabled ? "enabled" : "disabled"}`)
' asyncStopSemantics available since Roku OS 12.5
' It is set to true because stopping synchronously causes timeout and crash
@@ -381,9 +381,9 @@ function OnClose()
m.videoContentTask.cancel = true
m.videoContentTask = invalid
end if
if m.sponsorBlockTask <> invalid
m.sponsorBlockTask.cancel = true
m.sponsorBlockTask = invalid
if m.sponsorBlockSegmentsJob <> invalid
m.sponsorBlockSegmentsJob.cancel = true
m.sponsorBlockSegmentsJob = invalid
end if
parent = m.top.getParent()
@@ -538,7 +538,7 @@ function HandleOKPress() as boolean
state = m.top.state
uiVisible = m.top.trickPlaybar.visible
isLive = IsLiveVideo()
LogInfo(`Handling OK button: state = ${state}, uiVisible = ${uiVisible} isLive = ${isLive}`)
LogDebug(`Handling OK button: state = ${state}, uiVisible = ${uiVisible} isLive = ${isLive}`)
if state = "playing"
if not uiVisible
' Video is playing and UI is not visible, show it