-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
92 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,78 @@ | ||
import Config from "../config"; | ||
import { ActionType, SponsorSourceType, SponsorTime, SponsorTimeHashedID, VideoID } from "../types"; | ||
import { DataCache } from "../utils/cache"; | ||
import { getVideoIDHash, HashedValue } from "../utils/hash"; | ||
import { asyncRequestToServer } from "./requests"; | ||
|
||
const segmentCache = new DataCache<VideoID, SegmentResponse>(() => { | ||
return { | ||
segments: null, | ||
status: 200, | ||
}; | ||
}, 50); | ||
|
||
export interface SegmentResponse { | ||
segments: SponsorTime[] | null; | ||
status: number; | ||
} | ||
|
||
export async function getSegmentsByHash( | ||
hashPrefix: string, | ||
extraRequestData: Record<string, unknown>, | ||
ignoreServerCache: boolean | ||
ignoreCache: boolean | ||
) { | ||
const response = await asyncRequestToServer( | ||
"GET", | ||
"/api/skipSegments/" + hashPrefix, | ||
extraRequestData, | ||
ignoreServerCache | ||
ignoreCache | ||
); | ||
|
||
return response; | ||
} | ||
|
||
export async function getSegmentsByVideoID( | ||
videoID: string, | ||
extraRequestData: Record<string, unknown> = {}, | ||
ignoreCache: boolean = false | ||
): Promise<SegmentResponse> { | ||
if (ignoreCache) { | ||
segmentCache.delete(videoID); | ||
} | ||
const cachedData = segmentCache.getFromCache(videoID); | ||
if (cachedData) { | ||
return cachedData; | ||
} | ||
|
||
const categories: string[] = Config.config.categorySelections.map((category) => category.name); | ||
const hashPrefix = (await getVideoIDHash(videoID)).slice(0, 4) as VideoID & HashedValue; | ||
const response = await getSegmentsByHash(hashPrefix, extraRequestData, ignoreCache); | ||
|
||
const segments = JSON.parse(response?.responseText) | ||
?.filter((video: SponsorTimeHashedID) => video.videoID === videoID) | ||
?.map((video: SponsorTimeHashedID) => video.segments)?.[0] | ||
?.filter( | ||
(segment: SponsorTime) => | ||
getEnabledActionTypes().includes(segment.actionType) && categories.includes(segment.category) | ||
) | ||
?.map((segment: SponsorTime) => ({ | ||
...segment, | ||
source: SponsorSourceType.Server, | ||
})) | ||
?.sort((a: SponsorTime, b: SponsorTime) => a.segment[0] - b.segment[0]); | ||
|
||
console.log(segments); | ||
return null; | ||
} | ||
|
||
function getEnabledActionTypes(forceFullVideo = false): ActionType[] { | ||
const actionTypes = [ActionType.Skip, ActionType.Poi]; | ||
if (Config.config.muteSegments) { | ||
actionTypes.push(ActionType.Mute); | ||
} | ||
if (Config.config.fullVideoSegments || forceFullVideo) { | ||
actionTypes.push(ActionType.Full); | ||
} | ||
|
||
return actionTypes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters