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