Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup SponsorBlock video id hashing #4384

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/renderer/helpers/sponsorblock.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down