From 5547fd5486e61a260a208e343f837bd06978cd26 Mon Sep 17 00:00:00 2001 From: WebStreamr <210764791+webstreamr@users.noreply.github.com> Date: Wed, 3 Sep 2025 12:10:17 +0000 Subject: [PATCH] fix(source): move countryCode filtering into central place --- src/source/CineHDPlus.ts | 3 --- src/source/Cuevana.ts | 14 +++++--------- src/source/HomeCine.ts | 4 ++-- src/source/Source.ts | 28 ++++++++++++++-------------- src/source/VerHdLink.ts | 4 ++-- src/utils/StreamResolver.test.ts | 2 +- 6 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/source/CineHDPlus.ts b/src/source/CineHDPlus.ts index 9e60342..87adf0b 100644 --- a/src/source/CineHDPlus.ts +++ b/src/source/CineHDPlus.ts @@ -36,9 +36,6 @@ export class CineHDPlus extends Source { const $ = cheerio.load(html); const countryCode: CountryCode = ($('.details__langs').html() as string).includes('Latino') ? CountryCode.mx : CountryCode.es; - if (!(countryCode in ctx.config)) { - return []; - } const title = $('meta[property="og:title"]').attr('content') as string; diff --git a/src/source/Cuevana.ts b/src/source/Cuevana.ts index 7732638..1539a05 100644 --- a/src/source/Cuevana.ts +++ b/src/source/Cuevana.ts @@ -58,19 +58,15 @@ export class Cuevana extends Source { return []; } - const isLatino = elText.includes('Latino'); - - if (isLatino && CountryCode.mx in ctx.config) { + if (elText.includes('Latino')) { return $('[data-tr], [data-video]', el) .map((_i, el) => ({ countryCode: CountryCode.mx, title, url: new URL($(el).attr('data-tr') ?? $(el).attr('data-video') as string) })) .toArray(); - } else if (!isLatino && CountryCode.es in ctx.config) { - return $('[data-tr], [data-video]', el) - .map((_i, el) => ({ countryCode: CountryCode.es, title, url: new URL($(el).attr('data-tr') ?? $(el).attr('data-video') as string) })) - .toArray(); - } else { - return []; } + + return $('[data-tr], [data-video]', el) + .map((_i, el) => ({ countryCode: CountryCode.es, title, url: new URL($(el).attr('data-tr') ?? $(el).attr('data-video') as string) })) + .toArray(); }) .toArray(); diff --git a/src/source/HomeCine.ts b/src/source/HomeCine.ts index 2b1bb11..3a92218 100644 --- a/src/source/HomeCine.ts +++ b/src/source/HomeCine.ts @@ -52,9 +52,9 @@ export class HomeCine extends Source { return $('.les-content a') .map((_i, el) => { let countryCode: CountryCode; - if ($(el).text().toLowerCase().includes('latino') && CountryCode.mx in ctx.config) { + if ($(el).text().toLowerCase().includes('latino')) { countryCode = CountryCode.mx; - } else if ($(el).text().toLowerCase().includes('castellano') && CountryCode.es in ctx.config) { + } else if ($(el).text().toLowerCase().includes('castellano')) { countryCode = CountryCode.es; } else { return []; diff --git a/src/source/Source.ts b/src/source/Source.ts index a627a79..a1d7966 100644 --- a/src/source/Source.ts +++ b/src/source/Source.ts @@ -39,23 +39,23 @@ export abstract class Source { public async handle(ctx: Context, type: ContentType, id: Id): Promise<(SourceResult[])> { const cacheKey = `${this.id}_${id.toString()}`; - let sourceResults = await this.sourceResultCache.get(cacheKey); - if (sourceResults) { - return sourceResults.map(sourceResult => ({ ...sourceResult, url: new URL(sourceResult.url) })); - } + let sourceResults = (await this.sourceResultCache.get(cacheKey)) + ?.map(sourceResult => ({ ...sourceResult, url: new URL(sourceResult.url) })); - try { - sourceResults = await this.handleInternal(ctx, type, id); - } catch (error) { - if (error instanceof NotFoundError) { - sourceResults = []; - } else { - throw error; + if (!sourceResults) { + try { + sourceResults = await this.handleInternal(ctx, type, id); + } catch (error) { + if (error instanceof NotFoundError) { + sourceResults = []; + } else { + throw error; + } } + + await this.sourceResultCache.set(cacheKey, sourceResults); } - await this.sourceResultCache.set(cacheKey, sourceResults); - - return sourceResults; + return sourceResults.filter(sourceResult => sourceResult.countryCode in ctx.config); } } diff --git a/src/source/VerHdLink.ts b/src/source/VerHdLink.ts index 788d1fa..3204e7d 100644 --- a/src/source/VerHdLink.ts +++ b/src/source/VerHdLink.ts @@ -34,9 +34,9 @@ export class VerHdLink extends Source { return $('._player-mirrors') .map((_i, el) => { let countryCode: CountryCode; - if ($(el).hasClass('latino') && 'mx' in ctx.config) { + if ($(el).hasClass('latino')) { countryCode = CountryCode.mx; - } else if ($(el).hasClass('castellano') && 'es' in ctx.config) { + } else if ($(el).hasClass('castellano')) { countryCode = CountryCode.es; } else { return []; diff --git a/src/utils/StreamResolver.test.ts b/src/utils/StreamResolver.test.ts index 222a6b3..3ac83d0 100644 --- a/src/utils/StreamResolver.test.ts +++ b/src/utils/StreamResolver.test.ts @@ -73,7 +73,7 @@ describe('resolve', () => { test('returns ytId instead of url for YouTube video', async () => { const streamResolver = new StreamResolver(logger, new ExtractorRegistry(logger, createExtractors(fetcher))); - const streams = await streamResolver.resolve(ctx, [primeWire], 'movie', new ImdbId('tt0069293', undefined, undefined)); + const streams = await streamResolver.resolve({ ...ctx, config: { ...ctx.config, en: 'on' } }, [primeWire], 'movie', new ImdbId('tt0069293', undefined, undefined)); expect(streams.ttl).not.toBeUndefined(); expect(streams.streams).toMatchSnapshot(); });