From 43637a2ff4280db208ec9c94c0a5d52a49dc6672 Mon Sep 17 00:00:00 2001 From: WebStreamr <210764791+webstreamr@users.noreply.github.com> Date: Fri, 27 Mar 2026 20:17:03 +0000 Subject: [PATCH] refactor(source): extract hd hub link redirect resolver --- src/source/FourKHDHub.ts | 14 +++----------- src/source/hd-hub-helper.ts | 11 +++++++++++ 2 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 src/source/hd-hub-helper.ts diff --git a/src/source/FourKHDHub.ts b/src/source/FourKHDHub.ts index 73478e3..522a9e8 100644 --- a/src/source/FourKHDHub.ts +++ b/src/source/FourKHDHub.ts @@ -4,10 +4,10 @@ import { BasicAcceptedElems, CheerioAPI } from 'cheerio'; import { AnyNode } from 'domhandler'; import levenshtein from 'fast-levenshtein'; import memoize from 'memoizee'; -import rot13Cipher from 'rot13-cipher'; import { ContentType } from 'stremio-addon-sdk'; import { Context, CountryCode, Meta } from '../types'; import { Fetcher, findCountryCodes, getTmdbId, getTmdbNameAndYear, Id, TmdbId } from '../utils'; +import { resolveRedirectUrl } from './hd-hub-helper'; import { Source, SourceResult } from './Source'; export class FourKHDHub extends Source { @@ -115,7 +115,7 @@ export class FourKHDHub extends Source { .get(0); if (redirectUrlHubCloud) { - return { url: await this.resolveRedirectUrl(ctx, redirectUrlHubCloud), meta }; + return { url: await resolveRedirectUrl(ctx, this.fetcher, redirectUrlHubCloud), meta }; } const redirectUrlHubDrive = $('a', el) @@ -123,17 +123,9 @@ export class FourKHDHub extends Source { .map((_i, el) => new URL($(el).attr('href') as string)) .get(0) as URL; - return { url: await this.resolveRedirectUrl(ctx, redirectUrlHubDrive), meta }; + return { url: await resolveRedirectUrl(ctx, this.fetcher, redirectUrlHubDrive), meta }; }; - private async resolveRedirectUrl(ctx: Context, redirectUrl: URL): Promise { - const redirectHtml = await this.fetcher.text(ctx, redirectUrl); - const redirectDataMatch = redirectHtml.match(/'o','(.*?)'/) as string[]; - const redirectData = JSON.parse(atob(rot13Cipher(atob(atob(redirectDataMatch[1] as string))))) as { o: string }; - - return new URL(atob(redirectData['o'])); - } - private readonly getBaseUrl = async (ctx: Context): Promise => { return await this.fetcher.getFinalRedirectUrl(ctx, new URL(this.baseUrl)); }; diff --git a/src/source/hd-hub-helper.ts b/src/source/hd-hub-helper.ts new file mode 100644 index 0000000..dfff362 --- /dev/null +++ b/src/source/hd-hub-helper.ts @@ -0,0 +1,11 @@ +import rot13Cipher from 'rot13-cipher'; +import { Context } from '../types'; +import { Fetcher } from '../utils'; + +export const resolveRedirectUrl = async (ctx: Context, fetcher: Fetcher, redirectUrl: URL): Promise => { + const redirectHtml = await fetcher.text(ctx, redirectUrl); + const redirectDataMatch = redirectHtml.match(/'o','(.*?)'/) as string[]; + const redirectData = JSON.parse(atob(rot13Cipher(atob(atob(redirectDataMatch[1] as string))))) as { o: string }; + + return new URL(atob(redirectData['o'])); +};