Subtitle label conversion

This commit is contained in:
mrjvs
2023-11-18 23:14:06 +01:00
parent fe4882b43e
commit ab5dcc7b42
4 changed files with 22 additions and 3 deletions

View File

@@ -79,6 +79,7 @@
"cheerio": "^1.0.0-rc.12", "cheerio": "^1.0.0-rc.12",
"crypto-js": "^4.1.1", "crypto-js": "^4.1.1",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"iso-639-1": "^3.1.0",
"nanoid": "^3.3.6", "nanoid": "^3.3.6",
"node-fetch": "^2.7.0", "node-fetch": "^2.7.0",
"unpacker": "^1.0.1" "unpacker": "^1.0.1"

View File

@@ -1,3 +1,5 @@
import ISO6391 from 'iso-639-1';
export const captionTypes = { export const captionTypes = {
srt: 'srt', srt: 'srt',
vtt: 'vtt', vtt: 'vtt',
@@ -17,3 +19,14 @@ export function getCaptionTypeFromUrl(url: string): CaptionType | null {
if (!type) return null; if (!type) return null;
return type; return type;
} }
export function labelToLanguageCode(label: string): string | null {
const code = ISO6391.getCode(label);
if (code.length === 0) return null;
return code;
}
export function isValidLanguageCode(code: string | null): boolean {
if (!code) return false;
return ISO6391.validate(code);
}

View File

@@ -2,7 +2,7 @@ import crypto from 'crypto-js';
import { flags } from '@/main/targets'; import { flags } from '@/main/targets';
import { makeEmbed } from '@/providers/base'; import { makeEmbed } from '@/providers/base';
import { Caption, getCaptionTypeFromUrl } from '@/providers/captions'; import { Caption, getCaptionTypeFromUrl, labelToLanguageCode } from '@/providers/captions';
const { AES, enc } = crypto; const { AES, enc } = crypto;
@@ -102,8 +102,10 @@ export const upcloudScraper = makeEmbed({
if (track.kind !== 'captions') return; if (track.kind !== 'captions') return;
const type = getCaptionTypeFromUrl(track.file); const type = getCaptionTypeFromUrl(track.file);
if (!type) return; if (!type) return;
const language = labelToLanguageCode(track.label);
if (!language) return;
captions.push({ captions.push({
language: track.label, // TODO Turn language name into ISO code language,
hasCorsRestrictions: false, hasCorsRestrictions: false,
type, type,
url: track.file, url: track.file,

View File

@@ -1,4 +1,4 @@
import { Caption, getCaptionTypeFromUrl } from '@/providers/captions'; import { Caption, getCaptionTypeFromUrl, isValidLanguageCode } from '@/providers/captions';
import { sendRequest } from '@/providers/sources/superstream/sendRequest'; import { sendRequest } from '@/providers/sources/superstream/sendRequest';
import { ScrapeContext } from '@/utils/context'; import { ScrapeContext } from '@/utils/context';
@@ -42,6 +42,9 @@ export async function getSubtitles(
const subtitleType = getCaptionTypeFromUrl(subtitle.file_path); const subtitleType = getCaptionTypeFromUrl(subtitle.file_path);
if (!subtitleType) return; if (!subtitleType) return;
const validCode = isValidLanguageCode(subtitle.lang);
if (!validCode) return;
output.push({ output.push({
language: subtitle.lang, language: subtitle.lang,
hasCorsRestrictions: true, hasCorsRestrictions: true,