From 493032590e137383d6b8d5287d59f97f54a423e0 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Wed, 6 Sep 2023 20:33:39 +0200 Subject: [PATCH] Add individual runners --- src/main/individualRunner.ts | 82 ++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/main/individualRunner.ts diff --git a/src/main/individualRunner.ts b/src/main/individualRunner.ts new file mode 100644 index 0000000..36d45e5 --- /dev/null +++ b/src/main/individualRunner.ts @@ -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 { + 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 { + 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', + }); + }, + }); +}