Support proxy on runners

This commit is contained in:
TPN 2024-06-30 16:28:02 +05:30
parent 53534cb5ff
commit 0f95ed0b20
No known key found for this signature in database
GPG key ID: 40AE091637892B91
4 changed files with 36 additions and 6 deletions

View file

@ -31,8 +31,6 @@ export interface RunnerOptions {
// the media you want to see sources from
media: ScrapeMedia;
proxyStreams?: boolean; // temporary
}
export interface SourceRunnerOptions {
@ -44,8 +42,6 @@ export interface SourceRunnerOptions {
// id of the source scraper you want to scrape from
id: string;
proxyStreams?: boolean; // temporary
}
export interface EmbedRunnerOptions {
@ -57,8 +53,6 @@ export interface EmbedRunnerOptions {
// id of the embed scraper you want to scrape from
id: string;
proxyStreams?: boolean; // temporary
}
export interface ProviderControls {

View file

@ -7,6 +7,7 @@ import { ProviderList } from '@/providers/get';
import { ScrapeContext } from '@/utils/context';
import { NotFoundError } from '@/utils/errors';
import { addOpenSubtitlesCaptions } from '@/utils/opensubtitles';
import { requiresProxy, setupProxy } from '@/utils/proxy';
import { isValidStream, validatePlayableStreams } from '@/utils/valid';
export type IndividualSourceRunnerOptions = {
@ -16,6 +17,7 @@ export type IndividualSourceRunnerOptions = {
media: ScrapeMedia;
id: string;
events?: IndividualScraperEvents;
proxyStreams?: boolean; // temporary
};
export async function scrapeInvidualSource(
@ -56,6 +58,10 @@ export async function scrapeInvidualSource(
output.stream = output.stream
.filter((stream) => isValidStream(stream))
.filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags));
output.stream = output.stream.map((stream) =>
requiresProxy(stream) && ops.proxyStreams ? setupProxy(stream) : stream,
);
}
if (!output) throw new Error('output is null');
@ -107,6 +113,7 @@ export type IndividualEmbedRunnerOptions = {
url: string;
id: string;
events?: IndividualScraperEvents;
proxyStreams?: boolean; // temporary
};
export async function scrapeIndividualEmbed(
@ -138,6 +145,10 @@ export async function scrapeIndividualEmbed(
.filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags));
if (output.stream.length === 0) throw new NotFoundError('No streams found');
output.stream = output.stream.map((stream) =>
requiresProxy(stream) && ops.proxyStreams ? setupProxy(stream) : stream,
);
const playableStreams = await validatePlayableStreams(output.stream, ops, embedScraper.id);
if (playableStreams.length === 0) throw new NotFoundError('No playable streams found');

View file

@ -9,6 +9,7 @@ import { ScrapeContext } from '@/utils/context';
import { NotFoundError } from '@/utils/errors';
import { reorderOnIdList } from '@/utils/list';
import { addOpenSubtitlesCaptions } from '@/utils/opensubtitles';
import { requiresProxy, setupProxy } from '@/utils/proxy';
import { isValidStream, validatePlayableStream } from '@/utils/valid';
export type RunOutput = {
@ -36,6 +37,7 @@ export type ProviderRunnerOptions = {
embedOrder?: string[];
events?: FullScraperEvents;
media: ScrapeMedia;
proxyStreams?: boolean; // temporary
};
export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOptions): Promise<RunOutput | null> {
@ -85,6 +87,10 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
output.stream = (output.stream ?? [])
.filter(isValidStream)
.filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags));
output.stream = output.stream.map((stream) =>
requiresProxy(stream) && ops.proxyStreams ? setupProxy(stream) : stream,
);
}
if (!output || (!output.stream?.length && !output.embeds.length)) {
throw new NotFoundError('No streams found');
@ -161,6 +167,9 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
embedOutput.stream = embedOutput.stream
.filter(isValidStream)
.filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags));
embedOutput.stream = embedOutput.stream.map((stream) =>
requiresProxy(stream) && ops.proxyStreams ? setupProxy(stream) : stream,
);
if (embedOutput.stream.length === 0) {
throw new NotFoundError('No streams found');
}

16
src/utils/proxy.ts Normal file
View file

@ -0,0 +1,16 @@
import { flags } from '@/entrypoint/utils/targets';
import { Stream } from '@/providers/streams';
export function requiresProxy(stream: Stream): boolean {
if (!stream.flags.includes('cors-allowed') && !!(stream.headers && Object.keys(stream.headers).length > 0))
return true;
return false;
}
export function setupProxy(stream: Stream): Stream {
// todo
// stream.headers = {};
stream.flags = [flags.CORS_ALLOWED];
return stream;
}