Types adjustment, extract captions to a function

This commit is contained in:
teddyHV11
2024-04-10 07:24:53 +03:00
parent 5b836a4839
commit 5d6b93385e
3 changed files with 43 additions and 51 deletions

View File

@@ -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)
}

View File

@@ -2,6 +2,7 @@ import { flags } from '@/entrypoint/utils/targets';
import { makeSourcerer } from '@/providers/base'; import { makeSourcerer } from '@/providers/base';
import { Caption, removeDuplicatedLanguages } from '@/providers/captions'; import { Caption, removeDuplicatedLanguages } from '@/providers/captions';
import { NotFoundError } from '@/utils/errors'; import { NotFoundError } from '@/utils/errors';
import { getCaptions } from './captions';
import { Season, Subtitle } from './types'; import { Season, Subtitle } from './types';
@@ -39,34 +40,15 @@ export const insertunitScraper = makeSourcerer({
if (!currentEpisode?.hls) throw new NotFoundError('No result found'); 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); ctx.progress(80);
if (currentEpisode.cc != null) { 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); ctx.progress(95);
@@ -105,29 +87,7 @@ export const insertunitScraper = makeSourcerer({
if (subtitleJSONData != null && subtitleJSONData[1] != null) { if (subtitleJSONData != null && subtitleJSONData[1] != null) {
const subtitleData = JSON.parse(subtitleJSONData[1]); const subtitleData = JSON.parse(subtitleJSONData[1]);
let subtitle: Subtitle; captions = await getCaptions(subtitleData)
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);
} }
ctx.progress(90); ctx.progress(90);

View File

@@ -4,14 +4,14 @@ export interface Episode {
videoKey: string; videoKey: string;
hls: string; hls: string;
audio: { audio: {
names: []; names: string[];
order: []; order: number[];
} }
cc: [] cc: Subtitle[];
duration: number; duration: number;
title: string; title: string;
download: string; download: string;
sections: [] sections: string[]
poster: string; poster: string;
preview: { preview: {
src: string; src: string;