refactor: make CountryCode an enum

This commit is contained in:
WebStreamr 2025-06-12 15:56:33 +00:00
parent 9432ba5944
commit 250d6a1323
No known key found for this signature in database
14 changed files with 51 additions and 43 deletions

View file

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

View file

@ -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 [];
}

View file

@ -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}` })),
);
};

View file

@ -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}` })),
);
};
}

View file

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

View file

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

View file

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

View file

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

View file

@ -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<URL | undefined> => {

View file

@ -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}` })),
);
};

View file

@ -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 [];
}

View file

@ -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 }),
];
};
}

View file

@ -13,7 +13,14 @@ export type ManifestWithConfig = Manifest & { config: ManifestConfig[] };
export type Config = Partial<Record<CountryCode | 'excludeExternalUrls', string>>;
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';

View file

@ -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()),
};