Add individual runners

This commit is contained in:
mrjvs 2023-09-06 20:33:39 +02:00
parent f5b8d70cb5
commit 493032590e
1 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,82 @@
import { UseableFetcher } from '@/fetchers/types';
import { UpdateEvent } from '@/main/events';
import { ScrapeMedia } from '@/main/media';
import { EmbedOutput, SourcererOutput } from '@/providers/base';
import { ProviderList } from '@/providers/get';
import { ScrapeContext } from '@/utils/context';
export type IndividualSourceRunnerOptions = {
fetcher: UseableFetcher;
proxiedFetcher: UseableFetcher;
media: ScrapeMedia;
id: string;
events: {
update?: (evt: UpdateEvent) => void;
};
};
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?.({
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,
});
if (!output) throw new Error('output is null');
return output;
}
export type IndividualEmbedRunnerOptions = {
fetcher: UseableFetcher;
proxiedFetcher: UseableFetcher;
url: string;
id: string;
events?: {
update?: (evt: UpdateEvent) => void;
};
};
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');
return embedScraper.scrape({
fetcher: ops.fetcher,
proxiedFetcher: ops.proxiedFetcher,
url: ops.url,
progress(val) {
ops.events?.update?.({
percentage: val,
status: 'pending',
});
},
});
}