From b70d9aaaf71a483e9618d7a7261e01eb5c7d1d9f Mon Sep 17 00:00:00 2001 From: mrjvs Date: Mon, 25 Dec 2023 00:23:21 +0100 Subject: [PATCH] Make runners compatible with multi stream output --- src/main/individualRunner.ts | 17 +++++++++++------ src/main/runner.ts | 25 ++++++++++++++----------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main/individualRunner.ts b/src/main/individualRunner.ts index ac563ea..3b48a29 100644 --- a/src/main/individualRunner.ts +++ b/src/main/individualRunner.ts @@ -50,12 +50,16 @@ export async function scrapeInvidualSource( media: ops.media, }); - // stream doesn't satisfy the feature flags, so gets removed in output - if (output?.stream && (!isValidStream(output.stream) || !flagsAllowedInFeatures(ops.features, output.stream.flags))) { - output.stream = undefined; + // 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; } @@ -88,9 +92,10 @@ export async function scrapeIndividualEmbed( }, }); - if (!isValidStream(output.stream)) throw new NotFoundError('stream is incomplete'); - if (!flagsAllowedInFeatures(ops.features, output.stream.flags)) - throw new NotFoundError("stream doesn't satisfy target feature flags"); + 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; } diff --git a/src/main/runner.ts b/src/main/runner.ts index cea7106..cf9fc82 100644 --- a/src/main/runner.ts +++ b/src/main/runner.ts @@ -80,12 +80,14 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt ...contextBase, media: ops.media, }); - if (output?.stream && !isValidStream(output?.stream)) { - throw new NotFoundError('stream is incomplete'); - } - if (output?.stream && !flagsAllowedInFeatures(ops.features, output.stream.flags)) { - throw new NotFoundError("stream doesn't satisfy target feature flags"); + if (output) { + output.stream = (output.stream ?? []) + .filter((stream) => isValidStream(stream)) + .filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags)); } + if (!output) throw Error('No output'); + if ((!output.stream || output.stream.length === 0) && output.embeds.length === 0) + throw new NotFoundError('No streams found'); } catch (err) { if (err instanceof NotFoundError) { ops.events?.update?.({ @@ -107,10 +109,10 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt if (!output) throw new Error('Invalid media type'); // return stream is there are any - if (output.stream) { + if (output.stream?.[0]) { return { sourceId: s.id, - stream: output.stream, + stream: output.stream[0], }; } @@ -144,9 +146,10 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt ...contextBase, url: e.url, }); - if (!flagsAllowedInFeatures(ops.features, embedOutput.stream.flags)) { - throw new NotFoundError("stream doesn't satisfy target feature flags"); - } + embedOutput.stream = embedOutput.stream + .filter((stream) => isValidStream(stream)) + .filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags)); + if (embedOutput.stream.length === 0) throw new NotFoundError('No streams found'); } catch (err) { if (err instanceof NotFoundError) { ops.events?.update?.({ @@ -169,7 +172,7 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt return { sourceId: s.id, embedId: scraper.id, - stream: embedOutput.stream, + stream: embedOutput.stream[0], }; } }