Cleanup SponsorBlock video id hashing (#4384)

This commit is contained in:
absidue 2023-11-26 01:30:56 +01:00 committed by GitHub
parent 6af85b3726
commit cb883f254f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 7 deletions

View File

@ -1,17 +1,17 @@
import store from '../store/index'
async function getVideoHash(videoId) {
const videoIdBuffer = new TextEncoder().encode(videoId)
const hashBuffer = await crypto.subtle.digest('SHA-256', videoIdBuffer)
const hashArray = Array.from(new Uint8Array(hashBuffer))
const hashArray = new Uint8Array(hashBuffer)
return hashArray
.map(byte => byte.toString(16).padStart(2, '0'))
.slice(0, 4)
.join('')
return hashArray[0].toString(16).padStart(2, '0') +
hashArray[1].toString(16).padStart(2, '0')
}
export async function sponsorBlockSkipSegments(videoId, categories) {
const videoIdHashPrefix = (await getVideoHash(videoId)).substring(0, 4)
const videoIdHashPrefix = await getVideoHash(videoId)
const requestUrl = `${store.getters.getSponsorBlockUrl}/api/skipSegments/${videoIdHashPrefix}?categories=${JSON.stringify(categories)}`
try {
@ -22,6 +22,11 @@ export async function sponsorBlockSkipSegments(videoId, categories) {
return []
}
// Sometimes the sponsor block server goes down or returns other errors
if (!response.ok) {
throw new Error(await response.text())
}
const json = await response.json()
return json
.filter((result) => result.videoID === videoId)
@ -33,7 +38,7 @@ export async function sponsorBlockSkipSegments(videoId, categories) {
}
export async function deArrowData(videoId) {
const videoIdHashPrefix = (await getVideoHash(videoId)).substring(0, 4)
const videoIdHashPrefix = await getVideoHash(videoId)
const requestUrl = `${store.getters.getSponsorBlockUrl}/api/branding/${videoIdHashPrefix}`
try {