Files
playlet-piped/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs
T

156 lines
6.2 KiB
Plaintext

import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/ColorUtils.bs"
import "pkg:/source/utils/CryptoUtils.bs"
namespace SponsorBlock
const SKIP_SEGMENT_ENDPOINT = "/api/skipSegments"
const VIEWED_SEGMENT_ENDPOINT = "/api/viewedVideoSponsorTime"
class SponsorBlockService
function new()
' TODO:P2 this is being read and parsed on every skipped segment. It should be cached.
m.sponsorBlockConfig = StringUtils.ParseJsonSafe(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
end if
if not m.sponsorBlockConfig.categories.doesexist(category)
return 0
end if
color = m.sponsorBlockConfig.categories[category].color
return ColorUtils.HexToInt(color)
end function
function SegmentTitle(category as string) as string
if m.sponsorBlockConfig = invalid
return category
end if
if not m.sponsorBlockConfig.categories.doesexist(category)
return category
end if
return m.sponsorBlockConfig.categories[category].title
end function
function SegmentShortTitle(category as string) as string
if m.sponsorBlockConfig = invalid
return category
end if
if not m.sponsorBlockConfig.categories.doesexist(category)
return category
end if
return m.sponsorBlockConfig.categories[category].short_title
end function
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
' 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()
' 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).Cancellation(cancellation).Await()
if not response.IsSuccess()
if response.IsCancelled()
LogErrorNoTelemetry(response.ErrorMessage())
else if response.StatusCode() >= 500
LogErrorNoTelemetry("SponsorBlock server error:", response.StatusCode())
else
LogError(response.ErrorMessage())
end if
return invalid
end if
json = response.Json()
if json = invalid
return invalid
end if
for each item in json
if item.videoId = videoId
return item.segments
end if
end for
return invalid
end function
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, "").Cancellation(cancellation).Await()
end function
function GenerateProgressBarBackground(segments as object, lengthSeconds as integer, path as string)
bar = CreateObject("roBitmap", { width: 1000, height: 20, AlphaEnable: true })
bar.Clear(&hFFFFFF80)
width = bar.GetWidth()
height = bar.GetHeight()
for each segment in segments
videoDuration = lengthSeconds
if videoDuration = 0 or videoDuration = invalid
' We should ignore segment.videoDuration https://github.com/iBicha/playlet/pull/23#issuecomment-1431520676
' But in case where we don't have lengthSeconds from Invidious, it's better than nothing
videoDuration = segment.videoDuration
end if
if videoDuration = 0 or videoDuration = invalid
continue for
end if
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
if pixelStart = pixelEnd
pixelEnd += 5
end if
bar.DrawRect(pixelStart, 0, pixelEnd - pixelStart, height, color)
end for
bar.Finish()
buffer = bar.GetPng(0, 0, width, height)
buffer.WriteFile(path)
end function
end class
end namespace