From 5d6b93385ea5da5a7c6481f4e90f065a604c98f5 Mon Sep 17 00:00:00 2001 From: teddyHV11 Date: Wed, 10 Apr 2024 07:24:53 +0300 Subject: [PATCH] Types adjustment, extract captions to a function --- src/providers/sources/insertunit/captions.ts | 32 +++++++++++ .../sources/insertunit/insertunit.ts | 54 +++---------------- src/providers/sources/insertunit/types.ts | 8 +-- 3 files changed, 43 insertions(+), 51 deletions(-) create mode 100644 src/providers/sources/insertunit/captions.ts diff --git a/src/providers/sources/insertunit/captions.ts b/src/providers/sources/insertunit/captions.ts new file mode 100644 index 0000000..92ac7d9 --- /dev/null +++ b/src/providers/sources/insertunit/captions.ts @@ -0,0 +1,32 @@ +import { Caption } from "@/providers/captions"; +import { Subtitle } from "./types"; + +import { removeDuplicatedLanguages } from "@/providers/captions"; + +export async function getCaptions(data: Subtitle[]) { + let captions: Caption[] = []; + let subtitle: Subtitle; + for (subtitle of data) { + let language = ''; + + if (subtitle.name.includes('Рус')) { + language = 'ru'; + } else if (subtitle.name.includes('Укр')) { + language = 'uk'; + } else if (subtitle.name.includes('Eng')) { + language = 'en'; + } else { + continue; + } + + captions.push({ + id: subtitle.url, + url: subtitle.url, + language, + type: 'vtt', + hasCorsRestrictions: false, + }); + } + captions = removeDuplicatedLanguages(captions); + return(captions) +} \ No newline at end of file diff --git a/src/providers/sources/insertunit/insertunit.ts b/src/providers/sources/insertunit/insertunit.ts index 47455ae..3cecf52 100644 --- a/src/providers/sources/insertunit/insertunit.ts +++ b/src/providers/sources/insertunit/insertunit.ts @@ -2,6 +2,7 @@ import { flags } from '@/entrypoint/utils/targets'; import { makeSourcerer } from '@/providers/base'; import { Caption, removeDuplicatedLanguages } from '@/providers/captions'; import { NotFoundError } from '@/utils/errors'; +import { getCaptions } from './captions'; import { Season, Subtitle } from './types'; @@ -38,35 +39,16 @@ export const insertunitScraper = makeSourcerer({ ); if (!currentEpisode?.hls) throw new NotFoundError('No result found'); + + let captions: Caption[] = [] - let captions: Caption[] = []; + if (currentEpisode.cc != null) { + captions = await getCaptions(currentEpisode.cc) + } ctx.progress(80); if (currentEpisode.cc != null) { - let subtitle: Subtitle; - for (subtitle of currentEpisode.cc) { - let language = ''; - - if (subtitle.name.includes('Рус')) { - language = 'ru'; - } else if (subtitle.name.includes('Укр')) { - language = 'uk'; - } else if (subtitle.name.includes('Eng')) { - language = 'en'; - } else { - continue; - } - - captions.push({ - id: subtitle.url, - url: subtitle.url, - language, - type: 'vtt', - hasCorsRestrictions: false, - }); - } - captions = removeDuplicatedLanguages(captions); } ctx.progress(95); @@ -105,29 +87,7 @@ export const insertunitScraper = makeSourcerer({ if (subtitleJSONData != null && subtitleJSONData[1] != null) { const subtitleData = JSON.parse(subtitleJSONData[1]); - let subtitle: Subtitle; - for (subtitle of subtitleData as Subtitle[]) { - let language = ''; - - if (subtitle.name.includes('Рус')) { - language = 'ru'; - } else if (subtitle.name.includes('Укр')) { - language = 'uk'; - } else if (subtitle.name.includes('Eng')) { - language = 'en'; - } else { - continue; - } - - captions.push({ - id: subtitle.url, - url: subtitle.url, - language, - type: 'vtt', - hasCorsRestrictions: false, - }); - } - captions = removeDuplicatedLanguages(captions); + captions = await getCaptions(subtitleData) } ctx.progress(90); diff --git a/src/providers/sources/insertunit/types.ts b/src/providers/sources/insertunit/types.ts index 0649275..282c3c4 100644 --- a/src/providers/sources/insertunit/types.ts +++ b/src/providers/sources/insertunit/types.ts @@ -4,14 +4,14 @@ export interface Episode { videoKey: string; hls: string; audio: { - names: []; - order: []; + names: string[]; + order: number[]; } - cc: [] + cc: Subtitle[]; duration: number; title: string; download: string; - sections: [] + sections: string[] poster: string; preview: { src: string;