diff options
Diffstat (limited to 'src/utils/exporter.ts')
-rw-r--r-- | src/utils/exporter.ts | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/src/utils/exporter.ts b/src/utils/exporter.ts index 16b970f3..a5871b65 100644 --- a/src/utils/exporter.ts +++ b/src/utils/exporter.ts @@ -1,14 +1,15 @@ import { ActionType, Category, SegmentUUID, SponsorSourceType, SponsorTime } from "../types"; import { shortCategoryName } from "./categoryUtils"; -import { GenericUtils } from "./genericUtils"; import * as CompileConfig from "../../config.json"; +import { getFormattedTime, getFormattedTimeToSeconds } from "../../maze-utils/src/formating"; +import { generateUserID } from "../../maze-utils/src/setup"; const inTest = typeof chrome === "undefined"; const chapterNames = CompileConfig.categoryList.filter((code) => code !== "chapter") .map((code) => ({ code, - name: !inTest ? chrome.i18n.getMessage("category_" + code) : code + names: !inTest ? [chrome.i18n.getMessage("category_" + code), shortCategoryName(code)] : [code] })); export function exportTimes(segments: SponsorTime[]): string { @@ -26,9 +27,9 @@ export function exportTimes(segments: SponsorTime[]): string { function exportTime(segment: SponsorTime): string { const name = segment.description || shortCategoryName(segment.category); - return `${GenericUtils.getFormattedTime(segment.segment[0], true)}${ + return `${getFormattedTime(segment.segment[0], true)}${ segment.segment[1] && segment.segment[0] !== segment.segment[1] - ? ` - ${GenericUtils.getFormattedTime(segment.segment[1], true)}` : ""} ${name}`; + ? ` - ${getFormattedTime(segment.segment[1], true)}` : ""} ${name}`; } export function importTimes(data: string, videoDuration: number): SponsorTime[] { @@ -37,25 +38,31 @@ export function importTimes(data: string, videoDuration: number): SponsorTime[] for (const line of lines) { const match = line.match(/(?:((?:\d+:)?\d+:\d+)+(?:\.\d+)?)|(?:\d+(?=s| second))/g); if (match) { - const startTime = GenericUtils.getFormattedTimeToSeconds(match[0]); + const startTime = getFormattedTimeToSeconds(match[0]); if (startTime !== null) { - const specialCharsMatcher = /^(?:\s+seconds?)?[-:()\s]*|(?:\s+at)?[-:()\s]+$/g - const titleLeft = line.split(match[0])[0].replace(specialCharsMatcher, ""); + // Remove "seconds", "at", special characters, and ")" if there was a "(" + const specialCharMatchers = [{ + matcher: /^(?:\s+seconds?)?[-:()\s]*|(?:\s+at)?[-:(\s]+$/g + }, { + matcher: /[-:()\s]*$/g, + condition: (value) => !!value.match(/^\s*\(/) + }]; + const titleLeft = removeIf(line.split(match[0])[0], specialCharMatchers); let titleRight = null; const split2 = line.split(match[1] || match[0]); - titleRight = split2[split2.length - 1].replace(specialCharsMatcher, ""); + titleRight = removeIf(split2[split2.length - 1], specialCharMatchers) const title = titleLeft?.length > titleRight?.length ? titleLeft : titleRight; if (title) { - const determinedCategory = chapterNames.find(c => c.name === title)?.code as Category; + const determinedCategory = chapterNames.find(c => c.names.includes(title))?.code as Category; const segment: SponsorTime = { - segment: [startTime, GenericUtils.getFormattedTimeToSeconds(match[1])], + segment: [startTime, getFormattedTimeToSeconds(match[1])], category: determinedCategory ?? ("chapter" as Category), actionType: determinedCategory ? ActionType.Skip : ActionType.Chapter, description: title, source: SponsorSourceType.Local, - UUID: GenericUtils.generateUserID() as SegmentUUID + UUID: generateUserID() as SegmentUUID }; if (result.length > 0 && result[result.length - 1].segment[1] === null) { @@ -75,6 +82,17 @@ export function importTimes(data: string, videoDuration: number): SponsorTime[] return result; } +function removeIf(value: string, matchers: Array<{ matcher: RegExp; condition?: (value: string) => boolean }>): string { + let result = value; + for (const matcher of matchers) { + if (!matcher.condition || matcher.condition(value)) { + result = result.replace(matcher.matcher, ""); + } + } + + return result; +} + export function exportTimesAsHashParam(segments: SponsorTime[]): string { const hashparamSegments = segments.map(segment => ({ actionType: segment.actionType, @@ -85,3 +103,8 @@ export function exportTimesAsHashParam(segments: SponsorTime[]): string { return `#segments=${JSON.stringify(hashparamSegments)}`; } + + +export function normalizeChapterName(description: string): string { + return description.toLowerCase().replace(/\.|:|-/g, "").replace(/\s+/g, " "); +}
\ No newline at end of file |