Files
playlet-piped/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs
T
Brahim Hadriche 5335a0273b Feature/optimize task imports (#257)
* Prune empty code files

* delete file

* Restore

* refactor

* fix warnings

* Fix tasks

* Add diag

* tweak

* Log load times

* changelog
2024-01-08 19:38:39 -05:00

179 lines
6.2 KiB
Plaintext

import "pkg:/components/Services/SponsorBlock/Notifications/SponsorBlockNotificationUtils.bs"
import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs"
import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/AsyncTask/Tasks.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 = AsyncTask.Start(Tasks.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 = "#FF000080"
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"
duration = m.top.duration
' Sometimes segmentEnd is greater than duration
if duration > 0 and segmentEnd > duration
segmentEnd = duration
end if
' If the segment is at the end of the video, skip the video instead of seeking
if duration > 0 and Abs(segmentEnd - duration) < 2
LogInfo("Skipping section:", segment["category"], "at end of video")
SendSkippedSectionEvent(segment.UUID)
OnVideoFinished()
return
else
LogInfo("Skipping section:", segment["category"])
SendSkippedSectionEvent(segment.UUID)
m.top.seek = segmentEnd
return
end if
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)
AsyncTask.Start(Tasks.SponsorBlockSegmentViewedTask, { segmentId: segmentId })
end function
end namespace