mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 18:13:25 +00:00
Add runner
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { makeFullFetcher } from '@/fetchers/common';
|
||||
import { Fetcher } from '@/fetchers/types';
|
||||
import { FullScraperEvents } from '@/main/events';
|
||||
import { ScrapeMedia } from '@/main/media';
|
||||
@@ -16,9 +17,11 @@ export interface ProviderBuilderOptions {
|
||||
|
||||
export interface RunnerOptions {
|
||||
// overwrite the order of sources to run. list of ids
|
||||
// any omitted ids are in added to the end in order of rank (highest first)
|
||||
sourceOrder?: string[];
|
||||
|
||||
// overwrite the order of embeds to run. list of ids
|
||||
// any omitted ids are in added to the end in order of rank (highest first)
|
||||
embedOrder?: string[];
|
||||
|
||||
// object of event functions
|
||||
@@ -46,13 +49,13 @@ export interface ProviderControls {
|
||||
export function makeProviders(ops: ProviderBuilderOptions): ProviderControls {
|
||||
const list = getProviders();
|
||||
const providerRunnerOps = {
|
||||
fetcher: ops.fetcher,
|
||||
proxiedFetcher: ops.proxiedFetcher ?? ops.fetcher,
|
||||
fetcher: makeFullFetcher(ops.fetcher),
|
||||
proxiedFetcher: makeFullFetcher(ops.proxiedFetcher ?? ops.fetcher),
|
||||
};
|
||||
|
||||
return {
|
||||
runAll(runnerOps: RunnerOptions) {
|
||||
return runAllProviders({
|
||||
return runAllProviders(list, {
|
||||
...providerRunnerOps,
|
||||
...runnerOps,
|
||||
});
|
||||
|
@@ -1,3 +1,4 @@
|
||||
import { MediaTypes } from '@/main/media';
|
||||
import { ProviderList } from '@/providers/all';
|
||||
|
||||
export type MetaOutput = {
|
||||
@@ -5,17 +6,24 @@ export type MetaOutput = {
|
||||
id: string;
|
||||
rank: number;
|
||||
name: string;
|
||||
mediaTypes?: Array<MediaTypes>;
|
||||
};
|
||||
|
||||
export function getAllSourceMetaSorted(list: ProviderList): MetaOutput[] {
|
||||
return list.sources
|
||||
.sort((a, b) => b.rank - a.rank)
|
||||
.map((v) => ({
|
||||
type: 'source',
|
||||
id: v.id,
|
||||
name: v.name,
|
||||
rank: v.rank,
|
||||
}));
|
||||
.map((v) => {
|
||||
const types: Array<MediaTypes> = [];
|
||||
if (v.scrapeMovie) types.push('movie');
|
||||
if (v.scrapeShow) types.push('show');
|
||||
return {
|
||||
type: 'source',
|
||||
id: v.id,
|
||||
rank: v.rank,
|
||||
name: v.name,
|
||||
mediaTypes: types,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function getAllEmbedMetaSorted(_list: ProviderList): MetaOutput[] {
|
||||
|
@@ -1,7 +1,12 @@
|
||||
import { Fetcher } from '@/fetchers/types';
|
||||
import { UseableFetcher } from '@/fetchers/types';
|
||||
import { FullScraperEvents } from '@/main/events';
|
||||
import { ScrapeMedia } from '@/main/media';
|
||||
import { ProviderList } from '@/providers/all';
|
||||
import { EmbedOutput, SourcererOutput } from '@/providers/base';
|
||||
import { Stream } from '@/providers/streams';
|
||||
import { ScrapeContext } from '@/utils/context';
|
||||
import { NotFoundError } from '@/utils/errors';
|
||||
import { reorderOnIdList } from '@/utils/list';
|
||||
|
||||
export type RunOutput = {
|
||||
sourceId: string;
|
||||
@@ -21,20 +26,132 @@ export type EmbedRunOutput = {
|
||||
};
|
||||
|
||||
export type ProviderRunnerOptions = {
|
||||
fetcher: Fetcher;
|
||||
proxiedFetcher: Fetcher;
|
||||
fetcher: UseableFetcher;
|
||||
proxiedFetcher: UseableFetcher;
|
||||
sourceOrder?: string[];
|
||||
embedOrder?: string[];
|
||||
events?: FullScraperEvents;
|
||||
media: ScrapeMedia;
|
||||
};
|
||||
|
||||
export async function runAllProviders(_ops: ProviderRunnerOptions): Promise<RunOutput | null> {
|
||||
return {
|
||||
sourceId: '123',
|
||||
stream: {
|
||||
type: 'file',
|
||||
qualities: {},
|
||||
export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOptions): Promise<RunOutput | null> {
|
||||
const sources = reorderOnIdList(ops.sourceOrder ?? [], list.sources).filter((v) => {
|
||||
if (ops.media.type === 'movie') return !!v.scrapeMovie;
|
||||
if (ops.media.type === 'show') return !!v.scrapeShow;
|
||||
return false;
|
||||
});
|
||||
const embeds = reorderOnIdList(ops.embedOrder ?? [], list.embeds);
|
||||
const embedIds = embeds.map((v) => v.id);
|
||||
|
||||
const contextBase: ScrapeContext = {
|
||||
fetcher: ops.fetcher,
|
||||
proxiedFetcher: ops.proxiedFetcher,
|
||||
progress(val) {
|
||||
ops.events?.update?.({
|
||||
percentage: val,
|
||||
status: 'pending',
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
ops.events?.init?.({
|
||||
sourceIds: sources.map((v) => v.id),
|
||||
});
|
||||
|
||||
for (const s of sources) {
|
||||
ops.events?.start?.(s.id);
|
||||
|
||||
// run source scrapers
|
||||
let output: SourcererOutput | null = null;
|
||||
try {
|
||||
if (ops.media.type === 'movie' && s.scrapeMovie)
|
||||
output = await s.scrapeMovie({
|
||||
...contextBase,
|
||||
media: ops.media,
|
||||
});
|
||||
else if (ops.media.type === 'show' && s.scrapeShow)
|
||||
output = await s.scrapeShow({
|
||||
...contextBase,
|
||||
media: ops.media,
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof NotFoundError) {
|
||||
ops.events?.update?.({
|
||||
percentage: 100,
|
||||
status: 'notfound',
|
||||
});
|
||||
continue;
|
||||
}
|
||||
ops.events?.update?.({
|
||||
percentage: 100,
|
||||
status: 'failure',
|
||||
});
|
||||
// TODO log error
|
||||
continue;
|
||||
}
|
||||
if (!output) throw new Error('Invalid media type');
|
||||
|
||||
// return stream is there are any
|
||||
if (output.stream) {
|
||||
return {
|
||||
sourceId: s.id,
|
||||
stream: output.stream,
|
||||
};
|
||||
}
|
||||
|
||||
if (output.embeds.length > 0) {
|
||||
ops.events?.discoverEmbeds?.({
|
||||
embeds: output.embeds.map((v, i) => ({
|
||||
id: [s.id, i].join('-'),
|
||||
embedScraperId: v.embedId,
|
||||
})),
|
||||
sourceId: s.id,
|
||||
});
|
||||
}
|
||||
|
||||
// run embed scrapers on listed embeds
|
||||
const sortedEmbeds = output.embeds;
|
||||
sortedEmbeds.sort((a, b) => embedIds.indexOf(a.embedId) - embedIds.indexOf(b.embedId));
|
||||
|
||||
for (const ind in sortedEmbeds) {
|
||||
if (!Object.prototype.hasOwnProperty.call(sortedEmbeds, ind)) continue;
|
||||
const e = sortedEmbeds[ind];
|
||||
const scraper = embeds.find((v) => v.id === e.embedId);
|
||||
if (!scraper) throw new Error('Invalid embed returned');
|
||||
|
||||
// run embed scraper
|
||||
const id = [s.id, ind].join('-');
|
||||
ops.events?.start?.(id);
|
||||
let embedOutput: EmbedOutput;
|
||||
try {
|
||||
embedOutput = await scraper.scrape({
|
||||
...contextBase,
|
||||
url: e.url,
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof NotFoundError) {
|
||||
ops.events?.update?.({
|
||||
percentage: 100,
|
||||
status: 'notfound',
|
||||
});
|
||||
continue;
|
||||
}
|
||||
ops.events?.update?.({
|
||||
percentage: 100,
|
||||
status: 'failure',
|
||||
});
|
||||
// TODO log error
|
||||
continue;
|
||||
}
|
||||
|
||||
return {
|
||||
sourceId: s.id,
|
||||
embedId: scraper.id,
|
||||
stream: embedOutput.stream,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// no providers or embeds returns streams
|
||||
return null;
|
||||
}
|
||||
|
Reference in New Issue
Block a user