mirror of
https://github.com/Viren070/AIOStreams.git
synced 2025-12-01 23:14:04 +01:00
refactor: simplify stream skipping logic
This commit is contained in:
@@ -757,7 +757,7 @@ export const AIOStream = StreamSchema.extend({
|
||||
.optional(),
|
||||
duration: z.number().optional(),
|
||||
library: z.boolean().optional(),
|
||||
id: z.string().min(1),
|
||||
id: z.string().min(1).optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -3,13 +3,6 @@ import { constants, createLogger, FULL_LANGUAGE_MAPPING } from '../utils';
|
||||
import FileParser from './file';
|
||||
const logger = createLogger('parser');
|
||||
|
||||
export class SkipStreamError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'SkipStreamError';
|
||||
}
|
||||
}
|
||||
|
||||
class StreamParser {
|
||||
private count = 0;
|
||||
get errorRegexes(): { pattern: RegExp; message: string }[] | undefined {
|
||||
@@ -59,7 +52,7 @@ class StreamParser {
|
||||
|
||||
constructor(protected readonly addon: Addon) {}
|
||||
|
||||
parse(stream: Stream): ParsedStream {
|
||||
parse(stream: Stream): ParsedStream | { skip: true } {
|
||||
let parsedStream: ParsedStream = {
|
||||
id: this.getRandomId(),
|
||||
addon: this.addon,
|
||||
|
||||
@@ -10,12 +10,11 @@ import { Preset, baseOptions } from './preset';
|
||||
import { Env, formatZodError, RESOURCES } from '../utils';
|
||||
import { StreamParser } from '../parser';
|
||||
import { createLogger } from '../utils';
|
||||
import { SkipStreamError } from '../parser/streams';
|
||||
|
||||
const logger = createLogger('parser');
|
||||
|
||||
class AIOStreamsStreamParser extends StreamParser {
|
||||
override parse(stream: Stream): ParsedStream {
|
||||
override parse(stream: Stream): ParsedStream | { skip: true } {
|
||||
const aioStream = stream as AIOStream;
|
||||
const parsed = AIOStream.safeParse(aioStream);
|
||||
if (!parsed.success) {
|
||||
@@ -24,8 +23,8 @@ class AIOStreamsStreamParser extends StreamParser {
|
||||
);
|
||||
throw new Error('Invalid stream');
|
||||
}
|
||||
if (aioStream.streamData.id.endsWith('external-download')) {
|
||||
throw new SkipStreamError('External download stream');
|
||||
if (aioStream.streamData.id?.endsWith('external-download')) {
|
||||
return { skip: true };
|
||||
}
|
||||
const addonName = this.addon?.name?.trim();
|
||||
return {
|
||||
|
||||
@@ -39,7 +39,6 @@ import {
|
||||
import { PresetManager } from './presets';
|
||||
import { StreamParser } from './parser';
|
||||
import { z } from 'zod';
|
||||
import { SkipStreamError } from './parser/streams';
|
||||
|
||||
const logger = createLogger('wrappers');
|
||||
// const cache = Cache.getInstance<string, any>('wrappers');
|
||||
@@ -168,16 +167,9 @@ export class Wrapper {
|
||||
? PresetManager.fromId(this.addon.presetType).getParser()
|
||||
: StreamParser;
|
||||
const parser = new Parser(this.addon);
|
||||
return streams.flatMap((stream: Stream) => {
|
||||
try {
|
||||
return [parser.parse(stream)];
|
||||
} catch (error: any) {
|
||||
if (error instanceof SkipStreamError) {
|
||||
return [];
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
return streams
|
||||
.flatMap((stream: Stream) => parser.parse(stream))
|
||||
.filter((stream: any) => !stream.skip);
|
||||
}
|
||||
|
||||
async getCatalog(
|
||||
|
||||
Reference in New Issue
Block a user