mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 15:33:26 +00:00
Check if a stream returns a 200
This commit is contained in:
@@ -6,7 +6,7 @@ import { EmbedOutput, SourcererOutput } from '@/providers/base';
|
|||||||
import { ProviderList } from '@/providers/get';
|
import { ProviderList } from '@/providers/get';
|
||||||
import { ScrapeContext } from '@/utils/context';
|
import { ScrapeContext } from '@/utils/context';
|
||||||
import { NotFoundError } from '@/utils/errors';
|
import { NotFoundError } from '@/utils/errors';
|
||||||
import { isValidStream } from '@/utils/valid';
|
import { isValidStream, validatePlayableStreams } from '@/utils/valid';
|
||||||
|
|
||||||
export type IndividualSourceRunnerOptions = {
|
export type IndividualSourceRunnerOptions = {
|
||||||
features: FeatureMap;
|
features: FeatureMap;
|
||||||
@@ -68,6 +68,13 @@ export async function scrapeInvidualSource(
|
|||||||
|
|
||||||
if ((!output.stream || output.stream.length === 0) && output.embeds.length === 0)
|
if ((!output.stream || output.stream.length === 0) && output.embeds.length === 0)
|
||||||
throw new NotFoundError('No streams found');
|
throw new NotFoundError('No streams found');
|
||||||
|
|
||||||
|
// only check for playable streams if there are streams, and if there are no embeds
|
||||||
|
if (output.stream && output.stream.length > 0 && output.embeds.length === 0) {
|
||||||
|
const playableStreams = await validatePlayableStreams(output.stream, ops);
|
||||||
|
if (playableStreams.length === 0) throw new NotFoundError('No playable streams found');
|
||||||
|
output.stream = playableStreams;
|
||||||
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,5 +112,9 @@ export async function scrapeIndividualEmbed(
|
|||||||
.filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags));
|
.filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags));
|
||||||
if (output.stream.length === 0) throw new NotFoundError('No streams found');
|
if (output.stream.length === 0) throw new NotFoundError('No streams found');
|
||||||
|
|
||||||
|
const playableStreams = await validatePlayableStreams(output.stream, ops);
|
||||||
|
if (playableStreams.length === 0) throw new NotFoundError('No playable streams found');
|
||||||
|
output.stream = playableStreams;
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
@@ -8,7 +8,7 @@ import { Stream } from '@/providers/streams';
|
|||||||
import { ScrapeContext } from '@/utils/context';
|
import { ScrapeContext } from '@/utils/context';
|
||||||
import { NotFoundError } from '@/utils/errors';
|
import { NotFoundError } from '@/utils/errors';
|
||||||
import { reorderOnIdList } from '@/utils/list';
|
import { reorderOnIdList } from '@/utils/list';
|
||||||
import { isValidStream } from '@/utils/valid';
|
import { isValidStream, validatePlayableStream } from '@/utils/valid';
|
||||||
|
|
||||||
export type RunOutput = {
|
export type RunOutput = {
|
||||||
sourceId: string;
|
sourceId: string;
|
||||||
@@ -104,9 +104,11 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
|
|||||||
|
|
||||||
// return stream is there are any
|
// return stream is there are any
|
||||||
if (output.stream?.[0]) {
|
if (output.stream?.[0]) {
|
||||||
|
const playableStream = await validatePlayableStream(output.stream[0], ops);
|
||||||
|
if (!playableStream) throw new NotFoundError('No streams found');
|
||||||
return {
|
return {
|
||||||
sourceId: source.id,
|
sourceId: source.id,
|
||||||
stream: output.stream[0],
|
stream: playableStream,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,11 +163,13 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
|
|||||||
ops.events?.update?.(updateParams);
|
ops.events?.update?.(updateParams);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
const playableStream = await validatePlayableStream(embedOutput.stream[0], ops);
|
||||||
|
if (!playableStream) throw new NotFoundError('No streams found');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sourceId: source.id,
|
sourceId: source.id,
|
||||||
embedId: scraper.id,
|
embedId: scraper.id,
|
||||||
stream: embedOutput.stream[0],
|
stream: playableStream,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
import { Stream } from '@/providers/streams';
|
import { Stream } from '@/providers/streams';
|
||||||
|
import { IndividualEmbedRunnerOptions } from '@/runners/individualRunner';
|
||||||
|
import { ProviderRunnerOptions } from '@/runners/runner';
|
||||||
|
|
||||||
export function isValidStream(stream: Stream | undefined): boolean {
|
export function isValidStream(stream: Stream | undefined): boolean {
|
||||||
if (!stream) return false;
|
if (!stream) return false;
|
||||||
@@ -15,3 +17,54 @@ export function isValidStream(stream: Stream | undefined): boolean {
|
|||||||
// unknown file type
|
// unknown file type
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function validatePlayableStream(
|
||||||
|
stream: Stream,
|
||||||
|
ops: ProviderRunnerOptions | IndividualEmbedRunnerOptions,
|
||||||
|
): Promise<Stream | null> {
|
||||||
|
const fetcher = stream.flags.includes('cors-allowed') ? ops.fetcher : ops.proxiedFetcher;
|
||||||
|
if (stream.type === 'hls') {
|
||||||
|
const headResult = await fetcher.full(stream.playlist, {
|
||||||
|
method: 'HEAD',
|
||||||
|
headers: {
|
||||||
|
...stream.preferredHeaders,
|
||||||
|
...stream.headers,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (headResult.statusCode !== 200) return null;
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
if (stream.type === 'file') {
|
||||||
|
const validQualitiesResults = await Promise.all(
|
||||||
|
Object.values(stream.qualities).map((quality) =>
|
||||||
|
fetcher.full(quality.url, {
|
||||||
|
method: 'HEAD',
|
||||||
|
headers: {
|
||||||
|
...stream.preferredHeaders,
|
||||||
|
...stream.headers,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
// remove invalid qualities from the stream
|
||||||
|
const validQualities = stream.qualities;
|
||||||
|
Object.keys(stream.qualities).forEach((quality, index) => {
|
||||||
|
if (validQualitiesResults[index].statusCode !== 200) {
|
||||||
|
delete validQualities[quality as keyof typeof stream.qualities];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (Object.keys(validQualities).length === 0) return null;
|
||||||
|
return { ...stream, qualities: validQualities };
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function validatePlayableStreams(
|
||||||
|
streams: Stream[],
|
||||||
|
ops: ProviderRunnerOptions | IndividualEmbedRunnerOptions,
|
||||||
|
): Promise<Stream[]> {
|
||||||
|
return (await Promise.all(streams.map((stream) => validatePlayableStream(stream, ops)))).filter(
|
||||||
|
(v) => v !== null,
|
||||||
|
) as Stream[];
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user