From 250d6a13233867041e2ab67d9aa6d983bde52589 Mon Sep 17 00:00:00 2001 From: WebStreamr <210764791+webstreamr@users.noreply.github.com> Date: Thu, 12 Jun 2025 15:56:33 +0000 Subject: [PATCH] refactor: make CountryCode an enum --- src/extractor/ExtractorRegistry.test.ts | 19 ++++++++++--------- src/handler/CineHDPlus.ts | 4 ++-- src/handler/Eurostreaming.ts | 6 +++--- src/handler/Frembed.ts | 4 ++-- src/handler/FrenchCloud.ts | 4 ++-- src/handler/KinoGer.ts | 4 ++-- src/handler/MeineCloud.ts | 4 ++-- src/handler/MostraGuarda.ts | 4 ++-- src/handler/Soaper.ts | 6 +++--- src/handler/StreamKiste.ts | 4 ++-- src/handler/VerHdLink.ts | 6 +++--- src/handler/VidSrc.ts | 4 ++-- src/types.ts | 9 ++++++++- src/utils/StreamResolver.test.ts | 16 ++++++++-------- 14 files changed, 51 insertions(+), 43 deletions(-) diff --git a/src/extractor/ExtractorRegistry.test.ts b/src/extractor/ExtractorRegistry.test.ts index e640606..4bd9f09 100644 --- a/src/extractor/ExtractorRegistry.test.ts +++ b/src/extractor/ExtractorRegistry.test.ts @@ -1,7 +1,8 @@ import winston from 'winston'; import { ExtractorRegistry } from './ExtractorRegistry'; -import { Context } from '../types'; +import { Context, CountryCode } from '../types'; import { Fetcher } from '../utils'; + jest.mock('../utils/Fetcher'); const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'nope' })] }); @@ -13,41 +14,41 @@ describe('ExtractorRegistry', () => { const ctx: Context = { id: 'id', ip: '127.0.0.1', config: { de: 'on' } }; test('returns error result from extractor', async () => { - const urlResult = await extractorRegistry.handle(ctx, new URL('https://some-url.test'), { countryCode: 'en' }); + const urlResult = await extractorRegistry.handle(ctx, new URL('https://some-url.test'), { countryCode: CountryCode.de }); expect(urlResult).toMatchSnapshot(); }); test('return external URLs by default', async () => { - const urlResult = await extractorRegistry.handle(ctx, new URL('https://mixdrop.ag/e/3nzwveprim63or6'), { countryCode: 'de' }); + const urlResult = await extractorRegistry.handle(ctx, new URL('https://mixdrop.ag/e/3nzwveprim63or6'), { countryCode: CountryCode.de }); expect(urlResult).toMatchSnapshot(); }); test('does not return external URLs if disabled by config', async () => { - const urlResult = await extractorRegistry.handle({ ...ctx, config: { ...ctx.config, excludeExternalUrls: 'on' } }, new URL('https://mixdrop.ag/e/l7v73zqrfdj19z'), { countryCode: 'de' }); + const urlResult = await extractorRegistry.handle({ ...ctx, config: { ...ctx.config, excludeExternalUrls: 'on' } }, new URL('https://mixdrop.ag/e/l7v73zqrfdj19z'), { countryCode: CountryCode.de }); expect(urlResult).toStrictEqual([]); }); test('returns from memory cache if possible', async () => { - const urlResults1 = await extractorRegistry.handle(ctx, new URL('https://dropload.io/lyo2h1snpe5c.html'), { countryCode: 'de' }); - const urlResults2 = await extractorRegistry.handle(ctx, new URL('https://dropload.io/lyo2h1snpe5c.html'), { countryCode: 'de' }); + const urlResults1 = await extractorRegistry.handle(ctx, new URL('https://dropload.io/lyo2h1snpe5c.html'), { countryCode: CountryCode.de }); + const urlResults2 = await extractorRegistry.handle(ctx, new URL('https://dropload.io/lyo2h1snpe5c.html'), { countryCode: CountryCode.de }); expect(urlResults1).not.toStrictEqual([]); expect(urlResults2).not.toStrictEqual([]); }); test('ignores not found errors but caches them', async () => { - const urlResults1 = await extractorRegistry.handle(ctx, new URL('https://dropload.io/asdfghijklmn.html'), { countryCode: 'de' }); - const urlResults2 = await extractorRegistry.handle(ctx, new URL('https://dropload.io/asdfghijklmn.html'), { countryCode: 'de' }); + const urlResults1 = await extractorRegistry.handle(ctx, new URL('https://dropload.io/asdfghijklmn.html'), { countryCode: CountryCode.de }); + const urlResults2 = await extractorRegistry.handle(ctx, new URL('https://dropload.io/asdfghijklmn.html'), { countryCode: CountryCode.de }); expect(urlResults1).toStrictEqual([]); expect(urlResults2).toStrictEqual([]); }); test('returns external url for error', async () => { - const urlResults = await extractorRegistry.handle(ctx, new URL('https://dropload.io/mocked-blocked.html'), { countryCode: 'de' }); + const urlResults = await extractorRegistry.handle(ctx, new URL('https://dropload.io/mocked-blocked.html'), { countryCode: CountryCode.de }); expect(urlResults).toMatchSnapshot(); }); diff --git a/src/handler/CineHDPlus.ts b/src/handler/CineHDPlus.ts index 15f501f..5e97d9e 100644 --- a/src/handler/CineHDPlus.ts +++ b/src/handler/CineHDPlus.ts @@ -12,7 +12,7 @@ export class CineHDPlus implements Handler { readonly contentTypes: ContentType[] = ['series']; - readonly countryCodes: CountryCode[] = ['es', 'mx']; + readonly countryCodes: CountryCode[] = [CountryCode.es, CountryCode.mx]; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; @@ -34,7 +34,7 @@ export class CineHDPlus implements Handler { const $ = cheerio.load(html); - const countryCode = ($('.details__langs').html() as string).includes('Latino') ? 'mx' : 'es'; + const countryCode = ($('.details__langs').html() as string).includes('Latino') ? CountryCode.mx : CountryCode.es; if (!(countryCode in ctx.config)) { return []; } diff --git a/src/handler/Eurostreaming.ts b/src/handler/Eurostreaming.ts index 76444e5..990801e 100644 --- a/src/handler/Eurostreaming.ts +++ b/src/handler/Eurostreaming.ts @@ -1,7 +1,7 @@ import { ContentType } from 'stremio-addon-sdk'; import * as cheerio from 'cheerio'; import { Handler } from './types'; -import { ImdbId, Fetcher, Id, getImdbId } from '../utils'; +import { Fetcher, getImdbId, Id, ImdbId } from '../utils'; import { ExtractorRegistry } from '../extractor'; import { Context, CountryCode } from '../types'; @@ -12,7 +12,7 @@ export class Eurostreaming implements Handler { readonly contentTypes: ContentType[] = ['series']; - readonly countryCodes: CountryCode[] = ['it']; + readonly countryCodes: CountryCode[] = [CountryCode.it]; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; @@ -46,7 +46,7 @@ export class Eurostreaming implements Handler { .map((_i, el) => new URL(($(el).attr('data-link') as string).replace(/^(https:)?\/\//, 'https://'))) .toArray() .filter(url => !url.host.match(/eurostreaming/)) - .map(url => this.extractorRegistry.handle({ ...ctx, referer: seriesPageUrl }, url, { countryCode: 'it', title: `${title.trim()} ${imdbId.season}x${imdbId.episode}` })), + .map(url => this.extractorRegistry.handle({ ...ctx, referer: seriesPageUrl }, url, { countryCode: CountryCode.it, title: `${title.trim()} ${imdbId.season}x${imdbId.episode}` })), ); }; diff --git a/src/handler/Frembed.ts b/src/handler/Frembed.ts index afcdc74..432157b 100644 --- a/src/handler/Frembed.ts +++ b/src/handler/Frembed.ts @@ -11,7 +11,7 @@ export class Frembed implements Handler { readonly contentTypes: ContentType[] = ['series']; - readonly countryCodes: CountryCode[] = ['fr']; + readonly countryCodes: CountryCode[] = [CountryCode.fr]; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; @@ -40,7 +40,7 @@ export class Frembed implements Handler { } return Promise.all( - urls.map(url => this.extractorRegistry.handle({ ...ctx, referer: apiUrl }, url, { countryCode: 'fr', title: `${json['title']} ${tmdbId.season}x${tmdbId.episode}` })), + urls.map(url => this.extractorRegistry.handle({ ...ctx, referer: apiUrl }, url, { countryCode: CountryCode.fr, title: `${json['title']} ${tmdbId.season}x${tmdbId.episode}` })), ); }; } diff --git a/src/handler/FrenchCloud.ts b/src/handler/FrenchCloud.ts index 304113f..1b7a234 100644 --- a/src/handler/FrenchCloud.ts +++ b/src/handler/FrenchCloud.ts @@ -12,7 +12,7 @@ export class FrenchCloud implements Handler { readonly contentTypes: ContentType[] = ['movie']; - readonly countryCodes: CountryCode[] = ['fr']; + readonly countryCodes: CountryCode[] = [CountryCode.fr]; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; @@ -35,7 +35,7 @@ export class FrenchCloud implements Handler { .map((_i, el) => new URL(($(el).attr('data-link') as string).replace(/^(https:)?\/\//, 'https://'))) .toArray() .filter(url => !url.host.match(/frenchcloud/)) - .map(url => this.extractorRegistry.handle({ ...ctx, referer: pageUrl }, url, { countryCode: 'fr' })), + .map(url => this.extractorRegistry.handle({ ...ctx, referer: pageUrl }, url, { countryCode: CountryCode.fr })), ); }; } diff --git a/src/handler/KinoGer.ts b/src/handler/KinoGer.ts index 65c9814..f031cdc 100644 --- a/src/handler/KinoGer.ts +++ b/src/handler/KinoGer.ts @@ -12,7 +12,7 @@ export class KinoGer implements Handler { readonly contentTypes: ContentType[] = ['movie', 'series']; - readonly countryCodes: CountryCode[] = ['de']; + readonly countryCodes: CountryCode[] = [CountryCode.de]; private readonly baseUrl = 'https://kinoger.com'; @@ -44,7 +44,7 @@ export class KinoGer implements Handler { Array.from(html.matchAll(/\.show\(.*/g)) .map(showJsMatch => this.findEpisodeUrlInShowJs(showJsMatch[0], seasonIndex, episodeIndex)) .filter(url => url !== undefined && !['kinoger.be', 'kinoger.ru'].includes(url.host)) - .map(async url => await this.extractorRegistry.handle({ ...ctx, referer: pageUrl }, url as URL, { countryCode: 'de', title })), + .map(async url => await this.extractorRegistry.handle({ ...ctx, referer: pageUrl }, url as URL, { countryCode: CountryCode.de, title })), ); }; diff --git a/src/handler/MeineCloud.ts b/src/handler/MeineCloud.ts index 99aa2fe..d209e40 100644 --- a/src/handler/MeineCloud.ts +++ b/src/handler/MeineCloud.ts @@ -12,7 +12,7 @@ export class MeineCloud implements Handler { readonly contentTypes: ContentType[] = ['movie']; - readonly countryCodes: CountryCode[] = ['de']; + readonly countryCodes: CountryCode[] = [CountryCode.de]; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; @@ -35,7 +35,7 @@ export class MeineCloud implements Handler { .map((_i, el) => new URL(($(el).attr('data-link') as string).replace(/^(https:)?\/\//, 'https://'))) .toArray() .filter(url => !url.host.match(/meinecloud/)) - .map(url => this.extractorRegistry.handle({ ...ctx, referer: pageUrl }, url, { countryCode: 'de' })), + .map(url => this.extractorRegistry.handle({ ...ctx, referer: pageUrl }, url, { countryCode: CountryCode.de })), ); }; } diff --git a/src/handler/MostraGuarda.ts b/src/handler/MostraGuarda.ts index 992cc09..dddacda 100644 --- a/src/handler/MostraGuarda.ts +++ b/src/handler/MostraGuarda.ts @@ -12,7 +12,7 @@ export class MostraGuarda implements Handler { readonly contentTypes: ContentType[] = ['movie']; - readonly countryCodes: CountryCode[] = ['it']; + readonly countryCodes: CountryCode[] = [CountryCode.it]; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; @@ -35,7 +35,7 @@ export class MostraGuarda implements Handler { .map((_i, el) => new URL(($(el).attr('data-link') as string).replace(/^(https:)?\/\//, 'https://'))) .toArray() .filter(url => !url.host.match(/mostraguarda/)) - .map(url => this.extractorRegistry.handle({ ...ctx, referer: pageUrl }, url, { countryCode: 'it' })), + .map(url => this.extractorRegistry.handle({ ...ctx, referer: pageUrl }, url, { countryCode: CountryCode.it })), ); }; } diff --git a/src/handler/Soaper.ts b/src/handler/Soaper.ts index 5c041a3..7d0f6aa 100644 --- a/src/handler/Soaper.ts +++ b/src/handler/Soaper.ts @@ -12,7 +12,7 @@ export class Soaper implements Handler { readonly contentTypes: ContentType[] = ['movie', 'series']; - readonly countryCodes: CountryCode[] = ['en']; + readonly countryCodes: CountryCode[] = [CountryCode.en]; private readonly baseUrl = 'https://soaper.live'; @@ -46,10 +46,10 @@ export class Soaper implements Handler { const episodeUrl = new URL(episodePageHref, this.baseUrl); - return [await this.extractorRegistry.handle({ ...ctx, referer: episodeUrl }, episodeUrl, { countryCode: 'en', title: `${keyword} ${tmdbId.season}x${tmdbId.episode}` })]; + return [await this.extractorRegistry.handle({ ...ctx, referer: episodeUrl }, episodeUrl, { countryCode: CountryCode.en, title: `${keyword} ${tmdbId.season}x${tmdbId.episode}` })]; } - return [await this.extractorRegistry.handle({ ...ctx, referer: pageUrl }, pageUrl, { countryCode: 'en', title: `${keyword} (${year})` })]; + return [await this.extractorRegistry.handle({ ...ctx, referer: pageUrl }, pageUrl, { countryCode: CountryCode.en, title: `${keyword} (${year})` })]; }; private readonly fetchPageUrl = async (ctx: Context, keyword: string, year: number, hrefPrefix: string): Promise => { diff --git a/src/handler/StreamKiste.ts b/src/handler/StreamKiste.ts index b83e244..043c3f7 100644 --- a/src/handler/StreamKiste.ts +++ b/src/handler/StreamKiste.ts @@ -12,7 +12,7 @@ export class StreamKiste implements Handler { readonly contentTypes: ContentType[] = ['series']; - readonly countryCodes: CountryCode[] = ['de']; + readonly countryCodes: CountryCode[] = [CountryCode.de]; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; @@ -43,7 +43,7 @@ export class StreamKiste implements Handler { .map((_i, el) => new URL(($(el).attr('data-link') as string).replace(/^(https:)?\/\//, 'https://'))) .toArray() .filter(url => !url.host.match(/streamkiste/)) - .map(url => this.extractorRegistry.handle({ ...ctx, referer: seriesPageUrl }, url, { countryCode: 'de', title: `${title.trim()} ${imdbId.season}x${imdbId.episode}` })), + .map(url => this.extractorRegistry.handle({ ...ctx, referer: seriesPageUrl }, url, { countryCode: CountryCode.de, title: `${title.trim()} ${imdbId.season}x${imdbId.episode}` })), ); }; diff --git a/src/handler/VerHdLink.ts b/src/handler/VerHdLink.ts index 48d8883..7ccc7f3 100644 --- a/src/handler/VerHdLink.ts +++ b/src/handler/VerHdLink.ts @@ -12,7 +12,7 @@ export class VerHdLink implements Handler { readonly contentTypes: ContentType[] = ['movie']; - readonly countryCodes: CountryCode[] = ['es', 'mx']; + readonly countryCodes: CountryCode[] = [CountryCode.es, CountryCode.mx]; private readonly fetcher: Fetcher; private readonly extractorRegistry: ExtractorRegistry; @@ -35,9 +35,9 @@ export class VerHdLink implements Handler { .map((_i, el) => { let countryCode: CountryCode; if ($(el).hasClass('latino') && 'mx' in ctx.config) { - countryCode = 'mx'; + countryCode = CountryCode.mx; } else if ($(el).hasClass('castellano') && 'es' in ctx.config) { - countryCode = 'es'; + countryCode = CountryCode.es; } else { return []; } diff --git a/src/handler/VidSrc.ts b/src/handler/VidSrc.ts index 23e0bcf..ef1301b 100644 --- a/src/handler/VidSrc.ts +++ b/src/handler/VidSrc.ts @@ -11,7 +11,7 @@ export class VidSrc implements Handler { readonly contentTypes: ContentType[] = ['movie', 'series']; - readonly countryCodes: CountryCode[] = ['en']; + readonly countryCodes: CountryCode[] = [CountryCode.en]; private readonly baseUrl = 'https://vidsrc.xyz'; @@ -31,7 +31,7 @@ export class VidSrc implements Handler { : new URL(`/embed/movie/${imdbId.id}`, this.baseUrl); return [ - await this.extractorRegistry.handle(ctx, embedUrl, { countryCode: 'en' }), + await this.extractorRegistry.handle(ctx, embedUrl, { countryCode: CountryCode.en }), ]; }; } diff --git a/src/types.ts b/src/types.ts index ccbf450..0279805 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,7 +13,14 @@ export type ManifestWithConfig = Manifest & { config: ManifestConfig[] }; export type Config = Partial>; -export type CountryCode = 'de' | 'en' | 'es' | 'fr' | 'it' | 'mx'; +export enum CountryCode { + de = 'de', + en = 'en', + es = 'es', + fr = 'fr', + it = 'it', + mx = 'mx', +} export type BlockedReason = 'cloudflare_challenge' | 'flaresolverr_failed' | 'unknown'; diff --git a/src/utils/StreamResolver.test.ts b/src/utils/StreamResolver.test.ts index 1c3acdf..4b2580d 100644 --- a/src/utils/StreamResolver.test.ts +++ b/src/utils/StreamResolver.test.ts @@ -61,7 +61,7 @@ describe('resolve', () => { readonly contentTypes: ContentType[] = ['movie']; - readonly countryCodes: CountryCode[] = ['de']; + readonly countryCodes: CountryCode[] = [CountryCode.de]; readonly handle = async (): Promise<(UrlResult[])[]> => { return [ @@ -73,7 +73,7 @@ describe('resolve', () => { label: 'hoster.com', sourceId: '', meta: { - countryCode: 'de', + countryCode: CountryCode.de, }, }, { @@ -83,7 +83,7 @@ describe('resolve', () => { label: 'hoster.com', sourceId: '', meta: { - countryCode: 'de', + countryCode: CountryCode.de, }, }, { @@ -93,7 +93,7 @@ describe('resolve', () => { label: 'hoster.com', sourceId: '', meta: { - countryCode: 'de', + countryCode: CountryCode.de, }, }, { @@ -103,7 +103,7 @@ describe('resolve', () => { label: 'hoster.com', sourceId: '', meta: { - countryCode: 'de', + countryCode: CountryCode.de, }, }, { @@ -113,7 +113,7 @@ describe('resolve', () => { label: 'hoster.com', sourceId: '', meta: { - countryCode: 'de', + countryCode: CountryCode.de, }, }, { @@ -123,7 +123,7 @@ describe('resolve', () => { label: 'hoster.com', sourceId: '', meta: { - countryCode: 'de', + countryCode: CountryCode.de, }, }, ], @@ -143,7 +143,7 @@ describe('resolve', () => { id: 'mockhandler', label: 'MockHandler', contentTypes: ['movie'], - countryCodes: ['de'], + countryCodes: [CountryCode.de], handle: jest.fn().mockRejectedValue(new NotFoundError()), };