Files
playlet-piped/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs
T
Brahim Hadricheandgithub-action linter acce520752 Sort imports (#155)
* Sort imports

* Lint fix

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
2023-09-29 18:16:00 -04:00

166 lines
5.6 KiB
Plaintext

import "pkg:/components/Services/SponsorBlock/Notifications/SponsorBlockNotificationUtils.bs"
import "pkg:/components/Services/SponsorBlock/SponsorBlockSegmentViewedTask.bs"
import "pkg:/components/Services/SponsorBlock/SponsorBlockTask.bs"
import "pkg:/source/asyncTask/asyncTask.bs"
namespace SponsorBlock
function SetupSponsorBlock() as void
if not m.preferences["sponsorblock.enabled"]
return
end if
m.sponsorBlockService = new SponsorBlock.SponsorBlockService()
PositionChapterLabel()
m.top.trickPlayBar.observeField("visible", FuncName(OnTrickPlayBarVisible))
m.chapterLabelTimer = m.top.findNode("chapterLabelTimer")
m.chapterLabelTimer.observeField("fire", FuncName(OnChapterLabelTimer))
m._sponsorBlockLastTime = 0.0#
' TODO:P1 more precision on when notifications should be visible
' Should notifcations be visible when the video is not fullscreen?
SponsorBlock.SetVisible(m.notifications, m.container.fullscreen)
end function
function PositionChapterLabel()
m.chapterLabel = m.top.findNode("chapterLabel")
' We move the chapter label under m.top.trickPlayBar
' We could move it to m.top.pivotNode, but that is only available on Roku OS 11.5+
m.chapterLabel.reparent(m.top.trickPlayBar, false)
trickPlayBarWidth = m.top.trickPlayBar.boundingRect().width
m.chapterLabel.translation = [trickPlayBarWidth / 4 - m.chapterLabel.width / 2, 25]
end function
function OnTrickPlayBarVisible()
if m.top.trickPlayBar.visible
m.top.chapter = ""
m.chapterLabelTimer.control = "start"
else
m.chapterLabelTimer.control = "stop"
end if
end function
function OnChapterLabelTimer() as void
if m.skipSegments = invalid
return
end if
' trickPlayPosition is not documented on Roku website, this is why we're checking
if not IsDouble(m.top.trickPlayPosition)
return
end if
if m._sponsorBlockLastTime = m.top.trickPlayPosition
return
end if
m._sponsorBlockLastTime = m.top.trickPlayPosition
UpdateSponsorBlockChapter(m._sponsorBlockLastTime)
end function
function UpdateSponsorBlockChapter(time as double) as void
segments = m.skipSegments
for each segment in segments
segmentRange = segment["segment"]
segmentStart = segmentRange[0]
segmentEnd = segmentRange[1]
if (segmentStart <= time) and (segmentEnd >= time)
m.top.chapter = m.sponsorBlockService.SegmentTitle(segment["category"])
return
end if
end for
m.top.chapter = ""
end function
function FetchSponsorBlock(metadata as object) as void
if not m.preferences["sponsorblock.enabled"]
return
end if
if m.sponsorBlockTask <> invalid
m.sponsorBlockTask.cancel = true
end if
m.sponsorBlockTask = StartAsyncTask(SponsorBlockTask, { metadata: metadata }, OnSponsorBlockTaskResults)
end function
function OnSponsorBlockTaskResults(output as object) as void
m.sponsorBlockTask = invalid
if output.cancelled
return
end if
if not output.success
error = ErrorUtils.Format(output.error)
LogError(error)
return
end if
skipSegments = output.result.skipSegments
if skipSegments = invalid
return
end if
m.skipSegments = skipSegments
m.top.seekMode = "accurate"
barPath = output.result.barPath
if barPath <> invalid
m.top.trickPlayBar.trackImageUri = barPath
m.top.trickPlayBar.filledBarBlendColor = "0xFF000080"
end if
m.top.UnobserveFieldScoped("position")
m.top.ObserveFieldScoped("position", FuncName(OnPositionChangeSkipSponsorBlockSections))
end function
function OnPositionChangeSkipSponsorBlockSections() as void
segments = m.skipSegments
if segments = invalid
return
end if
for each segment in segments
segmentRange = segment["segment"]
segmentStart = segmentRange[0]
segmentEnd = segmentRange[1]
currentPosition = m.top.position
if (segmentStart < currentPosition) and ((segmentEnd - 1) > currentPosition)
action = GetActionForSegment(segment)
if m.preferences["sponsorblock.show_notifications"] = true
SponsorBlock.ShowNotifcation(m.notifications, segment, action)
end if
if action = "AutoSkip"
m.top.seek = segmentEnd
LogInfo("Skipping section:", segment["category"])
SendSkippedSectionEvent(segment.UUID)
return
end if
end if
end for
end function
function GetActionForSegment(segment as object) as dynamic
category = segment["category"]
if category = "sponsor"
return "AutoSkip"
else if category = "poi_highlight"
return "ManualSkip"
else if category = "exclusive_access"
return "ShowOverlay"
else if category = "chapter"
return "ShowOverlay"
end if
return ""
end function
function SendSkippedSectionEvent(segmentId as string)
StartAsyncTask(SponsorBlockSegmentViewedTask, { segmentId: segmentId })
end function
end namespace