refactor(fetcher): introduce json method

This commit is contained in:
WebStreamr 2025-09-24 18:27:03 +00:00
parent b1244086cd
commit a96d015a0c
No known key found for this signature in database
7 changed files with 19 additions and 7 deletions

View file

@ -30,7 +30,7 @@ export class Einschalten extends Source {
public async handleInternal(ctx: Context, _type: string, id: Id): Promise<SourceResult[]> {
const tmdbId = await getTmdbId(ctx, this.fetcher, id);
const { releaseName: title, streamUrl } = JSON.parse(await this.fetcher.text(ctx, new URL(`/api/movies/${tmdbId.id}/watch`, this.baseUrl))) as EinschaltenResponse;
const { releaseName: title, streamUrl } = await this.fetcher.json(ctx, new URL(`/api/movies/${tmdbId.id}/watch`, this.baseUrl)) as EinschaltenResponse;
return [{ url: new URL(streamUrl), meta: { countryCodes: [CountryCode.de], title } }];
};

View file

@ -29,7 +29,7 @@ export class Frembed extends Source {
? new URL(`/api/series?id=${tmdbId.id}&sa=${tmdbId.season}&epi=${tmdbId.episode}&idType=tmdb`, this.baseUrl)
: new URL(`/api/films?id=${tmdbId.id}&idType=tmdb`, this.baseUrl);
const json = JSON.parse(await this.fetcher.text(ctx, apiUrl));
const json = await this.fetcher.json(ctx, apiUrl);
const urls: URL[] = [];
for (const key in json) {

View file

@ -33,7 +33,7 @@ export class Movix extends Source {
? new URL(`/api/tmdb/tv/${tmdbId.id}?season=${tmdbId.season}&episode=${tmdbId.episode}`, this.baseUrl)
: new URL(`/api/tmdb/movie/${tmdbId.id}`, this.baseUrl);
const json = JSON.parse(await this.fetcher.text(ctx, apiUrl));
const json = await this.fetcher.json(ctx, apiUrl);
const data: MovixApiData | undefined = tmdbId.season ? json['current_episode'] : json;
if (!data || !data.player_links) {

View file

@ -70,7 +70,7 @@ export class PrimeWire extends Source {
primeSrcUrl.searchParams.set('type', 'tv');
}
const primeSrcResponse = JSON.parse(await this.fetcher.text(ctx, primeSrcUrl, { headers: { Referer: pageUrl.origin } })) as PrimeSrcResponsePartial;
const primeSrcResponse = await this.fetcher.json(ctx, primeSrcUrl, { headers: { Referer: pageUrl.origin } }) as PrimeSrcResponsePartial;
const linksTokenMatch = appJs.match(/t="(0\.x.*?)"/) as string[];
const linksToken = linksTokenMatch[1] as string;
@ -85,7 +85,7 @@ export class PrimeWire extends Source {
if (!targetUrlHref) {
const linkFetchUrl = new URL(redirectUrl.href.replace('/gos/', '/go/'));
linkFetchUrl.searchParams.set('token', linksToken);
targetUrlHref = JSON.parse(await this.fetcher.text(ctx, linkFetchUrl))['link'] as string;
targetUrlHref = (await this.fetcher.json(ctx, linkFetchUrl))['link'] as string;
await this.redirectUrlCache.set<string>(redirectUrl.href, targetUrlHref);
}

View file

@ -101,6 +101,18 @@ export class Fetcher {
return (await this.cachedFetch(ctx, url, { ...init, method: 'HEAD' })).headers;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async json(ctx: Context, url: URL, init?: CustomRequestInit): Promise<any> {
const jsonInit = {
headers: {
Accept: 'application/json,text/plain,*/*',
},
...init,
};
return JSON.parse(await this.text(ctx, url, jsonInit));
}
private getInit(ctx: Context, url: URL, init?: CustomRequestInit): CustomRequestInit {
const cookieString = this.cookieJar.getCookieStringSync(url.href);
const noProxyHeaders = init?.noProxyHeaders ?? false;

View file

@ -34,7 +34,7 @@ export const buildMediaFlowProxyExtractorStreamUrl = async (ctx: Context, fetche
mediaFlowProxyUrl.searchParams.set('h_' + headerKey.toLowerCase(), headers[headerKey] as string);
}
const extractResult: ExtractResult = JSON.parse(await fetcher.text(ctx, mediaFlowProxyUrl));
const extractResult: ExtractResult = await fetcher.json(ctx, mediaFlowProxyUrl);
const streamUrl = new URL(extractResult.mediaflow_proxy_url);

View file

@ -53,7 +53,7 @@ const tmdbFetch = async (ctx: Context, fetcher: Fetcher, path: string, searchPar
}
const data = await mutex.runExclusive(async () => {
return JSON.parse(await fetcher.text(ctx, url, config));
return await fetcher.json(ctx, url, config);
});
if (!mutex.isLocked()) {