Merge branch 'dev' into fix/improve-rn-docs

This commit is contained in:
Jorrin 2024-03-07 23:44:59 +01:00 committed by GitHub
commit 85ef0d1628
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 47 additions and 59 deletions

View File

@ -1,4 +1,4 @@
import { FullScraperEvents } from '@/entrypoint/utils/events'; import { FullScraperEvents, UpdateEvent } from '@/entrypoint/utils/events';
import { ScrapeMedia } from '@/entrypoint/utils/media'; import { ScrapeMedia } from '@/entrypoint/utils/media';
import { FeatureMap, flagsAllowedInFeatures } from '@/entrypoint/utils/targets'; import { FeatureMap, flagsAllowedInFeatures } from '@/entrypoint/utils/targets';
import { UseableFetcher } from '@/fetchers/types'; import { UseableFetcher } from '@/fetchers/types';
@ -38,13 +38,13 @@ export type ProviderRunnerOptions = {
}; };
export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOptions): Promise<RunOutput | null> { export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOptions): Promise<RunOutput | null> {
const sources = reorderOnIdList(ops.sourceOrder ?? [], list.sources).filter((v) => { const sources = reorderOnIdList(ops.sourceOrder ?? [], list.sources).filter((source) => {
if (ops.media.type === 'movie') return !!v.scrapeMovie; if (ops.media.type === 'movie') return !!source.scrapeMovie;
if (ops.media.type === 'show') return !!v.scrapeShow; if (ops.media.type === 'show') return !!source.scrapeShow;
return false; return false;
}); });
const embeds = reorderOnIdList(ops.embedOrder ?? [], list.embeds); const embeds = reorderOnIdList(ops.embedOrder ?? [], list.embeds);
const embedIds = embeds.map((v) => v.id); const embedIds = embeds.map((embed) => embed.id);
let lastId = ''; let lastId = '';
const contextBase: ScrapeContext = { const contextBase: ScrapeContext = {
@ -63,47 +63,41 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
sourceIds: sources.map((v) => v.id), sourceIds: sources.map((v) => v.id),
}); });
for (const s of sources) { for (const source of sources) {
ops.events?.start?.(s.id); ops.events?.start?.(source.id);
lastId = s.id; lastId = source.id;
// run source scrapers // run source scrapers
let output: SourcererOutput | null = null; let output: SourcererOutput | null = null;
try { try {
if (ops.media.type === 'movie' && s.scrapeMovie) if (ops.media.type === 'movie' && source.scrapeMovie)
output = await s.scrapeMovie({ output = await source.scrapeMovie({
...contextBase, ...contextBase,
media: ops.media, media: ops.media,
}); });
else if (ops.media.type === 'show' && s.scrapeShow) else if (ops.media.type === 'show' && source.scrapeShow)
output = await s.scrapeShow({ output = await source.scrapeShow({
...contextBase, ...contextBase,
media: ops.media, media: ops.media,
}); });
if (output) { if (output) {
output.stream = (output.stream ?? []) output.stream = (output.stream ?? [])
.filter((stream) => isValidStream(stream)) .filter(isValidStream)
.filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags)); .filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags));
} }
if (!output) throw Error('No output'); if (!output || (!output.stream?.length && !output.embeds.length)) {
if ((!output.stream || output.stream.length === 0) && output.embeds.length === 0)
throw new NotFoundError('No streams found'); throw new NotFoundError('No streams found');
} catch (err) {
if (err instanceof NotFoundError) {
ops.events?.update?.({
id: s.id,
percentage: 100,
status: 'notfound',
reason: err.message,
});
continue;
} }
ops.events?.update?.({ } catch (error) {
id: s.id, const updateParams: UpdateEvent = {
id: source.id,
percentage: 100, percentage: 100,
status: 'failure', status: error instanceof NotFoundError ? 'notfound' : 'failure',
error: err, reason: error instanceof NotFoundError ? error.message : undefined,
}); error: error instanceof NotFoundError ? undefined : error,
};
ops.events?.update?.(updateParams);
continue; continue;
} }
if (!output) throw new Error('Invalid media type'); if (!output) throw new Error('Invalid media type');
@ -111,7 +105,7 @@ 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]) {
return { return {
sourceId: s.id, sourceId: source.id,
stream: output.stream[0], stream: output.stream[0],
}; };
} }
@ -120,62 +114,56 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
const sortedEmbeds = output.embeds const sortedEmbeds = output.embeds
.filter((embed) => { .filter((embed) => {
const e = list.embeds.find((v) => v.id === embed.embedId); const e = list.embeds.find((v) => v.id === embed.embedId);
if (!e || e.disabled) return false; return e && !e.disabled;
return true;
}) })
.sort((a, b) => embedIds.indexOf(a.embedId) - embedIds.indexOf(b.embedId)); .sort((a, b) => embedIds.indexOf(a.embedId) - embedIds.indexOf(b.embedId));
if (sortedEmbeds.length > 0) { if (sortedEmbeds.length > 0) {
ops.events?.discoverEmbeds?.({ ops.events?.discoverEmbeds?.({
embeds: sortedEmbeds.map((v, i) => ({ embeds: sortedEmbeds.map((embed, i) => ({
id: [s.id, i].join('-'), id: [source.id, i].join('-'),
embedScraperId: v.embedId, embedScraperId: embed.embedId,
})), })),
sourceId: s.id, sourceId: source.id,
}); });
} }
for (const ind in sortedEmbeds) { for (const [ind, embed] of sortedEmbeds.entries()) {
if (!Object.prototype.hasOwnProperty.call(sortedEmbeds, ind)) continue; const scraper = embeds.find((v) => v.id === embed.embedId);
const e = sortedEmbeds[ind];
const scraper = embeds.find((v) => v.id === e.embedId);
if (!scraper) throw new Error('Invalid embed returned'); if (!scraper) throw new Error('Invalid embed returned');
// run embed scraper // run embed scraper
const id = [s.id, ind].join('-'); const id = [source.id, ind].join('-');
ops.events?.start?.(id); ops.events?.start?.(id);
lastId = id; lastId = id;
let embedOutput: EmbedOutput; let embedOutput: EmbedOutput;
try { try {
embedOutput = await scraper.scrape({ embedOutput = await scraper.scrape({
...contextBase, ...contextBase,
url: e.url, url: embed.url,
}); });
embedOutput.stream = embedOutput.stream embedOutput.stream = embedOutput.stream
.filter((stream) => isValidStream(stream)) .filter(isValidStream)
.filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags)); .filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags));
if (embedOutput.stream.length === 0) throw new NotFoundError('No streams found'); if (embedOutput.stream.length === 0) {
} catch (err) { throw new NotFoundError('No streams found');
if (err instanceof NotFoundError) {
ops.events?.update?.({
id,
percentage: 100,
status: 'notfound',
reason: err.message,
});
continue;
} }
ops.events?.update?.({ } catch (error) {
id, const updateParams: UpdateEvent = {
id: source.id,
percentage: 100, percentage: 100,
status: 'failure', status: error instanceof NotFoundError ? 'notfound' : 'failure',
error: err, reason: error instanceof NotFoundError ? error.message : undefined,
}); error: error instanceof NotFoundError ? undefined : error,
};
ops.events?.update?.(updateParams);
continue; continue;
} }
return { return {
sourceId: s.id, sourceId: source.id,
embedId: scraper.id, embedId: scraper.id,
stream: embedOutput.stream[0], stream: embedOutput.stream[0],
}; };