mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 17:03:26 +00:00
Add builder for adding custom sources
This commit is contained in:
101
src/runners/individualRunner.ts
Normal file
101
src/runners/individualRunner.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { IndividualScraperEvents } from '@/entrypoint/utils/events';
|
||||
import { ScrapeMedia } from '@/entrypoint/utils/media';
|
||||
import { FeatureMap, flagsAllowedInFeatures } from '@/entrypoint/utils/targets';
|
||||
import { UseableFetcher } from '@/fetchers/types';
|
||||
import { EmbedOutput, SourcererOutput } from '@/providers/base';
|
||||
import { ProviderList } from '@/providers/get';
|
||||
import { ScrapeContext } from '@/utils/context';
|
||||
import { NotFoundError } from '@/utils/errors';
|
||||
import { isValidStream } from '@/utils/valid';
|
||||
|
||||
export type IndividualSourceRunnerOptions = {
|
||||
features: FeatureMap;
|
||||
fetcher: UseableFetcher;
|
||||
proxiedFetcher: UseableFetcher;
|
||||
media: ScrapeMedia;
|
||||
id: string;
|
||||
events?: IndividualScraperEvents;
|
||||
};
|
||||
|
||||
export async function scrapeInvidualSource(
|
||||
list: ProviderList,
|
||||
ops: IndividualSourceRunnerOptions,
|
||||
): Promise<SourcererOutput> {
|
||||
const sourceScraper = list.sources.find((v) => ops.id === v.id);
|
||||
if (!sourceScraper) throw new Error('Source with ID not found');
|
||||
if (ops.media.type === 'movie' && !sourceScraper.scrapeMovie) throw new Error('Source is not compatible with movies');
|
||||
if (ops.media.type === 'show' && !sourceScraper.scrapeShow) throw new Error('Source is not compatible with shows');
|
||||
|
||||
const contextBase: ScrapeContext = {
|
||||
fetcher: ops.fetcher,
|
||||
proxiedFetcher: ops.proxiedFetcher,
|
||||
progress(val) {
|
||||
ops.events?.update?.({
|
||||
id: sourceScraper.id,
|
||||
percentage: val,
|
||||
status: 'pending',
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
let output: SourcererOutput | null = null;
|
||||
if (ops.media.type === 'movie' && sourceScraper.scrapeMovie)
|
||||
output = await sourceScraper.scrapeMovie({
|
||||
...contextBase,
|
||||
media: ops.media,
|
||||
});
|
||||
else if (ops.media.type === 'show' && sourceScraper.scrapeShow)
|
||||
output = await sourceScraper.scrapeShow({
|
||||
...contextBase,
|
||||
media: ops.media,
|
||||
});
|
||||
|
||||
// filter output with only valid streams
|
||||
if (output?.stream) {
|
||||
output.stream = output.stream
|
||||
.filter((stream) => isValidStream(stream))
|
||||
.filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags));
|
||||
}
|
||||
|
||||
if (!output) throw new Error('output is null');
|
||||
if ((!output.stream || output.stream.length === 0) && output.embeds.length === 0)
|
||||
throw new NotFoundError('No streams found');
|
||||
return output;
|
||||
}
|
||||
|
||||
export type IndividualEmbedRunnerOptions = {
|
||||
features: FeatureMap;
|
||||
fetcher: UseableFetcher;
|
||||
proxiedFetcher: UseableFetcher;
|
||||
url: string;
|
||||
id: string;
|
||||
events?: IndividualScraperEvents;
|
||||
};
|
||||
|
||||
export async function scrapeIndividualEmbed(
|
||||
list: ProviderList,
|
||||
ops: IndividualEmbedRunnerOptions,
|
||||
): Promise<EmbedOutput> {
|
||||
const embedScraper = list.embeds.find((v) => ops.id === v.id);
|
||||
if (!embedScraper) throw new Error('Embed with ID not found');
|
||||
|
||||
const output = await embedScraper.scrape({
|
||||
fetcher: ops.fetcher,
|
||||
proxiedFetcher: ops.proxiedFetcher,
|
||||
url: ops.url,
|
||||
progress(val) {
|
||||
ops.events?.update?.({
|
||||
id: embedScraper.id,
|
||||
percentage: val,
|
||||
status: 'pending',
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
output.stream = output.stream
|
||||
.filter((stream) => isValidStream(stream))
|
||||
.filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags));
|
||||
if (output.stream.length === 0) throw new NotFoundError('No streams found');
|
||||
|
||||
return output;
|
||||
}
|
Reference in New Issue
Block a user