From f27fc6c38a4ba013f67824f40ca8aed1e07c5f1e Mon Sep 17 00:00:00 2001 From: WebStreamr <210764791+webstreamr@users.noreply.github.com> Date: Sun, 25 May 2025 19:32:16 +0200 Subject: [PATCH] refactor: introduce type for CountryCode --- src/controller/StreamController.ts | 2 +- src/handler/CineHDPlus.ts | 4 ++-- src/handler/Eurostreaming.ts | 4 ++-- src/handler/Frembed.ts | 4 ++-- src/handler/FrenchCloud.ts | 4 ++-- src/handler/KinoKiste.ts | 4 ++-- src/handler/MeineCloud.ts | 4 ++-- src/handler/MostraGuarda.ts | 4 ++-- src/handler/VerHdLink.ts | 6 +++--- src/handler/types.ts | 4 ++-- src/types.ts | 4 +++- src/utils/StreamResolver.test.ts | 2 +- src/utils/manifest.ts | 16 ++++++++-------- 13 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/controller/StreamController.ts b/src/controller/StreamController.ts index 298f124..669ae4d 100644 --- a/src/controller/StreamController.ts +++ b/src/controller/StreamController.ts @@ -34,7 +34,7 @@ export class StreamController { this.logger.info(`Search stream for type "${type}" and id "${id}" for ip ${ctx.ip}`, ctx); - const handlers = this.handlers.filter(handler => handler.languages.filter(language => language in ctx.config).length); + const handlers = this.handlers.filter(handler => handler.countryCodes.filter(countryCode => countryCode in ctx.config).length); const streams = await this.streamResolver.resolve(ctx, handlers, type, id); diff --git a/src/handler/CineHDPlus.ts b/src/handler/CineHDPlus.ts index 96cb755..f704a87 100644 --- a/src/handler/CineHDPlus.ts +++ b/src/handler/CineHDPlus.ts @@ -2,7 +2,7 @@ import * as cheerio from 'cheerio'; import { Handler } from './types'; import { Fetcher, ImdbId, parseImdbId } from '../utils'; import { ExtractorRegistry } from '../extractor'; -import { Context } from '../types'; +import { Context, CountryCode } from '../types'; export class CineHDPlus implements Handler { readonly id = 'cinehdplus'; @@ -11,7 +11,7 @@ export class CineHDPlus implements Handler { readonly contentTypes = ['series']; - readonly languages = ['es', 'mx']; + readonly countryCodes: CountryCode[] = ['es', 'mx']; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; diff --git a/src/handler/Eurostreaming.ts b/src/handler/Eurostreaming.ts index 7edd5de..d1c4b48 100644 --- a/src/handler/Eurostreaming.ts +++ b/src/handler/Eurostreaming.ts @@ -2,7 +2,7 @@ import * as cheerio from 'cheerio'; import { Handler } from './types'; import { ImdbId, parseImdbId, Fetcher } from '../utils'; import { ExtractorRegistry } from '../extractor'; -import { Context } from '../types'; +import { Context, CountryCode } from '../types'; export class Eurostreaming implements Handler { readonly id = 'eurostreaming'; @@ -11,7 +11,7 @@ export class Eurostreaming implements Handler { readonly contentTypes = ['series']; - readonly languages = ['it']; + readonly countryCodes: CountryCode[] = ['it']; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; diff --git a/src/handler/Frembed.ts b/src/handler/Frembed.ts index 177e8d9..a4609f7 100644 --- a/src/handler/Frembed.ts +++ b/src/handler/Frembed.ts @@ -2,7 +2,7 @@ import axios from 'axios'; import { Handler } from './types'; import { parseImdbId, Fetcher, getTmdbIdFromImdbId } from '../utils'; import { ExtractorRegistry } from '../extractor'; -import { Context } from '../types'; +import { Context, CountryCode } from '../types'; export class Frembed implements Handler { readonly id = 'frembed'; @@ -11,7 +11,7 @@ export class Frembed implements Handler { readonly contentTypes = ['series']; - readonly languages = ['fr']; + readonly countryCodes: CountryCode[] = ['fr']; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; diff --git a/src/handler/FrenchCloud.ts b/src/handler/FrenchCloud.ts index 65f9468..2c9e707 100644 --- a/src/handler/FrenchCloud.ts +++ b/src/handler/FrenchCloud.ts @@ -2,7 +2,7 @@ import * as cheerio from 'cheerio'; import { Handler } from './types'; import { Fetcher, parseImdbId } from '../utils'; import { ExtractorRegistry } from '../extractor'; -import { Context } from '../types'; +import { Context, CountryCode } from '../types'; export class FrenchCloud implements Handler { readonly id = 'frenchcloud'; @@ -11,7 +11,7 @@ export class FrenchCloud implements Handler { readonly contentTypes = ['movie']; - readonly languages = ['fr']; + readonly countryCodes: CountryCode[] = ['fr']; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; diff --git a/src/handler/KinoKiste.ts b/src/handler/KinoKiste.ts index 963e1ce..9abf633 100644 --- a/src/handler/KinoKiste.ts +++ b/src/handler/KinoKiste.ts @@ -2,7 +2,7 @@ import * as cheerio from 'cheerio'; import { Handler } from './types'; import { ImdbId, parseImdbId, Fetcher } from '../utils'; import { ExtractorRegistry } from '../extractor'; -import { Context } from '../types'; +import { Context, CountryCode } from '../types'; export class KinoKiste implements Handler { readonly id = 'kinokiste'; @@ -11,7 +11,7 @@ export class KinoKiste implements Handler { readonly contentTypes = ['series']; - readonly languages = ['de']; + readonly countryCodes: CountryCode[] = ['de']; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; diff --git a/src/handler/MeineCloud.ts b/src/handler/MeineCloud.ts index 08d28b2..5607a61 100644 --- a/src/handler/MeineCloud.ts +++ b/src/handler/MeineCloud.ts @@ -2,7 +2,7 @@ import * as cheerio from 'cheerio'; import { Handler } from './types'; import { Fetcher, parseImdbId } from '../utils'; import { ExtractorRegistry } from '../extractor'; -import { Context } from '../types'; +import { Context, CountryCode } from '../types'; export class MeineCloud implements Handler { readonly id = 'meinecloud'; @@ -11,7 +11,7 @@ export class MeineCloud implements Handler { readonly contentTypes = ['movie']; - readonly languages = ['de']; + readonly countryCodes: CountryCode[] = ['de']; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; diff --git a/src/handler/MostraGuarda.ts b/src/handler/MostraGuarda.ts index 9c9bab2..4518991 100644 --- a/src/handler/MostraGuarda.ts +++ b/src/handler/MostraGuarda.ts @@ -2,7 +2,7 @@ import * as cheerio from 'cheerio'; import { Handler } from './types'; import { Fetcher, parseImdbId } from '../utils'; import { ExtractorRegistry } from '../extractor'; -import { Context } from '../types'; +import { Context, CountryCode } from '../types'; export class MostraGuarda implements Handler { readonly id = 'mostraguarda'; @@ -11,7 +11,7 @@ export class MostraGuarda implements Handler { readonly contentTypes = ['movie']; - readonly languages = ['it']; + readonly countryCodes: CountryCode[] = ['it']; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; diff --git a/src/handler/VerHdLink.ts b/src/handler/VerHdLink.ts index 2a085f6..1023d95 100644 --- a/src/handler/VerHdLink.ts +++ b/src/handler/VerHdLink.ts @@ -2,7 +2,7 @@ import * as cheerio from 'cheerio'; import { Handler } from './types'; import { Fetcher, parseImdbId } from '../utils'; import { ExtractorRegistry } from '../extractor'; -import { Context } from '../types'; +import { Context, CountryCode } from '../types'; export class VerHdLink implements Handler { readonly id = 'verhdlink'; @@ -11,7 +11,7 @@ export class VerHdLink implements Handler { readonly contentTypes = ['movie']; - readonly languages = ['es', 'mx']; + readonly countryCodes: CountryCode[] = ['es', 'mx']; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; @@ -33,7 +33,7 @@ export class VerHdLink implements Handler { return Promise.all( $('._player-mirrors') .map((_i, el) => { - let countryCode = undefined; + let countryCode: CountryCode; if ($(el).hasClass('latino') && 'mx' in ctx.config) { countryCode = 'mx'; } else if ($(el).hasClass('castellano') && 'es' in ctx.config) { diff --git a/src/handler/types.ts b/src/handler/types.ts index b0b96a6..78e4dd0 100644 --- a/src/handler/types.ts +++ b/src/handler/types.ts @@ -1,4 +1,4 @@ -import { Context, UrlResult } from '../types'; +import { Context, CountryCode, UrlResult } from '../types'; export interface Handler { readonly id: string; @@ -7,7 +7,7 @@ export interface Handler { readonly contentTypes: string[]; - readonly languages: string[]; + readonly countryCodes: CountryCode[]; readonly handle: (ctx: Context, type: string, id: string) => Promise<(UrlResult | undefined)[]>; } diff --git a/src/types.ts b/src/types.ts index 3efd692..0443318 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,9 +10,11 @@ export type ManifestWithConfig = Manifest & { config: ManifestConfig[] }; export type Config = Record; +export type CountryCode = 'de' | 'en' | 'es' | 'fr' | 'it' | 'mx'; + export interface Meta { bytes?: number; - countryCode: string; + countryCode: CountryCode; height?: number; } diff --git a/src/utils/StreamResolver.test.ts b/src/utils/StreamResolver.test.ts index 844befe..7b09a93 100644 --- a/src/utils/StreamResolver.test.ts +++ b/src/utils/StreamResolver.test.ts @@ -62,7 +62,7 @@ describe('resolve', () => { id: 'mockhandler', label: 'MockHandler', contentTypes: ['movie'], - languages: ['de'], + countryCodes: ['de'], handle: jest.fn().mockRejectedValue(new NotFoundError()), }; diff --git a/src/utils/manifest.ts b/src/utils/manifest.ts index b4ca9a5..323228d 100644 --- a/src/utils/manifest.ts +++ b/src/utils/manifest.ts @@ -30,20 +30,20 @@ export const buildManifest = (handlers: Handler[], config: Config): ManifestWith }, }; - const languageHandlers: Record = {}; + const countryCodeHandlers: Record = {}; handlers.forEach((handler) => { - handler.languages.forEach(language => languageHandlers[language] = [...(languageHandlers[language] ?? []), handler]); + handler.countryCodes.forEach(countryCode => countryCodeHandlers[countryCode] = [...(countryCodeHandlers[countryCode] ?? []), handler]); }); - const sortedLanguageHandlers = Object.entries(languageHandlers) - .sort(([languageA], [languageB]) => languageA.localeCompare(languageB)); + const sortedLanguageHandlers = Object.entries(countryCodeHandlers) + .sort(([countryCodeA], [countryCodeB]) => countryCodeA.localeCompare(countryCodeB)); - for (const [language, handlers] of sortedLanguageHandlers) { + for (const [countryCode, handlers] of sortedLanguageHandlers) { manifest.config.push({ - key: language, + key: countryCode, type: 'checkbox', - title: `${flag(language)} (${handlers.map(handler => handler.label).join(', ')})`, - ...(language in config && { default: 'checked' }), + title: `${flag(countryCode)} (${handlers.map(handler => handler.label).join(', ')})`, + ...(countryCode in config && { default: 'checked' }), }); }