SponsorBlock categories (#634)

* SponsorBlock categories

* perf

* remove todo

* tweak

* UI is working

* Lint fix

* changelog

* Bug fix

* Restore timer

* Locale

* Comment

* tweaks

* web app

* remove unsued svelte ignore

* lint web app

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
Brahim Hadricheandgithub-action linter authored and GitHub committed 2025-06-01 15:30:58 -04:00
1 parent 0bc5ab6117
commit fcae05a0da
34 files changed
+1039 -422

No files matched your search

@@ -9,6 +9,7 @@ function Init()
"number": "integer"
"radio": "string"
"array": "array"
"assocarray": "assocarray"
}
m.disableSavingToRegistry = false
@@ -1,4 +1,5 @@
import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs"
import "pkg:/components/VideoPlayer/SponsorBlockSegmentAction.bs"
function Init()
m.categoryLabel = m.top.findNode("categoryLabel")
@@ -13,16 +14,7 @@ function Init()
m.service = SponsorBlock.SponsorBlockService()
end function
function OnActionSet()
action = m.top.action
category = m.top.segment.category
categoryTitle = Tr(ValidString(m.service.SegmentShortTitle(category)))
if action = "AutoSkip"
m.categoryLabel.text = Tr(Locale.SponsorBlock.Skipped) + `\n` + categoryTitle
else
m.categoryLabel.text = categoryTitle
end if
function OnDoShow() as void
AnimateIn()
end function
@@ -1,7 +1,9 @@
<component name="SponsorBlockNotification" extends="Group">
<interface>
<field id="segment" type="assocarray" />
<field id="action" type="string" onChange="OnActionSet" alwaysNotify="true" />
<field id="segmentId" type="string" />
<field id="categoryText" type="string" alias="categoryLabel.text" />
<field id="actionText" type="string" alias="actionLabel.text" />
<field id="doShow" type="boolean" onChange="OnDoShow" alwaysNotify="true" />
</interface>
<children>
<Poster
@@ -16,18 +18,26 @@
height="50"
translation="[10,10]">
</Poster>
<!-- bs:disable-next-line LOCALE_MISSING_TRANSLATIONS -->
<Label text="..."
id="categoryLabel"
font="font:SmallestBoldSystemFont"
lineSpacing="5"
horizAlign="center"
vertAlign="center"
wrap="true"
width="170"
height="70"
translation="[70,0]">
</Label>
<LayoutGroup vertAlignment="center" translation="[70,35]">
<Label
id="categoryLabel"
font="font:TinyBoldSystemFont"
horizAlign="center"
width="170"
lineSpacing="0"
maxLines="2"
wrap="true">
</Label>
<Label
id="actionLabel"
font="font:TinySystemFont"
horizAlign="center"
width="170"
lineSpacing="0"
maxLines="2"
wrap="true">
</Label>
</LayoutGroup>
</Poster>
<Animation
id="translationAnimation"
@@ -1,27 +1,43 @@
namespace SponsorBlock
const NOTIFICATION_NODE_ID = "SponsorBlockNotification"
function GetCurrentNotification(notifications as object) as object
childCount = notifications.getChildCount()
for i = 0 to childCount - 1
child = notifications.getChild(i)
if child.id = NOTIFICATION_NODE_ID
return child
end if
end for
return invalid
end function
function ShowNotifcation(notifications as object, segment as object, action as dynamic) as void
' TODO:P2 avoid finding the node over and over
' The video player can probably own a single notification, and destroy it when the video is closed
notification = notifications.findNode(NOTIFICATION_NODE_ID)
notification = GetCurrentNotification(notifications)
if notification = invalid
notification = notifications.createChild("SponsorBlockNotification")
notification.id = NOTIFICATION_NODE_ID
end if
notification.segment = segment
notification.action = action
notification.segmentId = segment.UUID
notification.categoryText = segment.shortTitle
notification.actionText = segment.actionText
notification.doShow = true
end function
function RemoveNotifcation(notifications as object) as void
notification = notifications.findNode(NOTIFICATION_NODE_ID)
notification = GetCurrentNotification(notifications)
if notification <> invalid
notifications.RemoveChild(notification)
end if
end function
function SetVisible(notifications as object, visible as boolean)
notification = notifications.findNode(NOTIFICATION_NODE_ID)
notification = GetCurrentNotification(notifications)
if notification <> invalid
notification.visible = visible
end if
@@ -5,5 +5,5 @@ function SponsorBlockSegmentViewedTask(input as object)
segmentId = input.segmentId
service = new SponsorBlock.SponsorBlockService()
service.PostSegmentViewed(segmentId)
service.PostSegmentViewed(segmentId, m.top.cancellation)
end function
@@ -12,6 +12,26 @@ namespace SponsorBlock
m.sponsorBlockConfig = ParseJson(ReadAsciiFile("libpkg:/config/sponsorblock_config.json5"))
end function
function GetCategories() as object
if m.sponsorBlockConfig = invalid
return []
end if
return ValidArray(m.sponsorBlockConfig.categoryList)
end function
function GetOptionsForCategory(category as string) as object
if m.sponsorBlockConfig = invalid
return []
end if
if not m.sponsorBlockConfig.categories.doesexist(category)
return []
end if
return ValidArray(m.sponsorBlockConfig.categories[category].options)
end function
function SegmentColor(category as string) as integer
if m.sponsorBlockConfig = invalid
return 0
@@ -54,8 +74,10 @@ namespace SponsorBlock
return invalid
end if
categories = ["sponsor", "selfpromo", "interaction", "intro", "outro", "preview", "music_offtopic", "poi_highlight", "filler", "exclusive_access"]
actionTypes = ["skip", "mute", "chapter", "full", "poi"]
' categories = ["sponsor", "selfpromo", "interaction", "intro", "outro", "preview", "music_offtopic", "poi_highlight", "filler", "exclusive_access"]
categories = ["sponsor", "selfpromo", "interaction", "intro", "outro", "preview", "music_offtopic", "poi_highlight", "filler"]
' actionTypes = ["skip", "mute", "chapter", "full", "poi"]
actionTypes = ["skip", "mute", "poi"]
categoriesJson = FormatJson(categories).EncodeUriComponent()
actionTypesJson = FormatJson(actionTypes).EncodeUriComponent()
@@ -64,6 +86,11 @@ namespace SponsorBlock
hashPrefix = CryptoUtils.GetSha256(videoId).Left(4)
url = `${m.sponsorBlockConfig.serverAddress}${SponsorBlock.SKIP_SEGMENT_ENDPOINT}/${hashPrefix}?categories=${categoriesJson}&actionTypes=${actionTypesJson}`
response = HttpClient.Get(url).Await()
if not response.IsSuccess()
LogError("SponsorBlockService: GetSkipSegmentsForVideo failed", response.ErrorMessage())
return invalid
end if
json = response.Json()
if json = invalid
return invalid
@@ -78,13 +105,13 @@ namespace SponsorBlock
return invalid
end function
function PostSegmentViewed(segmentId as string) as void
function PostSegmentViewed(segmentId as string, cancellation = invalid as object) as void
if m.sponsorBlockConfig = invalid or StringUtils.IsNullOrEmpty(m.sponsorBlockConfig.serverAddress)
return
end if
url = `${m.sponsorBlockConfig.serverAddress}${SponsorBlock.VIEWED_SEGMENT_ENDPOINT}?UUID=${segmentId}`
HttpClient.Post(url, "").Await()
HttpClient.Post(url, "").Cancellation(cancellation).Await()
end function
function GenerateProgressBarBackground(segments as object, lengthSeconds as integer, path as string)
@@ -103,8 +130,8 @@ namespace SponsorBlock
continue for
end if
pixelStart = (segment.segment[0] / videoDuration) * width
pixelEnd = (segment.segment[1] / videoDuration) * width
pixelStart = (segment.start / videoDuration) * width
pixelEnd = (segment.end / videoDuration) * width
color = m.SegmentColor(segment.category)
' highlight's duration is zero, so it is not visble on the bar.
' Add a few pixels to see it
@@ -1,22 +1,135 @@
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
showNotifications = input.showNotifications
categories = input.categories
service = new SponsorBlock.SponsorBlockService()
skipSegments = service.GetSkipSegmentsForVideo(videoId)
barPath = invalid
skipSegments = ProcessSkipSegments(skipSegments, showNotifications, categories, service)
barPath = invalid
if skipSegments <> invalid
barPath = `tmp:/sponsorblock_bar.png`
service.GenerateProgressBarBackground(skipSegments, lengthSeconds, barPath)
end if
' Usually segment.start and segment.end are equal for poi_highlight
' We pad the duration for poi_highlight segments so that the chapter label
' can be visible when seeking in trickplay.
' Padding poi_highlight is done after generating the progress bar, so that the
' highlight is not too long in the bar.
skipSegments = PadHightlightSegment(skipSegments, lengthSeconds)
' Sort the segments by start time, so we can binary search them later.
if skipSegments <> invalid
skipSegments.SortBy("start")
end if
return {
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
if skipSegments = invalid
return invalid
end if
processedSegments = []
for each segment in skipSegments
' When a submission reaches the score of -2 or lower, it gets removed and will not appear anymore for users
' https://wiki.sponsor.ajay.app/w/FAQ#How_does_voting_work_and_what_do_certain_amounts_of_votes_mean?
if ValidInt(segment.votes) < -1
continue for
end if
option = GetOptionForSegment(segment, categories)
if option = SponsorBlock.SegmentAction.Disable
continue for
end if
segment.option = option
segment.showNotification = showNotifications and ShouldShowNotification(segment, categories)
if segment.showNotification
if option = SponsorBlock.SegmentAction.AutoSkip
segment.actionText = Tr(Locale.SponsorBlock.Skipped)
else if option = SponsorBlock.SegmentAction.ManualSkip
segment.actionText = Tr(Locale.SponsorBlock.PressOkToSkip)
else
segment.actionText = ""
end if
end if
segmentRange = segment["segment"]
segmentStart = segmentRange[0]
segmentEnd = segmentRange[1]
segment.Delete("segment")
segment.start = segmentStart
segment.end = segmentEnd
segment.title = Tr(ValidString(service.SegmentTitle(segment.category)))
segment.shortTitle = Tr(ValidString(service.SegmentShortTitle(segment.category)))
processedSegments.push(segment)
end for
return processedSegments
end function
function GetOptionForSegment(segment as object, categories as object) as string
category = categories[segment.category]
if not IsAssociativeArray(category)
return false
end if
option = category.option
if StringUtils.IsNullOrEmpty(option)
option = SponsorBlock.SegmentAction.Disable
end if
return option
end function
function ShouldShowNotification(segment as object, categories as object) as boolean
option = GetOptionForSegment(segment, categories)
return option = SponsorBlock.SegmentAction.AutoSkip or option = SponsorBlock.SegmentAction.ManualSkip
end function
function PadHightlightSegment(segments as object, lengthSeconds as integer) as object
for each segment in segments
if not segment.category = "poi_highlight"
continue for
end if
segmentPaddedDuration = 5
if lengthSeconds > 0
if lengthSeconds < 120
segmentPaddedDuration = 1
else if lengthSeconds < 300
segmentPaddedDuration = 2
else if lengthSeconds < 900
segmentPaddedDuration = 5
else
segmentPaddedDuration = 10
end if
end if
segmentDuration = Abs(segment.end - segment.start)
padding = Abs(segmentPaddedDuration - segmentDuration) / 2
segment.start = segment.start - padding
segment.end = segment.end + padding
end for
return segments
end function