Files
playlet-piped/playlet-lib/src/source/services/SponsorBlock.bs
T
Brahim Hadricheandgithub-action linter e0020ffa66 SponsorBlock Notification (#47)
* SponsorBlock Notification

* Lint fix

* Reuse animation for multiple skips

* Small change

* Rename color utils

* Lint fix

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
2023-02-10 15:20:08 -05:00

106 lines
3.9 KiB
Plaintext

import "pkg:/source/roku_modules/rokurequests/Requests.brs"
import "pkg:/source/utils/WebUtils.bs"
import "pkg:/source/utils/Crypto.bs"
import "pkg:/source/utils/ColorUtils.bs"
namespace SponsorBlock
const API_URL = "https://sponsor.ajay.app"
const SKIP_SEGMENT_ENDPOINT = "/api/skipSegments"
const VIEWED_SEGMENT_ENDPOINT = "/api/viewedVideoSponsorTime"
function GetSponsorBlockConfig() as object
if m.sponsorBlockConfig = invalid
m.sponsorBlockConfig = ParseJson(ReadAsciiFile("libpkg:/config/sponsorblock_config.json"))
end if
return m.sponsorBlockConfig
end function
function SegmentColor(category as string) as integer
config = SponsorBlock.GetSponsorBlockConfig()
if config = invalid
return 0
end if
if config.categories.doesexist(category)
color = config.categories[category].color
return ColorUtils.HexToInt(color)
end if
return 0
end function
function SegmentTitle(category as string) as string
config = SponsorBlock.GetSponsorBlockConfig()
if config = invalid
return ""
end if
if config.categories.doesexist(category)
return config.categories[category].title
end if
return ""
end function
function SegmentShortTitle(category as string) as string
config = SponsorBlock.GetSponsorBlockConfig()
if config = invalid
return ""
end if
if config.categories.doesexist(category)
return config.categories[category].short_title
end if
return ""
end function
function GetSkipSegmentsForVideo(videoId as string) as object
categories = ["sponsor", "selfpromo", "interaction", "intro", "outro", "preview", "music_offtopic", "poi_highlight", "chapter", "filler", "exclusive_access"]
actionTypes = ["skip", "mute", "chapter", "full", "poi"]
categoriesJson = FormatJson(categories).EncodeUriComponent()
actionTypesJson = FormatJson(actionTypes).EncodeUriComponent()
' From SponsorBlock docs: It should be the first 4 - 32 characters (4 is recommended).
hashPrefix = GetSha256(videoId).Left(4)
url = `${SponsorBlock.API_URL}${SponsorBlock.SKIP_SEGMENT_ENDPOINT}/${hashPrefix}?categories=${categoriesJson}&actionTypes=${actionTypesJson}`
response = Requests().get(url)
if response.statuscode = 200
results = response.json
for each item in results
if item.videoId = videoId
return item.segments
end if
end for
end if
return invalid
end function
function PostSegmentViewed(segmentId as string) as boolean
url = `${SponsorBlock.API_URL}${SponsorBlock.VIEWED_SEGMENT_ENDPOINT}?UUID=${segmentId}`
response = Requests().post(url, { parseJson: false })
return response.statuscode = 200
end function
function GenerateProgressBarBackground(segments as object, 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
pixelStart = (segment.segment[0] / segment.videoDuration) * width
pixelEnd = (segment.segment[1] / segment.videoDuration) * width
color = SponsorBlock.SegmentColor(segment.category)
' highlight's duration is zero, so it is not visble on the bar.
' Add a couple of pixels to see it
if pixelStart = pixelEnd
pixelEnd += 2
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 namespace