From 0f2fe16583405bba0b99ef82f2a454e0d4a7689d Mon Sep 17 00:00:00 2001 From: absidue <48293849+absidue@users.noreply.github.com> Date: Sat, 8 Oct 2022 22:23:14 +0200 Subject: [PATCH] Move fetching of the sponsorblock segments out of the store (#2684) --- .../ft-video-player/ft-video-player.js | 42 +++++++------- src/renderer/helpers/sponsorblock.js | 32 +++++++++++ src/renderer/store/modules/sponsorblock.js | 55 ------------------- 3 files changed, 52 insertions(+), 77 deletions(-) create mode 100644 src/renderer/helpers/sponsorblock.js delete mode 100644 src/renderer/store/modules/sponsorblock.js diff --git a/src/renderer/components/ft-video-player/ft-video-player.js b/src/renderer/components/ft-video-player/ft-video-player.js index 19b9d54f..307146a4 100644 --- a/src/renderer/components/ft-video-player/ft-video-player.js +++ b/src/renderer/components/ft-video-player/ft-video-player.js @@ -13,6 +13,7 @@ import 'videojs-contrib-quality-levels' import 'videojs-http-source-selector' import { IpcChannels } from '../../../constants' +import { sponsorBlockSkipSegments } from '../../helpers/sponsorblock' export default Vue.extend({ name: 'FtVideoPlayer', @@ -547,32 +548,30 @@ export default Vue.extend({ }, initializeSponsorBlock() { - this.sponsorBlockSkipSegments({ - videoId: this.videoId, - categories: this.sponsorSkips.seekBar - }).then((skipSegments) => { - if (skipSegments.length === 0) { - return - } + sponsorBlockSkipSegments(this.videoId, this.sponsorSkips.seekBar) + .then((skipSegments) => { + if (skipSegments.length === 0) { + return + } - this.player.ready(() => { - this.player.on('timeupdate', () => { - this.skipSponsorBlocks(skipSegments) - }) + this.player.ready(() => { + this.player.on('timeupdate', () => { + this.skipSponsorBlocks(skipSegments) + }) - skipSegments.forEach(({ - category, - segment: [startTime, endTime] - }) => { - this.addSponsorBlockMarker({ - time: startTime, - duration: endTime - startTime, - color: 'var(--primary-color)', - category: category + skipSegments.forEach(({ + category, + segment: [startTime, endTime] + }) => { + this.addSponsorBlockMarker({ + time: startTime, + duration: endTime - startTime, + color: 'var(--primary-color)', + category: category + }) }) }) }) - }) }, skipSponsorBlocks(skipSegments) { @@ -1927,7 +1926,6 @@ export default Vue.extend({ 'calculateColorLuminance', 'updateDefaultCaptionSettings', 'showToast', - 'sponsorBlockSkipSegments', 'parseScreenshotCustomFileName', 'updateScreenshotFolderPath', 'getPicturesPath', diff --git a/src/renderer/helpers/sponsorblock.js b/src/renderer/helpers/sponsorblock.js new file mode 100644 index 00000000..1f1b4a67 --- /dev/null +++ b/src/renderer/helpers/sponsorblock.js @@ -0,0 +1,32 @@ +import store from '../store/index' + +export async function sponsorBlockSkipSegments(videoId, categories) { + const videoIdBuffer = new TextEncoder().encode(videoId) + + const hashBuffer = await crypto.subtle.digest('SHA-256', videoIdBuffer) + const hashArray = Array.from(new Uint8Array(hashBuffer)) + + const videoIdHashPrefix = hashArray + .map(byte => byte.toString(16).padStart(2, '0')) + .slice(0, 4) + .join('') + + const requestUrl = `${store.getters.getSponsorBlockUrl}/api/skipSegments/${videoIdHashPrefix}?categories=${JSON.stringify(categories)}` + + try { + const response = await fetch(requestUrl) + + // 404 means that there are no segments registered for the video + if (response.status === 404) { + return [] + } + + const json = await response.json() + return json + .filter((result) => result.videoID === videoId) + .flatMap((result) => result.segments) + } catch (error) { + console.error('failed to fetch SponsorBlock segments', requestUrl, error) + throw error + } +} diff --git a/src/renderer/store/modules/sponsorblock.js b/src/renderer/store/modules/sponsorblock.js deleted file mode 100644 index bff09468..00000000 --- a/src/renderer/store/modules/sponsorblock.js +++ /dev/null @@ -1,55 +0,0 @@ -const state = {} -const getters = {} - -const actions = { - sponsorBlockSkipSegments ({ rootState }, { videoId, categories }) { - return new Promise((resolve, reject) => { - const videoIdBuffer = new TextEncoder().encode(videoId) - - crypto.subtle.digest('SHA-256', videoIdBuffer).then((hashBuffer) => { - const hashArray = Array.from(new Uint8Array(hashBuffer)) - - const videoIdHashPrefix = hashArray - .map(byte => byte.toString(16).padStart(2, '0')) - .slice(0, 4) - .join('') - - const requestUrl = `${rootState.settings.sponsorBlockUrl}/api/skipSegments/${videoIdHashPrefix}?categories=${JSON.stringify(categories)}` - - fetch(requestUrl) - .then((response) => { - // 404 means that there are no segments registered for the video - if (response.status === 404) { - resolve([]) - return - } - - response.json() - .then((json) => { - const segments = json - .filter((result) => result.videoID === videoId) - .flatMap((result) => result.segments) - resolve(segments) - }) - .catch((error) => { - console.error('failed to fetch SponsorBlock segments', requestUrl, error) - reject(error) - }) - }) - .catch((error) => { - console.error('failed to fetch SponsorBlock segments', requestUrl, error) - reject(error) - }) - }) - }) - } -} - -const mutations = {} - -export default { - state, - getters, - actions, - mutations -}